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.mpandroidpiechart;
-
- 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);
- PieData mPieData = getPieData(4, 100);
- showChart(mChart, mPieData);
- }
-
- private void showChart(PieChart pieChart, PieData pieData) {
- pieChart.setHoleColorTransparent(true);
-
- pieChart.setHoleRadius(60f);
- pieChart.setTransparentCircleRadius(64f);
-
-
- pieChart.setDescription("测试饼状图");
-
-
- pieChart.setDrawCenterText(true);
-
- pieChart.setDrawHoleEnabled(true);
-
- pieChart.setRotationAngle(90);
-
-
-
-
-
- pieChart.setRotationEnabled(true);
-
-
- pieChart.setUsePercentValues(true);
-
-
-
-
-
-
-
-
-
- pieChart.setCenterText("Quarterly Revenue");
-
-
- pieChart.setData(pieData);
-
-
-
-
-
- Legend mLegend = pieChart.getLegend();
- mLegend.setPosition(LegendPosition.RIGHT_OF_CHART);
-
- mLegend.setXEntrySpace(7f);
- mLegend.setYEntrySpace(5f);
-
- pieChart.animateXY(1000, 1000);
-
- }
-
-
-
-
-
-
- private PieData getPieData(int count, float range) {
-
- ArrayList<String> xValues = new ArrayList<String>();
-
- for (int i = 0; i < count; i++) {
- xValues.add("Quarterly" + (i + 1));
- }
-
- ArrayList<Entry> yValues = new ArrayList<Entry>();
-
-
-
-
-
-
- float quarterly1 = 14;
- float quarterly2 = 14;
- float quarterly3 = 34;
- float quarterly4 = 38;
-
- yValues.add(new Entry(quarterly1, 0));
- yValues.add(new Entry(quarterly2, 1));
- yValues.add(new Entry(quarterly3, 2));
- yValues.add(new Entry(quarterly4, 3));
-
-
- PieDataSet pieDataSet = new PieDataSet(yValues, "Quarterly Revenue 2014");
- pieDataSet.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));
-
- pieDataSet.setColors(colors);
-
- DisplayMetrics metrics = getResources().getDisplayMetrics();
- float px = 5 * (metrics.densityDpi / 160f);
- pieDataSet.setSelectionShift(px);
-
- PieData pieData = new PieData(xValues, pieDataSet);
-
- return pieData;
- }
- }
效果图如下:
主要是一些基本属性和API的调用,具体每个API都有什么样的效果和作用,只能靠自己去尝试。后面还会陆陆续续为大家介绍MPAndroidChart其他类型的图表。