1 2 package com.github.mikephil.charting.data; 3 4 import android.util.Log; 5 6 import com.github.mikephil.charting.highlight.Highlight; 7 import com.github.mikephil.charting.interfaces.datasets.IPieDataSet; 8 9 import java.util.ArrayList; 10 import java.util.List; 11 12 /** 13 * A PieData object can only represent one DataSet. Unlike all other charts, the 14 * legend labels of the PieChart are created from the x-values array, and not 15 * from the DataSet labels. Each PieData object can only represent one 16 * PieDataSet (multiple PieDataSets inside a single PieChart are not possible). 17 * 18 * @author Philipp Jahoda 19 */ 20 public class PieData extends ChartData<IPieDataSet> { 21 PieData()22 public PieData() { 23 super(); 24 } 25 PieData(IPieDataSet dataSet)26 public PieData(IPieDataSet dataSet) { 27 super(dataSet); 28 } 29 30 /** 31 * Sets the PieDataSet this data object should represent. 32 * 33 * @param dataSet 34 */ setDataSet(IPieDataSet dataSet)35 public void setDataSet(IPieDataSet dataSet) { 36 mDataSets.clear(); 37 mDataSets.add(dataSet); 38 notifyDataChanged(); 39 } 40 41 /** 42 * Returns the DataSet this PieData object represents. A PieData object can 43 * only contain one DataSet. 44 * 45 * @return 46 */ getDataSet()47 public IPieDataSet getDataSet() { 48 return mDataSets.get(0); 49 } 50 51 @Override getDataSets()52 public List<IPieDataSet> getDataSets() { 53 List<IPieDataSet> dataSets = super.getDataSets(); 54 55 if (dataSets.size() < 1) { 56 Log.e("MPAndroidChart", 57 "Found multiple data sets while pie chart only allows one"); 58 } 59 60 return dataSets; 61 } 62 63 /** 64 * The PieData object can only have one DataSet. Use getDataSet() method instead. 65 * 66 * @param index 67 * @return 68 */ 69 @Override getDataSetByIndex(int index)70 public IPieDataSet getDataSetByIndex(int index) { 71 return index == 0 ? getDataSet() : null; 72 } 73 74 @Override getDataSetByLabel(String label, boolean ignorecase)75 public IPieDataSet getDataSetByLabel(String label, boolean ignorecase) { 76 return ignorecase ? label.equalsIgnoreCase(mDataSets.get(0).getLabel()) ? mDataSets.get(0) 77 : null : label.equals(mDataSets.get(0).getLabel()) ? mDataSets.get(0) : null; 78 } 79 80 @Override getEntryForHighlight(Highlight highlight)81 public Entry getEntryForHighlight(Highlight highlight) { 82 return getDataSet().getEntryForIndex((int) highlight.getX()); 83 } 84 85 /** 86 * Returns the sum of all values in this PieData object. 87 * 88 * @return 89 */ getYValueSum()90 public float getYValueSum() { 91 92 float sum = 0; 93 94 for (int i = 0; i < getDataSet().getEntryCount(); i++) 95 sum += getDataSet().getEntryForIndex(i).getY(); 96 97 98 return sum; 99 } 100 } 101