-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDrawTooltipGeneralDataActivity.kt
87 lines (83 loc) · 3.65 KB
/
DrawTooltipGeneralDataActivity.kt
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
package com.liihuu.kline
import android.graphics.*
import android.os.Bundle
import com.liihuu.kline.utils.DimensionUtils
import com.liihuu.kline.utils.MeasureUtils
import com.liihuu.kline.utils.formatDate
import com.liihuu.kline.utils.formatDecimal
import com.liihuu.klinechart.component.Tooltip
import com.liihuu.klinechart.model.KLineModel
import kotlinx.android.synthetic.main.kline_layout.*
import kotlin.math.max
/**
* @Author lihu hu_li888@foxmail.com
* @Date 2019-09-28-15:30
*/
class DrawTooltipGeneralDataActivity : BasicKLineActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val textSize = DimensionUtils.dpToPx(this, 10f)
val dp5ToPx = DimensionUtils.dpToPx(this, 5f)
val dp3ToPx = DimensionUtils.dpToPx(this, 3f)
k_line_chart.tooltip.drawGeneralDataListener = object: Tooltip.DrawGeneralDataListener {
override fun draw(
canvas: Canvas,
paint: Paint,
point: PointF,
tooltip: Tooltip,
chartRect: RectF,
kLineModel: KLineModel
) {
if (point.y > chartRect.bottom) {
return
}
val labels = resources.getStringArray(R.array.tooltip_general_data_labels).toMutableList()
val values = mutableListOf(
kLineModel.timestamp.formatDate(),
kLineModel.openPrice.formatDecimal(),
kLineModel.closePrice.formatDecimal(),
kLineModel.highPrice.formatDecimal(),
kLineModel.lowPrice.formatDecimal(),
kLineModel.volume.formatDecimal(0)
)
paint.apply {
this.textSize = textSize
style = Paint.Style.FILL
color = Color.parseColor("#000000")
}
val textHeight = MeasureUtils.measureTextHeight(paint, "T")
val labelSize = labels.size
var labelValueMaxWidth = Float.NEGATIVE_INFINITY
for (i in 0 until labelSize) {
labelValueMaxWidth = max(labelValueMaxWidth, MeasureUtils.measureTextWidth(paint, "${labels[i]}${values[i]}"))
}
val rectWidth = labelValueMaxWidth + dp3ToPx * 2
val rectHeight = textHeight * labelSize + (labelSize + 1) * dp5ToPx
val startX = if (point.x + rectWidth > chartRect.right) {
point.x - rectWidth
} else {
point.x
}
val startY = if (point.y + rectHeight > chartRect.bottom) {
point.y - rectHeight
} else {
point.y
}
val rect = RectF(startX, startY, startX + rectWidth, startY + rectHeight)
canvas.drawRoundRect(rect, dp3ToPx, dp3ToPx, paint)
val paintTextAlign = paint.textAlign
val textStartX = startX + dp3ToPx
var textStartY = startY + dp5ToPx + textHeight
paint.color = Color.parseColor("#ffffff")
for (i in 0 until labelSize) {
paint.textAlign = Paint.Align.LEFT
canvas.drawText(labels[i], textStartX, textStartY, paint)
paint.textAlign = Paint.Align.RIGHT
canvas.drawText(values[i], startX + rectWidth - dp3ToPx, textStartY, paint)
textStartY += dp5ToPx + textHeight
}
paint.textAlign = paintTextAlign
}
}
}
}