RadarChartRenderer.java
13.1 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;
public class RadarChartRenderer extends LineRadarRenderer {
protected RadarChart mChart;
/**
* paint for drawing the web
*/
protected Paint mWebPaint;
protected Paint mHighlightCirclePaint;
public RadarChartRenderer(RadarChart chart, ChartAnimator animator,
ViewPortHandler viewPortHandler) {
super(animator, viewPortHandler);
mChart = chart;
mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mHighlightPaint.setStyle(Paint.Style.STROKE);
mHighlightPaint.setStrokeWidth(2f);
mHighlightPaint.setColor(Color.rgb(255, 187, 115));
mWebPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mWebPaint.setStyle(Paint.Style.STROKE);
mHighlightCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
public Paint getWebPaint() {
return mWebPaint;
}
@Override
public void initBuffers() {
// TODO Auto-generated method stub
}
@Override
public void drawData(Canvas c) {
RadarData radarData = mChart.getData();
int mostEntries = radarData.getMaxEntryCountSet().getEntryCount();
for (IRadarDataSet set : radarData.getDataSets()) {
if (set.isVisible()) {
drawDataSet(c, set, mostEntries);
}
}
}
protected Path mDrawDataSetSurfacePathBuffer = new Path();
/**
* Draws the RadarDataSet
*
* @param c
* @param dataSet
* @param mostEntries the entry count of the dataset with the most entries
*/
protected void drawDataSet(Canvas c, IRadarDataSet dataSet, int mostEntries) {
float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();
float sliceangle = mChart.getSliceAngle();
// calculate the factor that is needed for transforming the value to
// pixels
float factor = mChart.getFactor();
MPPointF center = mChart.getCenterOffsets();
MPPointF pOut = MPPointF.getInstance(0,0);
Path surface = mDrawDataSetSurfacePathBuffer;
surface.reset();
boolean hasMovedToPoint = false;
for (int j = 0; j < dataSet.getEntryCount(); j++) {
mRenderPaint.setColor(dataSet.getColor(j));
RadarEntry e = dataSet.getEntryForIndex(j);
Utils.getPosition(
center,
(e.getY() - mChart.getYChartMin()) * factor * phaseY,
sliceangle * j * phaseX + mChart.getRotationAngle(), pOut);
if (Float.isNaN(pOut.x))
continue;
if (!hasMovedToPoint) {
surface.moveTo(pOut.x, pOut.y);
hasMovedToPoint = true;
} else
surface.lineTo(pOut.x, pOut.y);
}
if (dataSet.getEntryCount() > mostEntries) {
// if this is not the largest set, draw a line to the center before closing
surface.lineTo(center.x, center.y);
}
surface.close();
if (dataSet.isDrawFilledEnabled()) {
final Drawable drawable = dataSet.getFillDrawable();
if (drawable != null) {
drawFilledPath(c, surface, drawable);
} else {
drawFilledPath(c, surface, dataSet.getFillColor(), dataSet.getFillAlpha());
}
}
mRenderPaint.setStrokeWidth(dataSet.getLineWidth());
mRenderPaint.setStyle(Paint.Style.STROKE);
// draw the line (only if filled is disabled or alpha is below 255)
if (!dataSet.isDrawFilledEnabled() || dataSet.getFillAlpha() < 255)
c.drawPath(surface, mRenderPaint);
MPPointF.recycleInstance(center);
MPPointF.recycleInstance(pOut);
}
@Override
public void drawValues(Canvas c) {
float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();
float sliceangle = mChart.getSliceAngle();
// calculate the factor that is needed for transforming the value to
// pixels
float factor = mChart.getFactor();
MPPointF center = mChart.getCenterOffsets();
MPPointF pOut = MPPointF.getInstance(0,0);
MPPointF pIcon = MPPointF.getInstance(0,0);
float yoffset = Utils.convertDpToPixel(5f);
for (int i = 0; i < mChart.getData().getDataSetCount(); i++) {
IRadarDataSet dataSet = mChart.getData().getDataSetByIndex(i);
if (!shouldDrawValues(dataSet))
continue;
// apply the text-styling defined by the DataSet
applyValueTextStyle(dataSet);
MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
for (int j = 0; j < dataSet.getEntryCount(); j++) {
RadarEntry entry = dataSet.getEntryForIndex(j);
Utils.getPosition(
center,
(entry.getY() - mChart.getYChartMin()) * factor * phaseY,
sliceangle * j * phaseX + mChart.getRotationAngle(),
pOut);
if (dataSet.isDrawValuesEnabled()) {
drawValue(c,
dataSet.getValueFormatter(),
entry.getY(),
entry,
i,
pOut.x,
pOut.y - yoffset,
dataSet.getValueTextColor
(j));
}
if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
Drawable icon = entry.getIcon();
Utils.getPosition(
center,
(entry.getY()) * factor * phaseY + iconsOffset.y,
sliceangle * j * phaseX + mChart.getRotationAngle(),
pIcon);
//noinspection SuspiciousNameCombination
pIcon.y += iconsOffset.x;
Utils.drawImage(
c,
icon,
(int)pIcon.x,
(int)pIcon.y,
icon.getIntrinsicWidth(),
icon.getIntrinsicHeight());
}
}
MPPointF.recycleInstance(iconsOffset);
}
MPPointF.recycleInstance(center);
MPPointF.recycleInstance(pOut);
MPPointF.recycleInstance(pIcon);
}
@Override
public void drawExtras(Canvas c) {
drawWeb(c);
}
protected void drawWeb(Canvas c) {
float sliceangle = mChart.getSliceAngle();
// calculate the factor that is needed for transforming the value to
// pixels
float factor = mChart.getFactor();
float rotationangle = mChart.getRotationAngle();
MPPointF center = mChart.getCenterOffsets();
// draw the web lines that come from the center
mWebPaint.setStrokeWidth(mChart.getWebLineWidth());
mWebPaint.setColor(mChart.getWebColor());
mWebPaint.setAlpha(mChart.getWebAlpha());
final int xIncrements = 1 + mChart.getSkipWebLineCount();
int maxEntryCount = mChart.getData().getMaxEntryCountSet().getEntryCount();
MPPointF p = MPPointF.getInstance(0,0);
for (int i = 0; i < maxEntryCount; i += xIncrements) {
Utils.getPosition(
center,
mChart.getYRange() * factor,
sliceangle * i + rotationangle,
p);
c.drawLine(center.x, center.y, p.x, p.y, mWebPaint);
}
MPPointF.recycleInstance(p);
// draw the inner-web
mWebPaint.setStrokeWidth(mChart.getWebLineWidthInner());
mWebPaint.setColor(mChart.getWebColorInner());
mWebPaint.setAlpha(mChart.getWebAlpha());
int labelCount = mChart.getYAxis().mEntryCount;
MPPointF p1out = MPPointF.getInstance(0,0);
MPPointF p2out = MPPointF.getInstance(0,0);
for (int j = 0; j < labelCount; j++) {
for (int i = 0; i < mChart.getData().getEntryCount(); i++) {
float r = (mChart.getYAxis().mEntries[j] - mChart.getYChartMin()) * factor;
Utils.getPosition(center, r, sliceangle * i + rotationangle, p1out);
Utils.getPosition(center, r, sliceangle * (i + 1) + rotationangle, p2out);
c.drawLine(p1out.x, p1out.y, p2out.x, p2out.y, mWebPaint);
}
}
MPPointF.recycleInstance(p1out);
MPPointF.recycleInstance(p2out);
}
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
float sliceangle = mChart.getSliceAngle();
// calculate the factor that is needed for transforming the value to
// pixels
float factor = mChart.getFactor();
MPPointF center = mChart.getCenterOffsets();
MPPointF pOut = MPPointF.getInstance(0,0);
RadarData radarData = mChart.getData();
for (Highlight high : indices) {
IRadarDataSet set = radarData.getDataSetByIndex(high.getDataSetIndex());
if (set == null || !set.isHighlightEnabled())
continue;
RadarEntry e = set.getEntryForIndex((int) high.getX());
if (!isInBoundsX(e, set))
continue;
float y = (e.getY() - mChart.getYChartMin());
Utils.getPosition(center,
y * factor * mAnimator.getPhaseY(),
sliceangle * high.getX() * mAnimator.getPhaseX() + mChart.getRotationAngle(),
pOut);
high.setDraw(pOut.x, pOut.y);
// draw the lines
drawHighlightLines(c, pOut.x, pOut.y, set);
if (set.isDrawHighlightCircleEnabled()) {
if (!Float.isNaN(pOut.x) && !Float.isNaN(pOut.y)) {
int strokeColor = set.getHighlightCircleStrokeColor();
if (strokeColor == ColorTemplate.COLOR_NONE) {
strokeColor = set.getColor(0);
}
if (set.getHighlightCircleStrokeAlpha() < 255) {
strokeColor = ColorTemplate.colorWithAlpha(strokeColor, set.getHighlightCircleStrokeAlpha());
}
drawHighlightCircle(c,
pOut,
set.getHighlightCircleInnerRadius(),
set.getHighlightCircleOuterRadius(),
set.getHighlightCircleFillColor(),
strokeColor,
set.getHighlightCircleStrokeWidth());
}
}
}
MPPointF.recycleInstance(center);
MPPointF.recycleInstance(pOut);
}
protected Path mDrawHighlightCirclePathBuffer = new Path();
public void drawHighlightCircle(Canvas c,
MPPointF point,
float innerRadius,
float outerRadius,
int fillColor,
int strokeColor,
float strokeWidth) {
c.save();
outerRadius = Utils.convertDpToPixel(outerRadius);
innerRadius = Utils.convertDpToPixel(innerRadius);
if (fillColor != ColorTemplate.COLOR_NONE) {
Path p = mDrawHighlightCirclePathBuffer;
p.reset();
p.addCircle(point.x, point.y, outerRadius, Path.Direction.CW);
if (innerRadius > 0.f) {
p.addCircle(point.x, point.y, innerRadius, Path.Direction.CCW);
}
mHighlightCirclePaint.setColor(fillColor);
mHighlightCirclePaint.setStyle(Paint.Style.FILL);
c.drawPath(p, mHighlightCirclePaint);
}
if (strokeColor != ColorTemplate.COLOR_NONE) {
mHighlightCirclePaint.setColor(strokeColor);
mHighlightCirclePaint.setStyle(Paint.Style.STROKE);
mHighlightCirclePaint.setStrokeWidth(Utils.convertDpToPixel(strokeWidth));
c.drawCircle(point.x, point.y, outerRadius, mHighlightCirclePaint);
}
c.restore();
}
}