-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathChartHighlighter.kt
More file actions
198 lines (165 loc) · 6.6 KB
/
ChartHighlighter.kt
File metadata and controls
198 lines (165 loc) · 6.6 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
package info.appdev.charting.highlight
import info.appdev.charting.components.YAxis.AxisDependency
import info.appdev.charting.data.ChartData
import info.appdev.charting.data.DataSet
import info.appdev.charting.data.Entry
import info.appdev.charting.interfaces.dataprovider.base.BarLineScatterCandleBubbleDataProvider
import info.appdev.charting.interfaces.datasets.IDataSet
import info.appdev.charting.utils.PointD
import kotlin.math.abs
import kotlin.math.hypot
open class ChartHighlighter<T : BarLineScatterCandleBubbleDataProvider<*>>(protected var provider: T) : IHighlighter {
/**
* buffer for storing previously highlighted values
*/
protected var highlightBuffer: MutableList<Highlight> = ArrayList()
override fun getHighlight(x: Float, y: Float): Highlight? {
val pos = getValsForTouch(x, y)
val xVal = pos.x.toFloat()
PointD.recycleInstance(pos)
val high = getHighlightForX(xVal, x, y)
return high
}
/**
* Returns a recyclable PointD instance.
* Returns the corresponding xPos for a given touch-position in pixels.
*/
protected fun getValsForTouch(x: Float, y: Float): PointD {
// take any transformer to determine the x-axis value
return provider.getTransformer(AxisDependency.LEFT)!!.getValuesByTouchPoint(x, y)
}
/**
* Returns the corresponding Highlight for a given xVal and x- and y-touch position in pixels.
*/
protected fun getHighlightForX(xVal: Float, x: Float, y: Float): Highlight? {
val closestValues = getHighlightsAtXValue(xVal, x, y)
if (closestValues!!.isEmpty()) {
return null
}
val leftAxisMinDist = getMinimumDistance(closestValues, y, AxisDependency.LEFT)
val rightAxisMinDist = getMinimumDistance(closestValues, y, AxisDependency.RIGHT)
val axis = if (leftAxisMinDist < rightAxisMinDist) AxisDependency.LEFT else AxisDependency.RIGHT
return getClosestHighlightByPixel(closestValues, x, y, axis, provider.maxHighlightDistance)
}
/**
* Returns the minimum distance from a touch value (in pixels) to the
* closest value (in pixels) that is displayed in the chart.
*/
protected fun getMinimumDistance(closestValues: MutableList<Highlight>, pos: Float, axis: AxisDependency?): Float {
var distance = Float.MAX_VALUE
for (i in closestValues.indices) {
val high = closestValues[i]
if (high.axis == axis) {
val tempDistance = abs(getHighlightPos(high) - pos)
if (tempDistance < distance) {
distance = tempDistance
}
}
}
return distance
}
protected fun getHighlightPos(h: Highlight): Float {
return h.yPx
}
/**
* Returns a list of Highlight objects representing the entries closest to the given xVal.
* The returned list contains two objects per DataSet (closest rounding up, closest rounding down).
*
* @param xVal the transformed x-value of the x-touch position
* @param x touch position
* @param y touch position
*/
override fun getHighlightsAtXValue(xVal: Float, x: Float, y: Float): MutableList<Highlight>? {
highlightBuffer.clear()
data?.let { myData ->
var i = 0
val dataSetCount = myData.dataSetCount
while (i < dataSetCount) {
val dataSet = myData.getDataSetByIndex(i)
// don't include DataSets that cannot be highlighted
dataSet?.let {
if (!it.isHighlight) {
i++
continue
}
highlightBuffer.addAll(buildHighlights(it, i, xVal, DataSet.Rounding.CLOSEST))
}
i++
}
}
return highlightBuffer
}
/**
* An array of `Highlight` objects corresponding to the selected xValue and dataSetIndex.
*/
@Suppress("SameParameterValue")
protected open fun buildHighlights(
set: IDataSet<*>,
dataSetIndex: Int,
xVal: Float,
rounding: DataSet.Rounding?
): MutableList<Highlight> {
val highlights = ArrayList<Highlight>()
var entries = set.getEntriesForXValue(xVal)
if (entries != null && entries.isEmpty()) {
// Try to find closest x-value and take all entries for that x-value
val closest: Entry? = set.getEntryForXValue(xVal, Float.NaN, rounding)
if (closest != null) {
entries = set.getEntriesForXValue(closest.x)
}
}
if (entries != null && entries.isEmpty())
return highlights
if (entries != null)
for (e in entries) {
val pixels = provider.getTransformer(set.axisDependency)!!.getPixelForValues(e.x, e.y)
highlights.add(
Highlight(
x = e.x,
y = e.y,
xPx = pixels.x.toFloat(),
yPx = pixels.y.toFloat(),
dataSetIndex = dataSetIndex,
axis = set.axisDependency
)
)
}
return highlights
}
/**
* Returns the Highlight of the DataSet that contains the closest value on the
* y-axis.
*
* @param closestValues contains two Highlight objects per DataSet closest to the selected x-position (determined by
* rounding up an down)
* @param axis the closest axis
*/
fun getClosestHighlightByPixel(
closestValues: MutableList<Highlight>, x: Float, y: Float,
axis: AxisDependency?, minSelectionDistance: Float
): Highlight? {
var closest: Highlight? = null
var distance = minSelectionDistance
for (i in closestValues.indices) {
val high = closestValues[i]
if (axis == null || high.axis == axis) {
val cDistance = getDistance(x, y, high.xPx, high.yPx)
if (cDistance < distance) {
closest = high
distance = cDistance
}
}
}
return closest
}
/**
* Calculates the distance between the two given points.
*/
protected open fun getDistance(x1: Float, y1: Float, x2: Float, y2: Float): Float {
//return Math.abs(y1 - y2);
//return Math.abs(x1 - x2);
return hypot((x1 - x2).toDouble(), (y1 - y2).toDouble()).toFloat()
}
protected open val data: ChartData<*>?
get() = provider.data
}