AchartEngine绘图引擎

https://code.google.com/p/achartengine/ 

Code Test代码:
/workspace/AChartEngineTest   /workspace/appcompat_v7
/workspace/AChartEngineDemo

AChartEngine is a charting library for Android applications. It currently supports the following chart types:

  • line chart
  • area chart
  • scatter chart
  • time chart
  • bar chart
  • pie chart
  • bubble chart
  • doughnut chart
  • range (high-low) bar chart
  • dial chart / gauge
  • combined (any combination of line, cubic line, scatter, bar, range bar, bubble) chart
  • cubic line chart
All the above supported chart types can contain multiple series, can be displayed with the X axis horizontally (default) or vertically and support many other custom features. The charts can be built as a view that can be added to a view group or as an intent, such as it can be used to start an activity.

The model and the graphing code is well optimized such as it can handle and display huge number of values.

AChartEngine is currently at the 1.0.0 release. New chart types will be added in the following releases. Please keep sending your feedback such as we can continually improve this library.

Find us on Facebook, too: http://www.facebook.com/achartengine

Read a short introduction to AChartEngine here: http://www.javaadvent.com/2012/12/achartengine-charting-library-for.html

Another good tutorial can be read here: http://jaxenter.com/effort-free-graphs-on-android-with-achartengine-46199.html


1、导入jar包

2、直接列出实现的绘图以及代码

(1)



  1. package com.example.achartenginetest.charts;
  2. import java.text.DecimalFormat;
  3. import java.text.NumberFormat;
  4. import org.achartengine.ChartFactory;
  5. import org.achartengine.GraphicalView;
  6. import org.achartengine.model.CategorySeries;
  7. import org.achartengine.renderer.DefaultRenderer;
  8. import org.achartengine.renderer.SimpleSeriesRenderer;
  9. import android.content.Context;
  10. import android.graphics.Color;
  11. public class SimplePieChart extends AbstractChart{
  12. @Override
  13. public String getName() {
  14. return "本周血糖分析";
  15. }
  16. @Override
  17. public String getDesc() {
  18. // TODO Auto-generated method stub
  19. return "本周血糖分析";
  20. }
  21. @Override
  22. public GraphicalView execute(Context context) {
  23. double[] values = new double[] { 0.52, 0.2, 0.16, 0.12};
  24. int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.YELLOW, Color.RED};
  25. DefaultRenderer renderer = buildCategoryRenderer(colors);
  26. renderer.setZoomButtonsVisible(false);
  27. renderer.setZoomEnabled(false);
  28. renderer.setChartTitleTextSize(30);
  29. renderer.setLabelsTextSize(25);
  30. renderer.setLabelsColor(Color.WHITE);
  31. renderer.setDisplayValues(true);
  32. renderer.setShowLabels(false);
  33. renderer.setPanEnabled(false);
  34. renderer.setZoomEnabled(false);
  35. SimpleSeriesRenderer mSimple = renderer.getSeriesRendererAt(0);
  36. NumberFormat nf = new DecimalFormat("正常00.#%");
  37. mSimple.setChartValuesFormat(nf);
  38. mSimple = renderer.getSeriesRendererAt(1);
  39. nf = new DecimalFormat("控制良好00.#%");
  40. mSimple.setChartValuesFormat(nf);
  41. mSimple = renderer.getSeriesRendererAt(2);
  42. nf = new DecimalFormat("控制一般00.#%");
  43. mSimple.setChartValuesFormat(nf);
  44. mSimple = renderer.getSeriesRendererAt(3);
  45. nf = new DecimalFormat("警告00.#%");
  46. mSimple.setChartValuesFormat(nf);
  47. GraphicalView view = ChartFactory.getPieChartView(context,
  48. buildCategoryDataset("血糖", values), renderer);
  49. return view;
  50. }
  51. @Override
  52. protected CategorySeries buildCategoryDataset(String title,double[]values){
  53. CategorySeries series = new CategorySeries(title);
  54. series.add("正常",values[0]);
  55. series.add("控制良好",values[1]);
  56. series.add("控制一般",values[2]);
  57. series.add("警告", values[3]);
  58. return series;
  59. }
  60. }
(2)

  1. package com.example.achartenginetest.charts;
  2. import java.text.DecimalFormat;
  3. import java.text.NumberFormat;
  4. import org.achartengine.ChartFactory;
  5. import org.achartengine.GraphicalView;
  6. import org.achartengine.model.CategorySeries;
  7. import org.achartengine.renderer.DefaultRenderer;
  8. import org.achartengine.renderer.SimpleSeriesRenderer;
  9. import android.content.Context;
  10. import android.graphics.Color;
  11. public class HighlightPieChart extends AbstractChart{
  12. @Override
  13. public String getName() {
  14. return "用药分析";
  15. }
  16. @Override
  17. public String getDesc() {
  18. return "用药分析";
  19. }
  20. @Override
  21. public GraphicalView execute(Context context) {
  22. double[] values = new double[] {0.86, 0.14};
  23. int[] colors = new int[] { Color.BLUE, Color.GREEN};
  24. DefaultRenderer renderer = buildCategoryRenderer(colors);
  25. renderer.setZoomButtonsVisible(false);
  26. renderer.setZoomEnabled(false);
  27. renderer.setChartTitleTextSize(30);
  28. renderer.setLabelsTextSize(25);
  29. renderer.setLabelsColor(Color.WHITE);
  30. renderer.setDisplayValues(true);
  31. renderer.setShowLabels(false);
  32. renderer.setPanEnabled(false);
  33. renderer.setZoomEnabled(false);
  34. SimpleSeriesRenderer mSimple = renderer.getSeriesRendererAt(0);
  35. NumberFormat nf = new DecimalFormat("正常用药00.#%");
  36. mSimple.setChartValuesFormat(nf);
  37. mSimple = renderer.getSeriesRendererAt(1);
  38. nf = new DecimalFormat("忘记用药00.#%");
  39. mSimple.setChartValuesFormat(nf);
  40. mSimple.setGradientEnabled(false);
  41. mSimple.setHighlighted(true);
  42. GraphicalView view = ChartFactory.getPieChartView(context,
  43. buildCategoryDataset("用药", values), renderer);
  44. return view;
  45. }
  46. @Override
  47. protected CategorySeries buildCategoryDataset(String title,double[]values){
  48. CategorySeries series = new CategorySeries(title);
  49. series.add("正常用药",values[0]);
  50. series.add("忘记用药",values[1]);
  51. return series;
  52. }
  53. }

(3)


  1. package com.example.achartenginetest.charts;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.achartengine.ChartFactory;
  5. import org.achartengine.GraphicalView;
  6. import org.achartengine.chart.PointStyle;
  7. import org.achartengine.model.XYMultipleSeriesDataset;
  8. import org.achartengine.renderer.XYMultipleSeriesRenderer;
  9. import org.achartengine.renderer.XYSeriesRenderer;
  10. import android.content.Context;
  11. import android.graphics.Color;
  12. import android.graphics.Paint.Align;
  13. public class SimpleLineChart extends AbstractChart{
  14. @Override
  15. public String getName() {
  16. return "本周运动分析";
  17. }
  18. @Override
  19. public String getDesc() {
  20. return "本周运动分析,LineChart";
  21. }
  22. @Override
  23. public GraphicalView execute(Context context) {
  24. String[] titles = new String[] {"上周运动", "本周运动"};
  25. List<double[]> x = new ArrayList<double[]>();
  26. for (int i = 0; i < titles.length; i++) {
  27. x.add(new double[] { 1, 2, 3, 4, 5, 6, 7});
  28. }
  29. List<double[]> values = new ArrayList<double[]>();
  30. values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 20.3});
  31. values.add(new double[] { 10, 10, 12, 15, 20, 24, 26});
  32. int[] colors = new int[] { Color.BLUE, Color.GREEN};
  33. PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND};
  34. XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
  35. int length = renderer.getSeriesRendererCount();
  36. for (int i = 0; i < length; i++) {
  37. ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
  38. }
  39. setChartSettings(renderer, "每周运动量", "时间", "运动时间(分钟)", 0.8, 7.2, 0, 30,
  40. Color.LTGRAY, Color.LTGRAY);
  41. // renderer.setXLabels(7);//设置X轴显示的刻度标签的个数
  42. renderer.setXLabels(0);//设置X轴显示的刻度采用自定义
  43. renderer.addXTextLabel(1, "周一");
  44. renderer.addXTextLabel(2, "周二");
  45. renderer.addXTextLabel(3, "周三");
  46. renderer.addXTextLabel(4, "周四");
  47. renderer.addXTextLabel(5, "周五");
  48. renderer.addXTextLabel(6, "周六");
  49. renderer.addXTextLabel(7, "周日");
  50. renderer.setYLabels(20);//设置Y轴显示的刻度标签的个数
  51. renderer.setShowGrid(true);
  52. renderer.setXLabelsAlign(Align.CENTER);
  53. renderer.setYLabelsAlign(Align.RIGHT);
  54. renderer.setZoomButtonsVisible(false);
  55. // renderer.setPanLimits(new double[] { 0.5, 7.5, 0, 30 }); /// /设置拖动时 X 轴 Y 轴允许的最大值最小值
  56. // renderer.setZoomLimits(new double[] { 0.5, 7.5, 0, 30 });
  57. renderer.setPanEnabled(false);
  58. renderer.setZoomEnabled(false);
  59. renderer.setAxisTitleTextSize(25);// 设置坐标轴标题文本大小
  60. renderer.setChartTitleTextSize(30); // 设置图表标题文本大小
  61. renderer.setLabelsTextSize(23); // 设置轴标签文本大小
  62. renderer.setXLabelsPadding(30);
  63. // renderer.setYLabelsPadding(50);
  64. renderer.setMargins(new int[]{0,80,0,0});
  65. renderer.setYLabelsVerticalPadding(50);
  66. XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
  67. // XYSeries series = dataset.getSeriesAt(0);
  68. // series.addAnnotation("Vacation", 6, 30);
  69. GraphicalView view = ChartFactory.getLineChartView(context, dataset, renderer);
  70. return view;
  71. }
  72. }

(4)


  1. package com.example.achartenginetest.charts;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Random;
  5. import org.achartengine.ChartFactory;
  6. import org.achartengine.GraphicalView;
  7. import org.achartengine.chart.CubicLineChart;
  8. import org.achartengine.chart.LineChart;
  9. import org.achartengine.chart.PointStyle;
  10. import org.achartengine.model.XYMultipleSeriesDataset;
  11. import org.achartengine.model.XYSeries;
  12. import org.achartengine.renderer.XYMultipleSeriesRenderer;
  13. import org.achartengine.renderer.XYSeriesRenderer;
  14. import org.achartengine.renderer.XYSeriesRenderer.FillOutsideLine;
  15. import android.content.Context;
  16. import android.graphics.Color;
  17. import android.graphics.Typeface;
  18. public class ComplexLineChart extends AbstractChart{
  19. private final static int NUM_X = 11;
  20. private final static int[] COLORS_DATA = new int[]{Color.RED, Color.YELLOW, Color.BLUE, Color.rgb(255, 124, 3),Color.GREEN,Color.rgb(85, 95, 10),Color.GRAY,Color.rgb(97,76,32)};
  21. private final static PointStyle[] STYLES_DATA = new PointStyle[]{PointStyle.CIRCLE,PointStyle.DIAMOND,PointStyle.SQUARE,PointStyle.TRIANGLE,PointStyle.POINT};
  22. private final static String[] TITLE_DAY = new String[]{"当天","昨天","前天"};
  23. private final static String[] TITLE_WEEK = new String[]{"本周","上一周","上两周","上三周"};
  24. private final static String[] TITLE_MONTH = new String[]{"本月","上一月","上两月","上三月"};
  25. private final static int DAY_DATA = 0;
  26. private final static int WEEK_DATA = 1;
  27. private final static int MONTH_DATA = 2;
  28. private XYMultipleSeriesRenderer mRenderer = null;
  29. private XYMultipleSeriesDataset mDataset = null;
  30. private int num_series = 0;
  31. private List<double[]> mDataList = null;
  32. private int mDataType = WEEK_DATA;
  33. private Random mRandom = null; //用于随机获取PointStyle;
  34. public ComplexLineChart(){
  35. initBackground();
  36. intParams();
  37. /**
  38. * 测试监测数据
  39. */
  40. mDataList = new ArrayList<double[]>();
  41. mDataList.add(new double[] {3.8, 7.8, 4.5, 3.9, 9.2, 4.0, 5.0, 8.2, 7.1, 5.0, 3.9});
  42. mDataList.add(new double[] {4.2, 8.8, 4.1, 3.7, 7.2, 4.1, 4.0, 8.7, 5.1, 5.0, 4.9});
  43. mDataList.add(new double[] {4.1, 9.0, 4.0, 3.1, 10.2, 5.1, 3.8, 8.5, 5.6, 4.0, 3.5});
  44. mDataList.add(new double[] {3.7, 6.1, 4.6, 4.1, 8.2, 4.1, 3.7, 9.5, 4.6, 2.0, 3.1});
  45. }
  46. public ComplexLineChart(List<double[]> data){
  47. initBackground();
  48. intParams();
  49. mDataList = data;
  50. }
  51. public ComplexLineChart(List<double[]> data, int datatype){
  52. initBackground();
  53. intParams();
  54. mDataList = data;
  55. mDataType = datatype;
  56. }
  57. private void intParams(){
  58. mRandom = new Random();
  59. }
  60. private void initBackground(){
  61. //*********************************************************************
  62. // 背景
  63. //*********************************************************************
  64. String[] titles = new String[] { "高血糖", "良好血糖","一般血糖","低血糖"};
  65. List<double[]> x = new ArrayList<double[]>();
  66. for (int i = 0; i < titles.length; i++) {
  67. x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
  68. }
  69. List<double[]> values = new ArrayList<double[]>();
  70. values.add(new double[] { 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12});
  71. values.add(new double[] { 6.1,10.0, 8.0, 6.1, 10.0, 8.0, 6.1, 10.0, 8.0, 6.1,6.1});
  72. values.add(new double[] { 5.1,8.5, 6.7, 5.1, 8.5, 6.7, 5.1, 8.5, 6.7, 5.1,5.1});
  73. values.add(new double[] { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4});
  74. int[] colors = new int[] { Color.argb(200, 255, 0, 0),
  75. Color.argb(200, 0, 0, 255),
  76. Color.argb(200, 0, 255, 0),
  77. Color.argb(200, 255, 0, 0)};
  78. PointStyle[] styles = new PointStyle[] { PointStyle.POINT, PointStyle.POINT, PointStyle.POINT, PointStyle.POINT};
  79. mRenderer = buildRenderer(colors, styles);
  80. setChartSettings(mRenderer, "", "时间", "血糖值", 0.8,11.2,0,12, Color.GRAY, Color.LTGRAY);
  81. mRenderer.setXLabels(0);//设置X轴显示的刻度采用自定义
  82. mRenderer.addXTextLabel(1, "早餐前");
  83. mRenderer.addXTextLabel(2, "早餐后1小时");
  84. mRenderer.addXTextLabel(3, "早餐后2小时");
  85. mRenderer.addXTextLabel(4, "午餐前");
  86. mRenderer.addXTextLabel(5, "午餐后1小时");
  87. mRenderer.addXTextLabel(6, "午餐后2小时");
  88. mRenderer.addXTextLabel(7, "晚餐前");
  89. mRenderer.addXTextLabel(8, "晚餐后1小时");
  90. mRenderer.addXTextLabel(9, "晚餐后2小时");
  91. mRenderer.addXTextLabel(10, "睡前");
  92. mRenderer.addXTextLabel(11, "凌晨2点");
  93. mRenderer.setYLabels(13);
  94. mRenderer.setChartTitleTextSize(30);
  95. mRenderer.setTextTypeface("sans_serif", Typeface.BOLD);
  96. mRenderer.setLabelsTextSize(20f);
  97. mRenderer.setAxisTitleTextSize(20);
  98. mRenderer.setLegendTextSize(20);
  99. // mRenderer.setShowLegend(true);
  100. mRenderer.setXLabelsAngle(-30);
  101. mRenderer.setZoomButtonsVisible(false);
  102. mRenderer.setPanEnabled(false);
  103. mRenderer.setZoomEnabled(false);
  104. int length = mRenderer.getSeriesRendererCount();
  105. for (int i = 0; i < length; i++) {
  106. XYSeriesRenderer seriesRenderer = (XYSeriesRenderer) mRenderer.getSeriesRendererAt(i);
  107. FillOutsideLine fill = new FillOutsideLine(FillOutsideLine.Type.BOUNDS_ALL);
  108. fill.setColor(colors[i]);
  109. seriesRenderer.addFillOutsideLine(fill);
  110. seriesRenderer.setLineWidth(2.5f);
  111. seriesRenderer.setDisplayChartValues(false);
  112. }
  113. mDataset = buildDataset(titles,x,values);
  114. num_series = titles.length;
  115. }
  116. @Override
  117. public String getName() {
  118. return "周血糖分析";
  119. }
  120. @Override
  121. public String getDesc() {
  122. return "周血糖分析,CubicLineChart & LineChart";
  123. }
  124. /**
  125. * 检查数据长度是否为11个取值点
  126. * @param data
  127. * @return 0:11个取值点,1:大于11个取值点,-1:小于11个取值点
  128. */
  129. private int checkData(double[] data){
  130. return data.length == NUM_X?0:(data.length<NUM_X?-1:1);
  131. }
  132. /**
  133. * 对数据进行修正,保证数据长度的正确性
  134. * @param data
  135. * @return
  136. */
  137. private double[] fixData(double[] data){
  138. int checkResult = checkData(data);
  139. if(checkResult == 0){
  140. return data;
  141. }else{
  142. double[] result = new double[NUM_X];
  143. if(checkResult == -1){
  144. for(int i =0; i<data.length;++i){
  145. result[i]=data[i];
  146. }
  147. for(int i = data.length; i<NUM_X; ++i){
  148. result[i] = 0;
  149. }
  150. }else{
  151. for(int i =0; i<NUM_X;++i){
  152. result[i]=data[i];
  153. }
  154. }
  155. return result;
  156. }
  157. }
  158. /**
  159. * 获取第indexData条数据线的点图
  160. * @return
  161. */
  162. private PointStyle getPointStyle(int indexData){
  163. if(null == mDataList || mDataList.size() == 0){
  164. return STYLES_DATA[0];
  165. }
  166. if(indexData<STYLES_DATA.length){
  167. return STYLES_DATA[indexData];
  168. }
  169. int index = mRandom.nextInt(STYLES_DATA.length);
  170. return STYLES_DATA[index];
  171. }
  172. /**
  173. * 获取第indexData条数据线的颜色
  174. * @param indexData
  175. * @return
  176. */
  177. private int getLineColor(int indexData){
  178. if(null == mDataList || mDataList.size() == 0){
  179. return COLORS_DATA[0];
  180. }
  181. if(indexData<COLORS_DATA.length){
  182. return COLORS_DATA[indexData];
  183. }
  184. int index = mRandom.nextInt(COLORS_DATA.length);
  185. return COLORS_DATA[index];
  186. }
  187. @Override
  188. public GraphicalView execute(Context context) {
  189. //*********************************************************************
  190. // 监测数值
  191. //*********************************************************************
  192. if(null != mDataList && mDataList.size() > 0){
  193. String[] titles;
  194. switch(mDataType){
  195. case DAY_DATA:
  196. titles = TITLE_DAY;
  197. break;
  198. case WEEK_DATA:
  199. titles = TITLE_WEEK;
  200. break;
  201. case MONTH_DATA:
  202. titles = TITLE_MONTH;
  203. break;
  204. default:
  205. titles = TITLE_WEEK;
  206. break;
  207. }
  208. int numLines = titles.length<=mDataList.size()?titles.length:mDataList.size();
  209. for(int j = 0; j< numLines; ++j){
  210. XYSeries week1Series = new XYSeries(titles[j]);
  211. double[] week1Data = fixData(mDataList.get(j));
  212. for(int i = 1; i<= week1Data.length; ++i){
  213. week1Series.add(i, week1Data[i-1]);
  214. }
  215. XYSeriesRenderer week1Renderer = new XYSeriesRenderer();
  216. week1Renderer.setColor(getLineColor(j));
  217. week1Renderer.setLineWidth(2.5f);
  218. week1Renderer.setPointStyle(getPointStyle(j));
  219. mDataset.addSeries(num_series, week1Series);
  220. mRenderer.addSeriesRenderer(num_series, week1Renderer);
  221. num_series++;
  222. }
  223. }
  224. //*********************************************************************
  225. // 将监测数值增加至图中
  226. //*********************************************************************
  227. String[] types_background = new String[] {CubicLineChart.TYPE,
  228. CubicLineChart.TYPE,
  229. CubicLineChart.TYPE,
  230. CubicLineChart.TYPE};
  231. String[] types = new String[num_series];
  232. for(int i = 0; i<types_background.length;++i){
  233. types[i] = types_background[i];
  234. }
  235. for(int i = types_background.length; i < num_series;++i){
  236. types[i] = LineChart.TYPE;
  237. }
  238. GraphicalView view = ChartFactory.getCombinedXYChartView(context, mDataset, mRenderer, types);
  239. return view;
  240. }
  241. }

(5)


  1. package com.example.achartenginetest.charts;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.achartengine.ChartFactory;
  5. import org.achartengine.GraphicalView;
  6. import org.achartengine.chart.BarChart;
  7. import org.achartengine.chart.LineChart;
  8. import org.achartengine.chart.PointStyle;
  9. import org.achartengine.model.XYMultipleSeriesDataset;
  10. import org.achartengine.model.XYSeries;
  11. import org.achartengine.renderer.SimpleSeriesRenderer;
  12. import org.achartengine.renderer.XYMultipleSeriesRenderer;
  13. import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;
  14. import org.achartengine.renderer.XYSeriesRenderer;
  15. import android.content.Context;
  16. import android.graphics.Color;
  17. import android.graphics.Paint.Align;
  18. public class HorizontalBarChart extends AbstractChart{
  19. private double[] mData = null;
  20. private double mAvg = 0;
  21. private double min_x = 0;
  22. private double max_x = 0;
  23. private double min_y = 0;
  24. private double max_y = 0;
  25. public HorizontalBarChart(){
  26. mData = new double[] {175,200,60,100,148};
  27. getAvg();
  28. initParas();
  29. }
  30. public HorizontalBarChart(double[] data){
  31. mData = data;
  32. getAvg();
  33. initParas();
  34. }
  35. private void initParas(){
  36. if(mData == null || mData.length==0){
  37. return;
  38. }
  39. max_x = mData.length+0.5;
  40. for(int i = 0; i<mData.length; ++i){
  41. max_y = max_y<mData[i]?mData[i]:max_y;
  42. }
  43. max_y += 50;
  44. }
  45. private void getAvg(){
  46. double sum = 0;
  47. for(int i = 0; i < mData.length; ++i){
  48. sum += mData[i];
  49. }
  50. mAvg= sum/mData.length;
  51. }
  52. @Override
  53. public String getName() {
  54. return "光荣榜";
  55. }
  56. @Override
  57. public String getDesc() {
  58. return "光荣榜,bar chart & line chart";
  59. }
  60. @Override
  61. public GraphicalView execute(Context context) {
  62. String[] titles = new String[] { "糖豆"};
  63. List<double[]> values = new ArrayList<double[]>();
  64. values.add(mData);
  65. int[] colors = new int[] {Color.BLUE};
  66. XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
  67. renderer.setOrientation(Orientation.VERTICAL);
  68. setChartSettings(renderer, "控糖光荣榜", "", "糖豆", min_x, max_x, min_y, max_y, Color.GRAY, Color.LTGRAY);
  69. renderer.setXLabels(0);
  70. renderer.addXTextLabel(1, "我");
  71. renderer.addXTextLabel(2, "老王");
  72. renderer.addXTextLabel(3, "张老太");
  73. renderer.addXTextLabel(4, "老李头");
  74. renderer.addXTextLabel(5, "小刘");
  75. renderer.setYLabels(6);
  76. renderer.setZoomEnabled(false);
  77. renderer.setPanEnabled(false);
  78. renderer.setZoomEnabled(false);
  79. renderer.setBarSpacing(0.5);
  80. renderer.setAxisTitleTextSize(25);// 设置坐标轴标题文本大小
  81. renderer.setChartTitleTextSize(30); // 设置图表标题文本大小
  82. renderer.setLabelsTextSize(23); // 设置轴标签文本大小
  83. renderer.setShowGrid(true);
  84. renderer.setXLabelsAlign(Align.CENTER);
  85. renderer.setYLabelsAlign(Align.RIGHT);
  86. renderer.setXLabelsPadding(30);
  87. renderer.setYLabelsPadding(30);
  88. renderer.setMargins(new int[]{0,30,80,30});
  89. renderer.setShowLegend(false);
  90. int length = renderer.getSeriesRendererCount();
  91. for (int i = 0; i < length; i++) {
  92. SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
  93. seriesRenderer.setDisplayChartValues(false);
  94. }
  95. XYMultipleSeriesDataset mDataset = buildBarDataset(titles, values);
  96. /****************************************************************
  97. * 加入平均值
  98. ****************************************************************/
  99. XYSeries week1Series = new XYSeries("平均值");
  100. week1Series.add(min_x, mAvg);
  101. week1Series.add(max_x, mAvg);
  102. XYSeriesRenderer week1Renderer = new XYSeriesRenderer();
  103. week1Renderer.setColor(Color.BLUE );
  104. week1Renderer.setLineWidth(2.5f);
  105. week1Renderer.setPointStyle(PointStyle.POINT);
  106. mDataset.addSeries(1, week1Series);
  107. renderer.addSeriesRenderer(1, week1Renderer);
  108. String[] types = new String[]{BarChart.TYPE,LineChart.TYPE};
  109. GraphicalView view = ChartFactory.getCombinedXYChartView(context, mDataset, renderer, types);
  110. return view;
  111. }
  112. }

3、其他相关代码
MainActivity.java
  1. package com.example.achartenginetest;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import android.app.ListActivity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.ListView;
  11. import android.widget.SimpleAdapter;
  12. import com.example.achartenginetest.charts.ComplexLineChart;
  13. import com.example.achartenginetest.charts.HighlightPieChart;
  14. import com.example.achartenginetest.charts.HorizontalBarChart;
  15. import com.example.achartenginetest.charts.IChart;
  16. import com.example.achartenginetest.charts.SimpleLineChart;
  17. import com.example.achartenginetest.charts.SimplePieChart;
  18. import com.example.achartenginetest.charts.TableChart;
  19. public class MainActivity extends ListActivity {
  20. private final static String NAME = "name";
  21. private final static String DEC = "dec";
  22. private IChart[] mCharts = new IChart[] {new SimplePieChart(),new HighlightPieChart(),
  23. new SimpleLineChart(), new ComplexLineChart(),
  24. new HorizontalBarChart()};
  25. private String[] mItemText;
  26. private String[] mItemSummary;
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. initTextInList();
  31. setListAdapter(new SimpleAdapter(this, getListValues(), android.R.layout.simple_list_item_2,
  32. new String[] {NAME, DEC }, new int[] { android.R.id.text1,
  33. android.R.id.text2 }));
  34. }
  35. @Override
  36. protected void onListItemClick(ListView l, View v, int position, long id) {
  37. super.onListItemClick(l, v, position, id);
  38. Intent intent = null;
  39. Bundle bundle = new Bundle();
  40. bundle.putInt("position", position);
  41. if (position == mCharts.length) {
  42. intent = new Intent(this, TableChart.class);
  43. }else{
  44. intent = new Intent(this,ChartShowActivity.class);
  45. }
  46. intent.putExtras(bundle);
  47. startActivity(intent);
  48. }
  49. private void initTextInList(){
  50. int length = mCharts.length;
  51. mItemText = new String[length + 1];
  52. mItemSummary = new String[length + 1];
  53. for (int i = 0; i < length; i++) {
  54. mItemText[i] = mCharts[i].getName();
  55. mItemSummary[i] = mCharts[i].getDesc();
  56. }
  57. mItemText[length] = "血糖实际监测值";
  58. mItemSummary[length] = "Table";
  59. }
  60. private List<Map<String, String>> getListValues() {
  61. List<Map<String, String>> values = new ArrayList<Map<String, String>>();
  62. int length = mItemText.length;
  63. for (int i = 0; i < length; i++) {
  64. Map<String, String> v = new HashMap<String, String>();
  65. v.put(NAME, mItemText[i]);
  66. v.put(DEC, mItemSummary[i]);
  67. values.add(v);
  68. }
  69. return values;
  70. }
  71. }

ChartShowActivity.java
  1. package com.example.achartenginetest;
  2. import org.achartengine.GraphicalView;
  3. import android.app.Activity;
  4. import android.graphics.Color;
  5. import android.os.Bundle;
  6. import android.widget.FrameLayout;
  7. import android.widget.TextView;
  8. import com.example.achartenginetest.charts.ComplexLineChart;
  9. import com.example.achartenginetest.charts.HighlightPieChart;
  10. import com.example.achartenginetest.charts.HorizontalBarChart;
  11. import com.example.achartenginetest.charts.IChart;
  12. import com.example.achartenginetest.charts.SimpleLineChart;
  13. import com.example.achartenginetest.charts.SimplePieChart;
  14. public class ChartShowActivity extends Activity {
  15. private IChart[] mCharts = new IChart[] {new SimplePieChart(),new HighlightPieChart(),
  16. new SimpleLineChart(), new ComplexLineChart(),
  17. new HorizontalBarChart()};
  18. private int mIndex = 0;
  19. private TextView mText;
  20. private FrameLayout mLayout;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. // TODO Auto-generated method stub
  24. super.onCreate(savedInstanceState);
  25. this.setContentView(R.layout.activity_main);
  26. mLayout = (FrameLayout)this.findViewById(R.id.chart_layout);
  27. mText = (TextView)this.findViewById(R.id.text);
  28. mIndex = this.getIntent().getExtras().getInt("position");
  29. GraphicalView graphicalView = mCharts[mIndex].execute(this);
  30. if(null == graphicalView){
  31. return;
  32. }
  33. mLayout.removeAllViews();
  34. mLayout.setBackgroundColor(Color.BLACK);
  35. mLayout.addView(graphicalView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
  36. mText.setText(mCharts[mIndex].getName());
  37. }
  38. }

activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="${relativePackage}.${activityClass}" >
  6. <TextView
  7. android:id="@+id/text"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentTop="true"
  11. android:text="@string/hello_world" />
  12. <FrameLayout
  13. android:id="@+id/chart_layout"
  14. android:layout_below="@+id/text"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content">
  17. </FrameLayout>
  18. </RelativeLayout>







附件列表

     

    posted @ 2016-01-04 12:59  风倾清凌  阅读(1362)  评论(0编辑  收藏  举报