MPAndroidChart开源图表库之饼状图

来源:互联网 发布:如何判断存在sql注入 编辑:程序博客网 时间:2024/04/26 21:40

MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,应用起来非常灵活。MPAndroidChart同样拥有常用的图表类型:线型图、饼图、柱状图和散点图。

GitHub地址:

https://github.com/PhilJay/MPAndroidChart

下面主要实现以下饼状图:

1.从上面的地址中下载最新mpandroidchartlibrary-2-0-8.jar包, 然后copy到项目的libs中

2. 定义xml文件


3.  主要Java逻辑代码如下,注释已经都添加上了。

[java] view plain copy

 在CODE上查看代码片派生到我的代码片


  1. package com.jackie.mpandroidpiechart;    
  2.     
  3. import java.util.ArrayList;    
  4.     
  5. import com.github.mikephil.charting.charts.PieChart;    
  6. import com.github.mikephil.charting.components.Legend;    
  7. import com.github.mikephil.charting.components.Legend.LegendPosition;    
  8. import com.github.mikephil.charting.data.Entry;    
  9. import com.github.mikephil.charting.data.PieData;    
  10. import com.github.mikephil.charting.data.PieDataSet;    
  11.     
  12. import android.support.v7.app.ActionBarActivity;    
  13. import android.graphics.Color;    
  14. import android.os.Bundle;    
  15. import android.util.DisplayMetrics;    
  16.     
  17. public class MainActivity extends ActionBarActivity {    
  18.     
  19.     private PieChart mChart;    
  20.     
  21.     @Override    
  22.     protected void onCreate(Bundle savedInstanceState) {    
  23.         super.onCreate(savedInstanceState);    
  24.         setContentView(R.layout.activity_main);    
  25.             
  26.         mChart = (PieChart) findViewById(R.id.spread_pie_chart);    
  27.         PieData mPieData = getPieData(4100);    
  28.         showChart(mChart, mPieData);    
  29.     }    
  30.     
  31.     private void showChart(PieChart pieChart, PieData pieData) {    
  32.         pieChart.setHoleColorTransparent(true);    
  33.     
  34.         pieChart.setHoleRadius(60f);  //半径    
  35.         pieChart.setTransparentCircleRadius(64f); // 半透明圈    
  36.         //pieChart.setHoleRadius(0)  //实心圆    
  37.     
  38.         pieChart.setDescription(”测试饼状图”);    
  39.     
  40.         // mChart.setDrawYValues(true);    
  41.         pieChart.setDrawCenterText(true);  //饼状图中间可以添加文字    
  42.     
  43.         pieChart.setDrawHoleEnabled(true);    
  44.     
  45.         pieChart.setRotationAngle(90); // 初始旋转角度    
  46.     
  47.         // draws the corresponding description value into the slice    
  48.         // mChart.setDrawXValues(true);    
  49.     
  50.         // enable rotation of the chart by touch    
  51.         pieChart.setRotationEnabled(true); // 可以手动旋转    
  52.     
  53.         // display percentage values    
  54.         pieChart.setUsePercentValues(true);  //显示成百分比    
  55.         // mChart.setUnit(“ €”);    
  56.         // mChart.setDrawUnitsInChart(true);    
  57.     
  58.         // add a selection listener    
  59. //      mChart.setOnChartValueSelectedListener(this);    
  60.         // mChart.setTouchEnabled(false);    
  61.     
  62. //      mChart.setOnAnimationListener(this);    
  63.     
  64.         pieChart.setCenterText(”Quarterly Revenue”);  //饼状图中间的文字    
  65.     
  66.         //设置数据    
  67.         pieChart.setData(pieData);     
  68.             
  69.         // undo all highlights    
  70. //      pieChart.highlightValues(null);    
  71. //      pieChart.invalidate();    
  72.     
  73.         Legend mLegend = pieChart.getLegend();  //设置比例图    
  74.         mLegend.setPosition(LegendPosition.RIGHT_OF_CHART);  //最右边显示    
  75. //      mLegend.setForm(LegendForm.LINE);  //设置比例图的形状,默认是方形    
  76.         mLegend.setXEntrySpace(7f);    
  77.         mLegend.setYEntrySpace(5f);    
  78.             
  79.         pieChart.animateXY(10001000);  //设置动画    
  80.         // mChart.spin(2000, 0, 360);    
  81.     }    
  82.     
  83.     /**  
  84.      *   
  85.      * @param count 分成几部分  
  86.      * @param range  
  87.      */    
  88.     private PieData getPieData(int count, float range) {    
  89.             
  90.         ArrayList<String> xValues = new ArrayList<String>();  //xVals用来表示每个饼块上的内容    
  91.     
  92.         for (int i = 0; i < count; i++) {    
  93.             xValues.add(”Quarterly” + (i + 1));  //饼块上显示成Quarterly1, Quarterly2, Quarterly3, Quarterly4    
  94.         }    
  95.     
  96.         ArrayList<Entry> yValues = new ArrayList<Entry>();  //yVals用来表示封装每个饼块的实际数据    
  97.     
  98.         // 饼图数据    
  99.         /**  
  100.          * 将一个饼形图分成四部分, 四部分的数值比例为14:14:34:38  
  101.          * 所以 14代表的百分比就是14%   
  102.          */    
  103.         float quarterly1 = 14;    
  104.         float quarterly2 = 14;    
  105.         float quarterly3 = 34;    
  106.         float quarterly4 = 38;    
  107.     
  108.         yValues.add(new Entry(quarterly1, 0));    
  109.         yValues.add(new Entry(quarterly2, 1));    
  110.         yValues.add(new Entry(quarterly3, 2));    
  111.         yValues.add(new Entry(quarterly4, 3));    
  112.     
  113.         //y轴的集合    
  114.         PieDataSet pieDataSet = new PieDataSet(yValues, “Quarterly Revenue 2014”/*显示在比例图上*/);    
  115.         pieDataSet.setSliceSpace(0f); //设置个饼状图之间的距离    
  116.     
  117.         ArrayList<Integer> colors = new ArrayList<Integer>();    
  118.     
  119.         // 饼图颜色    
  120.         colors.add(Color.rgb(205205205));    
  121.         colors.add(Color.rgb(114188223));    
  122.         colors.add(Color.rgb(255123124));    
  123.         colors.add(Color.rgb(57135200));    
  124.     
  125.         pieDataSet.setColors(colors);    
  126.     
  127.         DisplayMetrics metrics = getResources().getDisplayMetrics();    
  128.         float px = 5 * (metrics.densityDpi / 160f);    
  129.         pieDataSet.setSelectionShift(px); // 选中态多出的长度    
  130.     
  131.         PieData pieData = new PieData(xValues, pieDataSet);    
  132.             
  133.         return pieData;    
  134.     }    
  135. }   
效果图如下:


原创粉丝点击