android 图表引擎AChartEngine(线性图)

jingwenjingwen 2014-06-25

使用方法参考前面柱状图,下面定义线性图类

package com.example.test.achartengine;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.view.View;

public class LinearChart {

	public AbstractDemoChart abstractDemoChart;
	public XYMultipleSeriesRenderer renderer;
	public XYMultipleSeriesDataset dataset;

	private String[] titles;
	private List<double[]> Xvalues = new ArrayList<double[]>();
	private List<double[]> Yvalues = new ArrayList<double[]>();
	private int[] colors;
	private PointStyle[] styles;

	public LinearChart(Context context) {
		abstractDemoChart = new AbstractDemoChart();

		init();
	}

	private void init() {
		titles = new String[] { "Monday", "Tuesday", "Wednesday", "Thursday" };
		for (int i = 0; i < titles.length; i++) {
			Xvalues.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
		}

		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, 13.9 });
		Yvalues.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 });
		Yvalues.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 });
		Yvalues.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });

		colors = new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW };

		styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND, PointStyle.TRIANGLE, PointStyle.SQUARE };

		renderer = abstractDemoChart.buildRenderer(colors, styles);

		dataset = abstractDemoChart.buildDataset(titles, Xvalues, Yvalues);
		
//		dataset.addSeries(series);
	}

	public View execute(Context context) {

		int lenght = renderer.getSeriesRendererCount();
		for (int i = 0; i < lenght; i++) {
			((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
		}
		abstractDemoChart.setChartSettings(renderer, "Temperatuer", "Week", "Temp", 0, 13, -10, 30, Color.LTGRAY, Color.LTGRAY);
		renderer.setXLabels(12);
		renderer.setYLabels(10);
		renderer.setShowGrid(true);
		renderer.setXLabelsAlign(Align.RIGHT);
		renderer.setYLabelsAlign(Align.RIGHT);
		renderer.setZoomButtonsVisible(false);
		renderer.setPanEnabled(true, false); // 左右移动
		renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
		renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });

		// XYSeries series = dataset.getSeriesAt(0);
		// series.addAnnotation("Vacation", 6, 30);
		return ChartFactory.getLineChartView(context, dataset, renderer);
	}
}

相关推荐