因为前段时间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" />说明,否则会报错。
时间: 2024-10-18 09:32:39