MPAndroidChart
1.在项目当中很多时候要对数据进行分析就要用到图表,在gitHub上有很多优秀的图表开源库,这里就简单介绍一下MPAndroidChart。
他可以实现图表的拖动,3D,局部查看,数据动态展示等功能
代码下载:下载
2.贴一些效果图出来:
linechart
![](https://www.eimg.top/images/2020/03/27/00e038543ed0c23cf45fe7b764014558.jpg)
填充式lineChart
![](https://www.eimg.top/images/2020/03/27/745787fb84b4766fa57b44d9cc13e97c.png)
单条线的LineChart
![](https://www.eimg.top/images/2020/03/27/5809298f41a5b6e00394431dfa19f97c.png)
BarChart2D
![](https://www.eimg.top/images/2020/03/27/eb45d39c30ec506224ba8153aa51a9d0.jpg)
![](https://www.eimg.top/images/2020/03/27/affb7ed56ae8e6e8c1f5de791d9ee532.png)
![](https://www.eimg.top/images/2020/03/27/ac3e91226a63482fc093e068c4296876.png)
BarChart3D
PieChart
![](https://www.eimg.top/images/2020/03/27/d8cbe72ccc01c639f33260edb56fca76.png)
![](https://www.eimg.top/images/2020/03/27/ff17f4574aaa142ecf63e418aaa1193e.png)
ScatterChart
3.使用方法在giHub上下载这个库:https://github.com/PhilJay/MPAndroidChart
你在查看它给的示例的时候可能项目会出错,我这里是环境编码问题,改成utf-8就OK了
xml中
1 <com.github.mikephil.charting.charts.PieChart 2 android:id="@+id/spread_pie_chart" 3 android:layout_width="match_parent" 4 android:layout_height="320dip" />
activity中
初始化
1 ColorTemplate mCt; 2 mCt = new ColorTemplate(); 3 mCt.addDataSetColors(ColorTemplate.PASTEL_COLORS, this); 4 mChart = (PieChart) headView.findViewById(R.id.spread_pie_chart); 5 mChart.setColorTemplate(mCt); 6 mChart.setDescription(""); 7 mChart.setHoleRadius(30f); 8 mChart.setTransparentCircleRadius(0f); 9 mChart.setCenterTextSize(18f); 10 mChart.setDrawXValues(true); 11 mChart.setUsePercentValues(true); 12 mChart.setDrawLegend(false); 13 // space between slices 14 mChart.setSliceSpace(1f); 15 mChart.setDrawHoleEnabled(false); 16 mChart.setTouchEnabled(false);
数据:
1 ArrayList<Entry> yVals = new ArrayList<Entry>(); 2 ArrayList<String> xVals = new ArrayList<String>(); 3 for (int i = 0; i < listDatas.size(); i++) { 4 yVals.add(new Entry((float)listDatas.get(i).getProvinceCount(), i)); 5 xVals.add(listDatas.get(i).getProvinceName()); 6 } 7 DataSet set1 = new DataSet(yVals, "Content"); 8 ArrayList<DataSet> dataSets = new ArrayList<DataSet>(); 9 dataSets.add(set1); 10 ChartData data = new ChartData(xVals, dataSets); 11 mChart.setData(data);
其中listData为自己项目中的数据源。
来源:https://www.cnblogs.com/liangstudyhome/p/4135591.html