MpAndroidChart各种图表我都用了一遍,并且已经熟练掌握。总结我的Demo

view pla

首先从作者那里下载 MpAndroidChart.jar     :   https://github.com/PhilJay/MPAndroidChart

怎么导入项目我就不罗嗦了 ,来不及解释了,看图看代码

MpAndroidChart各种图表我都用了一遍,并且已经熟练掌握。总结我的Demo_第1张图片


[java]  view plain  copy
  1. import com.github.mikephil.charting.charts.BarChart;  
  2. import com.github.mikephil.charting.data.BarData;  
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.Window;  
  7.   
  8. public class MainActivity extends Activity {  
  9.   
  10.     private BarChart mBarChart,mBarChart2,mBarChart3,mBarChart4,mBarChart5,mBarChart6,mBarChart7,mBarChart8,mBarChart9;  
  11.     private BarData mBarData;  
  12.     private BarCharts mBarCharts;  
  13.       
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  18.         setContentView(R.layout.activity_main);  
  19.         mBarCharts = new BarCharts();  
  20.           
  21.         // 创建多张图表  
  22.         mBarChart = (BarChart) findViewById(R.id.spreadBarChart);  
  23.         mBarChart2 = (BarChart) findViewById(R.id.spreadBarChart2);  
  24.         mBarChart3 = (BarChart) findViewById(R.id.spreadBarChart3);  
  25.         mBarChart4 = (BarChart) findViewById(R.id.spreadBarChart4);  
  26.         mBarChart5 = (BarChart) findViewById(R.id.spreadBarChart5);  
  27.         mBarChart6 = (BarChart) findViewById(R.id.spreadBarChart6);  
  28.         mBarChart7 = (BarChart) findViewById(R.id.spreadBarChart7);  
  29.         mBarChart8 = (BarChart) findViewById(R.id.spreadBarChart8);  
  30.         mBarChart9 = (BarChart) findViewById(R.id.spreadBarChart9);  
  31.           
  32.         mBarData = mBarCharts.getBarData(14500);  
  33.         mBarCharts.showBarChart(mBarChart, mBarData);  
  34.           
  35.         mBarData = mBarCharts.getBarData(14900);  
  36.         mBarCharts.showBarChart(mBarChart2, mBarData);  
  37.           
  38.         mBarData = mBarCharts.getBarData(14100);  
  39.         mBarCharts.showBarChart(mBarChart3, mBarData);  
  40.           
  41.         mBarData = mBarCharts.getBarData(14100);  
  42.         mBarCharts.showBarChart(mBarChart4, mBarData);  
  43.           
  44.         mBarData = mBarCharts.getBarData(14100);  
  45.         mBarCharts.showBarChart(mBarChart5, mBarData);  
  46.           
  47.         mBarData = mBarCharts.getBarData(14100);  
  48.         mBarCharts.showBarChart(mBarChart6, mBarData);  
  49.           
  50.         mBarData = mBarCharts.getBarData(14100);  
  51.         mBarCharts.showBarChart(mBarChart7, mBarData);  
  52.           
  53.         mBarData = mBarCharts.getBarData(14100);    
  54.         mBarCharts.showBarChart(mBarChart8, mBarData);  
  55.           
  56.         mBarData = mBarCharts.getBarData(14100);  
  57.         mBarCharts.showBarChart(mBarChart9, mBarData);  
  58.     }  
  59. }  


然后再设置图表属性:

[java]  view plain  copy
  1. package com.mpadnroidchart.barchartdemo;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.graphics.Color;  
  6.   
  7. import com.github.mikephil.charting.charts.BarChart;  
  8. import com.github.mikephil.charting.components.Legend;  
  9. import com.github.mikephil.charting.components.Legend.LegendForm;  
  10. import com.github.mikephil.charting.components.XAxis;  
  11. import com.github.mikephil.charting.components.XAxis.XAxisPosition;  
  12. import com.github.mikephil.charting.data.BarData;  
  13. import com.github.mikephil.charting.data.BarDataSet;  
  14. import com.github.mikephil.charting.data.BarEntry;  
  15.   
  16. /** 
  17.  * @author 正月 | ZhouDaDo 
  18.  */  
  19. public class BarCharts {  
  20.       
  21.     private String[] color = {"#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD"};  
  22.       
  23.     public void showBarChart(BarChart barChart, BarData barData) {  
  24.         // 数据描述        
  25.         barChart.setDescription("");  
  26.         // 如果没有数据的时候,会显示这个,类似ListView的EmptyView  
  27.         barChart.setNoDataTextDescription("You need to provide data for the chart.");  
  28.         // 是否显示表格颜色    
  29.         barChart.setDrawGridBackground(false);    
  30.         // 设置是否可以触摸  
  31.         barChart.setTouchEnabled(true);   
  32.         // 是否可以拖拽  
  33.         barChart.setDragEnabled(false);       
  34.         // 是否可以缩放  
  35.         barChart.setScaleEnabled(false);  
  36.         // 集双指缩放  
  37.         barChart.setPinchZoom(false);  
  38.         // 设置背景  
  39.         barChart.setBackgroundColor(Color.parseColor("#01000000"));  
  40.         // 如果打开,背景矩形将出现在已经画好的绘图区域的后边。  
  41.         barChart.setDrawGridBackground(false);  
  42.         // 集拉杆阴影  
  43.         barChart.setDrawBarShadow(false);  
  44.         // 图例  
  45.         barChart.getLegend().setEnabled(false);  
  46.         // 设置数据  
  47.         barChart.setData(barData);   
  48.           
  49.         // 隐藏右边的坐标轴 (就是右边的0 - 100 - 200 - 300 ... 和图表中横线)  
  50.         barChart.getAxisRight().setEnabled(false);  
  51.         // 隐藏左边的左边轴 (同上)  
  52. //        barChart.getAxisLeft().setEnabled(false);  
  53.           
  54.         // 网格背景颜色  
  55.         barChart.setGridBackgroundColor(Color.parseColor("#00000000"));  
  56.         // 是否显示表格颜色  
  57.         barChart.setDrawGridBackground(false);  
  58.         // 设置边框颜色  
  59.         barChart.setBorderColor(Color.parseColor("#00000000"));  
  60.         // 说明颜色  
  61.         barChart.setDescriptionColor(Color.parseColor("#00000000"));  
  62.         // 拉杆阴影  
  63.         barChart.setDrawBarShadow(false);  
  64.         // 打开或关闭绘制的图表边框。(环绕图表的线)  
  65.         barChart.setDrawBorders(false);  
  66.           
  67.           
  68.         Legend mLegend = barChart.getLegend(); // 设置比例图标示  
  69.         // 设置窗体样式  
  70.         mLegend.setForm(LegendForm.CIRCLE);  
  71.         // 字体  
  72.         mLegend.setFormSize(4f);  
  73.         // 字体颜色  
  74.         mLegend.setTextColor(Color.parseColor("#00000000"));  
  75.           
  76.           
  77.         XAxis xAxis = barChart.getXAxis();  
  78.         xAxis.setPosition(XAxisPosition.BOTTOM);  
  79.         xAxis.setDrawGridLines(false);  
  80.         xAxis.setSpaceBetweenLabels(2);  
  81.           
  82.           
  83.         barChart.animateY(1000); // 立即执行的动画,Y轴  
  84.     }  
  85.     public BarData getBarData(int count, float range) {  
  86.         ArrayList xValues = new ArrayList();  
  87.         for (int i = 0; i < count; i++) {  
  88.             xValues.add(""+(i+1)+"周");// 设置每个壮图的文字描述  
  89.         }  
  90.         ArrayList yValues = new ArrayList();  
  91.         for (int i = 0; i < count; i++) {  
  92.             float value = (float) (Math.random() * range/*100以内的随机数*/) + 3;  
  93.             yValues.add(new BarEntry(value, i));  
  94.         }  
  95.         // y轴的数据集合        
  96.         BarDataSet barDataSet = new BarDataSet(yValues, "测试饼状图");  
  97.         ArrayList colors = new ArrayList();  
  98.         for(int i = 0;i < count ;i++){  
  99.             colors.add(Color.parseColor(color[i]));  
  100.         }  
  101.         barDataSet.setColors(colors);  
  102.         // 设置栏阴影颜色  
  103.         barDataSet.setBarShadowColor(Color.parseColor("#01000000"));  
  104.         ArrayList barDataSets = new ArrayList();  
  105.         barDataSets.add(barDataSet);  
  106.         barDataSet.setValueTextColor(Color.parseColor("#FF8F9D"));  
  107.         // 绘制值  
  108.         barDataSet.setDrawValues(true);  
  109.         BarData barData = new BarData(xValues, barDataSets);  
  110.         return barData;  
  111.     }  
  112. }  

点击下载柱状图Demo


有的小伙伴觉得柱状图不过瘾,需要多个柱状图在一起来表示一种信息 : 像这样的 

MpAndroidChart各种图表我都用了一遍,并且已经熟练掌握。总结我的Demo_第2张图片

来不及解释了,上代码 。

[java]  view plain  copy
  1. package com.barchart.barchart3sdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Window;  
  6.   
  7. import com.github.mikephil.charting.charts.BarChart;  
  8. import com.github.mikephil.charting.data.BarData;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     private BarChart3s mBarChart3s;  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  16.         setContentView(R.layout.activity_main);  
  17.           
  18.         BarChart chart = (BarChart) findViewById(R.id.chart);  
  19.         mBarChart3s = new BarChart3s(chart);  
  20.         BarData data = new BarData(mBarChart3s.getXAxisValues(), mBarChart3s.getDataSet());  
  21.         // 设置数据  
  22.         chart.setData(data);  
  23.           
  24.     }  
  25. }  

[java]  view plain  copy
  1. package com.barchart.barchart3sdemo;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.graphics.Color;  
  6.   
  7. import com.github.mikephil.charting.charts.BarChart;  
  8. import com.github.mikephil.charting.components.Legend;  
  9. import com.github.mikephil.charting.components.Legend.LegendForm;  
  10. import com.github.mikephil.charting.components.XAxis;  
  11. import com.github.mikephil.charting.components.XAxis.XAxisPosition;  
  12. import com.github.mikephil.charting.data.BarDataSet;  
  13. import com.github.mikephil.charting.data.BarEntry;  
  14.   
  15. public class BarChart3s {  
  16.   
  17.     public BarChart3s(BarChart chart) {  
  18.         // 数据描述    
  19.         chart.setDescription("");  
  20.         // 动画  
  21.         chart.animateY(1000);  
  22.         // 设置是否可以触摸  
  23.         chart.setTouchEnabled(true);  
  24.         // 是否可以拖拽  
  25.         chart.setDragEnabled(false);       
  26.         // 是否可以缩放  
  27.         chart.setScaleEnabled(false);  
  28.         // 集双指缩放  
  29.         chart.setPinchZoom(false);  
  30.         // 隐藏右边的坐标轴  
  31.         chart.getAxisRight().setEnabled(false);  
  32.         // 隐藏左边的左边轴  
  33.         chart.getAxisLeft().setEnabled(false);  
  34.           
  35.         Legend mLegend = chart.getLegend(); // 设置比例图标示  
  36.         // 设置窗体样式  
  37.         mLegend.setForm(LegendForm.SQUARE);  
  38.         // 字体  
  39.         mLegend.setFormSize(4f);  
  40.         // 字体颜色  
  41.         mLegend.setTextColor(Color.parseColor("#7e7e7e"));  
  42.           
  43.           
  44.         XAxis xAxis = chart.getXAxis();  
  45.         xAxis.setPosition(XAxisPosition.BOTTOM);  
  46.         xAxis.setDrawGridLines(false);  
  47.         xAxis.setSpaceBetweenLabels(2);  
  48.           
  49.         chart.invalidate();  
  50.     }  
  51.     public ArrayList getDataSet() {  
  52.         ArrayList dataSets = null;  
  53.         ArrayList valueSet1 = new ArrayList();  
  54.         BarEntry v1e1 = new BarEntry(110.000f, 0); // Jan  
  55.         valueSet1.add(v1e1);  
  56.         BarEntry v1e2 = new BarEntry(40.000f, 1); // Feb  
  57.         valueSet1.add(v1e2);  
  58.         BarEntry v1e3 = new BarEntry(60.000f, 2); // Mar  
  59.         valueSet1.add(v1e3);  
  60.         BarEntry v1e4 = new BarEntry(30.000f, 3); // Apr  
  61.         valueSet1.add(v1e4);  
  62.         BarEntry v1e5 = new BarEntry(90.000f, 4); // May  
  63.         valueSet1.add(v1e5);  
  64.         BarEntry v1e6 = new BarEntry(100.000f, 5); // Jun  
  65.         valueSet1.add(v1e6);  
  66.    
  67.         ArrayList valueSet2 = new ArrayList();  
  68.         BarEntry v2e1 = new BarEntry(150.000f, 0); // Jan  
  69.         valueSet2.add(v2e1);  
  70.         BarEntry v2e2 = new BarEntry(90.000f, 1); // Feb  
  71.         valueSet2.add(v2e2);  
  72.         BarEntry v2e3 = new BarEntry(120.000f, 2); // Mar  
  73.         valueSet2.add(v2e3);  
  74.         BarEntry v2e4 = new BarEntry(60.000f, 3); // Apr  
  75.         valueSet2.add(v2e4);  
  76.         BarEntry v2e5 = new BarEntry(20.000f, 4); // May  
  77.         valueSet2.add(v2e5);  
  78.         BarEntry v2e6 = new BarEntry(80.000f, 5); // Jun  
  79.         valueSet2.add(v2e6);  
  80.           
  81.         ArrayList valueSet3 = new ArrayList();  
  82.         BarEntry v3e1 = new BarEntry(20.000f, 0); // Jan  
  83.         valueSet3.add(v3e1);  
  84.         BarEntry v3e2 = new BarEntry(60.000f, 1); // Feb  
  85.         valueSet3.add(v3e2);  
  86.         BarEntry v3e3 = new BarEntry(90.000f, 2); // Mar  
  87.         valueSet3.add(v3e3);  
  88.         BarEntry v3e4 = new BarEntry(150.000f, 3); // Apr  
  89.         valueSet3.add(v3e4);  
  90.         BarEntry v3e5 = new BarEntry(120.000f, 4); // May  
  91.         valueSet3.add(v3e5);  
  92.         BarEntry v3e6 = new BarEntry(40.000f, 5); // Jun  
  93.         valueSet3.add(v3e6);  
  94.    
  95.         BarDataSet barDataSet1 = new BarDataSet(valueSet1, "数据1注解");  
  96.         barDataSet1.setColor(Color.parseColor("#F26077"));  
  97.         barDataSet1.setBarShadowColor(Color.parseColor("#01000000"));  
  98.           
  99.         BarDataSet barDataSet2 = new BarDataSet(valueSet2, "数据2注解");  
  100.         barDataSet2.setColor(Color.parseColor("#00C0BF"));  
  101.         barDataSet2.setBarShadowColor(Color.parseColor("#01000000"));  
  102.           
  103.         BarDataSet barDataSet3 = new BarDataSet(valueSet3, "数据3注解");  
  104.         barDataSet3.setColor(Color.parseColor("#DEAD26"));  
  105.         barDataSet3.setBarShadowColor(Color.parseColor("#01000000"));  
  106.           
  107.         dataSets = new ArrayList();  
  108.         dataSets.add(barDataSet1);  
  109.         dataSets.add(barDataSet2);  
  110.         dataSets.add(barDataSet3);  
  111.           
  112.         return dataSets;  
  113.     }  
  114.     public ArrayList getXAxisValues() {  
  115.         ArrayList xAxis = new ArrayList();  
  116.         xAxis.add("星期一");  
  117.         xAxis.add("星期二");  
  118.         xAxis.add("星期三");  
  119.         xAxis.add("星期四");  
  120.         xAxis.add("星期五");  
  121.         xAxis.add("星期六");  
  122.         return xAxis;  
  123.     }  
  124. }  

点击下载BarChart3sDemo


但是我们有时需求希望可以在柱状图上再来个折线图。或者来一条线代表柱状图的平均值。 像这样

MpAndroidChart各种图表我都用了一遍,并且已经熟练掌握。总结我的Demo_第3张图片


来不及解释了,看代码:

[java]  view plain  copy
  1. package com.mpchart.barchartcombined;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import android.graphics.Color;  
  8.   
  9. import com.github.mikephil.charting.charts.CombinedChart;  
  10. import com.github.mikephil.charting.charts.CombinedChart.DrawOrder;  
  11. import com.github.mikephil.charting.components.Legend;  
  12. import com.github.mikephil.charting.components.Legend.LegendForm;  
  13. import com.github.mikephil.charting.components.XAxis;  
  14. import com.github.mikephil.charting.components.XAxis.XAxisPosition;  
  15. import com.github.mikephil.charting.components.YAxis;  
  16. import com.github.mikephil.charting.data.BarData;  
  17. import com.github.mikephil.charting.data.BarDataSet;  
  18. import com.github.mikephil.charting.data.BarEntry;  
  19. import com.github.mikephil.charting.data.BubbleData;  
  20. import com.github.mikephil.charting.data.BubbleDataSet;  
  21. import com.github.mikephil.charting.data.BubbleEntry;  
  22. import com.github.mikephil.charting.data.CandleData;  
  23. import com.github.mikephil.charting.data.CandleDataSet;  
  24. import com.github.mikephil.charting.data.CandleEntry;  
  25. import com.github.mikephil.charting.data.CombinedData;  
  26. import com.github.mikephil.charting.data.Entry;  
  27. import com.github.mikephil.charting.data.LineData;  
  28. import com.github.mikephil.charting.data.LineDataSet;  
  29. import com.github.mikephil.charting.data.ScatterData;  
  30. import com.github.mikephil.charting.data.ScatterDataSet;  
  31. import com.github.mikephil.charting.utils.ColorTemplate;  
  32.   
  33. public class BarChartCombined {  
  34.       
  35.     private final int itemcount = 12;  
  36.     protected String[] mMonths = new String[] {"Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Okt""Nov""Dec"};  
  37.     protected String[] colors = {"#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD","#C4FF8E","#FFF88D","#FFD38C","#8CEBFF","#FF8F9D","#6BF3AD"};  
  38.       
  39.     // 设置图表样式  
  40.     public BarChartCombined(CombinedChart mChart) {  
  41.           
  42.         mChart.setDescription("");  
  43.         mChart.setDrawGridBackground(false);  
  44.         mChart.setDrawBarShadow(false);  
  45.         // 设置是否可以触摸  
  46.         mChart.setTouchEnabled(true);  
  47.         // 是否可以拖拽  
  48.         mChart.setDragEnabled(false);  
  49.         // 是否可以缩放  
  50.         mChart.setScaleEnabled(false);  
  51.         // 集双指缩放  
  52.         mChart.setPinchZoom(false);  
  53.         // 下面的图例图例  
  54.         mChart.getLegend().setEnabled(false);  
  55.         // 设置背景透明  
  56.         mChart.setBackgroundColor(Color.parseColor("#00000000"));  
  57.           
  58.         // draw bars behind lines  
  59.         mChart.setDrawOrder(new DrawOrder[] {DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.CANDLE, DrawOrder.LINE, DrawOrder.SCATTER});  
  60.   
  61.         // 下面标记解释设置  
  62.         Legend mLegend = mChart.getLegend(); // 设置比例图标示  
  63.         // 设置窗体样式  
  64.         mLegend.setForm(LegendForm.CIRCLE);  
  65.         // 字体  
  66.         mLegend.setFormSize(4f);  
  67.         // 字体颜色  
  68.         mLegend.setTextColor(Color.parseColor("#000000"));  
  69.           
  70.         /*YAxis rightAxis = mChart.getAxisRight(); 
  71.         rightAxis.setDrawGridLines(false);*/  
  72.   
  73.         /*YAxis leftAxis = mChart.getAxisLeft(); 
  74.         leftAxis.setDrawGridLines(false);*/  
  75.   
  76.         XAxis xAxis = mChart.getXAxis();  
  77.         xAxis.setPosition(XAxisPosition.BOTTOM);  
  78.         xAxis.setDrawGridLines(false);  
  79.         xAxis.setSpaceBetweenLabels(2);  
  80.           
  81.         CombinedData data = new CombinedData(mMonths);  
  82.         data.setData(generateLineData());  
  83.         data.setData(generateBarData());  
  84. //      data.setData(generateBubbleData());  
  85. //      data.setData(generateScatterData());  
  86. //      data.setData(generateCandleData());  
  87.   
  88.         // 隐藏右边的坐标轴  
  89.         mChart.getAxisRight().setEnabled(false);  
  90.         // 隐藏左边的左边轴  
  91.         mChart.getAxisLeft().setEnabled(false);  
  92.           
  93.         mChart.setData(data);  
  94.         // 立即执行的动画,Y轴  
  95.         mChart.animateY(1000);   
  96.         // 刷新图表  
  97. //      mChart.invalidate();  
  98.     }  
  99.       
  100.     // 柱状图上面的折线图  
  101.     public LineData generateLineData() {  
  102.         LineData d = new LineData();  
  103.           
  104.         ArrayList entries1 = new ArrayList();  
  105.         for (int index = 0; index < itemcount; index++)  
  106.             entries1.add(new Entry(45, index));  
  107.   
  108.         LineDataSet set1 = new LineDataSet(entries1, "50对比线");  
  109.         // 设置颜色  
  110.         set1.setColor(Color.parseColor("#00A918"));  
  111.         // 线宽  
  112.         set1.setLineWidth(1.0f);  
  113.         // 圈颜色  
  114.         set1.setCircleColor(Color.parseColor("#00A918"));  
  115.         // 圆的大小  
  116.         set1.setCircleSize(0f);  
  117.         // 填充颜色  
  118.         set1.setFillColor(Color.parseColor("#00A918"));  
  119.         // 绘制立方  
  120.         set1.setDrawCubic(true);  
  121.         // 绘制数据  
  122.         set1.setDrawValues(true);  
  123.         // 字体大小  
  124.         set1.setValueTextSize(10f);  
  125.         // 字体颜色  
  126.         set1.setValueTextColor(Color.parseColor("#00000000"));  
  127.         // 轴依赖  
  128.         set1.setAxisDependency(YAxis.AxisDependency.LEFT);  
  129.           
  130.         ArrayList entries2 = new ArrayList();  
  131.         for (int index = 0; index < itemcount; index++)  
  132.             entries2.add(new Entry(30, index));  
  133.   
  134.         LineDataSet set2 = new LineDataSet(entries2, "50对比线");  
  135.         // 设置颜色  
  136.         set2.setColor(Color.parseColor("#A12200"));  
  137.         // 线宽  
  138.         set2.setLineWidth(1.0f);  
  139.         // 圈颜色  
  140.         set2.setCircleColor(Color.parseColor("#A12200"));  
  141.         // 圆的大小  
  142.         set2.setCircleSize(0f);  
  143.         // 填充颜色  
  144.         set2.setFillColor(Color.parseColor("#A12200"));  
  145.         // 绘制立方  
  146.         set2.setDrawCubic(true);  
  147.         // 绘制数据  
  148.         set2.setDrawValues(true);  
  149.         // 字体大小  
  150.         set2.setValueTextSize(10f);  
  151.         // 字体颜色  
  152.         set2.setValueTextColor(Color.parseColor("#00000000"));  
  153.         // 轴依赖  
  154.         set2.setAxisDependency(YAxis.AxisDependency.LEFT);  
  155.           
  156.         d.addDataSet(set1);  
  157.         d.addDataSet(set2);  
  158.         return d;  
  159.     }  
  160.   
  161.     public BarData generateBarData() {  
  162.         BarData d = new BarData();  
  163.         ArrayList entries = new ArrayList();  
  164.         for (int index = 0; index < itemcount; index++)  
  165.             entries.add(new BarEntry(getRandom(1003), index));  
  166.         BarDataSet set = new BarDataSet(entries, "Bar DataSet");  
  167.         // 设置颜色  
  168.         List colorlist = new ArrayList();  
  169.         for(int i=0;i < itemcount;i++){  
  170.             colorlist.add(Color.parseColor(colors[i]));  
  171.         }  
  172.         set.setColors(colorlist);  
  173.         // 字体颜色  
  174.         set.setValueTextColor(Color.parseColor("#FF8F9D"));  
  175.         // 字体大小  
  176.         set.setValueTextSize(10f);  
  177.         d.addDataSet(set);  
  178.         set.setAxisDependency(YAxis.AxisDependency.LEFT);  
  179.         return d;  
  180.     }  
  181.   
  182.     public ScatterData generateScatterData() {  
  183.   
  184.         ScatterData d = new ScatterData();  
  185.   
  186.         ArrayList entries = new ArrayList();  
  187.   
  188.         for (int index = 0; index < itemcount; index++)  
  189.             entries.add(new Entry(getRandom(2015), index));  
  190.   
  191.         ScatterDataSet set = new ScatterDataSet(entries, "Scatter DataSet");  
  192.         set.setColor(Color.GREEN);  
  193.         set.setScatterShapeSize(7.5f);  
  194.         set.setDrawValues(false);  
  195.         set.setValueTextSize(10f);  
  196.         d.addDataSet(set);  
  197.   
  198.         return d;  
  199.     }  
  200.   
  201.     public CandleData generateCandleData() {  
  202.   
  203.         CandleData d = new CandleData();  
  204.   
  205.         ArrayList entries = new ArrayList();  
  206.   
  207.         for (int index = 0; index < itemcount; index++)  
  208.             entries.add(new CandleEntry(index, 20f, 10f, 13f, 17f));  
  209.   
  210.         CandleDataSet set = new CandleDataSet(entries, "Candle DataSet");  
  211.         set.setColor(Color.rgb(808080));  
  212.         set.setBodySpace(0.3f);  
  213.         set.setValueTextSize(10f);  
  214.         set.setDrawValues(false);  
  215.         d.addDataSet(set);  
  216.   
  217.         return d;  
  218.     }  
  219.       
  220.     public BubbleData generateBubbleData() {  
  221.   
  222.         BubbleData bd = new BubbleData();  
  223.   
  224.         ArrayList entries = new ArrayList();  
  225.   
  226.         for (int index = 0; index < itemcount; index++) {  
  227.             float rnd = getRandom(2030);  
  228.             entries.add(new BubbleEntry(index, rnd, rnd));  
  229.         }  
  230.   
  231.         BubbleDataSet set = new BubbleDataSet(entries, "Bubble DataSet");  
  232.         set.setColors(ColorTemplate.VORDIPLOM_COLORS);  
  233.         set.setValueTextSize(10f);  
  234.         set.setValueTextColor(Color.WHITE);  
  235.         set.setHighlightCircleWidth(1.5f);  
  236.         set.setDrawValues(true);  
  237.         bd.addDataSet(set);  
  238.   
  239.         return bd;  
  240.     }  
  241.   
  242.     private float getRandom(float range, float startsfrom) {  
  243.         return (float) (Math.random() * range) + startsfrom;  
  244.     }  
  245.       
  246. }  

点击下载BarChartCombined


好了,今天写柱状图。等会写饼图和折线图!


你可能感兴趣的:(04,Android有关)