大小:0.45MB更新时间:2018-04-10软件授权:国产软件
mpandroidchartlibrary-2-1-6.jar包是一个Android开源图表库MPAndroidChart的jar包,下载了mpandroidchartlibrary包即可使用后线图和饼图,支持选择、缩放和拖放。
文件预览
使用实例
下面主要实现以下饼状图:
1.下载最新mpandroidchartlibrary.jar包,然后copy到项目的libs中
2.定义xml文件
3.主要Java逻辑代码如下,注释已经都添加上了。
packagecom.jackie.mpandroidpiechart;
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;
importandroid.support.v7.app.ActionBarActivity;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.util.DisplayMetrics;
publicclassMainActivityextendsActionBarActivity{
privatePieChartmChart;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChart=(PieChart)findViewById(R.id.spread_pie_chart);
PieDatamPieData=getPieData(4,100);
showChart(mChart,mPieData);
}
privatevoidshowChart(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);//初始旋转角度
//drawsthecorrespondingdescriptionvalueintotheslice
//mChart.setDrawXValues(true);
//enablerotationofthechartbytouch
pieChart.setRotationEnabled(true);//可以手动旋转
//displaypercentagevalues
pieChart.setUsePercentValues(true);//显示成百分比
//mChart.setUnit(€);
//mChart.setDrawUnitsInChart(true);
//addaselectionlistener
//mChart.setOnChartValueSelectedListener(this);
//mChart.setTouchEnabled(false);
//mChart.setOnAnimationListener(this);
pieChart.setCenterText(QuarterlyRevenue);//饼状图中间的文字
//设置数据
pieChart.setData(pieData);
//undoallhighlights
//pieChart.highlightValues(null);
//pieChart.invalidate();
LegendmLegend=pieChart.getLegend();//设置比例图
mLegend.setPosition(LegendPosition.RIGHT_OF_CHART);//最右边显示
//mLegend.setForm(LegendForm.LINE);//设置比例图的形状,默认是方形
mLegend.setXEntrySpace(7f);
mLegend.setYEntrySpace(5f);
pieChart.animateXY(1000,1000);//设置动画
//mChart.spin(2000,0,360);
}
/**
*
*@paramcount分成几部分
*@paramrange
*/
privatePieDatagetPieData(intcount,floatrange){
ArrayListStringxValues=newArrayListString();//xVals用来表示每个饼块上的内容
for(inti=0;icount;i++){
xValues.add(Quarterly+(i+1));//饼块上显示成Quarterly1,Quarterly2,Quarterly3,Quarterly4
}
ArrayListEntryyValues=newArrayListEntry();//yVals用来表示封装每个饼块的实际数据
//饼图数据
/**
*将一个饼形图分成四部分,四部分的数值比例为14:14:34:38
*所以14代表的百分比就是14%
*/
floatquarterly1=14;
floatquarterly2=14;
floatquarterly3=34;
floatquarterly4=38;
yValues.add(newEntry(quarterly1,0));
yValues.add(newEntry(quarterly2,1));
yValues.add(newEntry(quarterly3,2));
yValues.add(newEntry(quarterly4,3));
//y轴的集合
PieDataSetpieDataSet=newPieDataSet(yValues,QuarterlyRevenue2014/*显示在比例图上*/);
pieDataSet.setSliceSpace(0f);//设置个饼状图之间的距离
ArrayListIntegercolors=newArrayListInteger();
//饼图颜色
colors.add(Color.rgb(205,205,205));
colors.add(Color.rgb(114,188,223));
colors.add(Color.rgb(255,123,124));
colors.add(Color.rgb(57,135,200));
pieDataSet.setColors(colors);
DisplayMetricsmetrics=getResources().getDisplayMetrics();
floatpx=5*(metrics.densityDpi/160f);
pieDataSet.setSelectionShift(px);//选中态多出的长度
PieDatapieData=newPieData(xValues,pieDataSet);
returnpieData;
}
}
效果图如下:
主要是一些基本属性和API的调用,具体每个API都有什么样的效果和作用,只能靠自己去尝试。后面还会陆陆续续为大家介绍MPAndroidChart其他类型的图表。