Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android通過achartengine畫折線圖,achartengine折線圖

Android通過achartengine畫折線圖,achartengine折線圖

編輯:關於android開發

Android通過achartengine畫折線圖,achartengine折線圖


因為前段時間Android項目中用到了折線圖的功能,在查資料的過程中發現achartengine這個包有非常強大的畫圖功能,正好最近有時間,就整理一下方便以後查看。

 首先要使用畫圖功能,就必須在lib中引入achartengine包。為了使用方便,直接使用示例中的兩個文件,源碼如下:

IDemoChart.java

復制代碼
import android.content.Context;
import android.content.Intent;

/**
 * Defines the demo charts.
 */
public interface IDemoChart {
  /** A constant for the name field in a list activity. */
  String NAME = "name";
  /** A constant for the description field in a list activity. */
  String DESC = "desc";

  /**
   * Returns the chart name.
   * 
   * @return the chart name
   */
  String getName();

  /**
   * Returns the chart description.
   * 
   * @return the chart description
   */
  String getDesc();

  /**
   * Executes the chart demo.
   * 
   * @param context the context
   * @return the built intent
   */
  Intent execute(Context context);

}
復制代碼

AbstractDemoChart.java

復制代碼
  1 import java.util.Date;
  2 import java.util.List;
  3 
  4 import org.achartengine.chart.PointStyle;
  5 import org.achartengine.model.CategorySeries;
  6 import org.achartengine.model.MultipleCategorySeries;
  7 import org.achartengine.model.TimeSeries;
  8 import org.achartengine.model.XYMultipleSeriesDataset;
  9 import org.achartengine.model.XYSeries;
 10 import org.achartengine.renderer.DefaultRenderer;
 11 import org.achartengine.renderer.SimpleSeriesRenderer;
 12 import org.achartengine.renderer.XYMultipleSeriesRenderer;
 13 import org.achartengine.renderer.XYSeriesRenderer;
 14 
 15 import android.app.Activity;
 16 
 17 /**
 18  * An abstract class for the demo charts to extend. It contains some methods for
 19  * building datasets and renderers.
 20  */
 21 public abstract class AbstractDemoChart extends Activity implements IDemoChart {
 22 
 23   /**
 24    * Builds an XY multiple dataset using the provided values.
 25    * 
 26    * @param titles the series titles
 27    * @param xValues the values for the X axis
 28    * @param yValues the values for the Y axis
 29    * @return the XY multiple dataset
 30    */
 31   protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues,
 32       List<double[]> yValues) {
 33     XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
 34     addXYSeries(dataset, titles, xValues, yValues, 0);
 35     return dataset;
 36   }
 37 
 38   public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues,
 39       List<double[]> yValues, int scale) {
 40     int length = titles.length;
 41     for (int i = 0; i < length; i++) {
 42       XYSeries series = new XYSeries(titles[i], scale);
 43       double[] xV = xValues.get(i);
 44       double[] yV = yValues.get(i);
 45       int seriesLength = xV.length;
 46       for (int k = 0; k < seriesLength; k++) {
 47         series.add(xV[k], yV[k]);
 48       }
 49       dataset.addSeries(series);
 50     }
 51   }
 52 
 53   /**
 54    * Builds an XY multiple series renderer.
 55    * 
 56    * @param colors the series rendering colors
 57    * @param styles the series point styles
 58    * @return the XY multiple series renderers
 59    */
 60   protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
 61     XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
 62     setRenderer(renderer, colors, styles);
 63     return renderer;
 64   }
 65 
 66   protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {
 67     renderer.setAxisTitleTextSize(16);
 68     renderer.setChartTitleTextSize(20);
 69     renderer.setLabelsTextSize(15);
 70     renderer.setLegendTextSize(15);
 71     renderer.setPointSize(5f);
 72     renderer.setMargins(new int[] { 20, 30, 15, 20 });
 73     int length = colors.length;
 74     for (int i = 0; i < length; i++) {
 75       XYSeriesRenderer r = new XYSeriesRenderer();
 76       r.setColor(colors[i]);
 77       r.setPointStyle(styles[i]);
 78       renderer.addSeriesRenderer(r);
 79     }
 80   }
 81 
 82   /**
 83    * Sets a few of the series renderer settings.
 84    * 
 85    * @param renderer the renderer to set the properties to
 86    * @param title the chart title
 87    * @param xTitle the title for the X axis
 88    * @param yTitle the title for the Y axis
 89    * @param xMin the minimum value on the X axis
 90    * @param xMax the maximum value on the X axis
 91    * @param yMin the minimum value on the Y axis
 92    * @param yMax the maximum value on the Y axis
 93    * @param axesColor the axes color
 94    * @param labelsColor the labels color
 95    */
 96   protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
 97       String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
 98       int labelsColor) {
 99     renderer.setChartTitle(title);
100     renderer.setXTitle(xTitle);
101     renderer.setYTitle(yTitle);
102     renderer.setXAxisMin(xMin);
103     renderer.setXAxisMax(xMax);
104     renderer.setYAxisMin(yMin);
105     renderer.setYAxisMax(yMax);
106     renderer.setAxesColor(axesColor);
107     renderer.setLabelsColor(labelsColor);
108   }
109 
110   /**
111    * Builds an XY multiple time dataset using the provided values.
112    * 
113    * @param titles the series titles
114    * @param xValues the values for the X axis
115    * @param yValues the values for the Y axis
116    * @return the XY multiple time dataset
117    */
118   protected XYMultipleSeriesDataset buildDateDataset(String[] titles, List<Date[]> xValues,
119       List<double[]> yValues) {
120     XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
121     int length = titles.length;
122     for (int i = 0; i < length; i++) {
123       TimeSeries series = new TimeSeries(titles[i]);
124       Date[] xV = xValues.get(i);
125       double[] yV = yValues.get(i);
126       int seriesLength = xV.length;
127       for (int k = 0; k < seriesLength; k++) {
128         series.add(xV[k], yV[k]);
129       }
130       dataset.addSeries(series);
131     }
132     return dataset;
133   }
134 
135   /**
136    * Builds a category series using the provided values.
137    * 
138    * @param titles the series titles
139    * @param values the values
140    * @return the category series
141    */
142   protected CategorySeries buildCategoryDataset(String title, double[] values) {
143     CategorySeries series = new CategorySeries(title);
144     int k = 0;
145     for (double value : values) {
146       series.add("Project " + ++k, value);
147     }
148 
149     return series;
150   }
151 
152   /**
153    * Builds a multiple category series using the provided values.
154    * 
155    * @param titles the series titles
156    * @param values the values
157    * @return the category series
158    */
159   protected MultipleCategorySeries buildMultipleCategoryDataset(String title,
160       List<String[]> titles, List<double[]> values) {
161     MultipleCategorySeries series = new MultipleCategorySeries(title);
162     int k = 0;
163     for (double[] value : values) {
164       series.add(2007 + k + "", titles.get(k), value);
165       k++;
166     }
167     return series;
168   }
169 
170   /**
171    * Builds a category renderer to use the provided colors.
172    * 
173    * @param colors the colors
174    * @return the category renderer
175    */
176   protected DefaultRenderer buildCategoryRenderer(int[] colors) {
177     DefaultRenderer renderer = new DefaultRenderer();
178     renderer.setLabelsTextSize(15);
179     renderer.setLegendTextSize(15);
180     renderer.setMargins(new int[] { 20, 30, 15, 0 });
181     for (int color : colors) {
182       SimpleSeriesRenderer r = new SimpleSeriesRenderer();
183       r.setColor(color);
184       renderer.addSeriesRenderer(r);
185     }
186     return renderer;
187   }
188 
189   /**
190    * Builds a bar multiple series dataset using the provided values.
191    * 
192    * @param titles the series titles
193    * @param values the values
194    * @return the XY multiple bar dataset
195    */
196   protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
197     XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
198     int length = titles.length;
199     for (int i = 0; i < length; i++) {
200       CategorySeries series = new CategorySeries(titles[i]);
201       double[] v = values.get(i);
202       int seriesLength = v.length;
203       for (int k = 0; k < seriesLength; k++) {
204         series.add(v[k]);
205       }
206       dataset.addSeries(series.toXYSeries());
207     }
208     return dataset;
209   }
210 
211   /**
212    * Builds a bar multiple series renderer to use the provided colors.
213    * 
214    * @param colors the series renderers colors
215    * @return the bar multiple series renderer
216    */
217   protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
218     XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
219     renderer.setAxisTitleTextSize(16);
220     renderer.setChartTitleTextSize(20);
221     renderer.setLabelsTextSize(15);
222     renderer.setLegendTextSize(15);
223     int length = colors.length;
224     for (int i = 0; i < length; i++) {
225       SimpleSeriesRenderer r = new SimpleSeriesRenderer();
226       r.setColor(colors[i]);
227       renderer.addSeriesRenderer(r);
228     }
229     return renderer;
230   }
231 
232 }
復制代碼

然後就是自定義的內容了,包括折線圖的數據、樣式等內容,具體詳見代碼注釋

LinearChart.java

復制代碼
  1 import java.util.ArrayList;
  2 import java.util.List;
  3 
  4 import org.achartengine.ChartFactory;
  5 import org.achartengine.chart.PointStyle;
  6 import org.achartengine.renderer.XYMultipleSeriesRenderer;
  7 import org.achartengine.renderer.XYSeriesRenderer;
  8 
  9 import android.content.Context;
 10 import android.content.Intent;
 11 import android.graphics.Color;
 12 import android.graphics.Paint.Align;
 13 
 14 public class LineChart extends AbstractDemoChart {
 15 
 16     public String getName() {
 17         return "曲線A";
 18     }
 19 
 20     public String getDesc() {
 21         return "曲線A";
 22     }
 23 
 24     /**
 25      * Executes the chart demo.
 26      * 
 27      * @param context the context
 28      * @return the built intent
 29      */
 30     public Intent execute(Context context, ArrayList<Float> standar, ArrayList<Float> collect) {
 31         ArrayList<Float> standardValues = standar;
 32         ArrayList<Float> collectValues = collect;
 33         int l = standardValues.size();
 34         double[] standards = new double[l];
 35         double[] collects = new double[l];
 36         int maxS,minS,maxC,minC;
 37         for (int i = 0; i < l; i++) {
 38             standards[i] = standardValues.get(i);
 39             collects[i] = collectValues.get(i);
 40         }
 41         maxS = (int)DataUtil.getMax(standards);
 42         minS = (int)DataUtil.getMin(standards);
 43         maxC = (int)DataUtil.getMax(collects);
 44         minC = (int)DataUtil.getMin(collects);
 45         // 每個item的title
 46         String[] titles = new String[] { "采集值——標准值"};
 47         // x軸的值
 48         List<double[]> x = new ArrayList<double[]>();
 49             x.add(collects);
 50         // y軸的值
 51         List<double[]> values = new ArrayList<double[]>();
 52         values.add(standards);
 53         int[] colors = new int[] { Color.BLUE};
 54         // 點的樣式
 55         PointStyle[] styles = new PointStyle[] { PointStyle.DIAMOND};
 56         XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
 57         int length = renderer.getSeriesRendererCount();
 58         // 點是空心還是實心
 59         for (int i = 0; i < length; i++) {
 60             ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
 61         }
 62 
 63         // 圖表部分的背景顏色
 64         renderer.setBackgroundColor(Color.parseColor("#f3f3f3"));
 65         renderer.setApplyBackgroundColor(true);
 66         // 圖表與屏幕四邊的間距顏色
 67         renderer.setMarginsColor(Color.argb(0, 0xF3, 0xF3, 0xF3));
 68         renderer.setChartTitleTextSize(30);
 69         renderer.setAxisTitleTextSize(25);
 70         // renderer.setLegendHeight(50);
 71         // 圖例文字的大小
 72         renderer.setLegendTextSize(20);
 73         renderer.setMargins(new int[] { 50, 50, 50, 30 });
 74         // x、y軸上刻度顏色
 75         renderer.setXLabelsColor(Color.BLACK);
 76         renderer.setYLabelsColor(0, Color.BLACK);
 77 
 78         // 最後兩個參數代表軸的顏色和軸標簽的顏色
 79         setChartSettings(renderer, "校准曲線", "采集值", "標准值", minC, maxC, minS, maxS, Color.BLACK, Color.BLACK);
 80         // 軸上數字的數量
 81         renderer.setXLabels(12);
 82         renderer.setYLabels(10);
 83         // 是否顯示網格
 84         renderer.setShowGrid(true);
 85         // x或y軸上數字的方向,相反的。
 86         renderer.setXLabelsAlign(Align.RIGHT);
 87         renderer.setYLabelsAlign(Align.RIGHT);
 88         // 有問題的方法
 89         renderer.setZoomButtonsVisible(true);
 90         renderer.setZoomEnabled(true);
 91         renderer.setAntialiasing(true);
 92         // renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
 93         // renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
 94 
 95         Intent intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, x, values), renderer, "曲線");
 96         return intent;
 97     }
 98 
 99     @Override
100     public Intent execute(Context context) {
101         // TODO Auto-generated method stub
102         return null;
103     }
104 }
復制代碼

 因為要調用到"org.achartengine.GraphicalActivity“類,所以不要忘記在manifest中添加 <activity android:name="org.achartengine.GraphicalActivity" />說明,否則會報錯。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved