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逻辑代码如下。

importjava.util.ArrayList; 
importcom.github.mikephil.charting.charts.PieChart; 
importcom.github.mikephil.charting.components.Legend; 
importcom.github.mikephil.charting.components.Legend.LegendPosition; 
importcom.github.mikephil.charting.data.Entry; 
importcom.github.mikephil.charting.data.PieData; 
importcom.github.mikephil.charting.data.PieDataSet; 
import android.support.v7.app.ActionBarActivity; 
importandroid.graphics.Color; 
importandroid.os.Bundle; 
importandroid.util.DisplayMetrics; 
public class MainActivity extends ActionBarActivity { 
privatePieChartmChart; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
mChart = (PieChart) findViewById(R.id.spread_pie_chart); 
PieDatamPieData = getPieData(4, 100); 
showChart(mChart, mPieData); 
} 
private void showChart(PieChartpieChart, PieDatapieData) { 
pieChart.setHoleColorTransparent(true); 
pieChart.setHoleRadius(60f); //半径
pieChart.setTransparentCircleRadius(64f); // 半透明圈
//pieChart.setHoleRadius(0) //实心圆
pieChart.setDescription("测试饼状图"); 
// mChart.setDrawYValues(true); 
pieChart.setDrawCenterText(true); //饼状图中间可以添加文字
pieChart.setDrawHoleEnabled(true); 
pieChart.setRotationAngle(90); // 初始旋转角度
// draws the corresponding description value into the slice 
// mChart.setDrawXValues(true); 
// enable rotation of the chart by touch 
pieChart.setRotationEnabled(true); // 可以手动旋转
// display percentage values 
pieChart.setUsePercentValues(true); //显示成百分比
// mChart.setUnit(" 

 
« 
» 

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3