圖表繪製引擎—AChartEngine

對於一些數據統計來講,圖表的表現形式每每是最好的,這個時候AChartEngine就應運而生了,它適用於餅圖,柱狀圖,折線圖。下面直接上截圖跟代碼吧。java

1.餅圖android

2.柱形圖數組

3.折線圖app

 

下面是實現上面效果的代碼:android.app.Activitcide

MainActontent.Inten字體

 1 import java.util.ArrayList;
 2 import android.app.Activity;
 3 import android.content.Intent;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 public class MainAct extends Activity {
 7     ArrayList<Information> informations;
 8 
 9     @Override
10     public void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13 
14         informations = new ArrayList<Information>();
15         // 數據假設 初始化
16         informations.add(new Information("語文", "100", "分"));
17         informations.add(new Information("數學", "119", "分"));
18         informations.add(new Information("英語", "93", "分"));
19         informations.add(new Information("物理", "80", "分"));
20         informations.add(new Information("化學", "70", "分"));
21         informations.add(new Information("生物", "66", "分"));
22 
23     }
24 
25     /**
26      * 按鍵監聽
27      */
28     public void bing(View view) {
29         Intent intent = new Intent(this, Pie_char.class);
30         intent.putExtra("informations", informations);
31         intent.putExtra("title", "各科分數");
32         startActivity(intent);
33     }
34 
35     public void zhu(View view) {
36         Intent intent = new Intent(this, Stacked.class);
37         intent.putExtra("informations", informations);
38         intent.putExtra("title", "各科分數");
39         startActivity(intent);
40     }
41 
42     public void zhexian(View view) {
43         Intent intent = new Intent(this, Zhexian.class);
44         intent.putExtra("informations", informations);
45         intent.putExtra("title", "各科分數");
46         startActivity(intent);
47     }
48 
49 }

 Pie_charui

  1 import java.util.ArrayList;
  2 import org.achartengine.ChartFactory;
  3 import org.achartengine.model.CategorySeries;
  4 import org.achartengine.renderer.DefaultRenderer;
  5 import org.achartengine.renderer.SimpleSeriesRenderer;
  6 import android.app.Activity;
  7 import android.graphics.Color;
  8 import android.os.Bundle;
  9 import android.view.View;
 10 
 11 /**
 12  * 餅圖
 13  */
 14 public class Pie_char extends Activity {
 15     private ArrayList<Information> list;
 16     private int[] colorsArr;
 17     private String title;
 18     private int[] colors;
 19 
 20     @Override
 21     public void onCreate(Bundle savedInstanceState) {
 22         super.onCreate(savedInstanceState);
 23         colors = new int[] { Color.BLUE, Color.GREEN, Color.MAGENTA,
 24                 Color.YELLOW, Color.CYAN, Color.RED };
 25         initData();
 26         DefaultRenderer renderer = buildCategoryRenderer(colorsArr); // 把分佈的顏色傳給渲染器
 27         renderer.setZoomButtonsVisible(true);
 28         renderer.setZoomEnabled(true);
 29         renderer.setChartTitleTextSize(20);
 30         renderer.setInScroll(true);
 31         renderer.setZoomButtonsVisible(false);
 32         // View view = ChartFactory.getPieChartView(this,
 33         // buildCategoryDataset("Project budget", values), renderer);
 34         // 餅狀圖文字信息和對應的百分比
 35         View view = ChartFactory.getPieChartView(this,
 36                 buildCategoryDataset("Project budget", list), renderer);
 37         view.setBackgroundColor(Color.BLACK);
 38 
 39         setContentView(view);
 40 
 41     }
 42 
 43     /**
 44      * 初始化數據
 45      */
 46     @SuppressWarnings("unchecked")
 47     private void initData() {
 48         // 獲取main傳來的集合
 49         list = (ArrayList<Information>) getIntent().getSerializableExtra(
 50                 "informations");
 51         title = getIntent().getStringExtra("title"); // 設置標題
 52         // 把數據所需顏色複製到數組
 53         colorsArr = new int[list.size()];
 54         for (int i = 0; i < list.size(); i++) {
 55             colorsArr[i] = colors[i];
 56         }
 57     }
 58 
 59     /**
 60      * 把分佈的顏色傳給渲染器
 61      * 
 62      * @param colors
 63      * @return
 64      */
 65     protected DefaultRenderer buildCategoryRenderer(int[] colors) {
 66         DefaultRenderer renderer = new DefaultRenderer();
 67         renderer.setLabelsTextSize(25);
 68         renderer.setLegendTextSize(25);
 69         renderer.setMargins(new int[] { 20, 30, 15, 0 });
 70         renderer.setChartTitle(title);
 71         renderer.setPanEnabled(false);// 設置是否容許拖動
 72         renderer.setShowLegend(false);// 設置是否顯示圖例
 73         renderer.setInScroll(false);
 74         renderer.setClickEnabled(false);
 75         renderer.setExternalZoomEnabled(false);// 設置是否顯示放大縮小按鈕
 76         renderer.setZoomButtonsVisible(false);
 77         renderer.setZoomEnabled(false);
 78         for (int color : colors) {
 79             SimpleSeriesRenderer r = new SimpleSeriesRenderer();
 80             r.setColor(color);
 81             renderer.addSeriesRenderer(r);
 82         }
 83         return renderer;
 84     }
 85 
 86     /**
 87      * 餅狀圖文字信息
 88      * 
 89      * @param title
 90      * @param values
 91      * @return
 92      */
 93     protected CategorySeries buildCategoryDataset(String title,
 94             ArrayList<Information> list) {
 95         CategorySeries series = new CategorySeries(title);
 96         // 根據list值分配視圖 顏色
 97         for (Information information : list) {
 98             double num = Double.parseDouble(information.dulian1);
 99             series.add(information.weidu + " (" + information.dulian1
100                     + information.dw + ")", num);
101         }
102         return series;
103     }
104 }

 Stackedthis

  1 import java.util.ArrayList;
  2 import org.achartengine.ChartFactory;
  3 import org.achartengine.chart.BarChart.Type;
  4 import org.achartengine.model.CategorySeries;
  5 import org.achartengine.model.XYMultipleSeriesDataset;
  6 import org.achartengine.renderer.SimpleSeriesRenderer;
  7 import org.achartengine.renderer.XYMultipleSeriesRenderer;
  8 import android.app.Activity;
  9 import android.graphics.Color;
 10 import android.os.Bundle;
 11 import android.view.View;
 12 
 13 /**
 14  * 柱狀圖
 15  * 
 16  *
 17  */
 18 public class Stacked extends Activity {
 19     private ArrayList<Information> list;
 20     private String title;
 21     private double maxValue = 0; // 數據的最大值
 22 
 23     @Override
 24     protected void onCreate(Bundle savedInstanceState) {
 25         // TODO Auto-generated method stub
 26         super.onCreate(savedInstanceState);
 27         initCompont();
 28         View view = ChartFactory.getBarChartView(this, getBarDataset(list),
 29                 getBarRenderer(list), Type.STACKED); // Type.STACKED
 30         view.setBackgroundColor(Color.BLACK);
 31         setContentView(view);
 32 
 33     }
 34 
 35     /**
 36      * 初始化數據
 37      * 
 38      * @param titles
 39      * @return
 40      */
 41     @SuppressWarnings("unchecked")
 42     private void initCompont() {
 43         list = (ArrayList<Information>) getIntent().getSerializableExtra(
 44                 "informations");
 45         title = getIntent().getStringExtra("title");
 46         // 算出全部數據的最大值
 47         for (Information information : list) {
 48             double value = Double.parseDouble(information.dulian1);
 49             if (value > maxValue) {
 50                 maxValue = value;
 51             }
 52         }
 53         maxValue = maxValue + (maxValue / 6); // 讓左邊刻度線高出數字的最大值
 54     }
 55 
 56     // 描繪器設置
 57     public XYMultipleSeriesRenderer getBarRenderer(
 58             ArrayList<Information> informations) {
 59         XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
 60         // 經過SimpleSeriesDenderer設置描繪器的顏色
 61         SimpleSeriesRenderer r = new SimpleSeriesRenderer();
 62         r.setColor(Color.rgb(125, 66, 0)); // 定義柱狀圖的顏色
 63         renderer.addSeriesRenderer(r);
 64         renderer.setZoomButtonsVisible(false);
 65         setChartSettings(renderer, informations);// 設置描繪器的其餘屬性
 66         return renderer;
 67     }
 68 
 69     private void setChartSettings(XYMultipleSeriesRenderer renderer,
 70             ArrayList<Information> informations) {
 71         // renderer.setXTitle("學科");// 設置X軸名稱
 72         // renderer.setYTitle("分數");// 設置Y軸名稱
 73         renderer.setChartTitle("各科分數表");// 設置柱圖名稱
 74         renderer.setXAxisMin(0);// 設置X軸的最小值爲0.5
 75         renderer.setXAxisMax(8.5);// 設置X軸的最大值爲5
 76         renderer.setYAxisMin(0);// 設置Y軸的最小值爲0
 77         renderer.setYAxisMax(maxValue);// 設置Y軸最大值爲500
 78         renderer.setDisplayChartValues(true); // 設置是否在柱體上方顯示值
 79         renderer.setShowGrid(true);// 設置是否在圖表中顯示網格
 80         renderer.setXLabels(0);// 設置X軸顯示的刻度標籤的個數
 81         renderer.setBarSpacing(0.4); // 柱狀間的間隔
 82         renderer.setMargins(new int[] { 100, 80, 45, 50 });// 上,左,下,右
 83         renderer.setAxisTitleTextSize(25); // 座標軸標題字體大小: 16
 84         renderer.setChartTitleTextSize(25); // 圖表標題字體大小: 20
 85         renderer.setLabelsTextSize(25); // 軸標籤字體大小: 15
 86         renderer.setLegendTextSize(25); // 圖例字體大小: 15
 87         renderer.setChartValuesTextSize(25);// 柱形頂上顯示的(分數)的文本的字體大小
 88         // 爲X軸的每一個柱狀圖設置底下的標題 好比 語文 ,數學.....
 89         int count = 1;
 90         for (Information information : informations) {
 91             renderer.addXTextLabel(count, information.weidu);
 92             count++;
 93         }
 94 
 95     }
 96 
 97     // 數據設置
 98     private XYMultipleSeriesDataset getBarDataset(
 99             ArrayList<Information> informations) {
100         XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
101         CategorySeries series = new CategorySeries(title + " (單位:"
102                 + informations.get(0).dw + ")");
103         // 聲明一個柱形圖
104         // 爲柱形圖添加值
105         for (Information information : informations) {
106             series.add(Double.parseDouble(information.dulian1));
107         }
108         dataset.addSeries(series.toXYSeries());// 添加該柱形圖到數據設置列表
109 
110         return dataset;
111     }
112 }

Zhexianspa

  1 import java.util.ArrayList;
  2 import java.util.List;
  3 import org.achartengine.ChartFactory;
  4 import org.achartengine.chart.PointStyle;
  5 import org.achartengine.model.XYMultipleSeriesDataset;
  6 import org.achartengine.model.XYSeries;
  7 import org.achartengine.renderer.XYMultipleSeriesRenderer;
  8 import org.achartengine.renderer.XYSeriesRenderer;
  9 import android.app.Activity;
 10 import android.graphics.Color;
 11 import android.os.Bundle;
 12 import android.view.View;
 13 
 14 /**
 15  * 折線圖
 16  */
 17 public class Zhexian extends Activity {
 18     private ArrayList<Information> list;
 19     private String title;
 20     private double maxValue = 0; // 數據的最大值
 21 
 22     @Override
 23     public void onCreate(Bundle savedInstanceState) {
 24         super.onCreate(savedInstanceState);
 25         initCompont(); // 初始化組件
 26         String[] titles = new String[] { title }; // 表示目前就一條折線信息
 27         // List有幾條數據
 28         List<double[]> x = new ArrayList<double[]>();
 29         // X軸所顯示的數目 1,2,3,4,5.... 1條數據有幾個點信息
 30         double[] xInfo = new double[list.size()];
 31         int count = 1;
 32         for (int i = 0; i < list.size(); i++) {
 33             xInfo[i] = count;
 34             count++;
 35         }
 36 
 37         // 折線數量 1
 38         for (int i = 0; i < titles.length; i++) {
 39             x.add(xInfo);
 40         }
 41         // 折線各個點的值
 42         double[] xValue = new double[list.size()];
 43         for (int i = 0; i < xValue.length; i++) {
 44             xValue[i] = Double.parseDouble(list.get(i).dulian1);
 45             // 把數據最大值賦給maxValue
 46             if (xValue[i] > maxValue) {
 47                 maxValue = xValue[i];
 48             }
 49         }
 50         maxValue = maxValue + (maxValue / 6);
 51 
 52         List<double[]> values = new ArrayList<double[]>();
 53         values.add(xValue);
 54         int[] colors = new int[] { Color.CYAN };// 折線的顏色
 55         PointStyle[] styles = new PointStyle[] { PointStyle.DIAMOND }; // 折線的樣式
 56         XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
 57         int length = renderer.getSeriesRendererCount();
 58         for (int i = 0; i < length; i++) {
 59             ((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
 60                     .setFillPoints(true);
 61         }
 62         setChartSettings(renderer, title, "", "", 0.5, 12.5, -10, 40,
 63                 Color.LTGRAY, Color.LTGRAY);
 64         renderer.setZoomButtonsVisible(false);
 65         View view = ChartFactory.getLineChartView(this,
 66                 buildDataset(titles, x, values), renderer);
 67         view.setBackgroundColor(Color.BLACK);
 68         setContentView(view);
 69     }
 70 
 71     /**
 72      * 初始化數據
 73      * 
 74      * @param titles
 75      * @return
 76      */
 77     @SuppressWarnings("unchecked")
 78     private void initCompont() {
 79         list = (ArrayList<Information>) getIntent().getSerializableExtra(
 80                 "informations");
 81         title = getIntent().getStringExtra("title");
 82         title = title + " (單位:" + list.get(0).dw + ")"; // 處理null字符串
 83     }
 84 
 85     private XYMultipleSeriesRenderer buildRenderer(int[] colors,
 86             PointStyle[] styles) {
 87         XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
 88         setRenderer(renderer, colors, styles);
 89         return renderer;
 90     }
 91 
 92     private void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors,
 93             PointStyle[] styles) {
 94         renderer.setAxisTitleTextSize(25);
 95         renderer.setChartTitleTextSize(20);
 96         renderer.setLabelsTextSize(25); // 標註字的大小
 97         renderer.setLegendTextSize(25);
 98         renderer.setPointSize(5f);
 99         renderer.setZoomEnabled(false, false);// 設置兩指縮放的屬性
100         renderer.setPanEnabled(false, false);// 設置圖形上下左右移動的屬性
101         renderer.setMargins(new int[] { 150, 30, 45, 40 });//設置圖例的位置
102         int length = colors.length;
103         for (int i = 0; i < length; i++) {
104             XYSeriesRenderer r = new XYSeriesRenderer();
105             r.setColor(colors[i]);
106             r.setPointStyle(styles[i]);
107             renderer.addSeriesRenderer(r);
108         }
109     }
110 
111     private void setChartSettings(XYMultipleSeriesRenderer renderer,
112             String title, String xTitle, String yTitle, double xMin,
113             double xMax, double yMin, double yMax, int axesColor,
114             int labelsColor) {
115         renderer.setYTitle("數量");// 設置Y軸名稱
116         renderer.setXAxisMin(0.5);// 設置X軸的最小值爲0.5
117         renderer.setXAxisMax(5.5);// 設置X軸的最大值爲5
118         renderer.setYAxisMin(0);// 設置Y軸的最小值爲0
119         renderer.setYAxisMax(maxValue);// 設置Y軸最大值爲500
120         renderer.setDisplayChartValues(true); // 設置是否在柱體上方顯示值
121         renderer.setShowGrid(true);// 設置是否在圖表中顯示網格
122         renderer.setXLabels(0);// 設置X軸顯示的刻度標籤的個數
123         renderer.setBarSpacing(1);
124         renderer.setXLabels(0);// 設置X軸顯示的刻度標籤的個數
125         int tempNum = 1;
126         for (Information information : list) {
127             renderer.addXTextLabel(tempNum, information.weidu);
128             tempNum++;
129         }
130     }
131 
132     private XYMultipleSeriesDataset buildDataset(String[] titles,
133             List<double[]> xValues, List<double[]> yValues) {
134         XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
135         addXYSeries(dataset, titles, xValues, yValues, 0);
136         return dataset;
137     }
138 
139     private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,
140             List<double[]> xValues, List<double[]> yValues, int scale) {
141         int length = titles.length;
142         for (int i = 0; i < length; i++) {
143             XYSeries series = new XYSeries(titles[i], scale);
144             double[] xV = xValues.get(i);
145             double[] yV = yValues.get(i);
146             int seriesLength = xV.length;
147             for (int k = 0; k < seriesLength; k++) {
148                 series.add(xV[k], yV[k]);
149             }
150             dataset.addSeries(series);
151         }
152     }
153 }
其實主要是一些屬性的設置,代碼上基本都有解釋。
相關文章
相關標籤/搜索