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

GitHub地址:

https://github.com/PhilJay/MPAndroidChart

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

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

2. 定义xml文件


3. 主要Java逻辑代码如下,注释已经都添加上了。

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


主要是一些基本属性和API的调用,具体每个API都有什么样的效果和作用,只能靠自己去尝试。后面还会陆陆续续为大家介绍MPAndroidChart其他类型的图表。

更多相关文章

  1. Android(安卓)ActionBar 使用详解
  2. Android(安卓)TextView文字超出一屏不能显示其它的文字 解决方案
  3. android APP字体大小,不随系统的字体大小变化而变化的方法
  4. Android(安卓)Permission 中文说明
  5. Android(安卓)Studio Checkout GitHub Error:cannot run program
  6. Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布
  7. Paint和Color的介绍
  8. android定制化显示toast
  9. Android设置AlertDialog点击按钮对话框不关闭

随机推荐

  1. 用Shape做动画实例代码
  2. httpHelper 从URL获取值的实例代码
  3. 分享PART_Editor的使用实例
  4. .net是怎么发送邮件的?
  5. 分享一个手机微网站的设计与实现
  6. socket传输protobuf字节流实例教程
  7. socket传输protobuf字节流的实例介绍
  8. 编写一个webapi框架的开端
  9. .Net Core + Angular Cli 实现开发环境搭
  10. Task用法之启动方法实例