??? AChartEngine是一个针对Android程序开发的免费图表开发库,目前已经发布了4个版本。支持散点图、折线图、饼图、气泡图、柱状图、短棒图、仪表图等多种图表。Android开发者可以很方便的试用AChartEngine Chart库画出各种图表。?
该项目地址位于:?
http://code.google.com/p/achartengine/?
你可以选择合适的版本下载,每个版本都有一个jar包、实例源代码和java doc提供下载。当前最新版本为1.0。?
更详细的介绍请查看http://blog.csdn.net/lk_blog/article/details/7645509
?
?
在下载的achartengine-1.0.0-demo-source官方的demo文档里面有了详细的代码演示了使用
其demo 类模型如下
1.?ChartDemo这个类是整个应用程序的入口,运行之后的效果显示一个list.
2.?IDemoChart接口,这个接口定义了三个方法,
getName()返回值是listitem上显示的标题;
getDesc()返回值是listitem上显示的描述内容.
excute(context)返回值是一个Intent,当点击listitem后跳转到此Intent.
3.?AbstractDemoChart类是一个抽象类,实现接口IDemoChart接口,这个类中封装了构建DataSet和renderer的方法,目的快速构建我们的dataset和renderer.
? ? ?/** * 报表统一信息接口 * * */public interface IChart { String NAME = "name"; String DESC = "desc"; /** * 报表名 * * @return */ String getName(); /** * 报表描述 * * @return */ String getDesc(); /** * 返回值是一个Intent,当点击后跳转到此Intent. * * @param context * @return */ Intent execute(Context context);}
/** * * */public abstract class AbstractChart implements IChart { /** * Builds an XY multiple dataset using the provided values. 构建 * XYMultipleSeriesDataset * * @param titles * 每个序列的图例 * @param xValues * X轴的坐标 * * @param yValues * y轴的坐标 * * @return the XY multiple dataset */ protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues, List<double[]> yValues) { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); addXYSeries(dataset, titles, xValues, yValues, 0); return dataset; } // 向DataSet中添加序列. public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues, List<double[]> yValues, int scale) { int length = titles.length; for (int i = 0; i < length; i++) { XYSeries series = new XYSeries(titles[i], scale);// 这里注意与TimeSeries区别. double[] xV = xValues.get(i); double[] yV = yValues.get(i); int seriesLength = xV.length; for (int k = 0; k < seriesLength; k++) { series.add(xV[k], yV[k]); } dataset.addSeries(series); } } /** * Builds an XY multiple series renderer. 构建XYMultipleSeriesRenderer. * * @param colors * 每个序列的颜色 * @param styles * 每个序列点的类型(可设置三角,圆点,菱形,方块等多种) * @return XYMultipleSeriesRenderer */ protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); setRenderer(renderer, colors, styles); return renderer; } // 整个图表属性设置 protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) { // -->start renderer.setAxisTitleTextSize(16);// 设置轴标题文字的大小 renderer.setChartTitleTextSize(20); // 设置整个图表标题文字的大小 renderer.setLabelsTextSize(15); // 设置轴刻度文字的大小 renderer.setLegendTextSize(15);// 设置图例文字大小 renderer.setPointSize(5f);// 设置点的大小(图上显示的点的大小和图例中点的大小都会被设置) renderer.setMargins(new int[] { 20, 30, 15, 20 });// 设置图表的外边框(上/左/下/右) // -->end // 以下代码设置每个序列的颜色. // -->start int length = colors.length; for (int i = 0; i < length; i++) { XYSeriesRenderer r = new XYSeriesRenderer(); r.setColor(colors[i]);// 设置颜色 r.setPointStyle(styles[i]); renderer.addSeriesRenderer(r); } // -->end } /** * 设置renderer的一些属性. * * @param renderer * 要设置的renderer * @param title * 图表标题 * @param xTitle * X轴标题 * @param yTitle * Y轴标题 * @param xMin * X轴最小值 * @param xMax * X轴最大值 * @param yMin * Y轴最小值 * @param yMax * Y轴最大值 * @param axesColor * X轴颜色 * @param labelsColor * Y轴颜色 */ protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) { renderer.setChartTitle(title); renderer.setXTitle(xTitle); renderer.setYTitle(yTitle); renderer.setXAxisMin(xMin); renderer.setXAxisMax(xMax); renderer.setYAxisMin(yMin); renderer.setYAxisMax(yMax); renderer.setAxesColor(axesColor); renderer.setLabelsColor(labelsColor); } /** * 构建和时间有关的XYMultipleSeriesDataset,这个方法与buildDataset在参数上区别是需要List<Date[]>作参数 * . * * @param titles * 序列图例 * @param xValues * X轴值 * @param yValues * Y轴值 * @return XYMultipleSeriesDataset */ protected XYMultipleSeriesDataset buildDateDataset(String[] titles, List<Date[]> xValues, List<double[]> yValues) { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); int length = titles.length; for (int i = 0; i < length; i++) { TimeSeries series = new TimeSeries(titles[i]);// 构建时间序列TimeSeries Date[] xV = xValues.get(i); double[] yV = yValues.get(i); int seriesLength = xV.length; for (int k = 0; k < seriesLength; k++) { series.add(xV[k], yV[k]); } dataset.addSeries(series); } return dataset; } /** * 构建单个CategorySeries,可用于生成饼图,注意与buildMultipleCategoryDataset(构建圆环图)相区别. * * @param titles * the series titles * @param values * the values * @return the category 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; } /** * 构建MultipleCategorySeries,可用于构建圆环图(每个环是一个序列) * * @param titles * the series titles * @param values * the values * @return the category series */ protected MultipleCategorySeries buildMultipleCategoryDataset(String title, List<String[]> titles, List<double[]> values) { MultipleCategorySeries series = new MultipleCategorySeries(title); int k = 0; for (double[] value : values) { series.add(2007 + k + "", titles.get(k), value); k++; } return series; } /** * 构建DefaultRenderer. * * @param colors * 每个序列的颜色 * @return DefaultRenderer */ 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; } /** * 构建XYMultipleSeriesDataset,适用于柱状图. * * @param titles * 每中柱子序列的图列 * @param values * 柱子的高度值 * @return XYMultipleSeriesDataset */ protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); int length = titles.length; for (int i = 0; i < length; i++) { CategorySeries series = new CategorySeries(titles[i]); double[] v = values.get(i); int seriesLength = v.length; for (int k = 0; k < seriesLength; k++) { series.add(v[k]); } dataset.addSeries(series.toXYSeries()); } return dataset; } /** * 构建XYMultipleSeriesRenderer,适用于柱状图. * * @param colors * 每个序列的颜色 * @return XYMultipleSeriesRenderer */ protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); renderer.setAxisTitleTextSize(16); renderer.setChartTitleTextSize(20); renderer.setLabelsTextSize(15); renderer.setLegendTextSize(15); int length = colors.length; for (int i = 0; i < length; i++) { SimpleSeriesRenderer r = new SimpleSeriesRenderer(); r.setColor(colors[i]); renderer.addSeriesRenderer(r); } return renderer; }}
?