-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathLegendRenderer.kt
More file actions
498 lines (414 loc) · 19.3 KB
/
LegendRenderer.kt
File metadata and controls
498 lines (414 loc) · 19.3 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
package com.github.mikephil.charting.renderer
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Paint.Align
import android.graphics.Path
import androidx.core.graphics.withSave
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.Legend.LegendDirection
import com.github.mikephil.charting.components.Legend.LegendForm
import com.github.mikephil.charting.components.Legend.LegendHorizontalAlignment
import com.github.mikephil.charting.components.Legend.LegendOrientation
import com.github.mikephil.charting.components.Legend.LegendVerticalAlignment
import com.github.mikephil.charting.components.LegendEntry
import com.github.mikephil.charting.data.ChartData
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet
import com.github.mikephil.charting.interfaces.datasets.IPieDataSet
import com.github.mikephil.charting.utils.ColorTemplate
import com.github.mikephil.charting.utils.Utils
import com.github.mikephil.charting.utils.ViewPortHandler
import java.util.Collections
import kotlin.math.min
@Suppress("MemberVisibilityCanBePrivate")
open class LegendRenderer(
viewPortHandler: ViewPortHandler,
protected var legend: Legend
) : Renderer(viewPortHandler) {
/**
* paint for the legend labels
*/
var labelPaint: Paint
protected set
var formPaint: Paint
protected set
protected var computedEntries: MutableList<LegendEntry> = ArrayList(16)
/**
* Prepares the legend and calculates all needed forms, labels and colors.
*/
fun computeLegend(data: ChartData<*>) {
if (!legend.isLegendCustom) {
computedEntries.clear()
// loop for building up the colors and labels used in the legend
for (i in 0..<data.dataSetCount) {
val dataSet = data.getDataSetByIndex(i) ?: continue
val clrs = dataSet.colors
val entryCount = dataSet.entryCount
// if we have a barchart with stacked bars
if (dataSet is IBarDataSet && dataSet.isStacked) {
val bds = dataSet
val sLabels = bds.stackLabels
val minEntries = min(clrs.size.toDouble(), bds.stackSize.toDouble()).toInt()
for (j in 0..<minEntries) {
val label: String?
if (sLabels.isNotEmpty()) {
val labelIndex = j % minEntries
label = if (labelIndex < sLabels.size) sLabels[labelIndex] else null
} else {
label = null
}
computedEntries.add(
LegendEntry(
label,
dataSet.getForm(),
dataSet.getFormSize(),
dataSet.getFormLineWidth(),
dataSet.getFormLineDashEffect(),
clrs[j]
)
)
}
if (bds.label != null) {
// add the legend description label
computedEntries.add(
LegendEntry(
dataSet.getLabel(),
LegendForm.NONE,
Float.NaN,
Float.NaN,
null,
ColorTemplate.COLOR_NONE
)
)
}
} else if (dataSet is IPieDataSet) {
val pds = dataSet
var j = 0
while (j < clrs.size && j < entryCount) {
computedEntries.add(
LegendEntry(
pds.getEntryForIndex(j).label,
dataSet.getForm(),
dataSet.getFormSize(),
dataSet.getFormLineWidth(),
dataSet.getFormLineDashEffect(),
clrs[j]
)
)
j++
}
if (pds.label != null) {
// add the legend description label
computedEntries.add(
LegendEntry(
dataSet.getLabel(),
LegendForm.NONE,
Float.NaN,
Float.NaN,
null,
ColorTemplate.COLOR_NONE
)
)
}
} else if (dataSet is ICandleDataSet && dataSet.decreasingColor !=
ColorTemplate.COLOR_NONE
) {
val decreasingColor = dataSet.decreasingColor
val increasingColor = dataSet.increasingColor
computedEntries.add(
LegendEntry(
null,
dataSet.getForm(),
dataSet.getFormSize(),
dataSet.getFormLineWidth(),
dataSet.getFormLineDashEffect(),
decreasingColor
)
)
computedEntries.add(
LegendEntry(
dataSet.getLabel(),
dataSet.getForm(),
dataSet.getFormSize(),
dataSet.getFormLineWidth(),
dataSet.getFormLineDashEffect(),
increasingColor
)
)
} else { // all others
var j = 0
while (j < clrs.size && j < entryCount) {
// if multiple colors are set for a DataSet, group them
val label = if (j < clrs.size - 1 && j < entryCount - 1) {
null
} else { // add label to the last entry
data.getDataSetByIndex(i).label
}
computedEntries.add(
LegendEntry(
label,
dataSet.form,
dataSet.formSize,
dataSet.formLineWidth,
dataSet.formLineDashEffect,
clrs[j]
)
)
j++
}
}
}
if (legend.extraEntries != null) {
Collections.addAll(computedEntries, *legend.extraEntries)
}
legend.setEntries(computedEntries)
}
val tf = legend.typeface
if (tf != null) labelPaint.typeface = tf
labelPaint.textSize = legend.textSize
labelPaint.color = legend.textColor
// calculate all dimensions of the mLegend
legend.calculateDimensions(labelPaint, viewPortHandler)
}
protected var legendFontMetrics: Paint.FontMetrics = Paint.FontMetrics()
fun renderLegend(canvas: Canvas) {
if (!legend.isEnabled) return
val tf = legend.typeface
if (tf != null) labelPaint.typeface = tf
labelPaint.textSize = legend.textSize
labelPaint.color = legend.textColor
val labelLineHeight = Utils.getLineHeight(labelPaint, legendFontMetrics)
val labelLineSpacing = (Utils.getLineSpacing(labelPaint, legendFontMetrics)
+ Utils.convertDpToPixel(legend.yEntrySpace))
val formYOffset = labelLineHeight - Utils.calcTextHeight(labelPaint, "ABC") / 2f
val entries = legend.entries
val formToTextSpace = Utils.convertDpToPixel(legend.formToTextSpace)
val xEntrySpace = Utils.convertDpToPixel(legend.xEntrySpace)
val orientation = legend.orientation
val horizontalAlignment = legend.horizontalAlignment
val verticalAlignment = legend.verticalAlignment
val direction = legend.direction
val defaultFormSize = Utils.convertDpToPixel(legend.formSize)
// space between the entries
val stackSpace = Utils.convertDpToPixel(legend.stackSpace)
val yOffset = legend.yOffset
val xOffset = legend.xOffset
var originPosX: Float
when (horizontalAlignment) {
LegendHorizontalAlignment.LEFT -> {
originPosX = if (orientation == LegendOrientation.VERTICAL) xOffset
else viewPortHandler.contentLeft() + xOffset
if (direction == LegendDirection.RIGHT_TO_LEFT) originPosX += legend.mNeededWidth
}
LegendHorizontalAlignment.RIGHT -> {
originPosX = if (orientation == LegendOrientation.VERTICAL) viewPortHandler.chartWidth - xOffset
else viewPortHandler.contentRight() - xOffset
if (direction == LegendDirection.LEFT_TO_RIGHT)
originPosX -= legend.mNeededWidth
}
LegendHorizontalAlignment.CENTER -> {
originPosX = if (orientation == LegendOrientation.VERTICAL)
viewPortHandler.chartWidth / 2f
else
(viewPortHandler.contentLeft() + viewPortHandler.contentWidth() / 2f)
originPosX += (if (direction == LegendDirection.LEFT_TO_RIGHT)
+xOffset
else
-xOffset)
// Horizontally layed out legends do the center offset on a line basis,
// So here we offset the vertical ones only.
if (orientation == LegendOrientation.VERTICAL) {
originPosX += (if (direction == LegendDirection.LEFT_TO_RIGHT)
-legend.mNeededWidth / 2.0 + xOffset
else
legend.mNeededWidth / 2.0 - xOffset).toFloat()
}
}
}
when (orientation) {
LegendOrientation.HORIZONTAL -> {
val calculatedLineSizes = legend.calculatedLineSizes
val calculatedLabelSizes = legend.calculatedLabelSizes
val calculatedLabelBreakPoints = legend.calculatedLabelBreakPoints
var posX = originPosX
var posY: Float = when (verticalAlignment) {
LegendVerticalAlignment.TOP -> yOffset
LegendVerticalAlignment.BOTTOM -> viewPortHandler.chartHeight - yOffset - legend.mNeededHeight
LegendVerticalAlignment.CENTER -> (viewPortHandler.chartHeight - legend.mNeededHeight) / 2f + yOffset
}
var lineIndex = 0
var i = 0
val count = entries.size
while (i < count) {
val e = entries[i]
val drawingForm = e.form != LegendForm.NONE
val formSize = if (java.lang.Float.isNaN(e.formSize)) defaultFormSize else Utils.convertDpToPixel(e.formSize)
if (i < calculatedLabelBreakPoints.size && calculatedLabelBreakPoints[i]) {
posX = originPosX
posY += labelLineHeight + labelLineSpacing
}
if (posX == originPosX && horizontalAlignment == LegendHorizontalAlignment.CENTER && lineIndex < calculatedLineSizes.size) {
posX += (if (direction == LegendDirection.RIGHT_TO_LEFT)
calculatedLineSizes[lineIndex].width
else
-calculatedLineSizes[lineIndex].width) / 2f
lineIndex++
}
val isStacked = e.label == null // grouped forms have null labels
if (drawingForm) {
if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= formSize
drawForm(canvas, posX, posY + formYOffset, e, legend)
if (direction == LegendDirection.LEFT_TO_RIGHT) posX += formSize
}
if (!isStacked) {
if (drawingForm) posX += if (direction == LegendDirection.RIGHT_TO_LEFT) -formToTextSpace else formToTextSpace
if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= calculatedLabelSizes[i].width
drawLabel(canvas, posX, posY + labelLineHeight, e.label)
if (direction == LegendDirection.LEFT_TO_RIGHT) posX += calculatedLabelSizes[i].width
posX += if (direction == LegendDirection.RIGHT_TO_LEFT) -xEntrySpace else xEntrySpace
} else posX += if (direction == LegendDirection.RIGHT_TO_LEFT) -stackSpace else stackSpace
i++
}
}
LegendOrientation.VERTICAL -> {
// contains the stacked legend size in pixels
var stack = 0f
var wasStacked = false
var posY: Float
when (verticalAlignment) {
LegendVerticalAlignment.TOP -> {
posY = (if (horizontalAlignment == LegendHorizontalAlignment.CENTER)
0f
else
viewPortHandler.contentTop())
posY += yOffset
}
LegendVerticalAlignment.BOTTOM -> {
posY = (if (horizontalAlignment == LegendHorizontalAlignment.CENTER)
viewPortHandler.chartHeight
else
viewPortHandler.contentBottom())
posY -= legend.mNeededHeight + yOffset
}
LegendVerticalAlignment.CENTER -> posY = (viewPortHandler.chartHeight / 2f
- legend.mNeededHeight / 2f
+ legend.yOffset)
}
var i = 0
while (i < entries.size) {
val e = entries[i]
val drawingForm = e.form != LegendForm.NONE
val formSize = if (java.lang.Float.isNaN(e.formSize)) defaultFormSize else Utils.convertDpToPixel(e.formSize)
var posX = originPosX
if (drawingForm) {
if (direction == LegendDirection.LEFT_TO_RIGHT) posX += stack
else posX -= formSize - stack
drawForm(canvas, posX, posY + formYOffset, e, legend)
if (direction == LegendDirection.LEFT_TO_RIGHT) posX += formSize
}
if (e.label != null) {
if (drawingForm && !wasStacked) posX += if (direction == LegendDirection.LEFT_TO_RIGHT)
formToTextSpace
else
-formToTextSpace
else if (wasStacked) posX = originPosX
if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= Utils.calcTextWidth(labelPaint, e.label).toFloat()
if (!wasStacked) {
drawLabel(canvas, posX, posY + labelLineHeight, e.label)
} else {
posY += labelLineHeight + labelLineSpacing
drawLabel(canvas, posX, posY + labelLineHeight, e.label)
}
// make a step down
posY += labelLineHeight + labelLineSpacing
stack = 0f
} else {
stack += formSize + stackSpace
wasStacked = true
}
i++
}
}
}
}
private val mLineFormPath = Path()
init {
labelPaint = Paint(Paint.ANTI_ALIAS_FLAG)
labelPaint.textSize = Utils.convertDpToPixel(9f)
labelPaint.textAlign = Align.LEFT
formPaint = Paint(Paint.ANTI_ALIAS_FLAG)
formPaint.style = Paint.Style.FILL
}
/**
* Draws the Legend-form at the given position with the color at the given
* index.
*
* @param canvas canvas to draw with
* @param x position
* @param y position
* @param entry the entry to render
* @param legend the legend context
*/
protected fun drawForm(
canvas: Canvas,
x: Float, y: Float,
entry: LegendEntry,
legend: Legend
) {
if (entry.formColor == ColorTemplate.COLOR_SKIP || entry.formColor == ColorTemplate.COLOR_NONE || entry.formColor == 0) return
canvas.withSave {
var form = entry.form
if (form == LegendForm.DEFAULT) form = legend.form
formPaint.color = entry.formColor
val formSize = Utils.convertDpToPixel(
if (java.lang.Float.isNaN(entry.formSize))
legend.formSize
else
entry.formSize
)
val half = formSize / 2f
when (form) {
LegendForm.NONE -> {}
LegendForm.EMPTY -> {}
LegendForm.DEFAULT, LegendForm.CIRCLE -> {
formPaint.style = Paint.Style.FILL
canvas.drawCircle(x + half, y, half, formPaint)
}
LegendForm.SQUARE -> {
formPaint.style = Paint.Style.FILL
canvas.drawRect(x, y - half, x + formSize, y + half, formPaint)
}
LegendForm.LINE -> {
val formLineWidth = Utils.convertDpToPixel(
if (java.lang.Float.isNaN(entry.formLineWidth))
legend.formLineWidth
else
entry.formLineWidth
)
val formLineDashEffect = if (entry.formLineDashEffect == null)
legend.formLineDashEffect
else
entry.formLineDashEffect
formPaint.style = Paint.Style.STROKE
formPaint.strokeWidth = formLineWidth
formPaint.pathEffect = formLineDashEffect
mLineFormPath.reset()
mLineFormPath.moveTo(x, y)
mLineFormPath.lineTo(x + formSize, y)
canvas.drawPath(mLineFormPath, formPaint)
}
}
}
}
/**
* Draws the provided label at the given position.
*
* @param canvas canvas to draw with
* @param x
* @param y
* @param label the label to draw
*/
protected fun drawLabel(canvas: Canvas, x: Float, y: Float, label: String) {
canvas.drawText(label, x, y, labelPaint)
}
}