MPAndroidChart餅圖屬性及相關設置


公司最近在做統計功能,所以用到了餅圖,在網上查了一些資料最終決定使用MPAndroidChart,使用起來非常方便,還有一些問題通過各種查找,終於解決...廢話不多說,先看下效果圖:

 

 

 

 

 

 

 

 

布局文件:

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <com.github.mikephil.charting.charts.PieChart  
  2.                android:id="@+id/chart"  
  3.                android:layout_width="match_parent"  
  4.                android:layout_height="match_parent" />  

 

java代碼:

1、初始化餅圖

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1.  private void initChart(){  
  2.   
  3.         mChart = (PieChart) findViewById(R.id.chart);  
  4.         mChart.setUsePercentValues(true);  
  5.         mChart.setDescription("");  
  6.         mChart.setExtraOffsets(5, 10, 5, 5);  
  7. //        mChart.setDrawSliceText(false);//設置隱藏餅圖上文字,只顯示百分比  
  8.         mChart.setDrawHoleEnabled(true);  
  9.         mChart.setHoleColorTransparent(true);  
  10.   
  11.         mChart.setTransparentCircleColor(getResources().getColor(R.color.buttombar_bg));  
  12.         mChart.setTransparentCircleAlpha(110);  
  13.         mChart.setOnChartValueSelectedListener(this);  
  14.         mChart.setHoleRadius(45f); //半徑  
  15.         //mChart.setHoleRadius(0)  //實心圓  
  16.         mChart.setTransparentCircleRadius(48f);// 半透明圈  
  17.         mChart.setDrawCenterText(true);//餅狀圖中間可以添加文字  
  18.         // 如果沒有數據的時候,會顯示這個,類似ListView的EmptyView  
  19.         mChart.setNoDataText(getResources().getString(R.string.no_data));  
  20.         mChart.setUsePercentValues(true);//設置顯示成比例  
  21.         SimpleDateFormat format = new SimpleDateFormat("yyyy");  
  22.         String year = format.format(since_at*1000);  
  23.         mChart.setCenterText(generateCenterSpannableText(year));  
  24.         mChart.setRotationAngle(0); // 初始旋轉角度  
  25.         // enable rotation of the chart by touch  
  26.         mChart.setRotationEnabled(true); // 可以手動旋轉  
  27.         mChart.setHighlightPerTapEnabled(true);  
  28.         mChart.animateY(1000, Easing.EasingOption.EaseInOutQuad); //設置動畫  
  29.         Legend mLegend = mChart.getLegend();  //設置比例圖  
  30.         mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);  //左下邊顯示  
  31.         mLegend.setFormSize(12f);//比例塊字體大小  
  32.         mLegend.setXEntrySpace(2f);//設置距離餅圖的距離,防止與餅圖重合  
  33.         mLegend.setYEntrySpace(2f);  
  34.         //設置比例塊換行...  
  35.         mLegend.setWordWrapEnabled(true);  
  36.         mLegend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);  
  37.   
  38.         mLegend.setTextColor(getResources().getColor(R.color.alpha_80));  
  39.         mLegend.setForm(Legend.LegendForm.SQUARE);//設置比例塊形狀,默認為方塊  
  40. //        mLegend.setEnabled(false);//設置禁用比例塊  
  41.     }  

 

 

2、設置餅圖數據

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1.  /** 
  2.      * 設置餅圖的數據 
  3.      * @param names 餅圖上顯示的比例名稱 
  4.      * @param counts 百分比 
  5.      */  
  6.     private void setData(ArrayList<String> names,ArrayList<Entry> counts) {  
  7.         PieDataSet dataSet = new PieDataSet(counts, "");  
  8.         dataSet.setSliceSpace(2f);  
  9.         dataSet.setSelectionShift(5f);  
  10.   
  11.         ArrayList<Integer> colors = new ArrayList<Integer>();  
  12.         for (int c : ColorTemplate.JOYFUL_COLORS)  
  13.             colors.add(c);  
  14. //  
  15.         for (int c : ColorTemplate.COLORFUL_COLORS)  
  16.             colors.add(c);  
  17.   
  18.         for (int c : ColorTemplate.LIBERTY_COLORS)  
  19.             colors.add(c);  
  20.   
  21. //        for (int c : ColorTemplate.PASTEL_COLORS)  
  22. //            colors.add(c);  
  23.         colors.add(ColorTemplate.getHoloBlue());  
  24. //        colors.add(getResources().getColor(R.color.stastic_team));  
  25.         dataSet.setColors(colors);  
  26.         //dataSet.setSelectionShift(0f);  
  27.         PieData data = new PieData(names, dataSet);  
  28.         data.setValueFormatter(new PercentFormatter());  
  29.         data.setValueTextSize(12f);  
  30.         data.setValueTextColor(getResources().getColor(R.color.whrite));  
  31.         mChart.setData(data);  
  32.         // undo all highlights  
  33.         mChart.highlightValues(null);  
  34.   
  35.         mChart.invalidate();  
  36.     }  


 

 

在這個過程中遇到幾個問題:

1、底部的比例塊顯示只有一行,如果數據較多的話,會超出屏幕外邊,所以需要換行顯示比例塊,設置如下:

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. //設置比例塊換行...  
  2.   mLegend.setWordWrapEnabled(true);  
[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. 同時需要把比例塊放到下邊或者放到上邊  <span style="font-family: Arial, Helvetica, sans-serif;"> mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);  //左下邊顯示</span>  
2、餅圖上怎么只顯示百分比,查了好多資料沒有看有人提到這個,最后在stackoverflow上找到答案。

 

      http://stackoverflow.com/questions/31154706/mpandroidchart-piechart-remove-percents

pieChart.setDrawSliceText(false)

3、如果比例塊放在左邊或者右邊,如果字體太長,會和餅圖疊加在一起,做以下處理可以防止疊加。

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. mLegend.setXEntrySpace(2f);//設置距離餅圖的距離,防止與餅圖重合  
  2. mLegend.setYEntrySpace(2f);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM