首先需要下载Jar包:图表jar.rar
线性图:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // 1, 构造显示用渲染图XY XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); // 2,进行显示 XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); // 2.1, 构建数据 Random r = new Random(); for (int i = 0; i < 2; i++) { // 创建代表哪一条线的标题,这里是画两条 XYSeries series = new XYSeries("test" + (i + 1)); // 填充数据,一条标题10个点 for (int k = 0; k < 10; k++) { // 填x,y值 series.add(k, 20 + r.nextInt() % 100); } // 需要绘制的点放进dataset中 dataset.addSeries(series); } // 3, 对点的绘制进行设置 XYSeriesRenderer xyRenderer = new XYSeriesRenderer(); // 3.1设置颜色 xyRenderer.setColor(Color.BLUE); // 3.2设置点的样式 xyRenderer.setPointStyle(PointStyle.SQUARE); // 3.3, 将要绘制的点添加到坐标绘制中 renderer.addSeriesRenderer(xyRenderer); // 3.4,重复 1~3的步骤绘制第二个系列点 xyRenderer = new XYSeriesRenderer(); xyRenderer.setColor(Color.RED); xyRenderer.setPointStyle(PointStyle.CIRCLE); renderer.addSeriesRenderer(xyRenderer); // Intent intent = new LinChart().execute(this); // 打开画图的Activity,需要在manifest中设置 <activity android:name="org.achartengine.GraphicalActivity" /> Intent intent = ChartFactory .getLineChartIntent(this, dataset, renderer); startActivity(intent); } }
柱状图:
// 柱状图 XYMultipleSeriesRenderer renderer = getBarDemoRenderer(); Intent intent = ChartFactory.getBarChartIntent(this, getBarDemoDataset(), renderer, Type.DEFAULT); startActivity(intent); //设置柱状图的Series private XYMultipleSeriesDataset getBarDemoDataset() { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); final int nr = 10; Random r = new Random(); // 一共是3种不同的柱状图 for (int i = 0; i < SERIES_NR; i++) { CategorySeries series = new CategorySeries("Demo series " + (i + 1)); // 设置每个的高度 for (int k = 0; k < nr; k++) { series.add(100 + r.nextInt() % 100); } dataset.addSeries(series.toXYSeries()); } return dataset; } //设置柱状图的Renderer,设置颜色 public XYMultipleSeriesRenderer getBarDemoRenderer() { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); SimpleSeriesRenderer r = new SimpleSeriesRenderer(); r.setColor(Color.BLUE); renderer.addSeriesRenderer(r); r = new SimpleSeriesRenderer(); r.setColor(Color.GREEN); renderer.addSeriesRenderer(r); r = new SimpleSeriesRenderer(); r.setColor(Color.WHITE); renderer.addSeriesRenderer(r); setChartSettings(renderer); return renderer; } //设置柱状图Renderer的属性 private void setChartSettings(XYMultipleSeriesRenderer renderer) { renderer.setChartTitle("Chart demo"); renderer.setXTitle("x values"); renderer.setYTitle("y values"); renderer.setXAxisMin(0);// 设置X轴的最小值为0.5 renderer.setXAxisMax(10.5);// 设置X轴的最大值为10.5 renderer.setYAxisMin(0);// 设置Y轴的最小值为0 renderer.setYAxisMax(210);// 设置Y轴最大值为210 renderer.setDisplayChartValues(true); // 设置是否在柱体上方显示值 renderer.setShowGrid(true);// 设置是否在图表中显示网格 renderer.setXLabels(0);// 设置X轴显示的刻度标签的个数 for (int i = 1; i < 11; i++) { // 设置x轴刻度的值 renderer.addTextLabel(i, "" + i); } }
饼状图:
// 饼状图 Intent intent = execute(MainActivity.this); startActivity(intent); //设置饼状图 public Intent execute(Context context) { double[] values = new double[] { 12, 14, 11, 10, 19 }; int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN }; DefaultRenderer renderer = buildCategoryRenderer(colors); renderer.setZoomButtonsVisible(true); renderer.setZoomEnabled(true); // 设置饼状图的title字体大小 renderer.setChartTitleTextSize(20); // 设置饼状图的title renderer.setChartTitle("这是一个饼状图"); return ChartFactory.getPieChartIntent(context, buildCategoryDataset("Project budget", values), renderer, "Budget"); } //设置饼状图的Renderer protected DefaultRenderer buildCategoryRenderer(int[] colors) { DefaultRenderer renderer = new DefaultRenderer(); renderer.setLabelsTextSize(15); renderer.setLegendTextSize(15); renderer.setMargins(new int[] { 20, 30, 15, 0 }); // 设置颜色 for (int color : colors) { SimpleSeriesRenderer r = new SimpleSeriesRenderer(); r.setColor(color); renderer.addSeriesRenderer(r); } return renderer; } //设置饼状图的Series protected CategorySeries buildCategoryDataset(String title, double[] values) { CategorySeries series = new CategorySeries(title); int k = 0; // 设置每份的大小 for (double value : values) { series.add("Project " + ++k, value); } return series; }