Android中图表AChartEngine

2015/7/29问题集

Android中图表AChartEngine

很多时候项目中我们需要对一些统计数据进行绘制表格,更多直观查看报表分析结果。基本有以下几种方法:

  • 1:可以进行android api进行draw这样的话,效率比较低

  • 2:使用开源绘表引擎,这样效率比较高.下面就介绍一种。

AChartEngine(简称ACE)是Google的一个开源图表库(for Android)。它功能强大,支持散点图、折线图、饼图、气泡图、柱状图、短棒图、仪表图等多种图表。

参考文章:http://www.cnblogs.com/android100/p/Android-AChartEngine.html

achartengine的数据模型

  • XYSeries: 最常用的数据结构,主要包括一系列的double型(x,y)点对及一个名称(title)。

  • XYValueSeries:包括一系列的(x,y,value)点对及一个名称(title)。

  • XYMultipleSeriesDataset:包含一系列XYSeries,是最终的数据结构

  • TimeSeries:与XYSeries类似,x变为Date型,可以转化为XYSeries。

  • CategorySeries:与XYSeries类似,x变为string型,可以转化为XYSeries。

  • MultipleCategorySeries:一系列CategorySeries。

不同的数据模型用于不同的图表显示。XYSeries可以用于折线图、直方图。CategorySeries可以用于圆饼图。每种数据模型都提供了丰富的操作接口,用于插入删除数据等多种操作。我们可以根据需要将数据存为合适的数据模型。

绘制器(渲染器Renderer)

绘制器声明了绘制图表的类型。主要包括以下几种。

SimpleSeriesRenderer、 XYSeriesRenderer用于设置每张图中每一个序列的绘制方法。而DefaultRenderer和 XYMultipleSeriesRenderer用于设置整张图的绘制格式。它们之间的关系类似于XYSeries 与XYMultipleSeriesDataset之间的关系。

画图概念

要 画一张图,我们需要设置好需要绘制的数据及绘制的方法。确定好data model及renderer后,再调用合适的绘制方法绘制图形。库提供了两种绘制图形的方法,一种是getIntent( )(如getLineChartIntent())直接新建一个Activity全屏显示生成的图表,另一种是getView( )(如getLineChartView())生成一个View,用户可以自行设置它的显示。

主要通过设置几个对象

1、XYSeries对象:用于存储一条线的数据信息;
2、XYMultipleSeriesDataset对象:即数据集,可添加多个XYSeries对象,因为一个折线图中可能有多条线。
3、XYSeriesRenderer对象:主要是用来设置一条线条的风格,颜色啊,粗细之类的。
4、XYMultipleSeriesRenderer对象:主要用来定义一个图的整体风格,设置xTitle,yTitle,chartName等等整体性的风格,
可添加多个XYSeriesRenderer对象,因为一个图中可以有多条折线。
设置完那些对象之后,可通过 org.achartengine.ChartFactory调用数据集XYMultipleSeriesDataset对象
与XYMultipleSeriesRenderer对象来画图并将图加载到GraphicalView中,
ChartFactory有多种api,通过这些api调用来决定是画折线图还是柱状图。

绘制基础

1,ChartFactory ,传入XYMutilpleSeriesRenderer,XYMutilpleSeriesDataset,然后,我们只需用 getXXXChartIntent(Context context,XYMutilpleSeriesRenderer,XYMutilpleSeriesDataset,)方法就可以进行图表的显示

2,XYMutilpleSeriesRenderer 用于进行绘制的设置,添加的XYSeriesRender对象,用于定义绘制的点集合设置,注意数量要和XYMutilpleSeriesDataset,添加的XYseries一致!!!

3,XYMutilpleSeriesDataset 用于数据的存放,添加的XYseries对象,用于提供绘制的点集合的数据

绘制实例

这个实例告诉我们,不必跟着AChartEngine也可以照样实现。只需要懂得原理就行。
原理就是

  • 1.构造渲染图
  • 2.进行显示(构建数据)
  • 3.对点的绘制进行设置
  • / 3.1设置颜色

    3.2设置点的样式

    3.3, 将要绘制的点添加到坐标绘制中

    3.4,重复 1~3的步骤绘制第二个系列点

package com.zsz.develop.mychart;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

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

import java.util.Random;

/**
 * Created by shengzhong on 2015/7/29.
 */
public class ShowExamActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // 1, 构造显示用渲染图
        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));
            // 填充数据
            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);
        Intent intent = ChartFactory
                .getLineChartIntent(this, dataset, renderer);
        startActivity(intent);



//          GraphicalView graphicalView = ChartFactory.getLineChartView(this, dataset, renderer);
//    llChart= (LinearLayout) findViewById(R.id.llChart);
  //  llChart.addView(graphicalView);
    }

}    

把得到的图标设置成View

绑定layout后找到LinearLayout,把他添加到LinearLayout里去

  GraphicalView graphicalView = ChartFactory.getLineChartView(this, dataset, renderer);
llChart= (LinearLayout) findViewById(R.id.llChart);
llChart.addView(graphicalView);

构建一个柱状图实例

先定义一个接口AChartAbstract

package com.zsz.develop.chartdemo;

import android.content.Context;
import android.content.Intent;

public interface AChartAbstract {
/**
 * 获取一个当前类型图标的Intent实例
 */
public Intent getIntent(Context context);
}

新建一个java类BarChart来实现AChartAbstract接口

package com.zsz.develop.chartdemo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;

import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

/**
 * Created by shengzhong on 2015/7/29.
 */
public class BarChart implements AChartAbstract {

public Intent getIntent(Context context) {
    Intent mIntent=ChartFactory.getBarChartIntent(context, getDataSet(), getRenderer(), Type.STACKED, "当月开票金额树状图");
    return mIntent;
}

/**
 * 构造数据
 * @return
 */
public XYMultipleSeriesDataset getDataSet() {
    // 构造数据
    XYMultipleSeriesDataset barDataset = new XYMultipleSeriesDataset();
    CategorySeries barSeries = new CategorySeries("2014年3月");
    barSeries.add(865.5969);
    barSeries.add(2492.6479);
    barSeries.add(891.0137);
    barSeries.add(0.0);
    barSeries.add(691.0568);
    barDataset.addSeries(barSeries.toXYSeries());
    return barDataset;
}

/**
 * 构造渲染器
 * @return
 */
public XYMultipleSeriesRenderer getRenderer() {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
//      renderer.setChartTitle("当月开票");
//      // 设置标题的字体大小
//      renderer.setChartTitleTextSize(16);
    renderer.setXTitle("事业部");
    renderer.setYTitle("单位(万元)");
    renderer.setAxesColor(Color.WHITE);
    renderer.setLabelsColor(Color.WHITE);
    // 设置X轴的最小数字和最大数字
    renderer.setXAxisMin(0.5);
    renderer.setXAxisMax(5.5);
    // 设置Y轴的最小数字和最大数字
    renderer.setYAxisMin(0);
    renderer.setYAxisMax(3000);
    renderer.addXTextLabel(1, "电网");
    renderer.addXTextLabel(2, "通信");
    renderer.addXTextLabel(3, "宽带");
    renderer.addXTextLabel(4, "专网");
    renderer.addXTextLabel(5, "轨交");
    renderer.setZoomButtonsVisible(true);
    // 设置渲染器允许放大缩小
    renderer.setZoomEnabled(true);
    // 消除锯齿
    renderer.setAntialiasing(true);
    // 设置背景颜色
    renderer.setApplyBackgroundColor(true);
    renderer.setBackgroundColor(Color.GRAY);
    // 设置每条柱子的颜色
    SimpleSeriesRenderer sr = new SimpleSeriesRenderer();
    sr.setColor(Color.YELLOW);
    renderer.addSeriesRenderer(sr);
    // 设置每个柱子上是否显示数值
    renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
    // X轴的近似坐标数  (这样不显示横坐标)
    renderer.setXLabels(0);
    // Y轴的近似坐标数
    renderer.setYLabels(6);
    // 刻度线与X轴坐标文字左侧对齐
    renderer.setXLabelsAlign(Paint.Align.LEFT);
    // Y轴与Y轴坐标文字左对齐
    renderer.setYLabelsAlign(Paint.Align.LEFT);
    // 允许左右拖动,但不允许上下拖动.
    renderer.setPanEnabled(true, false);
    // 柱子间宽度
    renderer.setBarSpacing(0.5f);
    // 设置X,Y轴单位的字体大小
    renderer.setAxisTitleTextSize(20);
    return renderer;
}
}

主函数中调用

Intent lineIntent = new BarChart().getIntent(this);
    startActivity(lineIntent);

最后不要忘了在Androidmanifest.xml文件中进行配置文件

<activity android:name="org.achartengine.GraphicalActivity" />

Android Studio的两个概念:Project 和 Module

Android Studio中的两个概念。 Project 和 Module 。
在Android Studio中, Project 的真实含义是工作空间, Module 为一个具体的项目。

在 Eclipse 中,我们可以同时对多个 Eclipse 的 Project 进行同时编辑,这些 Project 在同一个 workspace 之中。在Android Studio中,我们可以同时对多个Android Studio的 Module 进行同时编辑,这些 Module 在同一个 Project 之中。

Eclipse 的 Project 等同于Android Studio的 Module 。
Eclipse 的 workspace 等同于Android Studio的 Project 。


AChartEngine日期折线图的例子

首先要导入他给的实例代码类AbstractDemoChart抽象类,和接口IDemoChart,与WeightDialChart

package com.zsz.develop.mychart;

import java.util.Date;
import java.util.List;

import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.MultipleCategorySeries;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

public abstract class AbstractDemoChart implements IDemoChart {

/**
 * 构建 XYMultipleSeriesDataset.
 *
 * @param titles 每个序列的图例
 * @param xValues X轴的坐标
 * @param yValues Y轴的坐标
 * @return XYMultipleSeriesDataset
 */
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);
    }
}

/**
 * 构建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(40);//设置整个图表标题文字的大小
    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;
}

}

=======
接口IDemoChart

/**
 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zsz.develop.mychart;

import android.content.Context;
import android.content.Intent;

/**
 * Defines the demo charts.
 */
public interface IDemoChart {
  /** A constant for the name field in a list activity. */
  String NAME = "name";
  /** A constant for the description field in a list activity. */
  String DESC = "desc";

  /**
   * Returns the chart name.
   * 
   * @return the chart name
   */
  String getName();

  /**
   * Returns the chart description.
   * 
   * @return the chart description
   */
  String getDesc();

  /**
   * Executes the chart demo.
   * 
   * @param context the context
   * @return the built intent
   */
  Intent execute(Context context);

}

======
/**

 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zsz.develop.mychart;

import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DialRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.DialRenderer.Type;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

/**
 * Budget demo pie chart.
 */
public class WeightDialChart extends AbstractDemoChart {
  /**
   * Returns the chart name.
   * @return the chart name
   */
  public String getName() {
    return "Weight chart";
  }

  /**
   * Returns the chart description.
   * @return the chart description
   */
  public String getDesc() {
    return "The weight indicator (dial chart)";
  }

  /**
   * Executes the chart demo.
   * @param context the context
   * @return the built intent
   */
  public Intent execute(Context context) {
   CategorySeries category = new CategorySeries("Weight indic");
category.add("Current", 75);
category.add("Minimum", 65);
category.add("Maximum", 90);
DialRenderer renderer = new DialRenderer();
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] {20, 30, 15, 0});
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(Color.BLUE);
renderer.addSeriesRenderer(r);
r = new SimpleSeriesRenderer();
r.setColor(Color.rgb(0, 150, 0));
renderer.addSeriesRenderer(r);
r = new SimpleSeriesRenderer();
r.setColor(Color.GREEN);
renderer.addSeriesRenderer(r);
renderer.setLabelsTextSize(10);
renderer.setLabelsColor(Color.WHITE);
renderer.setShowLabels(true);
renderer.setVisualTypes(new Type[] {Type.ARROW, Type.NEEDLE, Type.NEEDLE});
renderer.setMinValue(0);
renderer.setMaxValue(150);
return ChartFactory.getDialChartIntent(context, category, renderer, "Weight indicator");
  }

}

通过这个WeightTimeChart类进行实现

/**
 * WeightTimeChart.java
 * 版权所有(C) 2013 
 * 创建:cuiran 2013-10-24 下午4:11:28
 */
package com.zsz.develop.mychart;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

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



import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

/**
 * TODO 折线图
 * @author cuiran
 * @version 1.0.0
 */
public class WeightTimeChart extends AbstractDemoChart {
private static final int SERIES_NR = 1;
/* (non-Javadoc)
 * @see com.cayden.weightnotes.achartengine.IDemoChart#getName()
 */
@Override
public String getName() {
    // TODO Auto-generated method stub
    return "WeightTimeChart";
}

public static Date strToDate(String strDate) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    ParsePosition pos = new ParsePosition(0);
    Date strtodate = formatter.parse(strDate, pos);
    return strtodate;
}

/* (non-Javadoc)
 * @see com.cayden.weightnotes.achartengine.IDemoChart#getDesc()
 */
@Override
public String getDesc() {
    // TODO Auto-generated method stub
    return "WeightTimeChart";
}

private XYMultipleSeriesDataset getDateDemoDataset() {
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    final int nr = 7;



    long value = new Date().getTime() - 3 * TimeChart.DAY;
    Random r = new Random();
    for (int i = 0; i < SERIES_NR; i++) {
        TimeSeries series = new TimeSeries("体重变化趋势 ");

        series.add(WeightTimeChart.strToDate("2013-10-19"), 85.3);
        series.add(WeightTimeChart.strToDate("2013-10-20"), 85.6);
        series.add(WeightTimeChart.strToDate("2013-10-21"), 84.5);
        series.add(WeightTimeChart.strToDate("2013-10-22"), 85.0);
        series.add(WeightTimeChart.strToDate("2013-10-23"), 85.1);
        series.add(WeightTimeChart.strToDate("2013-10-24"), 85.3);
        series.add(WeightTimeChart.strToDate("2013-10-25"), 85.3);
        series.add(WeightTimeChart.strToDate("2013-10-26"), 85.3);

//              for (int k = 0; k < nr; k++) {
//                series.add(new Date(value + k * TimeChart.DAY / 4), 20 + r.nextInt() % 100);
//              }

        dataset.addSeries(series);

    }
    return dataset;
}

private XYMultipleSeriesRenderer getDemoRenderer() {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    renderer.setAxisTitleTextSize(40);
    renderer.setChartTitleTextSize(40);
    renderer.setLabelsTextSize(40);
    renderer.setLegendTextSize(18);
    renderer.setPointSize(6f);
    //in this order: top, left, bottom, right
    renderer.setMargins(new int[] {20, 30, 15, 0});


    XYSeriesRenderer r = new XYSeriesRenderer();

    r = new XYSeriesRenderer();
    r.setPointStyle(PointStyle.CIRCLE);
    r.setColor(Color.GREEN);
    r.setFillPoints(true);

    renderer.addSeriesRenderer(r);
    renderer.setAxesColor(Color.DKGRAY);
    renderer.setLabelsColor(Color.LTGRAY);
    renderer.setChartTitle("体重变化趋势");
    renderer.setXTitle("日期");
    renderer.setYTitle("体重(kg)");
    renderer.setZoomButtonsVisible(true);

    int length = renderer.getSeriesRendererCount();
    for (int i = 0; i < length; i++) {
        SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
        seriesRenderer.setDisplayChartValues(true);
    }
    return renderer;
}
/* (non-Javadoc)
 * @see com.cayden.weightnotes.achartengine.IDemoChart#execute(android.content.Context)
 */
@Override
public Intent execute(Context context) {
    // TODO Auto-generated method stub

    return ChartFactory.getTimeChartIntent(context, getDateDemoDataset(), getDemoRenderer(), "yyyy-MM-dd");
}

}

最后在MainActivity里调用

package com.zsz.develop.mychart;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

private IDemoChart[] mCharts = new IDemoChart[] {new WeightDialChart(),
        new WeightTimeChart()};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent=mCharts[1].execute(this);

    startActivity(intent);

}



}

最后别忘了在配置文件中添加