MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,应用起来非常灵活。MPAndroidChart同样拥有常用的图表类型:线型图、饼图、柱状图和散点图。
GitHub地址:
https://github.com/PhilJay/MPAndroidChart
下面主要实现以下饼状图:
1.从上面的地址中下载最新mpandroidchartlibrary-2-0-8.jar包, 然后copy到项目的libs中
2. 定义xml文件
3. 主要Java逻辑代码如下,注释已经都添加上了。
package com.jackie.mpandroidchart;
import java.util.ArrayList;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
public class MainActivity extends ActionBarActivity {
private PieChart mChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChart = (PieChart) findViewById(R.id.spread_pie_chart);
showChart();
}
private void showChart() {
mChart.setHoleColorTransparent(true);
mChart.setHoleRadius(60f); //半径
mChart.setTransparentCircleRadius(64f); // 半透明圈
mChart.setDescription("测试饼状图");
// mChart.setDrawYValues(true);
mChart.setDrawCenterText(true); //饼状图中间可以添加文字
mChart.setDrawHoleEnabled(true);
mChart.setRotationAngle(90); // 初始旋转角度
// draws the corresponding description value into the slice
// mChart.setDrawXValues(true);
// enable rotation of the chart by touch
mChart.setRotationEnabled(true); // 可以手动旋转
// display percentage values
mChart.setUsePercentValues(true); //显示成百分比
// mChart.setUnit(" €");
// mChart.setDrawUnitsInChart(true);
// add a selection listener
// mChart.setOnChartValueSelectedListener(this);
// mChart.setTouchEnabled(false);
// mChart.setOnAnimationListener(this);
mChart.setCenterText("Quarterly Revenue"); //饼状图中间的文字
//为饼状图填充数据
setData(4, 100);
mChart.animateXY(1000, 1000); //设置动画
// mChart.spin(2000, 0, 360);
Legend mLegend = mChart.getLegend(); //设置比例图
mLegend.setPosition(LegendPosition.RIGHT_OF_CHART); //最右边显示
// mLegend.setForm(LegendForm.LINE); //设置比例图的形状,默认是方形
mLegend.setXEntrySpace(7f);
mLegend.setYEntrySpace(5f);
}
/**
*
* @param count 分成几部分
* @param range
*/
private void setData(int count, float range) {
ArrayList<Entry> yVals1 = new ArrayList<Entry>(); //yVals用来表示封装每个饼块的实际数据
// 饼图数据
/**
* 将一个饼形图分成四部分, 四部分的数值比例为14:14:34:38
* 所以 14代表的百分比就是14%
*/
float quarterley1 = 14;
float quarterley2 = 14;
float quarterley3 = 34;
float quarterley4 = 38;
yVals1.add(new Entry(quarterley1, 0));
yVals1.add(new Entry(quarterley2, 1));
yVals1.add(new Entry(quarterley3, 2));
yVals1.add(new Entry(quarterley4, 3));
ArrayList<String> xVals = new ArrayList<String>(); //xVals用来表示每个饼块上的内容
for (int i = 1; i <= count; i++)
xVals.add("Quarterly" + i); //饼块上显示成Quarterly1, Quarterly2, Quarterly3, Quarterly4
PieDataSet mPieDataSet = new PieDataSet(yVals1, "Quarterly Revenue 2014"/*显示在比例图上*/);
mPieDataSet.setSliceSpace(0f); //设置个饼状图之间的距离
ArrayList<Integer> colors = new ArrayList<Integer>();
// 饼图颜色
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));
mPieDataSet.setColors(colors);
DisplayMetrics metrics = getResources().getDisplayMetrics();
float px = 5 * (metrics.densityDpi / 160f);
mPieDataSet.setSelectionShift(px); // 选中态多出的长度
PieData mPieData = new PieData(xVals, mPieDataSet);
mChart.setData(mPieData);
// undo all highlights
mChart.highlightValues(null);
mChart.invalidate();
}
}
效果图如下:
原文地址:http://blog.csdn.net/shineflowers/article/details/44701645