AChartEngine高级应用之CombinedXYChart(组合统计图)在单个统计图上显示多样化的数据

阅读更多

AChartEngine高级应用之CombinedXYChart(组合统计图)在单个统计图上显示多样化的数据

现在是信息飞速发展时代,单个的统计图已经无法满足实际生产的要求,人们只想通过快速的观察就能知道很多信息,所以组合统计图就运用而生了,诸如,人们想了解一个地方的大致情况,首先可能要了解当地的天气情况,交通运输情况等,以下我模拟的是一个地方的天气、光照时间分布情况的统计图。这个是AChartEngine图表的高级应用。

构建CombinedXYChart图的步骤主要分为以下三步,还需要在项目中引入AChartEngine依赖jar包,在Manifest中添加:

1.     设置XYMultipleSeriesRenderer

XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();// 设置描绘器

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

                   }

renderer.setXLabels(12);// X轴均分12项

                   renderer.setYLabels(10);// Y轴均分10项

                   renderer.setShowGrid(true);// 显示表格

                   renderer.setXLabelsAlign(Align.RIGHT);// 右对齐

                   renderer.setYLabelsAlign(Align.RIGHT);

                   renderer.setZoomButtonsVisible(false);// 不显示放大缩小

                   renderer.setPanEnabled(false, false);// 上下左右都不可以移动

//dataset.addSeries(0, sunSeries);

                   dataset.addSeries(0, waterSeries);

                   //renderer.addSeriesRenderer(0, lightRenderer);

                   renderer.addSeriesRenderer(0, waterRenderer);

 

2.     构建数据源XYMultipleSeriesDataset

// --------------------------曲线start---------------------------------------

                   String[] titles = new String[] { "最低温度", "最高温度", "平均光照时间" };// 曲线标题

                   List x = new ArrayList();

                   for (int i = 0; i < titles.length; i++) {

                            x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });

                   }

                   List values = new ArrayList();

                   values.add(new double[] { -12.3, -8, -2, 3, 16, 18, 26.4, 26.9, 18.6,

                                     12.3, -2, -10 });

                   values.add(new double[] { 9, 10, 11, 15, 19, 23, 38, 40, 37, 32, 23, 10 });

 

                   values.add(new double[] { 6.3, 7.5, 8.5, 9.8, 10.8, 11.3, 13.5, 14.1, 10.3, 9.5, 9.2, 8 });

 

                   int[] colors = new int[] { Color.GREEN, Color.RED, Color.YELLOW };// 绿色,红色

                   PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,// 圆圈状

                                     PointStyle.DIAMOND, PointStyle.SQUARE };// 菱形状,矩形状

XYSeries waterSeries = new XYSeries("平均温度");

                   waterSeries.add(1, -3);// 添加数据

                   waterSeries.add(2, 6);

                   waterSeries.add(3, 10);

                   waterSeries.add(4, 17);

                   waterSeries.add(5, 20);

                   waterSeries.add(6, 26);

                   waterSeries.add(7, 29);

                   waterSeries.add(8, 30);

                   waterSeries.add(9, 23);

                   waterSeries.add(10, 19);

                   waterSeries.add(11, 10);

                   waterSeries.add(12, -1);

                   renderer.setBarSpacing(0.5);

 

                   XYSeriesRenderer waterRenderer = new XYSeriesRenderer();

                   waterRenderer.setColor(Color.CYAN);// 青色

                   waterRenderer.setDisplayChartValues(true);// 显示数值

                   waterRenderer.setChartValuesTextSize(10);// 设置数值字体

 

                   XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);

                   //dataset.addSeries(0, sunSeries);

                   dataset.addSeries(0, waterSeries);

                   //renderer.addSeriesRenderer(0, lightRenderer);

                   renderer.addSeriesRenderer(0, waterRenderer);

 

3.     通过ChartFactory生成CombinedXYChartView组合统计图

mViewChart = ChartFactory.getCombinedXYChartView(

                                     getApplicationContext(), dataset, renderer, types);// 通过ChartFactory生成图表

 

                   mLayout.addView(mViewChart, new LayoutParams(LayoutParams.FILL_PARENT,

                                     LayoutParams.FILL_PARENT));// 将图表添加到布局中去

效果图:

 AChartEngine高级应用之CombinedXYChart(组合统计图)在单个统计图上显示多样化的数据_第1张图片
 

Code:

 

[java]  view plain copy 在CODE上查看代码片
  1. package com.qiuzhping.achart;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.achartengine.ChartFactory;  
  7. import org.achartengine.GraphicalView;  
  8. import org.achartengine.chart.BarChart;  
  9. import org.achartengine.chart.BubbleChart;  
  10. import org.achartengine.chart.CubicLineChart;  
  11. import org.achartengine.chart.LineChart;  
  12. import org.achartengine.chart.PointStyle;  
  13. import org.achartengine.model.XYMultipleSeriesDataset;  
  14. import org.achartengine.model.XYSeries;  
  15. import org.achartengine.model.XYValueSeries;  
  16. import org.achartengine.renderer.XYMultipleSeriesRenderer;  
  17. import org.achartengine.renderer.XYSeriesRenderer;  
  18.   
  19. import android.app.Activity;  
  20. import android.graphics.Color;  
  21. import android.graphics.Paint.Align;  
  22. import android.os.Bundle;  
  23. import android.util.Log;  
  24. import android.view.ViewGroup.LayoutParams;  
  25. import android.view.Window;  
  26. import android.widget.LinearLayout;  
  27.   
  28. public class CombinedChart extends Activity {  
  29.     protected XYMultipleSeriesRenderer buildRenderer(int[] colors,  
  30.             PointStyle[] styles) {  
  31.         XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();// 设置两个描绘器  
  32.         setRenderer(renderer, colors, styles);  
  33.         return renderer;  
  34.     }  
  35.   
  36.     protected void setChartSettings(XYMultipleSeriesRenderer renderer,  
  37.             String title, String xTitle, String yTitle, double xMin,  
  38.             double xMax, double yMin, double yMax, int axesColor,  
  39.             int labelsColor) {// 图表样式设置  
  40.         renderer.setChartTitle(title);  
  41.         renderer.setXTitle(xTitle);// X轴标题  
  42.         renderer.setYTitle(yTitle);// Y轴标题  
  43.         renderer.setXAxisMin(xMin);// X最小值  
  44.         renderer.setXAxisMax(xMax);// X最大值  
  45.         renderer.setYAxisMin(yMin);// Y最小值  
  46.         renderer.setYAxisMax(yMax);// Y最小值  
  47.         renderer.setAxesColor(axesColor);// X轴颜色  
  48.         renderer.setLabelsColor(labelsColor);// Y轴颜色  
  49.     }  
  50.   
  51.     protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors,  
  52.             PointStyle[] styles) {// 设置描绘器属性  
  53.         renderer.setAxisTitleTextSize(16);  
  54.         renderer.setChartTitleTextSize(20);  
  55.         renderer.setLabelsTextSize(15);  
  56.         renderer.setLegendTextSize(15);  
  57.         renderer.setPointSize(5f);  
  58.         renderer.setMargins(new int[] { 20301520 });// 上,左,下,右  
  59.         int length = colors.length;  
  60.         for (int i = 0; i < length; i++) {  
  61.             XYSeriesRenderer r = new XYSeriesRenderer();  
  62.             r.setColor(colors[i]);  
  63.             r.setPointStyle(styles[i]);  
  64.             renderer.addSeriesRenderer(r);  
  65.         }  
  66.     }  
  67.   
  68.     public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,  
  69.             List<double[]> xValues, List<double[]> yValues, int scale) {// 设置点集  
  70.         int length = titles.length;  
  71.         for (int i = 0; i < length; i++) {  
  72.             XYSeries series = new XYSeries(titles[i], scale);  
  73.             double[] xV = xValues.get(i);  
  74.             double[] yV = yValues.get(i);  
  75.             int seriesLength = xV.length;  
  76.             for (int k = 0; k < seriesLength; k++) {  
  77.                 series.add(xV[k], yV[k]);  
  78.             }  
  79.             dataset.addSeries(series);  
  80.         }  
  81.     }  
  82.   
  83.     protected XYMultipleSeriesDataset buildDataset(String[] titles,  
  84.             List<double[]> xValues, List<double[]> yValues) {// 设置数据集  
  85.         XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();  
  86.         addXYSeries(dataset, titles, xValues, yValues, 0);  
  87.         return dataset;  
  88.     }  
  89.   
  90.     @Override  
  91.     protected void onCreate(Bundle savedInstanceState) {  
  92.         super.onCreate(savedInstanceState);  
  93.         requestWindowFeature(Window.FEATURE_NO_TITLE);// 消除标题栏  
  94.         setContentView(R.layout.xy_chart);  
  95.         LinearLayout mLinear = (LinearLayout) findViewById(R.id.chart);// 获取布局  
  96.         mLinear.setBackgroundColor(Color.BLACK);// 设置背景色  
  97.         // --------------------------曲线start---------------------------------------  
  98.         String[] titles = new String[] { "最低温度""最高温度""平均光照时间" };// 曲线标题  
  99.         List<double[]> x = new ArrayList<double[]>();  
  100.         for (int i = 0; i < titles.length; i++) {  
  101.             x.add(new double[] { 123456789101112 });  
  102.         }  
  103.         List<double[]> values = new ArrayList<double[]>();  
  104.         values.add(new double[] { -12.3, -8, -23161826.426.918.6,  
  105.                 12.3, -2, -10 });  
  106.         values.add(new double[] { 91011151923384037322310 });  
  107.   
  108.         values.add(new double[] { 6.37.58.59.810.811.313.514.110.39.59.28 });  
  109.   
  110.         int[] colors = new int[] { Color.GREEN, Color.RED, Color.YELLOW };// 绿色,红色  
  111.         PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,// 圆圈状  
  112.                 PointStyle.DIAMOND, PointStyle.SQUARE };// 菱形状,矩形状  
  113.         // 还有其他类型:X("x"), CIRCLE("circle"), TRIANGLE("triangle"),  
  114.         // SQUARE("square"), DIAMOND("diamond"), POINT("point")  
  115.         // ----------------------------曲线end---------------------------------  
  116.   
  117.         XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();// 设置描绘器  
  118.         setRenderer(renderer, colors, styles);  
  119.         renderer.setPointSize(5.5f);  
  120.         int length = renderer.getSeriesRendererCount();  
  121.         for (int i = 0; i < length; i++) {  
  122.             XYSeriesRenderer r = (XYSeriesRenderer) renderer  
  123.                     .getSeriesRendererAt(i);  
  124.             Log.i("length""length = " + length);  
  125.             r.setLineWidth(2f);// 宽度  
  126.             r.setFillPoints(true);// 完全填充  
  127.         }  
  128.         setChartSettings(renderer, "气温光照分布图""月份""数量(度或小时)"0.512.5, -2040,  
  129.                 Color.LTGRAY, Color.LTGRAY);// 设置图表的X轴,Y轴,标题  
  130.   
  131.         renderer.setXLabels(12);// X轴均分12项  
  132.         renderer.setYLabels(10);// Y轴均分10项  
  133.         renderer.setShowGrid(true);// 显示表格  
  134.         renderer.setXLabelsAlign(Align.RIGHT);// 右对齐  
  135.         renderer.setYLabelsAlign(Align.RIGHT);  
  136.         renderer.setZoomButtonsVisible(false);// 不显示放大缩小  
  137.         renderer.setPanEnabled(falsefalse);// 上下左右都不可以移动  
  138.   
  139.         /* -----------------------------光照分布图start------------------------------------ 
  140.         XYValueSeries sunSeries = new XYValueSeries("光照时间"); 
  141.         sunSeries.add(1f, 10, 4.3); 
  142.         sunSeries.add(2f, 10, 4.9); 
  143.         sunSeries.add(3f, 10, 5.9); 
  144.         sunSeries.add(4f, 10, 8.8); 
  145.         sunSeries.add(5f, 10, 10.8); 
  146.         sunSeries.add(6f, 10, 11.9); 
  147.         sunSeries.add(7f, 10, 13.6); 
  148.         sunSeries.add(8f, 10, 12.8); 
  149.         sunSeries.add(9f, 10, 11.4); 
  150.         sunSeries.add(10f, 10, 9.5); 
  151.         sunSeries.add(11f, 10, 7.5); 
  152.         sunSeries.add(12f, 10, 5.5); 
  153.  
  154.         XYSeriesRenderer lightRenderer = new XYSeriesRenderer(); 
  155.         lightRenderer.setColor(Color.YELLOW); 
  156.         -----------------------------光照分布图end------------------------------------*/   
  157.   
  158.         XYSeries waterSeries = new XYSeries("平均温度");  
  159.         waterSeries.add(1, -3);// 添加数据  
  160.         waterSeries.add(26);  
  161.         waterSeries.add(310);  
  162.         waterSeries.add(417);  
  163.         waterSeries.add(520);  
  164.         waterSeries.add(626);  
  165.         waterSeries.add(729);  
  166.         waterSeries.add(830);  
  167.         waterSeries.add(923);  
  168.         waterSeries.add(1019);  
  169.         waterSeries.add(1110);  
  170.         waterSeries.add(12, -1);  
  171.         renderer.setBarSpacing(0.5);  
  172.   
  173.         XYSeriesRenderer waterRenderer = new XYSeriesRenderer();  
  174.         waterRenderer.setColor(Color.CYAN);// 青色  
  175.         waterRenderer.setDisplayChartValues(true);// 显示数值  
  176.         waterRenderer.setChartValuesTextSize(10);// 设置数值字体  
  177.   
  178.         XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);  
  179.         //dataset.addSeries(0, sunSeries);  
  180.         dataset.addSeries(0, waterSeries);  
  181.         //renderer.addSeriesRenderer(0, lightRenderer);  
  182.         renderer.addSeriesRenderer(0, waterRenderer);  
  183.   
  184.         String[] types = new String[] { BarChart.TYPE, LineChart.TYPE,  
  185.                 LineChart.TYPE, CubicLineChart.TYPE };// 块状,泡沫,线性、柱形类型  
  186.         // 其他的还有RangeStackedBarChart区域图、BubbleChart泡沫状、ScatterChart  
  187.         // 散点、TimeChart时间  
  188.   
  189.         GraphicalView mChartView = ChartFactory.getCombinedXYChartView(  
  190.                 getApplicationContext(), dataset, renderer, types);  
  191.         mLinear.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,  
  192.                 LayoutParams.FILL_PARENT));  
  193.     }  
  194. }  

 

 

对应完整项目:http://download.csdn.net/detail/qiu_11/6864045

未完待续…..

 

AChartEngine应用系列文章

 

 

 

(一)AChartEngine简介

(二)AChartEngine应用之PieChart(饼图)

(三)AChartEngine应用之BarChart(柱形图)

(四)AChartEngine应用之PieChart(动态饼图,允许产生动态数据并显示)

(五)AChartEngine应用之LineChart(模拟生命特征值图)

(六)AChartEngine应用之LineChart(模拟三角函数sin,cos)

(七)AChartEngine高级应用CombinedXYChart(组合统计图)

  • AChartEngine高级应用之CombinedXYChart(组合统计图)在单个统计图上显示多样化的数据.rar (1.7 MB)
  • 下载次数: 1
  • AChartEngine高级应用之CombinedXYChart(组合统计图)在单个统计图上显示多样化的数据_第2张图片
  • 大小: 48.2 KB
  • 查看图片附件

你可能感兴趣的:(AChartEngine,android4.0,高级图表,复合图表应用)