经过上一篇的简单介绍我们开始通过分析AChartEngine自带的例子学习如何使用它吧
第一个类:MainActivity程序入口:
1 package com.example.achartenginedemo3; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.achartengine.chart.PointStyle; 7 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.graphics.Color; 11 import android.os.Bundle; 12 13 public class MainActivity extends Activity { 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 String[] titles = new String[] { "Crete", "Corfu", "Thassos", "Skiathos" }; 20 List<double[]> xValues = new ArrayList<double[]>(); 21 22 List<double[]> yValues = new ArrayList<double[]>(); 23 for (int i = 0; i < titles.length; i++) { 24 //加了4个一样数据到x中 25 xValues.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }); 26 } 27 28 //四条折线的值(y) 29 yValues.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2, 30 13.9 }); 31 yValues.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 }); 32 yValues.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 }); 33 yValues.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 }); 34 int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW }; 35 //四条折线的点的形状 36 PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND, 37 PointStyle.TRIANGLE, PointStyle.SQUARE }; 38 Intent intent = new Polygram(titles, xValues, yValues, colors, styles).excute(this); 39 finish(); 40 startActivity(intent); 41 42 } 43 44 45 }
Polygram:负责绘制折线图:
1 package com.example.achartenginedemo3; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.achartengine.ChartFactory; 7 import org.achartengine.chart.PointStyle; 8 import org.achartengine.model.XYMultipleSeriesDataset; 9 import org.achartengine.model.XYSeries; 10 import org.achartengine.renderer.XYMultipleSeriesRenderer; 11 import org.achartengine.renderer.XYSeriesRenderer; 12 13 import android.content.Context; 14 import android.content.Intent; 15 import android.graphics.Color; 16 import android.graphics.Paint.Align; 17 18 public class Polygram{ 19 //折线标题和x,y坐标,颜色,形状 20 private String[] titles = null; 21 private List<double[]> xValues = new ArrayList<double[]>(); 22 private List<double[]> yValues = new ArrayList<double[]>(); 23 private int[] colors = null; 24 private PointStyle[] styles = null; 25 26 //构造函数 27 public Polygram(String[] titles,List<double[]> xValues,List<double[]> yValues, 28 int[] colors, PointStyle[] styles){ 29 this.titles = titles; 30 this.xValues.addAll(xValues); 31 this.yValues.addAll(yValues); 32 this.colors = colors; 33 this.styles = styles; 34 35 if(titles.length != xValues.size() || xValues.size() != yValues.size() || 36 yValues.size() != colors.length ||colors.length != styles.length){ 37 throw new RuntimeException("五个参数的长度不一样"); 38 } 39 } 40 41 public Intent excute(Context context){ 42 Intent intent = null; 43 XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles); 44 45 int length = renderer.getSeriesRendererCount(); 46 // Log.i("TAG","线的数量:" + length); 47 for (int i = 0; i < length; i++) { 48 //设置是否填充点,如果是false则点是不填充颜色 49 ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true); 50 } 51 setChartSettings(renderer, "Average temperature", "Month", "Temperature", 0.5, 12.5, -10, 40, 52 Color.LTGRAY, Color.LTGRAY); 53 //x,y轴的间距,值越小间距就越大 54 renderer.setXLabels(10); 55 renderer.setYLabels(10); 56 //是否显示网格 57 renderer.setShowGrid(true); 58 59 //按照官方的解释是x,y的排列集合,但是实际用什么值结果没有明显变化 60 renderer.setXLabelsAlign(Align.RIGHT); 61 renderer.setYLabelsAlign(Align.RIGHT); 62 //是否显示放缩按钮 63 renderer.setZoomButtonsVisible(true); 64 // Sets the pan limits as an array of 4 values. 65 renderer.setPanLimits(new double[] { -10, 20, -10, 40 }); 66 renderer.setZoomLimits(new double[] { -10, 20, -10, 40 }); 67 //获取图表intent对象 68 intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, xValues, yValues), 69 renderer, "Average temperature"); 70 return intent; 71 } 72 73 protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues, 74 List<double[]> yValues) { 75 XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); 76 addXYSeries(dataset, titles, xValues, yValues, 0); 77 return dataset; 78 } 79 80 public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues, 81 List<double[]> yValues, int scale) { 82 int length = titles.length; 83 for (int i = 0; i < length; i++) { 84 XYSeries series = new XYSeries(titles[i], scale); 85 double[] xV = xValues.get(i); 86 double[] yV = yValues.get(i); 87 int seriesLength = xV.length; 88 for (int k = 0; k < seriesLength; k++) { 89 series.add(xV[k], yV[k]); 90 } 91 dataset.addSeries(series); 92 } 93 } 94 95 private XYMultipleSeriesRenderer buildRenderer(int[] colors2, 96 PointStyle[] styles2) { 97 XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); 98 //色绘制各个属性 99 renderer.setAxisTitleTextSize(16); 100 renderer.setChartTitleTextSize(20); 101 renderer.setLabelsTextSize(15); 102 renderer.setLegendTextSize(15); 103 renderer.setPointSize(5f); 104 renderer.setMargins(new int[] { 20, 30, 15, 20 }); 105 106 int length = colors.length; 107 for (int i = 0; i < length; i++) { 108 XYSeriesRenderer r = new XYSeriesRenderer(); 109 r.setColor(colors[i]); 110 r.setPointStyle(styles[i]); 111 renderer.addSeriesRenderer(r); 112 } 113 return renderer; 114 } 115 protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, 116 String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, 117 int labelsColor) { 118 renderer.setChartTitle(title); 119 renderer.setXTitle(xTitle); 120 renderer.setYTitle(yTitle); 121 renderer.setXAxisMin(xMin); 122 renderer.setXAxisMax(xMax); 123 renderer.setYAxisMin(yMin); 124 renderer.setYAxisMax(yMax); 125 renderer.setAxesColor(axesColor); 126 renderer.setLabelsColor(labelsColor); 127 } 128 }
效果图如下:
需要注意的是你必须要在AndroidManifest.xml中添加:
<activity android:name = "org.achartengine.GraphicalActivity" />