forked from PhilJay/MPAndroidChart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarChart.java
More file actions
368 lines (296 loc) · 10.2 KB
/
BarChart.java
File metadata and controls
368 lines (296 loc) · 10.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package com.github.mikephil.charting.charts;
import android.content.Context;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.interfaces.BarDataProvider;
import com.github.mikephil.charting.renderer.BarChartRenderer;
import com.github.mikephil.charting.renderer.XAxisRendererBarChart;
import com.github.mikephil.charting.utils.Highlight;
/**
* Chart that draws bars.
*
* @author Philipp Jahoda
*/
public class BarChart extends BarLineChartBase<BarData> implements BarDataProvider {
/** flag that enables or disables the highlighting arrow */
private boolean mDrawHighlightArrow = false;
/**
* if set to true, all values are drawn above their bars, instead of below
* their top
*/
private boolean mDrawValueAboveBar = true;
/**
* if set to true, all values of a stack are drawn individually, and not
* just their sum
*/
private boolean mDrawValuesForWholeStack = true;
/**
* if set to true, a grey area is drawn behind each bar that indicates the
* maximum value
*/
private boolean mDrawBarShadow = false;
public BarChart(Context context) {
super(context);
}
public BarChart(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BarChart(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void init() {
super.init();
mRenderer = new BarChartRenderer(this, mAnimator, mViewPortHandler);
mXAxisRenderer = new XAxisRendererBarChart(mViewPortHandler, mXAxis, mLeftAxisTransformer,
this);
mXChartMin = -0.5f;
}
@Override
protected void calcMinMax() {
super.calcMinMax();
// increase deltax by 1 because the bars have a width of 1
mDeltaX += 0.5f;
// extend xDelta to make space for multiple datasets (if ther are one)
mDeltaX *= mData.getDataSetCount();
int maxEntry = 0;
for (int i = 0; i < mData.getDataSetCount(); i++) {
DataSet<? extends Entry> set = mData.getDataSetByIndex(i);
if (maxEntry < set.getEntryCount())
maxEntry = set.getEntryCount();
}
float groupSpace = mData.getGroupSpace();
mDeltaX += maxEntry * groupSpace;
mXChartMax = mDeltaX - mXChartMin;
}
/**
* Returns the Highlight object (contains x-index and DataSet index) of the
* selected value at the given touch point inside the BarChart.
*
* @param x
* @param y
* @return
*/
@Override
public Highlight getHighlightByTouchPoint(float x, float y) {
if (mDataNotSet || mData == null) {
Log.e(LOG_TAG, "Can't select by touch. No data set.");
return null;
}
// create an array of the touch-point
float[] pts = new float[2];
pts[0] = x;
pts[1] = y;
mLeftAxisTransformer.pixelsToValue(pts);
if (pts[0] < mXChartMin || pts[0] > mXChartMax)
return null;
return getHighlight(pts[0], pts[1]);
}
/**
* Returns the correct Highlight object (including xIndex and dataSet-index)
* for the specified touch position.
*
* @param xPosition
* @return
*/
protected Highlight getHighlight(double xPosition, double yPosition) {
int setCount = mData.getDataSetCount();
int valCount = mData.getXValCount();
int dataSetIndex = 0;
int xIndex = 0;
// only one dataset exists
if (!mData.isGrouped()) {
xIndex = (int) Math.round(xPosition);
// check bounds
if (xIndex < 0) {
xIndex = 0;
} else if (xIndex >= valCount) {
xIndex = valCount - 1;
}
// if this bardata is grouped into more datasets
} else {
// calculate how often the group-space appears
int steps = (int) ((float) xPosition / ((float) setCount + mData.getGroupSpace()));
float groupSpaceSum = mData.getGroupSpace() * (float) steps;
float baseNoSpace = (float) xPosition - groupSpaceSum;
if (mLogEnabled)
Log.i(LOG_TAG, "base: " + xPosition + ", steps: " + steps + ", groupSpaceSum: "
+ groupSpaceSum
+ ", baseNoSpace: " + baseNoSpace);
dataSetIndex = (int) baseNoSpace % setCount;
xIndex = (int) baseNoSpace / setCount;
if (mLogEnabled)
Log.i(LOG_TAG, "xIndex: " + xIndex + ", dataSet: " + dataSetIndex);
// check bounds
if (xIndex < 0) {
xIndex = 0;
dataSetIndex = 0;
} else if (xIndex >= valCount) {
xIndex = valCount - 1;
dataSetIndex = setCount - 1;
}
// check bounds
if (dataSetIndex < 0)
dataSetIndex = 0;
else if (dataSetIndex >= setCount)
dataSetIndex = setCount - 1;
}
if (!mData.getDataSetByIndex(dataSetIndex).isStacked())
return new Highlight(xIndex, dataSetIndex);
else
return getStackedHighlight(xIndex, dataSetIndex, yPosition);
}
/**
* This method creates the Highlight object that also indicates which value
* of a stacked BarEntry has been selected.
*
* @param xIndex
* @param dataSet
* @param yValue
* @return
*/
protected Highlight getStackedHighlight(int xIndex, int dataSet, double yValue) {
BarEntry entry = mData.getDataSetByIndex(dataSet).getEntryForXIndex(xIndex);
if (entry != null) {
int stackIndex = entry.getClosestIndexAbove((float) yValue);
Highlight h = new Highlight(xIndex, dataSet, stackIndex);
return h;
} else
return null;
}
/**
* Returns the bounding box of the specified Entry in the specified DataSet.
* Returns null if the Entry could not be found in the charts data.
*
* @param e
* @return
*/
public RectF getBarBounds(BarEntry e) {
BarDataSet set = mData.getDataSetForEntry(e);
if (set == null)
return null;
float barspace = set.getBarSpace();
float y = e.getVal();
float x = e.getXIndex();
float barWidth = 0.5f;
float spaceHalf = barspace / 2f;
float left = x - barWidth + spaceHalf;
float right = x + barWidth - spaceHalf;
float top = y >= 0 ? y : 0;
float bottom = y <= 0 ? y : 0;
RectF bounds = new RectF(left, top, right, bottom);
getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);
return bounds;
}
/**
* set this to true to draw the highlightning arrow
*
* @param enabled
*/
public void setDrawHighlightArrow(boolean enabled) {
mDrawHighlightArrow = enabled;
}
/**
* returns true if drawing the highlighting arrow is enabled, false if not
*
* @return
*/
public boolean isDrawHighlightArrowEnabled() {
return mDrawHighlightArrow;
}
/**
* If set to true, all values are drawn above their bars, instead of below
* their top.
*
* @param enabled
*/
public void setDrawValueAboveBar(boolean enabled) {
mDrawValueAboveBar = enabled;
}
/**
* returns true if drawing values above bars is enabled, false if not
*
* @return
*/
public boolean isDrawValueAboveBarEnabled() {
return mDrawValueAboveBar;
}
/**
* if set to true, all values of a stack are drawn individually, and not
* just their sum
*
* @param enabled
*/
public void setDrawValuesForWholeStack(boolean enabled) {
mDrawValuesForWholeStack = enabled;
}
/**
* returns true if all values of a stack are drawn, and not just their sum
*
* @return
*/
public boolean isDrawValuesForWholeStackEnabled() {
return mDrawValuesForWholeStack;
}
/**
* If set to true, a grey area is drawn behind each bar that indicates the
* maximum value. Enabling his will reduce performance by about 50%.
*
* @param enabled
*/
public void setDrawBarShadow(boolean enabled) {
mDrawBarShadow = enabled;
}
/**
* returns true if drawing shadows (maxvalue) for each bar is enabled, false
* if not
*
* @return
*/
public boolean isDrawBarShadowEnabled() {
return mDrawBarShadow;
}
@Override
public BarData getBarData() {
return mData;
}
/**
* Returns the lowest x-index (value on the x-axis) that is still visible on
* the chart.
*
* @return
*/
@Override
public int getLowestVisibleXIndex() {
float step = mData.getDataSetCount();
float div = (step <= 1) ? 1 : step + mData.getGroupSpace();
float[] pts = new float[] {
mViewPortHandler.contentLeft(), mViewPortHandler.contentBottom()
};
getTransformer(AxisDependency.LEFT).pixelsToValue(pts);
return (int) ((pts[0] <= getXChartMin()) ? 0 : (pts[0] / div) + 1);
}
/**
* Returns the highest x-index (value on the x-axis) that is still visible
* on the chart.
*
* @return
*/
@Override
public int getHighestVisibleXIndex() {
float step = mData.getDataSetCount();
float div = (step <= 1) ? 1 : step + mData.getGroupSpace();
float[] pts = new float[] {
mViewPortHandler.contentRight(), mViewPortHandler.contentBottom()
};
getTransformer(AxisDependency.LEFT).pixelsToValue(pts);
return (int) ((pts[0] >= getXChartMax()) ? getXChartMax() / div : (pts[0] / div));
}
}