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