Android报achartengine再详细的介绍可以查看
http://blog.csdn.net/lk_blog/article/details/7642751
在achartengine中两种创建报表的方式
1,是在Activity中直接创建
2 是在Activity的view控件中创建(这种方式更灵活),使用achartengine中GraphicalView控件可以响应点击事件和动态数据的更新。现在只介绍在GraphicalView中创建响应点击事件
package com.sinosoft.chartdemo; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.achartengine.ChartFactory; import org.achartengine.GraphicalView; import org.achartengine.chart.PointStyle; import org.achartengine.model.SeriesSelection; import org.achartengine.model.XYMultipleSeriesDataset; import org.achartengine.model.XYSeries; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer; import org.achartengine.tools.PanListener; import org.achartengine.tools.ZoomEvent; import org.achartengine.tools.ZoomListener; import android.app.Activity; import android.graphics.Color; import android.graphics.Paint.Align; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.Toast; /** * 嵌入Acitivity中的折线图 * * @author jianfan * */ public class LineChartActivity extends BasicActivity { private XYMultipleSeriesDataset mDataset; private XYMultipleSeriesRenderer mRenderer; private XYSeries mCurrentSeries; private XYSeriesRenderer mCurrentRenderer; private GraphicalView mChartView; @Override protected void onCreate(Bundle savedInstanceState) { Log.i("onCreate", "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Init(); } /******** * 初始化数据DataSet和Renderer *****/ private void Init() { InitRenderer(); InitDataSet(); } /*********** * 初始化 XYMultipleSeriesRenderer **********/ private void InitRenderer() { // 每个序列的颜色设置 int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW }; // 每个序列中点的形状设置 PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND, PointStyle.TRIANGLE, PointStyle.SQUARE }; // 调用AbstractChart中的方法设置renderer. // 设置线条的颜色和点的形状 mRenderer = buildRenderer(colors, styles); int length = mRenderer.getSeriesRendererCount(); // 设置图上线上的点为实心 for (int i = 0; i < length; i++) { ((XYSeriesRenderer) mRenderer.getSeriesRendererAt(i)).setFillPoints(true); } // 调用AbstractChart中的方法设置图表的renderer属性. setChartSettings(mRenderer, "平均温度", "月份", "温度", 0.5, 12.5, -10, 40, Color.LTGRAY, Color.LTGRAY); // 设置x轴显示12个点,根据setChartSettings的最大值和最小值自动计算点的间隔 mRenderer.setXLabels(12); // 设置y轴显示10个点,根据setChartSettings的最大值和最小值自动计算点的间隔 mRenderer.setYLabels(10); mRenderer.setBackgroundColor(Color.BLACK);// 设置背景颜色 mRenderer.setApplyBackgroundColor(true); // 使背景色生效 mRenderer.setShowGrid(true);// 是否显示网格 mRenderer.setXLabelsAlign(Align.RIGHT);// 刻度线与刻度标注之间的相对位置关系 mRenderer.setYLabelsAlign(Align.RIGHT);// 刻度线与刻度标注之间的相对位置关系 mRenderer.setZoomButtonsVisible(true);// 是否显示放大缩小按钮 mRenderer.setPanLimits(new double[] { -10, 20, -10, 40 }); // 设置拖动时X轴Y轴允许的最大值最小值. mRenderer.setZoomLimits(new double[] { -10, 20, -10, 40 });// 设置放大缩小时X轴Y轴允许的最大最小值. } /*********** * 初始化 XYMultipleSeriesDataset **********/ private void InitDataSet() { // 图例 String[] titles = new String[] { "克里特岛", "科孚岛", "萨索斯岛", "斯基亚索斯岛" }; // 每个序列中点的X坐标 Listx = new ArrayList (); for (int i = 0; i < titles.length; i++) { x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }); } // titles数组中每个标识物对应的线条 List values = new ArrayList (); // 克里特岛的点的y坐标 values.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 }); // 科孚岛的点的y坐标 values.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 }); // 萨索斯岛的点的y坐标 values.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 }); // 斯基亚索斯岛的点的y坐标 values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 }); mDataset = buildDataset(titles, x, values); } /********************* * 当Activity失去焦点的时候用来保存数据的方法 */ @Override protected void onSaveInstanceState(Bundle outState) { Log.i("onSaveInstanceState", "onSaveInstanceState"); super.onSaveInstanceState(outState); outState.putSerializable("dataset", mDataset); outState.putSerializable("renderer", mRenderer); outState.putSerializable("current_series", mCurrentSeries); outState.putSerializable("current_renderer", mCurrentRenderer); } /********************* * 当Activity重新获取焦点的时,取回之前保存的数据 */ @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { Log.i("onRestoreInstanceState", "onRestoreInstanceState"); super.onRestoreInstanceState(savedInstanceState); mDataset = (XYMultipleSeriesDataset) savedInstanceState.getSerializable("dataset"); mRenderer = (XYMultipleSeriesRenderer) savedInstanceState.getSerializable("renderer"); mCurrentSeries = (XYSeries) savedInstanceState.getSerializable("current_series"); mCurrentRenderer = (XYSeriesRenderer) savedInstanceState.getSerializable("current_renderer"); } @Override protected void onResume() { Log.i("onResume", "onResume"); super.onResume(); if (mChartView == null) { LinearLayout layout = (LinearLayout) findViewById(R.id.chart); mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer); mRenderer.setClickEnabled(true);// 设置图表是否允许点击 // 设置点的缓冲半径值(在某点附件点击时,多大范围内都算点击这个点) mRenderer.setSelectableBuffer(10); mChartView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 这段代码处理点击一个点后,获得所点击的点在哪个序列中以及点的坐标. // -->start SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint(); double[] xy = mChartView.toRealPoint(0); if (seriesSelection == null) { Toast.makeText(LineChartActivity.this, "没有图表元素被点击", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(LineChartActivity.this, "图表元素系列指数 " + seriesSelection.getSeriesIndex() + " 数据点指数 " + seriesSelection.getPointIndex() + " 被点击了" + " 最近点值X=" + seriesSelection.getXValue() + ", 最近点值Y=" + seriesSelection.getValue() + " 点击点值X=" + (float) xy[0] + ", 点击点值Y=" + (float) xy[1], Toast.LENGTH_SHORT).show(); } } // -->end }); mChartView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint(); if (seriesSelection == null) { Toast.makeText(LineChartActivity.this, "No chart element was long pressed", Toast.LENGTH_SHORT); return false; // no chart element was long pressed, so // let something // else handle the event } else { Toast.makeText(LineChartActivity.this, "Chart element in series index " + seriesSelection.getSeriesIndex() + " data point index " + seriesSelection.getPointIndex() + " was long pressed", Toast.LENGTH_SHORT); return true; // the element was long pressed - the event // has been // handled } } }); // 这段代码处理放大缩小 // -->start mChartView.addZoomListener(new ZoomListener() { public void zoomApplied(ZoomEvent e) { String type = "out"; if (e.isZoomIn()) { type = "in"; } System.out.println("Zoom " + type + " rate " + e.getZoomRate()); } public void zoomReset() { System.out.println("Reset"); } }, true, true); // -->end // 设置拖动图表时后台打印出图表坐标的最大最小值. mChartView.addPanListener(new PanListener() { public void panApplied() { System.out.println("New X range=[" + mRenderer.getXAxisMin() + ", " + mRenderer.getXAxisMax() + "], Y range=[" + mRenderer.getYAxisMax() + ", " + mRenderer.getYAxisMax() + "]"); } }); layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); boolean enabled = mDataset.getSeriesCount() > 0; } else { mChartView.repaint(); } } }