android报表图形引擎(AChartEngine)demo解析与源码

AchartEngine支持多种图表样式,本文介绍两种:线状表和柱状表。 AchartEngine有两种启动的方式:一种是通过ChartFactory.get***View()方式来直接获取到view放入到我们设置好的布局文件中,还有一种是通过ChartFactory.get***Intent()来获得启动activity的intent(需要在AndroidManifest.xml中注册一个activity:org.achartengine.GraphicalActivity)。 需有添加jar包:achartengin.jar,可以到http://code.google.com/p/achartengine/downloads/list这个地址去下载,也可以下地我写的demo的源码,libs文件夹里有。 先上两张效果图: 代码如下: 线状图布局文件: [html] <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".LineChartBuilder" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <LinearLayout android:id="@+id/chart" android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent" ></LinearLayout> </RelativeLayout> [/html] 主布局文件 [html] <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/lineButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:layout_gravity="center_horizontal" android:text="线状图" /> <Button android:id="@+id/barButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_gravity="center_horizontal" android:text="柱状图" /> </LinearLayout> [/html] 线状图主要代码: [java] public class LineChartBuilder extends Activity { private XYMultipleSeriesDataset mDataset =new XYMultipleSeriesDataset(); private XYMultipleSeriesRenderer mRenderer =new XYMultipleSeriesRenderer(); private XYSeries mCurrentSeries; private XYSeriesRenderer mCurrentRenderer; private String mDataFormatString; private GraphicalView mChartView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.line_chart); //设置图表的属性 String seriesTitle="玩家年龄趋势"; XYSeries series =new XYSeries(seriesTitle); mDataset.addSeries(series); mCurrentSeries=series; XYSeriesRenderer seriesRenderer=new XYSeriesRenderer(); seriesRenderer.setColor(Color.GREEN); seriesRenderer.setFillBelowLine(true); seriesRenderer.setFillBelowLineColor(Color.RED); seriesRenderer.setFillPoints(true); seriesRenderer.setPointStyle(PointStyle.POINT); seriesRenderer.setLineWidth(3.0f); mRenderer.addSeriesRenderer(seriesRenderer); mRenderer.setYAxisMin(0d); mRenderer.setYAxisMax(120d); mRenderer.setXAxisMax(24d); mRenderer.setShowGrid(true); mRenderer.setXLabels(24); mRenderer.setChartTitle("玩家年龄--24小时变化图"); mCurrentRenderer=seriesRenderer; double x=0; double y=0; int [] [] xyValues=getValues(); for (int i = 0; i < xyValues.length; i++) { x=xyValues[i][0]; y=xyValues[i][1]; mCurrentSeries.add(x, y); } } /** * 画图 */ protected void onResume() { super.onResume(); if (mChartView==null) { LinearLayout layout=(LinearLayout) findViewById(R.id.chart); mChartView=ChartFactory.getLineChartView(this, mDataset, mRenderer); layout.addView(mChartView,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); }else { mChartView.setBackgroundResource(R.id.chart); mChartView.repaint(); } } /** * 拆线点的坐标 * @return */ private int[][] getValues(){ int [][] xyValues={{ 0, 88 }, { 1, 79 }, { 2, 75 }, { 3, 71 }, { 4, 90 }, { 5, 88 }, { 6, 66 }, { 7, 65 }, { 8, 50 }, { 9, 78 }, { 10, 67 }, { 11, 77 }, { 12, 79 }, { 13, 85 }, { 14, 89 }, { 15, 93 }, { 16, 95 }, { 17, 99 }, { 18, 66 }, { 19, 55 }, { 20, 44 },{24,30}}; return xyValues; } } [/java] 主界面和柱状图主要代码: [java] public class ChartBuilder extends Activity { private XYMultipleSeriesDataset dataset; private XYMultipleSeriesRenderer renderer; private Button lineButton; private Button barButton; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.chart_builder); lineButton = (Button) findViewById(R.id.lineButton); lineButton.setOnClickListener(onButtonClickListener); barButton = (Button) findViewById(R.id.barButton); barButton.setOnClickListener(onButtonClickListener); } private OnClickListener onButtonClickListener = new OnClickListener() { @Override public void onClick(View v) { if (v.getId() == R.id.lineButton) { startLineChart(); } if (v.getId() == R.id.barButton) { startBarChart(); } } }; private void startLineChart() { Intent intent = new Intent(); intent.setClass(this, LineChartBuilder.class); startActivity(intent); } private void startBarChart() { renderer = this.getBarRenderer(); dataset = this.getBarDataset(this.getMoney()); initNames(); Intent intent = ChartFactory.getBarChartIntent(this, dataset, renderer, org.achartengine.chart.BarChart.Type.DEFAULT); startActivity(intent); } private XYMultipleSeriesRenderer getBarRenderer() { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); SimpleSeriesRenderer simpleRenderer = new SimpleSeriesRenderer(); simpleRenderer.setColor(Color.RED); renderer.addSeriesRenderer(simpleRenderer); this.setCharSettings(renderer); return renderer; } /** * 设置描绘器的属性 * @param renderer */ private void setCharSettings(XYMultipleSeriesRenderer renderer) { renderer.setChartTitle("个人收入表"); renderer.setXTitle("姓名"); renderer.setYTitle("金额"); renderer.setXAxisMin(0.5); renderer.setXAxisMax(10.5); renderer.setBarSpacing(0.5); renderer.setYAxisMin(0); renderer.setYAxisMax(10000); renderer.setYLabelsAlign(Align.RIGHT); renderer.setDisplayChartValues(true); renderer.setShowGrid(true); renderer.setXLabels(0); renderer.setXLabelsAlign(Align.CENTER); renderer.setPanEnabled(true, false); } /** * 适配数据到数据集XYMultipleSeriesDataset * @param moneys * @return */ private XYMultipleSeriesDataset getBarDataset(double[] moneys) { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); CategorySeries series = new CategorySeries("2013年3月金额"); for (int i = 0; i < moneys.length; i++) { series.add(moneys[i]); } dataset.addSeries(series.toXYSeries()); return dataset; } /** * y轴上的数值 * @return */ private double[] getMoney() { double[] moneys = { 1500, 2000, 4000, 4500, 5000, 6000, 7600, 8000, 9100, 9500 }; return moneys; } /** * x轴下面的label名称 */ private void initNames() { String[] names = { "张三", "李四", "王五", "刘六", "肖七", "周一", "杨二", "袁八", "封九", "潘十" }; for (int i = 0; i < names.length; i++) { renderer.addTextLabel(i+1, names[i]); } } } [/java] 源码下载地址:http://download.csdn.net/detail/homg92/6216873 源码用utf-8编码 获得intent和view的方法介绍:

条形图

static android.content.Intent

getBarChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,BarChart.Type type)

条形图

static android.content.Intent

getBarChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,BarChart.Type type, java.lang.String activityTitle)

条形图

static GraphicalView

getBarChartView(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,BarChart.Type type)

气泡图

static android.content.Intent

getBubbleChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)

气泡图

static android.content.Intent

getBubbleChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, java.lang.String activityTitle)

气泡图

static GraphicalView

getBubbleChartView(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)

组合图

static android.content.Intent

getCombinedXYChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, java.lang.String[] types, java.lang.String activityTitle)

组合图

static GraphicalView

getCombinedXYChartView(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, java.lang.String[] types)

立方折线图

static GraphicalView

getCubeLineChartView(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, float smoothness)

立方折线图

static android.content.Intent

getCubicLineChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, float smoothness)

立方折线图

static android.content.Intent

getCubicLineChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, float smoothness, java.lang.String activityTitle)

拨号盘

static android.content.Intent

getDialChartIntent(android.content.Context context,CategorySeries dataset, DialRenderer renderer, java.lang.String activityTitle)

拨号盘

static GraphicalView

getDialChartView(android.content.Context context, CategorySeries dataset,DialRenderer renderer)

圆环图

static android.content.Intent

getDoughnutChartIntent(android.content.Context context,MultipleCategorySeries dataset, DefaultRenderer renderer, java.lang.String activityTitle)

圆环图

static GraphicalView

getDoughnutChartView(android.content.Context context,MultipleCategorySeries dataset, DefaultRenderer renderer)

圆环图

static android.content.Intent

getLineChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)

折线图

static android.content.Intent

getLineChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, java.lang.String activityTitle)

折线图

static GraphicalView

getLineChartView(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)

饼图

static android.content.Intent

getPieChartIntent(android.content.Context context, CategorySeries dataset,DefaultRenderer renderer, java.lang.String activityTitle)

饼图

static GraphicalView

getPieChartView(android.content.Context context, CategorySeries dataset,DefaultRenderer renderer)

范围条形图

static android.content.Intent

getRangeBarChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,BarChart.Type type, java.lang.String activityTitle)

范围条形图

static GraphicalView

getRangeBarChartView(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,BarChart.Type type)

散点图

static android.content.Intent

getScatterChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)

散点图

static android.content.Intent

getScatterChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, java.lang.String activityTitle)

散点图

static GraphicalView

getScatterChartView(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)

时间图

static android.content.Intent

getTimeChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, java.lang.String format)

时间图

static android.content.Intent

getTimeChartIntent(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, java.lang.String format, java.lang.String activityTitle)

时间图

static GraphicalView

getTimeChartView(android.content.Context context,XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, java.lang.String format)

描绘器和数据集的属性解析:

DefaultRenderer:

void

addSeriesRenderer(int index, SimpleSeriesRenderer renderer) 增加一个renderer到multiple renderer中,index是指定的位置.

void

addSeriesRenderer(SimpleSeriesRenderer renderer)增加一个renderer到multiple renderer中

int

getAxesColor() 得到X轴的颜色.

int

getBackgroundColor() 得到背景色

java.lang.String

getChartTitle() 得到图表的标题

float

getChartTitleTextSize() 得到图表标题的字号

int

getLabelsColor() 得到Y轴的颜色.

float

getLabelsTextSize() 得到Y轴的字号.

int

getLegendHeight() 得到图例的高度.

float

getLegendTextSize() 得到图例的字号.

int[]

getMargins() 得到图表外边框的大小.

float

getOriginalScale() Returns the original value to be used for scaling the chart.

float

getScale() Returns the value to be used for scaling the chart.

int

getSelectableBuffer() 得到点的缓冲半径值(在某点附件点击时,多大范围内都算点击这个点) Returns the selectable radius value around clickable points.

SimpleSeriesRenderer

getSeriesRendererAt(int index) 在multiple renderer中得到指定位置的 renderer.

int

getSeriesRendererCount() 得到multiple renderer集合中renderer的个数.

SimpleSeriesRenderer[]

getSeriesRenderers() 得到 multiple renderer 中的renderer数组.

float

getStartAngle() 得到圆形图(例如:饼图/圆环图等)的开始角度.

java.lang.String

getTextTypefaceName()

得到字体名.

int

getTextTypefaceStyle()

得到字体类型.

float

getZoomRate()

得到放大缩小的倍率.

boolean

isAntialiasing() Returns the antialiasing flag value.

boolean

isApplyBackgroundColor()

得到是否应用背景色的设置结果(true/false).

boolean

isClickEnabled()

得到是否启用点击事件.

boolean

isExternalZoomEnabled() Returns the enabled state of the external (application implemented) zoom.

boolean

isFitLegend()

得到是否设置图例大小自适应.

boolean

isInScroll() Returns if the chart is inside a scroll view and doesn't need to shrink.

boolean

isPanEnabled() 得到是否允许拖动 Returns the enabled state of the pan.

boolean

isShowAxes()

得到是否显示X轴.

boolean

isShowCustomTextGrid()

得到是否显示X轴和Y轴网格.

boolean

isShowGridX()

得到是否显示X轴网格.

boolean

isShowGridY()

得到是否显示Y轴网格.

boolean

isShowLabels()

得到是否显示刻度.

boolean

isShowLegend()

得到是否显示图例.

boolean

isZoomButtonsVisible()

得到是否显示放到缩小按钮.

boolean

isZoomEnabled()

是否支持放大缩小.

void

removeSeriesRenderer(SimpleSeriesRenderer renderer) 在multiple renderer中移除一个renderer.

void

setAntialiasing(boolean antialiasing) Sets the antialiasing value.

void

setApplyBackgroundColor(boolean apply)

设置是否应用背景色.

void

setAxesColor(int color)

设置X轴的颜色.

void

setBackgroundColor(int color)

设置背景色.

void

setChartTitle(java.lang.String title)

设置图表的标题.

void

setChartTitleTextSize(float textSize)

设置图表标题字号.

void

setClickEnabled(boolean enabled)

设置是否允许单击事件.

void

setExternalZoomEnabled(boolean enabled) Sets the enabled state of the external (application implemented) zoom.

void

setFitLegend(boolean fit)

设置图例字号自适应.

void

setInScroll(boolean inScroll) To be set if the chart is inside a scroll view and doesn't need to shrink when not enough space.

void

setLabelsColor(int color)

设置坐标颜色.

void

setLabelsTextSize(float textSize) 设置坐标字号.

void

setLegendHeight(int height) 设置图例高度(单位: pixels).

void

setLegendTextSize(float textSize)

设置图例字号.

void

setMargins(int[] margins)

设置外边框(单位:pixels)上/左/下/右.

void

setPanEnabled(boolean enabled) 设置是否允许拖动 Sets the enabled state of the pan.

void

setScale(float scale) Sets the value to be used for scaling the chart.

void

setSelectableBuffer(int buffer) 设置点的缓冲半径值(在某点附件点击时,多大范围内都算点击这个点) Sets the selectable radius value around clickable points.

void

setShowAxes(boolean showAxes)

设置是否显示X轴.

void

setShowCustomTextGrid(boolean showGrid) 设置X轴和Y轴网格是否显示.

void

setShowGrid(boolean showGrid)

设置是否显示网格.

void

setShowGridX(boolean showGrid)

设置是否显示X轴网格.

void

setShowGridY(boolean showGrid)

设置是否显示Y轴网格.

void

setShowLabels(boolean showLabels)

设置是否显示坐标.

void

setShowLegend(boolean showLegend)

设置是否显示图例.

void

setStartAngle(float startAngle) 设置圆形图(例如:饼图/圆环图等)的开始角度.

void

setTextTypeface(java.lang.String typefaceName, int style)

设置字体名和类型.

void

setZoomButtonsVisible(boolean visible)

设置是否显示放大缩小按钮.

void

setZoomEnabled(boolean enabled)

设置是否允许放大和缩小.

void

setZoomRate(float rate) 设置放大缩小的倍率.

XYMultipleSeriesDataset

void

addSeries(int index, XYSeries series) 在指定位置增加一个序列.

void

addSeries(XYSeries series) 增加一个序列.

XYSeries[]

getSeries() 得到序列的数组

XYSeries

getSeriesAt(int index) 得到指定位置的序列.

int

getSeriesCount() 得到序列的个数.

void

removeSeries(int index) 移除指定位置的序列.

void

removeSeries(XYSeries series) 移除一个序列

你可能感兴趣的:(achartengine)