android图表工具achartengine之ChartFactory分析

要充分应用AChartEngine还需要查看源码,只有从本质上知道这些运作原理才能画出适合自己程序的图表

code:

package org.achartengine;

import org.achartengine.chart.BarChart;
import org.achartengine.chart.BubbleChart;
import org.achartengine.chart.CombinedXYChart;
import org.achartengine.chart.CubicLineChart;
import org.achartengine.chart.DialChart;
import org.achartengine.chart.DoughnutChart;
import org.achartengine.chart.LineChart;
import org.achartengine.chart.PieChart;
import org.achartengine.chart.RangeBarChart;
import org.achartengine.chart.ScatterChart;
import org.achartengine.chart.TimeChart;
import org.achartengine.chart.XYChart;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.MultipleCategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.DialRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

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

public class ChartFactory//这个是AChartEngine的工厂类
{
  public static final String CHART = "chart";
  public static final String TITLE = "title";
  
  //---------------------以下这些是用GraphicalView显示的图表 需要寄托在一些Activity的布局上才能正常显示-----------------------
  public static final GraphicalView getLineChartView(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)
  {//LineChart可以画柱形图,折线图等
    checkParameters(dataset, renderer);
    XYChart chart = new LineChart(dataset, renderer);
    return new GraphicalView(context, chart);
  }

  public static final GraphicalView getCubeLineChartView(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, float smoothness)
  {//这个是组合的线性图,可以自定义横轴,纵轴,
    checkParameters(dataset, renderer);
    XYChart chart = new CubicLineChart(dataset, renderer, smoothness);
    return new GraphicalView(context, chart);
  }

  public static final GraphicalView getScatterChartView(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)
  {//散点图
    checkParameters(dataset, renderer);
    XYChart chart = new ScatterChart(dataset, renderer);
    return new GraphicalView(context, chart);
  }

  public static final GraphicalView getBubbleChartView(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)
  {//泡沫图
    checkParameters(dataset, renderer);
    XYChart chart = new BubbleChart(dataset, renderer);
    return new GraphicalView(context, chart);
  }

  public static final GraphicalView getTimeChartView(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, String format)
  {//时间分布图
    checkParameters(dataset, renderer);
    TimeChart chart = new TimeChart(dataset, renderer);
    chart.setDateFormat(format);
    return new GraphicalView(context, chart);
  }

  public static final GraphicalView getBarChartView(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, BarChart.Type type)
  {//柱状图
    checkParameters(dataset, renderer);
    XYChart chart = new BarChart(dataset, renderer, type);
    return new GraphicalView(context, chart);
  }

  public static final GraphicalView getRangeBarChartView(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, BarChart.Type type)
  {//区域图
    checkParameters(dataset, renderer);
    XYChart chart = new RangeBarChart(dataset, renderer, type);
    return new GraphicalView(context, chart);
  }

  public static final GraphicalView getCombinedXYChartView(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, String[] types)
  {//组合图,可以让柱状图+折线图+曲线+泡沫图融为一体
    if ((dataset == null) || (renderer == null) || (types == null) || (dataset.getSeriesCount() != types.length))
    {
      throw new IllegalArgumentException("Dataset, renderer and types should be not null and the datasets series count should be equal to the types length");
    }

    checkParameters(dataset, renderer);
    CombinedXYChart chart = new CombinedXYChart(dataset, renderer, types);
    return new GraphicalView(context, chart);
  }

  public static final GraphicalView getPieChartView(Context context, CategorySeries dataset, DefaultRenderer renderer)
  {//饼图
    checkParameters(dataset, renderer);
    PieChart chart = new PieChart(dataset, renderer);
    return new GraphicalView(context, chart);
  }

  public static final GraphicalView getDialChartView(Context context, CategorySeries dataset, DialRenderer renderer)
  {//刻表图 像汽车速度表那样的图
    checkParameters(dataset, renderer);
    DialChart chart = new DialChart(dataset, renderer);
    return new GraphicalView(context, chart);
  }

  public static final GraphicalView getDoughnutChartView(Context context, MultipleCategorySeries dataset, DefaultRenderer renderer)
  {//圆圈图
    checkParameters(dataset, renderer);
    DoughnutChart chart = new DoughnutChart(dataset, renderer);
    return new GraphicalView(context, chart);
  }
  //---------------------以下这些是用Intent(意图)显示的图表 可以不用寄托在Activity中也能正常显示-----------------------
  public static final Intent getLineChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)
  {
    return getLineChartIntent(context, dataset, renderer, "");
  }

  public static final Intent getCubicLineChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, float smoothness)
  {
    return getCubicLineChartIntent(context, dataset, renderer, smoothness, "");
  }

  public static final Intent getScatterChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)
  {
    return getScatterChartIntent(context, dataset, renderer, "");
  }

  public static final Intent getBubbleChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)
  {
    return getBubbleChartIntent(context, dataset, renderer, "");
  }

  public static final Intent getTimeChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, String format)
  {
    return getTimeChartIntent(context, dataset, renderer, format, "");
  }

  public static final Intent getBarChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, BarChart.Type type)
  {
    return getBarChartIntent(context, dataset, renderer, type, "");
  }

  public static final Intent getLineChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, String activityTitle)
  {
    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    XYChart chart = new LineChart(dataset, renderer);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  public static final Intent getCubicLineChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, float smoothness, String activityTitle)
  {
    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    XYChart chart = new CubicLineChart(dataset, renderer, smoothness);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  public static final Intent getScatterChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, String activityTitle)
  {
    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    XYChart chart = new ScatterChart(dataset, renderer);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  public static final Intent getBubbleChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, String activityTitle)
  {
    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    XYChart chart = new BubbleChart(dataset, renderer);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  public static final Intent getTimeChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, String format, String activityTitle)
  {
    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    TimeChart chart = new TimeChart(dataset, renderer);
    chart.setDateFormat(format);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  public static final Intent getBarChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, BarChart.Type type, String activityTitle)
  {
    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    BarChart chart = new BarChart(dataset, renderer, type);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  public static final Intent getRangeBarChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, BarChart.Type type, String activityTitle)
  {
    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    RangeBarChart chart = new RangeBarChart(dataset, renderer, type);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  public static final Intent getCombinedXYChartIntent(Context context, XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, String[] types, String activityTitle)
  {
    if ((dataset == null) || (renderer == null) || (types == null) || (dataset.getSeriesCount() != types.length))
    {
      throw new IllegalArgumentException("Datasets, renderers and types should be not null and the datasets series count should be equal to the types length");
    }

    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    CombinedXYChart chart = new CombinedXYChart(dataset, renderer, types);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  public static final Intent getPieChartIntent(Context context, CategorySeries dataset, DefaultRenderer renderer, String activityTitle)
  {
    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    PieChart chart = new PieChart(dataset, renderer);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  public static final Intent getDoughnutChartIntent(Context context, MultipleCategorySeries dataset, DefaultRenderer renderer, String activityTitle)
  {
    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    DoughnutChart chart = new DoughnutChart(dataset, renderer);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  public static final Intent getDialChartIntent(Context context, CategorySeries dataset, DialRenderer renderer, String activityTitle)
  {
    checkParameters(dataset, renderer);
    Intent intent = new Intent(context, GraphicalActivity.class);
    DialChart chart = new DialChart(dataset, renderer);
    intent.putExtra("chart", chart);
    intent.putExtra("title", activityTitle);
    return intent;
  }

  private static void checkParameters(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer)
  {
    if ((dataset == null) || (renderer == null) || (dataset.getSeriesCount() != renderer.getSeriesRendererCount()))
    {
      throw new IllegalArgumentException("Dataset and renderer should be not null and should have the same number of series");
    }
  }

  private static void checkParameters(CategorySeries dataset, DefaultRenderer renderer)
  {
    if ((dataset == null) || (renderer == null) || (dataset.getItemCount() != renderer.getSeriesRendererCount()))
    {
      throw new IllegalArgumentException("Dataset and renderer should be not null and the dataset number of items should be equal to the number of series renderers");
    }
  }

  private static void checkParameters(MultipleCategorySeries dataset, DefaultRenderer renderer)
  {
    if ((dataset == null) || (renderer == null) || (!checkMultipleSeriesItems(dataset, renderer.getSeriesRendererCount())))
    {
      throw new IllegalArgumentException("Titles and values should be not null and the dataset number of items should be equal to the number of series renderers");
    }
  }

  private static boolean checkMultipleSeriesItems(MultipleCategorySeries dataset, int value)
  {
    int count = dataset.getCategoriesCount();
    boolean equal = true;
    for (int k = 0; (k < count) && (equal); k++) {
      equal = dataset.getValues(k).length == dataset.getTitles(k).length;
    }
    return equal;
  }
}


android图表工具achartengine之ChartFactory分析

上一篇:手把手教你玩转android应用Microsoft Remote Desktop


下一篇:【转】Android Service被关闭后自动重启,解决被异常kill 服务