BarData.java
3.38 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.github.mikephil.charting.data;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import java.util.List;
/**
* Data object that represents all data for the BarChart.
*
* @author Philipp Jahoda
*/
public class BarData extends BarLineScatterCandleBubbleData<IBarDataSet> {
/**
* the width of the bars on the x-axis, in values (not pixels)
*/
private float mBarWidth = 0.85f;
public BarData() {
super();
}
public BarData(IBarDataSet... dataSets) {
super(dataSets);
}
public BarData(List<IBarDataSet> dataSets) {
super(dataSets);
}
/**
* Sets the width each bar should have on the x-axis (in values, not pixels).
* Default 0.85f
*
* @param mBarWidth
*/
public void setBarWidth(float mBarWidth) {
this.mBarWidth = mBarWidth;
}
public float getBarWidth() {
return mBarWidth;
}
/**
* Groups all BarDataSet objects this data object holds together by modifying the x-value of their entries.
* Previously set x-values of entries will be overwritten. Leaves space between bars and groups as specified
* by the parameters.
* Do not forget to call notifyDataSetChanged() on your BarChart object after calling this method.
*
* @param fromX the starting point on the x-axis where the grouping should begin
* @param groupSpace the space between groups of bars in values (not pixels) e.g. 0.8f for bar width 1f
* @param barSpace the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f
*/
public void groupBars(float fromX, float groupSpace, float barSpace) {
int setCount = mDataSets.size();
if (setCount <= 1) {
throw new RuntimeException("BarData needs to hold at least 2 BarDataSets to allow grouping.");
}
IBarDataSet max = getMaxEntryCountSet();
int maxEntryCount = max.getEntryCount();
float groupSpaceWidthHalf = groupSpace / 2f;
float barSpaceHalf = barSpace / 2f;
float barWidthHalf = mBarWidth / 2f;
float interval = getGroupWidth(groupSpace, barSpace);
for (int i = 0; i < maxEntryCount; i++) {
float start = fromX;
fromX += groupSpaceWidthHalf;
for (IBarDataSet set : mDataSets) {
fromX += barSpaceHalf;
fromX += barWidthHalf;
if (i < set.getEntryCount()) {
BarEntry entry = set.getEntryForIndex(i);
if (entry != null) {
entry.setX(fromX);
}
}
fromX += barWidthHalf;
fromX += barSpaceHalf;
}
fromX += groupSpaceWidthHalf;
float end = fromX;
float innerInterval = end - start;
float diff = interval - innerInterval;
// correct rounding errors
if (diff > 0 || diff < 0) {
fromX += diff;
}
}
notifyDataChanged();
}
/**
* In case of grouped bars, this method returns the space an individual group of bar needs on the x-axis.
*
* @param groupSpace
* @param barSpace
* @return
*/
public float getGroupWidth(float groupSpace, float barSpace) {
return mDataSets.size() * (mBarWidth + barSpace) + groupSpace;
}
}