Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android圖表工具achartengine之ChartFactory分析

android圖表工具achartengine之ChartFactory分析

編輯:關於Android編程

要充分應用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;
  }
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved