在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

MPAndroidChart开源图表库的使用介绍之饼状图、折线图和柱状图

2016-8-29 13:10| 发布者: zhangjf| 查看: 930| 评论: 0

摘要: MPAndroidChart开源图表库之饼状图   为大家介绍一款图标开源库MPAndroidChart,它不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,用起来非常灵活。MPAndroidChart同样拥有常用的图 ...

MPAndroidChart开源图表库之饼状图

  为大家介绍一款图标开源库MPAndroidChart,它不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,用起来非常灵活。MPAndroidChart同样拥有常用的图表类型:线型图、饼图、柱状图和散点图。

mpandroidchartlibrary.jar包下载地址:

https://github.com/PhilJay/MPAndroidChart/releases

  下面主要实现以下饼状图:

  1.从上面的地址中下载最新mpandroidchartlibrary-2-0-8.jar包, 然后copy到项目的libs中

  2. 定义xml文件

3. 主要Java逻辑代码如下。

  1. importjava.util.ArrayList;
  2. importcom.github.mikephil.charting.charts.PieChart;
  3. importcom.github.mikephil.charting.components.Legend;
  4. importcom.github.mikephil.charting.components.Legend.LegendPosition;
  5. importcom.github.mikephil.charting.data.Entry;
  6. importcom.github.mikephil.charting.data.PieData;
  7. importcom.github.mikephil.charting.data.PieDataSet;
  8. import android.support.v7.app.ActionBarActivity;
  9. importandroid.graphics.Color;
  10. importandroid.os.Bundle;
  11. importandroid.util.DisplayMetrics;
  12. public class MainActivity extends ActionBarActivity {
  13. privatePieChartmChart;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. mChart = (PieChart) findViewById(R.id.spread_pie_chart);
  19. PieDatamPieData = getPieData(4, 100);
  20. showChart(mChart, mPieData);
  21. }
  22. private void showChart(PieChartpieChart, PieDatapieData) {
  23. pieChart.setHoleColorTransparent(true);
  24. pieChart.setHoleRadius(60f); //半径
  25. pieChart.setTransparentCircleRadius(64f); // 半透明圈
  26. //pieChart.setHoleRadius(0) //实心圆
  27. pieChart.setDescription("测试饼状图");
  28. // mChart.setDrawYValues(true);
  29. pieChart.setDrawCenterText(true); //饼状图中间可以添加文字
  30. pieChart.setDrawHoleEnabled(true);
  31. pieChart.setRotationAngle(90); // 初始旋转角度
  32. // draws the corresponding description value into the slice
  33. // mChart.setDrawXValues(true);
  34. // enable rotation of the chart by touch
  35. pieChart.setRotationEnabled(true); // 可以手动旋转
  36. // display percentage values
  37. pieChart.setUsePercentValues(true); //显示成百分比
  38. // mChart.setUnit(" ?");
  39. // mChart.setDrawUnitsInChart(true);
  40. // add a selection listener
  41. // mChart.setOnChartValueSelectedListener(this);
  42. // mChart.setTouchEnabled(false);
  43. // mChart.setOnAnimationListener(this);
  44. pieChart.setCenterText("Quarterly Revenue"); //饼状图中间的文字
  45. //设置数据
  46. pieChart.setData(pieData);
  47. // undo all highlights
  48. // pieChart.highlightValues(null);
  49. // pieChart.invalidate();
  50. Legend mLegend = pieChart.getLegend(); //设置比例图
  51. mLegend.setPosition(LegendPosition.RIGHT_OF_CHART); //最右边显示
  52. // mLegend.setForm(LegendForm.LINE); //设置比例图的形状,默认是方形
  53. mLegend.setXEntrySpace(7f);
  54. mLegend.setYEntrySpace(5f);
  55. pieChart.animateXY(1000, 1000); //设置动画
  56. // mChart.spin(2000, 0, 360);
  57. }
  58. /**
  59. *
  60. * @param count 分成几部分
  61. * @param range
  62. */
  63. privatePieDatagetPieData(int count, float range) {
  64. ArrayList<String>xValues = new ArrayList<String>(); //xVals用来表示每个饼块上的内容
  65. for (inti = 0; i< count; i++) {
  66. xValues.add("Quarterly" + (i + 1)); //饼块上显示成Quarterly1, Quarterly2, Quarterly3, Quarterly4
  67. }
  68. ArrayList<Entry>yValues = new ArrayList<Entry>(); //yVals用来表示封装每个饼块的实际数据
  69. // 饼图数据
  70. /**
  71. * 将一个饼形图分成四部分,四部分的数值比例为14:14:34:38
  72. * 所以 14代表的百分比就是14%
  73. */
  74. float quarterly1 = 14;
  75. float quarterly2 = 14;
  76. float quarterly3 = 34;
  77. float quarterly4 = 38;
  78. yValues.add(new Entry(quarterly1, 0));
  79. yValues.add(new Entry(quarterly2, 1));
  80. yValues.add(new Entry(quarterly3, 2));
  81. yValues.add(new Entry(quarterly4, 3));
  82. //y轴的集合
  83. PieDataSetpieDataSet = new PieDataSet(yValues, "Quarterly Revenue 2014"/*显示在比例图上*/);
  84. pieDataSet.setSliceSpace(0f); //设置个饼状图之间的距离
  85. ArrayList<Integer> colors = new ArrayList<Integer>();
  86. // 饼图颜色
  87. colors.add(Color.rgb(205, 205, 205));
  88. colors.add(Color.rgb(114, 188, 223));
  89. colors.add(Color.rgb(255, 123, 124));
  90. colors.add(Color.rgb(57, 135, 200));
  91. pieDataSet.setColors(colors);
  92. DisplayMetrics metrics = getResources().getDisplayMetrics();
  93. floatpx = 5 * (metrics.densityDpi / 160f);
  94. pieDataSet.setSelectionShift(px); // 选中态多出的长度
  95. PieDatapieData = new PieData(xValues, pieDataSet);
  96. returnpieData;
  97. }
  98. }
复制代码

效果图如下:


MPAndroidChart开源图表库之折线图

1. 将mpandroidchartlibrary-2-0-8.jar包copy到项目的libs中

2. 定义xml文件

3. 主要Java逻辑代码如下。

  1. packagecom.example.mpandroidlinechart;
  2. importjava.util.ArrayList;
  3. importcom.github.mikephil.charting.charts.LineChart;
  4. importcom.github.mikephil.charting.components.Legend;
  5. importcom.github.mikephil.charting.components.Legend.LegendForm;
  6. importcom.github.mikephil.charting.data.Entry;
  7. importcom.github.mikephil.charting.data.LineData;
  8. importcom.github.mikephil.charting.data.LineDataSet;
  9. import android.support.v7.app.ActionBarActivity;
  10. importandroid.graphics.Color;
  11. importandroid.os.Bundle;
  12. public class MainActivity extends ActionBarActivity {
  13. privateLineChartmLineChart;
  14. // private Typeface mTf;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. mLineChart = (LineChart) findViewById(R.id.spread_line_chart);
  20. // mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf");
  21. LineDatamLineData = getLineData(36, 100);
  22. showChart(mLineChart, mLineData, Color.rgb(114, 188, 223));
  23. }
  24. // 设置显示的样式
  25. private void showChart(LineChartlineChart, LineDatalineData, int color) {
  26. lineChart.setDrawBorders(false); //是否在折线图上添加边框
  27. // no description text
  28. lineChart.setDescription("");// 数据描述
  29. // 如果没有数据的时候,会显示这个,类似listview的emtpyview
  30. lineChart.setNoDataTextDescription("You need to provide data for the chart.");
  31. // enable / disable grid background
  32. lineChart.setDrawGridBackground(false); // 是否显示表格颜色
  33. lineChart.setGridBackgroundColor(Color.WHITE& 0x70FFFFFF); // 表格的的颜色,在这里是是给颜色设置一个透明度
  34. // enable touch gestures
  35. lineChart.setTouchEnabled(true); // 设置是否可以触摸
  36. // enable scaling and dragging
  37. lineChart.setDragEnabled(true);// 是否可以拖拽
  38. lineChart.setScaleEnabled(true);// 是否可以缩放
  39. // if disabled, scaling can be done on x- and y-axis separately
  40. lineChart.setPinchZoom(false);//
  41. lineChart.setBackgroundColor(color);// 设置背景
  42. // add data
  43. lineChart.setData(lineData); // 设置数据
  44. // get the legend (only possible after setting data)
  45. Legend mLegend = lineChart.getLegend(); // 设置比例图标示,就是那个一组y的value的
  46. // modify the legend ...
  47. // mLegend.setPosition(LegendPosition.LEFT_OF_CHART);
  48. mLegend.setForm(LegendForm.CIRCLE);// 样式
  49. mLegend.setFormSize(6f);// 字体
  50. mLegend.setTextColor(Color.WHITE);// 颜色
  51. // mLegend.setTypeface(mTf);// 字体
  52. lineChart.animateX(2500); // 立即执行的动画,x轴
  53. }
  54. /**
  55. * 生成一个数据
  56. * @param count 表示图表中有多少个坐标点
  57. * @param range 用来生成range以内的随机数
  58. * @return
  59. */
  60. privateLineDatagetLineData(int count, float range) {
  61. ArrayList<String>xValues = new ArrayList<String>();
  62. for (inti = 0; i< count; i++) {
  63. // x轴显示的数据,这里默认使用数字下标显示
  64. xValues.add("" + i);
  65. }
  66. // y轴的数据
  67. ArrayList<Entry>yValues = new ArrayList<Entry>();
  68. for (inti = 0; i< count; i++) {
  69. float value = (float) (Math.random() * range) + 3;
  70. yValues.add(new Entry(value, i));
  71. }
  72. // create a dataset and give it a type
  73. // y轴的数据集合
  74. LineDataSetlineDataSet = new LineDataSet(yValues, "测试折线图" /*显示在比例图上*/);
  75. // mLineDataSet.setFillAlpha(110);
  76. // mLineDataSet.setFillColor(Color.RED);
  77. //用y轴的集合来设置参数
  78. lineDataSet.setLineWidth(1.75f); // 线宽
  79. lineDataSet.setCircleSize(3f);// 显示的圆形大小
  80. lineDataSet.setColor(Color.WHITE);// 显示颜色
  81. lineDataSet.setCircleColor(Color.WHITE);// 圆形的颜色
  82. lineDataSet.setHighLightColor(Color.WHITE); // 高亮的线的颜色
  83. ArrayList<LineDataSet>lineDataSets = new ArrayList<LineDataSet>();
  84. lineDataSets.add(lineDataSet); // add the datasets
  85. // create a data object with the datasets
  86. LineDatalineData = new LineData(xValues, lineDataSets);
  87. returnlineData;
  88. }
  89. }
复制代码

效果图如下:

MPAndroidChart开源图表库之柱状图

1. 将mpandroidchartlibrary-2-0-8.jar包copy到项目的libs中

2. 定义xml文件

3. 主要Java逻辑代码如下。

  1. packagecom.jackie.mpandoidbarchart;
  2. importjava.util.ArrayList;
  3. importcom.github.mikephil.charting.charts.BarChart;
  4. importcom.github.mikephil.charting.charts.LineChart;
  5. importcom.github.mikephil.charting.components.Legend;
  6. importcom.github.mikephil.charting.components.Legend.LegendForm;
  7. importcom.github.mikephil.charting.components.XAxis;
  8. importcom.github.mikephil.charting.components.XAxis.XAxisPosition;
  9. importcom.github.mikephil.charting.data.BarData;
  10. importcom.github.mikephil.charting.data.BarDataSet;
  11. importcom.github.mikephil.charting.data.BarEntry;
  12. import android.support.v7.app.ActionBarActivity;
  13. importandroid.graphics.Color;
  14. importandroid.os.Bundle;
  15. public class MainActivity extends ActionBarActivity {
  16. privateBarChartmBarChart;
  17. privateBarDatamBarData;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. mBarChart = (BarChart) findViewById(R.id.spread_bar_chart);
  23. mBarData = getBarData(4, 100);
  24. showBarChart(mBarChart, mBarData);
  25. }
  26. private void showBarChart(BarChartbarChart, BarDatabarData) {
  27. barChart.setDrawBorders(false); ////是否在折线图上添加边框
  28. barChart.setDescription("");// 数据描述
  29. // 如果没有数据的时候,会显示这个,类似ListView的EmptyView
  30. barChart.setNoDataTextDescription("You need to provide data for the chart.");
  31. barChart.setDrawGridBackground(false); // 是否显示表格颜色
  32. barChart.setGridBackgroundColor(Color.WHITE& 0x70FFFFFF); // 表格的的颜色,在这里是是给颜色设置一个透明度
  33. barChart.setTouchEnabled(true); // 设置是否可以触摸
  34. barChart.setDragEnabled(true);// 是否可以拖拽
  35. barChart.setScaleEnabled(true);// 是否可以缩放
  36. barChart.setPinchZoom(false);//
  37. // barChart.setBackgroundColor();// 设置背景
  38. barChart.setDrawBarShadow(true);
  39. barChart.setData(barData); // 设置数据
  40. Legend mLegend = barChart.getLegend(); // 设置比例图标示
  41. mLegend.setForm(LegendForm.CIRCLE);// 样式
  42. mLegend.setFormSize(6f);// 字体
  43. mLegend.setTextColor(Color.BLACK);// 颜色
  44. // X轴设定
  45. // XAxisxAxis = barChart.getXAxis();
  46. // xAxis.setPosition(XAxisPosition.BOTTOM);
  47. barChart.animateX(2500); // 立即执行的动画,x轴
  48. }
  49. privateBarDatagetBarData(int count, float range) {
  50. ArrayList<String>xValues = new ArrayList<String>();
  51. for (inti = 0; i< count; i++) {
  52. xValues.add("第" + (i + 1) + "季度");
  53. }
  54. ArrayList<BarEntry>yValues = new ArrayList<BarEntry>();
  55. for (inti = 0; i< count; i++) {
  56. float value = (float) (Math.random() * range/*100以内的随机数*/) + 3;
  57. yValues.add(new BarEntry(value, i));
  58. }
  59. // y轴的数据集合
  60. BarDataSetbarDataSet = new BarDataSet(yValues, "测试饼状图");
  61. barDataSet.setColor(Color.rgb(114, 188, 223));
  62. ArrayList<BarDataSet>barDataSets = new ArrayList<BarDataSet>();
  63. barDataSets.add(barDataSet); // add the datasets
  64. BarDatabarData = new BarData(xValues, barDataSets);
  65. returnbarData;
  66. }
  67. }
  68. packagecom.jackie.mpandoidbarchart;
  69. importjava.util.ArrayList;
  70. importcom.github.mikephil.charting.charts.BarChart;
  71. importcom.github.mikephil.charting.charts.LineChart;
  72. importcom.github.mikephil.charting.components.Legend;
  73. importcom.github.mikephil.charting.components.Legend.LegendForm;
  74. importcom.github.mikephil.charting.components.XAxis;
  75. importcom.github.mikephil.charting.components.XAxis.XAxisPosition;
  76. importcom.github.mikephil.charting.data.BarData;
  77. importcom.github.mikephil.charting.data.BarDataSet;
  78. importcom.github.mikephil.charting.data.BarEntry;
  79. import android.support.v7.app.ActionBarActivity;
  80. importandroid.graphics.Color;
  81. importandroid.os.Bundle;
  82. public class MainActivity extends ActionBarActivity {
  83. privateBarChartmBarChart;
  84. privateBarDatamBarData;
  85. @Override
  86. protected void onCreate(Bundle savedInstanceState) {
  87. super.onCreate(savedInstanceState);
  88. setContentView(R.layout.activity_main);
  89. mBarChart = (BarChart) findViewById(R.id.spread_bar_chart);
  90. mBarData = getBarData(4, 100);
  91. showBarChart(mBarChart, mBarData);
  92. }
  93. private void showBarChart(BarChartbarChart, BarDatabarData) {
  94. barChart.setDrawBorders(false); ////是否在折线图上添加边框
  95. barChart.setDescription("");// 数据描述
  96. // 如果没有数据的时候,会显示这个,类似ListView的EmptyView
  97. barChart.setNoDataTextDescription("You need to provide data for the chart.");
  98. barChart.setDrawGridBackground(false); // 是否显示表格颜色
  99. barChart.setGridBackgroundColor(Color.WHITE& 0x70FFFFFF); // 表格的的颜色,在这里是是给颜色设置一个透明度
  100. barChart.setTouchEnabled(true); // 设置是否可以触摸
  101. barChart.setDragEnabled(true);// 是否可以拖拽
  102. barChart.setScaleEnabled(true);// 是否可以缩放
  103. barChart.setPinchZoom(false);//
  104. // barChart.setBackgroundColor();// 设置背景
  105. barChart.setDrawBarShadow(true);
  106. barChart.setData(barData); // 设置数据
  107. Legend mLegend = barChart.getLegend(); // 设置比例图标示
  108. mLegend.setForm(LegendForm.CIRCLE);// 样式
  109. mLegend.setFormSize(6f);// 字体
  110. mLegend.setTextColor(Color.BLACK);// 颜色
  111. // X轴设定
  112. // XAxisxAxis = barChart.getXAxis();
  113. // xAxis.setPosition(XAxisPosition.BOTTOM);
  114. barChart.animateX(2500); // 立即执行的动画,x轴
  115. }
  116. privateBarDatagetBarData(int count, float range) {
  117. ArrayList<String>xValues = new ArrayList<String>();
  118. for (inti = 0; i< count; i++) {
  119. xValues.add("第" + (i + 1) + "季度");
  120. }
  121. ArrayList<BarEntry>yValues = new ArrayList<BarEntry>();
  122. for (inti = 0; i< count; i++) {
  123. float value = (float) (Math.random() * range/*100以内的随机数*/) + 3;
  124. yValues.add(new BarEntry(value, i));
  125. }
  126. // y轴的数据集合
  127. BarDataSetbarDataSet = new BarDataSet(yValues, "测试饼状图");
  128. barDataSet.setColor(Color.rgb(114, 188, 223));
  129. ArrayList<BarDataSet>barDataSets = new ArrayList<BarDataSet>();
  130. barDataSets.add(barDataSet); // add the datasets
  131. BarDatabarData = new BarData(xValues, barDataSets);
  132. returnbarData;
  133. }
  134. }
复制代码

效果图如下:


以上所述是小编给大家介绍的MPAndroidChart开源图表库的使用介绍之饼状图、折线图和柱状图的相关知识,希望对大家有所帮助。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 16:19

Copyright 2015-2025 djqfx

返回顶部