forked from PhilJay/MPAndroidChart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHorizontalBarChartRenderer.java
More file actions
301 lines (218 loc) · 11.5 KB
/
HorizontalBarChartRenderer.java
File metadata and controls
301 lines (218 loc) · 11.5 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
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
import android.graphics.Paint.Align;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.buffer.BarBuffer;
import com.github.mikephil.charting.buffer.HorizontalBarBuffer;
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.interfaces.BarDataProvider;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;
import java.util.List;
/**
* Renderer for the HorizontalBarChart.
*
* @author Philipp Jahoda
*/
public class HorizontalBarChartRenderer extends BarChartRenderer {
private float mYOffset = 0f;
public HorizontalBarChartRenderer(BarDataProvider chart, ChartAnimator animator,
ViewPortHandler viewPortHandler) {
super(chart, animator, viewPortHandler);
mValuePaint.setTextAlign(Align.LEFT);
}
@Override
public void initBuffers() {
BarData barData = mChart.getBarData();
mBarBuffers = new HorizontalBarBuffer[barData.getDataSetCount()];
for (int i = 0; i < mBarBuffers.length; i++) {
BarDataSet set = barData.getDataSetByIndex(i);
mBarBuffers[i] = new HorizontalBarBuffer(set.getValueCount() * 4 * set.getStackSize(),
barData.getGroupSpace(),
barData.getDataSetCount(), set.isStacked());
}
}
protected void drawDataSet(Canvas c, BarDataSet dataSet, int index) {
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
mShadowPaint.setColor(dataSet.getBarShadowColor());
float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();
List<BarEntry> entries = dataSet.getYVals();
// initialize the buffer
BarBuffer buffer = mBarBuffers[index];
buffer.setPhases(phaseX, phaseY);
buffer.setBarSpace(dataSet.getBarSpace());
buffer.setDataSet(index);
buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
buffer.feed(entries);
trans.pointValuesToPixel(buffer.buffer);
for (int j = 0; j < buffer.size(); j += 4) {
if (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 3]))
break;
if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1]))
continue;
if (mChart.isDrawBarShadowEnabled()) {
c.drawRect(mViewPortHandler.contentLeft(), buffer.buffer[j + 1],
mViewPortHandler.contentRight(),
buffer.buffer[j + 3], mShadowPaint);
}
// Set the color for the currently drawn value. If the index
// is
// out of bounds, reuse colors.
mRenderPaint.setColor(dataSet.getColor(j / 4));
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
buffer.buffer[j + 3], mRenderPaint);
}
}
@Override
public void drawValues(Canvas c) {
// if values are drawn
if (passesCheck()) {
List<BarDataSet> dataSets = mChart.getBarData().getDataSets();
final float valueOffsetPlus = Utils.convertDpToPixel(5f);
float posOffset = 0f;
float negOffset = 0f;
boolean drawValueAboveBar = mChart.isDrawValueAboveBarEnabled();
if (drawValueAboveBar)
mValuePaint.setTextAlign(Align.LEFT);
else
mValuePaint.setTextAlign(Align.RIGHT);
for (int i = 0; i < mChart.getBarData().getDataSetCount(); i++) {
BarDataSet dataSet = dataSets.get(i);
if (!dataSet.isDrawValuesEnabled())
continue;
boolean isInverted = mChart.isInverted(dataSet.getAxisDependency());
// apply the text-styling defined by the DataSet
applyValueTextStyle(dataSet);
mYOffset = Utils.calcTextHeight(mValuePaint, "10") / 2f;
ValueFormatter formatter = dataSet.getValueFormatter();
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
List<BarEntry> entries = dataSet.getYVals();
float[] valuePoints = getTransformedValues(trans, entries, i);
// if only single values are drawn (sum)
if (!mChart.isDrawValuesForWholeStackEnabled()) {
for (int j = 0; j < valuePoints.length * mAnimator.getPhaseX(); j += 2) {
if (!mViewPortHandler.isInBoundsX(valuePoints[j]))
continue;
if (!mViewPortHandler.isInBoundsTop(valuePoints[j + 1]))
break;
if (!mViewPortHandler.isInBoundsBottom(valuePoints[j + 1]))
continue;
float val = entries.get(j / 2).getVal();
String valueText = formatter.getFormattedValue(val);
// calculate the correct offset depending on the draw position of the value
float valueTextWidth = Utils.calcTextWidth(mValuePaint, valueText);
posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus));
negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus);
if (isInverted) {
posOffset = -posOffset - valueTextWidth;
negOffset = -negOffset - valueTextWidth;
}
drawValue(c, valueText, valuePoints[j] + (val >= 0 ? posOffset : negOffset),
valuePoints[j + 1]);
}
// if each value of a potential stack should be drawn
} else {
for (int j = 0; j < (valuePoints.length - 1) * mAnimator.getPhaseX(); j += 2) {
BarEntry e = entries.get(j / 2);
float[] vals = e.getVals();
// we still draw stacked bars, but there is one
// non-stacked
// in between
if (vals == null) {
if (!mViewPortHandler.isInBoundsX(valuePoints[j]))
continue;
if (!mViewPortHandler.isInBoundsTop(valuePoints[j + 1]))
break;
if (!mViewPortHandler.isInBoundsBottom(valuePoints[j + 1]))
continue;
float val = e.getVal();
String valueText = formatter.getFormattedValue(val);
// calculate the correct offset depending on the draw position of the value
float valueTextWidth = Utils.calcTextWidth(mValuePaint, valueText);
posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus));
negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus);
if (isInverted) {
posOffset = -posOffset - valueTextWidth;
negOffset = -negOffset - valueTextWidth;
}
drawValue(c, valueText, valuePoints[j]
+ (e.getVal() >= 0 ? posOffset : negOffset),
valuePoints[j + 1]);
} else {
float[] transformed = new float[vals.length * 2];
int cnt = 0;
float allPos = e.getPositiveSum();
float allNeg = e.getNegativeSum();
for (int k = 0; k < transformed.length; k += 2) {
float value = vals[cnt];
float y = 0f;
if(value >= 0f) {
allPos -= value;
y = value + allPos;
} else {
allNeg -= Math.abs(value);
y = value + allNeg;
}
transformed[k] = y * mAnimator.getPhaseY();
cnt++;
}
trans.pointValuesToPixel(transformed);
for (int k = 0; k < transformed.length; k += 2) {
float val = vals[k / 2];
String valueText = formatter.getFormattedValue(val);
// calculate the correct offset depending on the draw position of the value
float valueTextWidth = Utils.calcTextWidth(mValuePaint, valueText);
posOffset = (drawValueAboveBar ? valueOffsetPlus : -(valueTextWidth + valueOffsetPlus));
negOffset = (drawValueAboveBar ? -(valueTextWidth + valueOffsetPlus) : valueOffsetPlus);
if (isInverted) {
posOffset = -posOffset - valueTextWidth;
negOffset = -negOffset - valueTextWidth;
}
float x = transformed[k]
+ (val >= 0 ? posOffset : negOffset);
float y = valuePoints[j + 1];
if (!mViewPortHandler.isInBoundsX(x))
continue;
if (!mViewPortHandler.isInBoundsTop(y))
break;
if (!mViewPortHandler.isInBoundsBottom(y))
continue;
drawValue(c, valueText, x, y);
}
}
}
}
}
}
}
@Override
protected void prepareBarHighlight(float x, float y, float barspaceHalf, float from,
Transformer trans) {
float top = x - 0.5f + barspaceHalf;
float bottom = x + 0.5f - barspaceHalf;
float left = y >= from ? y : from;
float right = y <= from ? y : from;
mBarRect.set(left, top, right, bottom);
trans.rectValueToPixelHorizontal(mBarRect, mAnimator.getPhaseY());
}
@Override
public float[] getTransformedValues(Transformer trans, List<BarEntry> entries,
int dataSetIndex) {
return trans.generateTransformedValuesHorizontalBarChart(entries, dataSetIndex,
mChart.getBarData(), mAnimator.getPhaseY());
}
@Override
protected void drawValue(Canvas c, String value, float xPos, float yPos) {
super.drawValue(c, value, xPos, yPos + mYOffset);
}
@Override
protected boolean passesCheck() {
return mChart.getBarData().getYValCount() < mChart.getMaxVisibleCount()
* mViewPortHandler.getScaleY();
}
}