Android通过achartengine画折线图

因为前段时间Android项目中用到了折线图的功能,在查资料的过程中发现achartengine这个包有非常强大的画图功能,正好最近有时间,就整理一下方便以后查看。

首先要使用画图功能,就必须在lib中引入achartengine包。为了使用方便,直接使用示例中的两个文件,源码如下:

IDemoChart.java

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);

}

AbstractDemoChart.java

  1 import java.util.Date;
  2 import java.util.List;
  3
  4 import org.achartengine.chart.PointStyle;
  5 import org.achartengine.model.CategorySeries;
  6 import org.achartengine.model.MultipleCategorySeries;
  7 import org.achartengine.model.TimeSeries;
  8 import org.achartengine.model.XYMultipleSeriesDataset;
  9 import org.achartengine.model.XYSeries;
 10 import org.achartengine.renderer.DefaultRenderer;
 11 import org.achartengine.renderer.SimpleSeriesRenderer;
 12 import org.achartengine.renderer.XYMultipleSeriesRenderer;
 13 import org.achartengine.renderer.XYSeriesRenderer;
 14
 15 import android.app.Activity;
 16
 17 /**
 18  * An abstract class for the demo charts to extend. It contains some methods for
 19  * building datasets and renderers.
 20  */
 21 public abstract class AbstractDemoChart extends Activity implements IDemoChart {
 22
 23   /**
 24    * Builds an XY multiple dataset using the provided values.
 25    *
 26    * @param titles the series titles
 27    * @param xValues the values for the X axis
 28    * @param yValues the values for the Y axis
 29    * @return the XY multiple dataset
 30    */
 31   protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues,
 32       List<double[]> yValues) {
 33     XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
 34     addXYSeries(dataset, titles, xValues, yValues, 0);
 35     return dataset;
 36   }
 37
 38   public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues,
 39       List<double[]> yValues, int scale) {
 40     int length = titles.length;
 41     for (int i = 0; i < length; i++) {
 42       XYSeries series = new XYSeries(titles[i], scale);
 43       double[] xV = xValues.get(i);
 44       double[] yV = yValues.get(i);
 45       int seriesLength = xV.length;
 46       for (int k = 0; k < seriesLength; k++) {
 47         series.add(xV[k], yV[k]);
 48       }
 49       dataset.addSeries(series);
 50     }
 51   }
 52
 53   /**
 54    * Builds an XY multiple series renderer.
 55    *
 56    * @param colors the series rendering colors
 57    * @param styles the series point styles
 58    * @return the XY multiple series renderers
 59    */
 60   protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
 61     XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
 62     setRenderer(renderer, colors, styles);
 63     return renderer;
 64   }
 65
 66   protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {
 67     renderer.setAxisTitleTextSize(16);
 68     renderer.setChartTitleTextSize(20);
 69     renderer.setLabelsTextSize(15);
 70     renderer.setLegendTextSize(15);
 71     renderer.setPointSize(5f);
 72     renderer.setMargins(new int[] { 20, 30, 15, 20 });
 73     int length = colors.length;
 74     for (int i = 0; i < length; i++) {
 75       XYSeriesRenderer r = new XYSeriesRenderer();
 76       r.setColor(colors[i]);
 77       r.setPointStyle(styles[i]);
 78       renderer.addSeriesRenderer(r);
 79     }
 80   }
 81
 82   /**
 83    * Sets a few of the series renderer settings.
 84    *
 85    * @param renderer the renderer to set the properties to
 86    * @param title the chart title
 87    * @param xTitle the title for the X axis
 88    * @param yTitle the title for the Y axis
 89    * @param xMin the minimum value on the X axis
 90    * @param xMax the maximum value on the X axis
 91    * @param yMin the minimum value on the Y axis
 92    * @param yMax the maximum value on the Y axis
 93    * @param axesColor the axes color
 94    * @param labelsColor the labels color
 95    */
 96   protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
 97       String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
 98       int labelsColor) {
 99     renderer.setChartTitle(title);
100     renderer.setXTitle(xTitle);
101     renderer.setYTitle(yTitle);
102     renderer.setXAxisMin(xMin);
103     renderer.setXAxisMax(xMax);
104     renderer.setYAxisMin(yMin);
105     renderer.setYAxisMax(yMax);
106     renderer.setAxesColor(axesColor);
107     renderer.setLabelsColor(labelsColor);
108   }
109
110   /**
111    * Builds an XY multiple time dataset using the provided values.
112    *
113    * @param titles the series titles
114    * @param xValues the values for the X axis
115    * @param yValues the values for the Y axis
116    * @return the XY multiple time dataset
117    */
118   protected XYMultipleSeriesDataset buildDateDataset(String[] titles, List<Date[]> xValues,
119       List<double[]> yValues) {
120     XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
121     int length = titles.length;
122     for (int i = 0; i < length; i++) {
123       TimeSeries series = new TimeSeries(titles[i]);
124       Date[] xV = xValues.get(i);
125       double[] yV = yValues.get(i);
126       int seriesLength = xV.length;
127       for (int k = 0; k < seriesLength; k++) {
128         series.add(xV[k], yV[k]);
129       }
130       dataset.addSeries(series);
131     }
132     return dataset;
133   }
134
135   /**
136    * Builds a category series using the provided values.
137    *
138    * @param titles the series titles
139    * @param values the values
140    * @return the category series
141    */
142   protected CategorySeries buildCategoryDataset(String title, double[] values) {
143     CategorySeries series = new CategorySeries(title);
144     int k = 0;
145     for (double value : values) {
146       series.add("Project " + ++k, value);
147     }
148
149     return series;
150   }
151
152   /**
153    * Builds a multiple category series using the provided values.
154    *
155    * @param titles the series titles
156    * @param values the values
157    * @return the category series
158    */
159   protected MultipleCategorySeries buildMultipleCategoryDataset(String title,
160       List<String[]> titles, List<double[]> values) {
161     MultipleCategorySeries series = new MultipleCategorySeries(title);
162     int k = 0;
163     for (double[] value : values) {
164       series.add(2007 + k + "", titles.get(k), value);
165       k++;
166     }
167     return series;
168   }
169
170   /**
171    * Builds a category renderer to use the provided colors.
172    *
173    * @param colors the colors
174    * @return the category renderer
175    */
176   protected DefaultRenderer buildCategoryRenderer(int[] colors) {
177     DefaultRenderer renderer = new DefaultRenderer();
178     renderer.setLabelsTextSize(15);
179     renderer.setLegendTextSize(15);
180     renderer.setMargins(new int[] { 20, 30, 15, 0 });
181     for (int color : colors) {
182       SimpleSeriesRenderer r = new SimpleSeriesRenderer();
183       r.setColor(color);
184       renderer.addSeriesRenderer(r);
185     }
186     return renderer;
187   }
188
189   /**
190    * Builds a bar multiple series dataset using the provided values.
191    *
192    * @param titles the series titles
193    * @param values the values
194    * @return the XY multiple bar dataset
195    */
196   protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
197     XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
198     int length = titles.length;
199     for (int i = 0; i < length; i++) {
200       CategorySeries series = new CategorySeries(titles[i]);
201       double[] v = values.get(i);
202       int seriesLength = v.length;
203       for (int k = 0; k < seriesLength; k++) {
204         series.add(v[k]);
205       }
206       dataset.addSeries(series.toXYSeries());
207     }
208     return dataset;
209   }
210
211   /**
212    * Builds a bar multiple series renderer to use the provided colors.
213    *
214    * @param colors the series renderers colors
215    * @return the bar multiple series renderer
216    */
217   protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
218     XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
219     renderer.setAxisTitleTextSize(16);
220     renderer.setChartTitleTextSize(20);
221     renderer.setLabelsTextSize(15);
222     renderer.setLegendTextSize(15);
223     int length = colors.length;
224     for (int i = 0; i < length; i++) {
225       SimpleSeriesRenderer r = new SimpleSeriesRenderer();
226       r.setColor(colors[i]);
227       renderer.addSeriesRenderer(r);
228     }
229     return renderer;
230   }
231
232 }

然后就是自定义的内容了,包括折线图的数据、样式等内容,具体详见代码注释

LinearChart.java

  1 import java.util.ArrayList;
  2 import java.util.List;
  3
  4 import org.achartengine.ChartFactory;
  5 import org.achartengine.chart.PointStyle;
  6 import org.achartengine.renderer.XYMultipleSeriesRenderer;
  7 import org.achartengine.renderer.XYSeriesRenderer;
  8
  9 import android.content.Context;
 10 import android.content.Intent;
 11 import android.graphics.Color;
 12 import android.graphics.Paint.Align;
 13
 14 public class LineChart extends AbstractDemoChart {
 15
 16     public String getName() {
 17         return "曲线A";
 18     }
 19
 20     public String getDesc() {
 21         return "曲线A";
 22     }
 23
 24     /**
 25      * Executes the chart demo.
 26      *
 27      * @param context the context
 28      * @return the built intent
 29      */
 30     public Intent execute(Context context, ArrayList<Float> standar, ArrayList<Float> collect) {
 31         ArrayList<Float> standardValues = standar;
 32         ArrayList<Float> collectValues = collect;
 33         int l = standardValues.size();
 34         double[] standards = new double[l];
 35         double[] collects = new double[l];
 36         int maxS,minS,maxC,minC;
 37         for (int i = 0; i < l; i++) {
 38             standards[i] = standardValues.get(i);
 39             collects[i] = collectValues.get(i);
 40         }
 41         maxS = (int)DataUtil.getMax(standards);
 42         minS = (int)DataUtil.getMin(standards);
 43         maxC = (int)DataUtil.getMax(collects);
 44         minC = (int)DataUtil.getMin(collects);
 45         // 每个item的title
 46         String[] titles = new String[] { "采集值——标准值"};
 47         // x轴的值
 48         List<double[]> x = new ArrayList<double[]>();
 49             x.add(collects);
 50         // y轴的值
 51         List<double[]> values = new ArrayList<double[]>();
 52         values.add(standards);
 53         int[] colors = new int[] { Color.BLUE};
 54         // 点的样式
 55         PointStyle[] styles = new PointStyle[] { PointStyle.DIAMOND};
 56         XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
 57         int length = renderer.getSeriesRendererCount();
 58         // 点是空心还是实心
 59         for (int i = 0; i < length; i++) {
 60             ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
 61         }
 62
 63         // 图表部分的背景颜色
 64         renderer.setBackgroundColor(Color.parseColor("#f3f3f3"));
 65         renderer.setApplyBackgroundColor(true);
 66         // 图表与屏幕四边的间距颜色
 67         renderer.setMarginsColor(Color.argb(0, 0xF3, 0xF3, 0xF3));
 68         renderer.setChartTitleTextSize(30);
 69         renderer.setAxisTitleTextSize(25);
 70         // renderer.setLegendHeight(50);
 71         // 图例文字的大小
 72         renderer.setLegendTextSize(20);
 73         renderer.setMargins(new int[] { 50, 50, 50, 30 });
 74         // x、y轴上刻度颜色
 75         renderer.setXLabelsColor(Color.BLACK);
 76         renderer.setYLabelsColor(0, Color.BLACK);
 77
 78         // 最后两个参数代表轴的颜色和轴标签的颜色
 79         setChartSettings(renderer, "校准曲线", "采集值", "标准值", minC, maxC, minS, maxS, Color.BLACK, Color.BLACK);
 80         // 轴上数字的数量
 81         renderer.setXLabels(12);
 82         renderer.setYLabels(10);
 83         // 是否显示网格
 84         renderer.setShowGrid(true);
 85         // x或y轴上数字的方向,相反的。
 86         renderer.setXLabelsAlign(Align.RIGHT);
 87         renderer.setYLabelsAlign(Align.RIGHT);
 88         // 有问题的方法
 89         renderer.setZoomButtonsVisible(true);
 90         renderer.setZoomEnabled(true);
 91         renderer.setAntialiasing(true);
 92         // renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
 93         // renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
 94
 95         Intent intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, x, values), renderer, "曲线");
 96         return intent;
 97     }
 98
 99     @Override
100     public Intent execute(Context context) {
101         // TODO Auto-generated method stub
102         return null;
103     }
104 }

因为要调用到"org.achartengine.GraphicalActivity“类,所以不要忘记在manifest中添加 <activity android:name="org.achartengine.GraphicalActivity" />说明,否则会报错。

时间: 2024-10-18 09:32:39

Android通过achartengine画折线图的相关文章

Android 用achartengine 画折线图怎么显示不正确

============问题描述============ X.Y的刻度值都是我自己定义的 显示的点的位置不正确 请问是什么原因 怎么处理  还有我设置了显示网格 为什么不显示了 Y轴不设置自己的定义的刻度值 但是刻度个数又不对 这种情况显示是正确的就是刻度个数不够 ============解决方案1============ 属性设置不对,找个demo仔细看看设置,看自己哪里设置不对. ============解决方案2============ 我以前整理的一个曲线图,我共享下,你下载看看,里面东

android使用achartengine 实现折线图

折线图的实现分为下边几个步骤: 1.第一步:数据的准备 XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset(); XYSeries series1 = new XYSeries("第一条线"); series1.add(1, 7); series1.add(2, 9); series1.add(3,12); series1.add(4, 5); mDataset.addSeries(series1); XYSer

python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)

最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指正. 一.最简单的基本框架如下:已知x,y,画出折线图并保存.此时x和y均为数字. 1 # -*- coding: utf-8 -*- 2 3 import matplotlib.pyplot as plt #引入matplotlib的pyplot子库,用于画简单的2D图 4 import random 5

Android自定义控件 -Canvas绘制折线图(实现动态报表效果)

有时候我们在项目中会遇到使用折线图等图形,Android的开源项目中为我们提供了很多插件,但是很多时候我们需要根据具体项目自定义这些图表,这一篇文章我们一起来看看如何在Android中使用Canvas绘制折线图.先看看绘制的效果: 代码: public class MyView extends View { //坐标轴原点的位置 private int xPoint=60; private int yPoint=260; //刻度长度 private int xScale=8;  //8个单位构

IOS使用Core-Plot画折线图

关于Core-Plot的配置,大家可以参考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权所有,转载请注明原文转自:http://blog.csdn.net/wildcatlele/article/details/25483923 大家可以到:http://1.wildcat.sinaapp.com/?p=102观看本篇博客更友好的排版格式 或者你英语好也可以参考github上的wiki介绍:https://code.google.com/p/core-

Cocos2d3.0 画折线图

实现用2dx画折线图,为以后用2dx开发应用做准备 下面记录下使用方法 auto lineView = DJLineChart::create(); std::vector<float> vec; vec.push_back(130); vec.push_back(520); vec.push_back(60); vec.push_back(0); vec.push_back(140); vec.push_back(100); vec.push_back(30); vec.push_back(

Java画折线图

??? JFreeChart 是开放源代码站点SourceForge.net 上的一个 JAVA 项目,它主要用来各种各样的图表,这些图表包括:饼图.柱状图 ( 普通柱状图以及堆栈柱状图 ).线图.区域图.分布图.混合图.甘特图以及一些仪表盘等等. ??? 应用jfreechart来画图需要两个jar包:jfreechart.jar和jcommon.jar,直接去官网下载就成: https://sourceforge.net/projects/jfreechart/files/ ?? 下载完成后

Matplotlib学习---用matplotlib画折线图(line chart)

这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图. 数据地址:https://raw.githubusercontent.com/jakevdp/data-CDCbirths/master/births.csv 准备工作:先导入matplotlib和pandas,用pandas读取csv文件,然后创建一个图像和一个坐标轴 import pandas as pd from matplotlib import pyplot as plt birth=p

unity 画折线图,饼型图插件

在unity中画折线图,和饼型图等数据分析图是很困难 的一件事,幸好我找到了一个插件很方便的解决了这件事,效果如下图: 折线图,饼型图,等. 运行效果如下: 百度网盘下载地址:链接:https://pan.baidu.com/s/10oLG1Zmffv7ASWG0pvx05w 提取码:lub1 如果链接失效,请留言. 原文地址:https://blog.51cto.com/14058389/2425723