大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Android技巧 > Android通过achartengine画折线图

Android通过achartengine画折线图

关键词:折线图Androidachartengine  阅读(923) 赞(20)

[摘要]本文是对Android通过achartengine画折线图的讲解,对学习Android编程技术有所帮助,与大家分享。

因为前段时间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

 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;
 
 import android.app.Activity;
 
 /**
  * An abstract class for the demo charts to extend. It contains some methods for
  * building datasets and renderers.
  */
 public abstract class AbstractDemoChart extends Activity implements IDemoChart {
 
   /**
    * Builds an XY multiple dataset using the provided values.
    * 
    * @param titles the series titles
    * @param xValues the values for the X axis
    * @param yValues the values for the Y axis
    * @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;
   }
 
   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);
       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.
    * 
    * @param colors the series rendering colors
    * @param styles the series point styles
    * @return the XY multiple series renderers
    */
   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) {
     renderer.setAxisTitleTextSize(16);
     renderer.setChartTitleTextSize(20);
     renderer.setLabelsTextSize(15);
     renderer.setLegendTextSize(15);
     renderer.setPointSize(5f);
     renderer.setMargins(new int[] { 20, 30, 15, 20 });
     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);
     }
   }
 
   /**
    * Sets a few of the series renderer settings.
    * 
    * @param renderer the renderer to set the properties to
    * @param title the chart title
    * @param xTitle the title for the X axis
    * @param yTitle the title for the Y axis
    * @param xMin the minimum value on the X axis
    * @param xMax the maximum value on the X axis
    * @param yMin the minimum value on the Y axis
    * @param yMax the maximum value on the Y axis
    * @param axesColor the axes color
    * @param labelsColor the labels color
    */
   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);
   }
 
   /**
    * Builds an XY multiple time dataset using the provided values.
    * 
    * @param titles the series titles
    * @param xValues the values for the X axis
    * @param yValues the values for the Y axis
    * @return the XY multiple time dataset
    */
   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]);
       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;
   }
 
   /**
    * Builds a category series using the provided values.
    * 
    * @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;
   }
 
   /**
    * Builds a multiple category series using the provided values.
    * 
    * @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;
   }
 
   /**
    * Builds a category renderer to use the provided colors.
    * 
    * @param colors the colors
    * @return the category renderer
    */
   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;
   }
 
   /**
    * Builds a bar multiple series dataset using the provided values.
    * 
    * @param titles the series titles
    * @param values the values
    * @return the XY multiple bar dataset
    */
   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;
   }
 
   /**
    * Builds a bar multiple series renderer to use the provided colors.
    * 
    * @param colors the series renderers colors
    * @return the bar multiple series renderer
    */
   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;
   }
 
 }

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

LinearChart.java

 import java.util.ArrayList;
 import java.util.List;
 
 import org.achartengine.ChartFactory;
 import org.achartengine.chart.PointStyle;
 import org.achartengine.renderer.XYMultipleSeriesRenderer;
 import org.achartengine.renderer.XYSeriesRenderer;
 
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Color;
 import android.graphics.Paint.Align;
 
 public class LineChart extends AbstractDemoChart {
 
     public String getName() {
         return "曲线A";
     }
 
     public String getDesc() {
         return "曲线A";
     }
 
     /**
      * Executes the chart demo.
      * 
      * @param context the context
      * @return the built intent
      */
     public Intent execute(Context context, ArrayList<Float> standar, ArrayList<Float> collect) {
         ArrayList<Float> standardValues = standar;
         ArrayList<Float> collectValues = collect;
         int l = standardValues.size();
         double[] standards = new double[l];
         double[] collects = new double[l];
         int maxS,minS,maxC,minC;
         for (int i = 0; i < l; i++) {
             standards[i] = standardValues.get(i);
             collects[i] = collectValues.get(i);
         }
         maxS = (int)DataUtil.getMax(standards);
         minS = (int)DataUtil.getMin(standards);
         maxC = (int)DataUtil.getMax(collects);
         minC = (int)DataUtil.getMin(collects);
         // 每个item的title
         String[] titles = new String[] { "采集值——标准值"};
         // x轴的值
         List<double[]> x = new ArrayList<double[]>();
             x.add(collects);
         // y轴的值
         List<double[]> values = new ArrayList<double[]>();
         values.add(standards);
         int[] colors = new int[] { Color.BLUE};
         // 点的样式
         PointStyle[] styles = new PointStyle[] { PointStyle.DIAMOND};
         XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
         int length = renderer.getSeriesRendererCount();
         // 点是空心还是实心
         for (int i = 0; i < length; i++) {
             ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
         }
 
         // 图表部分的背景颜色
         renderer.setBackgroundColor(Color.parseColor("#f3f3f3"));
         renderer.setApplyBackgroundColor(true);
         // 图表与屏幕四边的间距颜色
         renderer.setMarginsColor(Color.argb(0, 0xF3, 0xF3, 0xF3));
         renderer.setChartTitleTextSize(30);
         renderer.setAxisTitleTextSize(25);
         // renderer.setLegendHeight(50);
         // 图例文字的大小
         renderer.setLegendTextSize(20);
         renderer.setMargins(new int[] { 50, 50, 50, 30 });
         // x、y轴上刻度颜色
         renderer.setXLabelsColor(Color.BLACK);
         renderer.setYLabelsColor(0, Color.BLACK);
 
         // 最后两个参数代表轴的颜色和轴标签的颜色
         setChartSettings(renderer, "校准曲线", "采集值", "标准值", minC, maxC, minS, maxS, Color.BLACK, Color.BLACK);
         // 轴上数字的数量
         renderer.setXLabels(12);
         renderer.setYLabels(10);
         // 是否显示网格
         renderer.setShowGrid(true);
         // x或y轴上数字的方向,相反的。
         renderer.setXLabelsAlign(Align.RIGHT);
         renderer.setYLabelsAlign(Align.RIGHT);
         // 有问题的方法
         renderer.setZoomButtonsVisible(true);
         renderer.setZoomEnabled(true);
         renderer.setAntialiasing(true);
         // renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
         // renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
 
         Intent intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, x, values), renderer, "曲线");
         return intent;
     }
 
     @Override
     public Intent execute(Context context) {
         // TODO Auto-generated method stub
         return null;
     }
 }

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


相关Android技巧推荐

    暂时没有相关推荐



    相关评论