forked from PhilJay/MPAndroidChart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineChartRenderer.java
More file actions
567 lines (403 loc) · 18.8 KB
/
LineChartRenderer.java
File metadata and controls
567 lines (403 loc) · 18.8 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
package com.github.mikephil.charting.renderer;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.buffer.CircleBuffer;
import com.github.mikephil.charting.buffer.LineBuffer;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.LineDataProvider;
import com.github.mikephil.charting.utils.Highlight;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.ViewPortHandler;
import java.util.List;
public class LineChartRenderer extends DataRenderer {
protected LineDataProvider mChart;
/** paint for the inner circle of the value indicators */
protected Paint mCirclePaintInner;
/**
* Bitmap object used for drawing the paths (otherwise they are too long if
* rendered directly on the canvas)
*/
protected Bitmap mDrawBitmap;
/**
* on this canvas, the paths are rendered, it is initialized with the
* pathBitmap
*/
protected Canvas mBitmapCanvas;
protected Path cubicPath = new Path();
protected Path cubicFillPath = new Path();
protected LineBuffer[] mLineBuffers;
protected CircleBuffer[] mCircleBuffers;
public LineChartRenderer(LineDataProvider chart, ChartAnimator animator,
ViewPortHandler viewPortHandler) {
super(animator, viewPortHandler);
mChart = chart;
mCirclePaintInner = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaintInner.setStyle(Paint.Style.FILL);
mCirclePaintInner.setColor(Color.WHITE);
}
@Override
public void initBuffers() {
LineData lineData = mChart.getLineData();
mLineBuffers = new LineBuffer[lineData.getDataSetCount()];
mCircleBuffers = new CircleBuffer[lineData.getDataSetCount()];
for (int i = 0; i < mLineBuffers.length; i++) {
LineDataSet set = lineData.getDataSetByIndex(i);
mLineBuffers[i] = new LineBuffer(set.getEntryCount() * 4 - 4);
mCircleBuffers[i] = new CircleBuffer(set.getEntryCount() * 2);
}
}
@Override
public void drawData(Canvas c) {
int width = (int) mViewPortHandler.getChartWidth();
int height = (int) mViewPortHandler.getChartHeight();
if (mDrawBitmap == null
|| (mDrawBitmap.getWidth() != width)
|| (mDrawBitmap.getHeight() != height)) {
if (width > 0 && height > 0) {
mDrawBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
mBitmapCanvas = new Canvas(mDrawBitmap);
} else
return;
}
mDrawBitmap.eraseColor(Color.TRANSPARENT);
LineData lineData = mChart.getLineData();
for (LineDataSet set : lineData.getDataSets()) {
if (set.isVisible())
drawDataSet(c, set);
}
c.drawBitmap(mDrawBitmap, 0, 0, mRenderPaint);
}
protected void drawDataSet(Canvas c, LineDataSet dataSet) {
List<Entry> entries = dataSet.getYVals();
if (entries.size() < 1)
return;
mRenderPaint.setStrokeWidth(dataSet.getLineWidth());
mRenderPaint.setPathEffect(dataSet.getDashPathEffect());
// if drawing cubic lines is enabled
if (dataSet.isDrawCubicEnabled()) {
drawCubic(c, dataSet, entries);
// draw normal (straight) lines
} else {
drawLinear(c, dataSet, entries);
}
mRenderPaint.setPathEffect(null);
}
/**
* Draws a cubic line.
*
* @param c
* @param dataSet
* @param entries
*/
protected void drawCubic(Canvas c, LineDataSet dataSet, List<Entry> entries) {
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
Entry entryFrom = dataSet.getEntryForXIndex(mMinX);
Entry entryTo = dataSet.getEntryForXIndex(mMaxX);
int minx = Math.max(dataSet.getEntryPosition(entryFrom), 0);
int maxx = Math.min(dataSet.getEntryPosition(entryTo) + 1, entries.size());
float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();
float intensity = dataSet.getCubicIntensity();
cubicPath.reset();
int size = (int) Math.ceil((maxx - minx) * phaseX + minx);
if (size - minx >= 2) {
float prevDx = 0f;
float prevDy = 0f;
float curDx = 0f;
float curDy = 0f;
Entry prevPrev = entries.get(minx);
Entry prev = entries.get(minx);
Entry cur = entries.get(minx);
Entry next = entries.get(minx + 1);
// let the spline start
cubicPath.moveTo(cur.getXIndex(), cur.getVal() * phaseY);
prevDx = (cur.getXIndex() - prev.getXIndex()) * intensity;
prevDy = (cur.getVal() - prev.getVal()) * intensity;
curDx = (next.getXIndex() - cur.getXIndex()) * intensity;
curDy = (next.getVal() - cur.getVal()) * intensity;
// the first cubic
cubicPath.cubicTo(prev.getXIndex() + prevDx, (prev.getVal() + prevDy) * phaseY,
cur.getXIndex() - curDx,
(cur.getVal() - curDy) * phaseY, cur.getXIndex(), cur.getVal() * phaseY);
for (int j = minx + 1, count = Math.min(size, entries.size() - 1); j < count; j++) {
prevPrev = entries.get(j == 1 ? 0 : j - 2);
prev = entries.get(j - 1);
cur = entries.get(j);
next = entries.get(j + 1);
prevDx = (cur.getXIndex() - prevPrev.getXIndex()) * intensity;
prevDy = (cur.getVal() - prevPrev.getVal()) * intensity;
curDx = (next.getXIndex() - prev.getXIndex()) * intensity;
curDy = (next.getVal() - prev.getVal()) * intensity;
cubicPath.cubicTo(prev.getXIndex() + prevDx, (prev.getVal() + prevDy) * phaseY,
cur.getXIndex() - curDx,
(cur.getVal() - curDy) * phaseY, cur.getXIndex(), cur.getVal() * phaseY);
}
if (size > entries.size() - 1) {
prevPrev = entries.get((entries.size() >= 3) ? entries.size() - 3
: entries.size() - 2);
prev = entries.get(entries.size() - 2);
cur = entries.get(entries.size() - 1);
next = cur;
prevDx = (cur.getXIndex() - prevPrev.getXIndex()) * intensity;
prevDy = (cur.getVal() - prevPrev.getVal()) * intensity;
curDx = (next.getXIndex() - prev.getXIndex()) * intensity;
curDy = (next.getVal() - prev.getVal()) * intensity;
// the last cubic
cubicPath.cubicTo(prev.getXIndex() + prevDx, (prev.getVal() + prevDy) * phaseY,
cur.getXIndex() - curDx,
(cur.getVal() - curDy) * phaseY, cur.getXIndex(), cur.getVal() * phaseY);
}
}
// if filled is enabled, close the path
if (dataSet.isDrawFilledEnabled()) {
cubicFillPath.reset();
cubicFillPath.addPath(cubicPath);
// create a new path, this is bad for performance
drawCubicFill(dataSet, cubicFillPath, trans,
entryFrom.getXIndex(), entryFrom.getXIndex() + size);
}
mRenderPaint.setColor(dataSet.getColor());
mRenderPaint.setStyle(Paint.Style.STROKE);
trans.pathValueToPixel(cubicPath);
mBitmapCanvas.drawPath(cubicPath, mRenderPaint);
mRenderPaint.setPathEffect(null);
}
protected void drawCubicFill(LineDataSet dataSet, Path spline, Transformer trans,
int from, int to) {
float fillMin = mChart.getFillFormatter()
.getFillLinePosition(dataSet, mChart.getLineData(), mChart.getYChartMax(),
mChart.getYChartMin());
spline.lineTo(to - 1, fillMin);
spline.lineTo(from, fillMin);
spline.close();
mRenderPaint.setStyle(Paint.Style.FILL);
mRenderPaint.setColor(dataSet.getFillColor());
// filled is drawn with less alpha
mRenderPaint.setAlpha(dataSet.getFillAlpha());
trans.pathValueToPixel(spline);
mBitmapCanvas.drawPath(spline, mRenderPaint);
mRenderPaint.setAlpha(255);
}
/**
* Draws a normal line.
*
* @param c
* @param dataSet
* @param entries
*/
protected void drawLinear(Canvas c, LineDataSet dataSet, List<Entry> entries) {
int dataSetIndex = mChart.getLineData().getIndexOfDataSet(dataSet);
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();
mRenderPaint.setStyle(Paint.Style.STROKE);
Canvas canvas = null;
// if the data-set is dashed, draw on bitmap-canvas
if (dataSet.isDashedLineEnabled()) {
canvas = mBitmapCanvas;
} else {
canvas = c;
}
Entry entryFrom = dataSet.getEntryForXIndex(mMinX);
Entry entryTo = dataSet.getEntryForXIndex(mMaxX);
int minx = Math.max(dataSet.getEntryPosition(entryFrom), 0);
int maxx = Math.min(dataSet.getEntryPosition(entryTo) + 1, entries.size());
int range = (maxx - minx) * 4 - 4;
LineBuffer buffer = mLineBuffers[dataSetIndex];
buffer.setPhases(phaseX, phaseY);
buffer.limitFrom(minx);
buffer.limitTo(maxx);
buffer.feed(entries);
trans.pointValuesToPixel(buffer.buffer);
// more than 1 color
if (dataSet.getColors().size() > 1) {
for (int j = 0; j < range; j += 4) {
if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
break;
// make sure the lines don't do shitty things outside
// bounds
if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2])
|| (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 1]) && !mViewPortHandler
.isInBoundsBottom(buffer.buffer[j + 3]))
|| (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 1]) && !mViewPortHandler
.isInBoundsBottom(buffer.buffer[j + 3])))
continue;
// get the color that is set for this line-segment
mRenderPaint.setColor(dataSet.getColor(j / 4 + minx));
canvas.drawLine(buffer.buffer[j], buffer.buffer[j + 1],
buffer.buffer[j + 2], buffer.buffer[j + 3], mRenderPaint);
}
} else { // only one color per dataset
mRenderPaint.setColor(dataSet.getColor());
// c.drawLines(buffer.buffer, mRenderPaint);
canvas.drawLines(buffer.buffer, 0, range,
mRenderPaint);
}
mRenderPaint.setPathEffect(null);
// if drawing filled is enabled
if (dataSet.isDrawFilledEnabled() && entries.size() > 0) {
drawLinearFill(c, dataSet, entries, minx, maxx, trans);
}
}
protected void drawLinearFill(Canvas c, LineDataSet dataSet, List<Entry> entries, int minx,
int maxx,
Transformer trans) {
mRenderPaint.setStyle(Paint.Style.FILL);
mRenderPaint.setColor(dataSet.getFillColor());
// filled is drawn with less alpha
mRenderPaint.setAlpha(dataSet.getFillAlpha());
Path filled = generateFilledPath(
entries,
mChart.getFillFormatter().getFillLinePosition(dataSet, mChart.getLineData(),
mChart.getYChartMax(), mChart.getYChartMin()), minx, maxx);
trans.pathValueToPixel(filled);
c.drawPath(filled, mRenderPaint);
// restore alpha
mRenderPaint.setAlpha(255);
}
/**
* Generates the path that is used for filled drawing.
*
* @param entries
* @return
*/
private Path generateFilledPath(List<Entry> entries, float fillMin, int from, int to) {
float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();
Path filled = new Path();
filled.moveTo(entries.get(from).getXIndex(), fillMin);
filled.lineTo(entries.get(from).getXIndex(), entries.get(from).getVal() * phaseY);
// create a new path
for (int x = from + 1, count = (int) Math.ceil((to - from) * phaseX + from); x < count; x++) {
Entry e = entries.get(x);
filled.lineTo(e.getXIndex(), e.getVal() * phaseY);
}
// close up
filled.lineTo(
entries.get(
Math.max(
Math.min((int) Math.ceil((to - from) * phaseX + from) - 1,
entries.size() - 1), 0)).getXIndex(), fillMin);
filled.close();
return filled;
}
@Override
public void drawValues(Canvas c) {
if (mChart.getLineData().getYValCount() < mChart.getMaxVisibleCount()
* mViewPortHandler.getScaleX()) {
List<LineDataSet> dataSets = mChart.getLineData().getDataSets();
for (int i = 0; i < dataSets.size(); i++) {
LineDataSet dataSet = dataSets.get(i);
if (!dataSet.isDrawValuesEnabled())
continue;
// apply the text-styling defined by the DataSet
applyValueTextStyle(dataSet);
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
// make sure the values do not interfear with the circles
int valOffset = (int) (dataSet.getCircleSize() * 1.75f);
if (!dataSet.isDrawCirclesEnabled())
valOffset = valOffset / 2;
List<Entry> entries = dataSet.getYVals();
Entry entryFrom = dataSet.getEntryForXIndex(mMinX);
Entry entryTo = dataSet.getEntryForXIndex(mMaxX);
int minx = Math.max(dataSet.getEntryPosition(entryFrom), 0);
int maxx = Math.min(dataSet.getEntryPosition(entryTo) + 1, entries.size());
float[] positions = trans.generateTransformedValuesLine(
entries, mAnimator.getPhaseX(), mAnimator.getPhaseY(), minx, maxx);
for (int j = 0; j < positions.length; j += 2) {
float x = positions[j];
float y = positions[j + 1];
if (!mViewPortHandler.isInBoundsRight(x))
break;
if (!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y))
continue;
float val = entries.get(j / 2 + minx).getVal();
c.drawText(dataSet.getValueFormatter().getFormattedValue(val), x,
y - valOffset,
mValuePaint);
}
}
}
}
@Override
public void drawExtras(Canvas c) {
drawCircles(c);
}
protected void drawCircles(Canvas c) {
mRenderPaint.setStyle(Paint.Style.FILL);
float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();
List<LineDataSet> dataSets = mChart.getLineData().getDataSets();
for (int i = 0; i < dataSets.size(); i++) {
LineDataSet dataSet = dataSets.get(i);
if (!dataSet.isVisible() || !dataSet.isDrawCirclesEnabled())
continue;
mCirclePaintInner.setColor(dataSet.getCircleHoleColor());
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
List<Entry> entries = dataSet.getYVals();
Entry entryFrom = dataSet.getEntryForXIndex((mMinX < 0) ? 0 : mMinX);
Entry entryTo = dataSet.getEntryForXIndex(mMaxX);
int minx = Math.max(dataSet.getEntryPosition(entryFrom), 0);
int maxx = Math.min(dataSet.getEntryPosition(entryTo) + 1, entries.size());
CircleBuffer buffer = mCircleBuffers[i];
buffer.setPhases(phaseX, phaseY);
buffer.limitFrom(minx);
buffer.limitTo(maxx);
buffer.feed(entries);
trans.pointValuesToPixel(buffer.buffer);
float halfsize = dataSet.getCircleSize() / 2f;
for (int j = 0, count = (int) Math.ceil((maxx - minx) * phaseX + minx) * 2; j < count; j += 2) {
float x = buffer.buffer[j];
float y = buffer.buffer[j + 1];
if (!mViewPortHandler.isInBoundsRight(x))
break;
// make sure the circles don't do shitty things outside
// bounds
if (!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y))
continue;
int circleColor = dataSet.getCircleColor(j / 2 + minx);
mRenderPaint.setColor(circleColor);
c.drawCircle(x, y, dataSet.getCircleSize(),
mRenderPaint);
if (dataSet.isDrawCircleHoleEnabled()
&& circleColor != mCirclePaintInner.getColor())
c.drawCircle(x, y,
halfsize,
mCirclePaintInner);
}
}
}
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
for (int i = 0; i < indices.length; i++) {
LineDataSet set = mChart.getLineData().getDataSetByIndex(indices[i]
.getDataSetIndex());
if (set == null || !set.isHighlightEnabled())
continue;
mHighlightPaint.setColor(set.getHighLightColor());
int xIndex = indices[i].getXIndex(); // get the
// x-position
if (xIndex > mChart.getXChartMax() * mAnimator.getPhaseX())
continue;
float yValue = set.getYValForXIndex(xIndex);
if (yValue == Float.NaN)
continue;
float y = yValue * mAnimator.getPhaseY(); // get
// the
// y-position
float[] pts = new float[] {
xIndex, mChart.getYChartMax(), xIndex, mChart.getYChartMin(), mChart.getXChartMin(), y,
mChart.getXChartMax(), y
};
mChart.getTransformer(set.getAxisDependency()).pointValuesToPixel(pts);
// draw the highlight lines
c.drawLines(pts, mHighlightPaint);
}
}
}