Skip to content

Commit 7d81e54

Browse files
committed
DataSet more generic
1 parent 148f823 commit 7d81e54

File tree

5 files changed

+14
-31
lines changed

5 files changed

+14
-31
lines changed

chartLib/src/main/kotlin/info/appdev/charting/data/BarLineScatterCandleBubbleDataSet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import info.appdev.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataS
77
/**
88
* Baseclass of all DataSets for Bar-, Line-, Scatter- and CandleStickChart.
99
*/
10-
abstract class BarLineScatterCandleBubbleDataSet<T : EntryFloat>(yVals: MutableList<T>, label: String) :
10+
abstract class BarLineScatterCandleBubbleDataSet<T : BaseEntry<Float>>(yVals: MutableList<T>, label: String) :
1111
DataSet<T>(yVals, label), IBarLineScatterCandleBubbleDataSet<T> {
1212
/**
1313
* Sets the color that is used for drawing the highlight indicators.

chartLib/src/main/kotlin/info/appdev/charting/data/BaseDataSet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import info.appdev.charting.utils.convertDpToPixel
1919
* This is the base dataset of all DataSets. Its purpose is to implement critical methods
2020
* provided by the IDataSet interface.
2121
*/
22-
abstract class BaseDataSet<T : EntryFloat>() : IDataSet<T> {
22+
abstract class BaseDataSet<T : BaseEntry<Float>>() : IDataSet<T> {
2323
/**
2424
* List representing all colors that are used for this DataSet
2525
*/

chartLib/src/main/kotlin/info/appdev/charting/data/DataSet.kt

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlin.math.abs
1010
* groups of values inside the Chart (e.g. the values for a specific line in the
1111
* LineChart, or the values of a specific group of bars in the BarChart).
1212
*/
13-
abstract class DataSet<T : EntryFloat>(
13+
abstract class DataSet<T : BaseEntry<Float>>(
1414
protected var entriesInternal: MutableList<T>,
1515
label: String = ""
1616
) : BaseDataSet<T>(label), Serializable {
@@ -219,72 +219,55 @@ abstract class DataSet<T : EntryFloat>(
219219
while (low < high) {
220220
val m = low + (high - low) / 2
221221

222-
val currentEntryFloat: EntryFloat = entriesInternal[m]
222+
val current = entriesInternal[m]
223+
val next = entriesInternal[m + 1]
223224

224-
val nextEntryFloat: EntryFloat = entriesInternal[m + 1]
225-
226-
val d1 = currentEntryFloat.x - xValue
227-
val d2 = nextEntryFloat.x - xValue
225+
val d1 = current.x - xValue
226+
val d2 = next.x - xValue
228227
val ad1 = abs(d1)
229228
val ad2 = abs(d2)
230229

231230
if (ad2 < ad1) {
232-
// [m + 1] is closer to xValue
233-
// Search in a higher place
234231
low = m + 1
235232
} else if (ad1 < ad2) {
236-
// [m] is closer to xValue
237-
// Search in a lower place
238233
high = m
239234
} else {
240-
// We have multiple sequential x-value with same distance
241-
242235
if (d1 >= 0.0) {
243-
// Search in a lower place
244236
high = m
245237
} else if (d1 < 0.0) {
246-
// Search in a higher place
247238
low = m + 1
248239
}
249240
}
250241

251242
closest = high
252243
}
253244

254-
val closestEntryFloat: EntryFloat = entriesInternal[closest]
255-
val closestXValue = closestEntryFloat.x
245+
val closestEntry = entriesInternal[closest]
246+
val closestXValue = closestEntry.x
256247
if (rounding == Rounding.UP) {
257-
// If rounding up, and found x-value is lower than specified x, and we can go upper...
258248
if (closestXValue < xValue && closest < entriesInternal.size - 1) {
259249
++closest
260250
}
261251
} else if (rounding == Rounding.DOWN) {
262-
// If rounding down, and found x-value is upper than specified x, and we can go lower...
263252
if (closestXValue > xValue && closest > 0) {
264253
--closest
265254
}
266255
}
267256

268-
// Search by closest to y-value
269257
if (!closestToY.isNaN()) {
270258
while (closest > 0 && entriesInternal[closest - 1].x == closestXValue) {
271259
closest -= 1
272260
}
273261

274-
var closestYValue = closestEntryFloat.y
262+
var closestYValue = closestEntry.y
275263
var closestYIndex = closest
276264

277265
while (true) {
278266
closest += 1
279-
if (closest >= entriesInternal.size) {
280-
break
281-
}
267+
if (closest >= entriesInternal.size) break
282268

283269
val value: T = entriesInternal[closest]
284-
285-
if (value.x != closestXValue) {
286-
break
287-
}
270+
if (value.x != closestXValue) break
288271

289272
if (abs(value.y - closestToY) <= abs(closestYValue - closestToY)) {
290273
closestYValue = closestToY

chartLib/src/main/kotlin/info/appdev/charting/data/LineRadarDataSet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import info.appdev.charting.utils.convertDpToPixel
99
/**
1010
* Base dataset for line and radar DataSets.
1111
*/
12-
abstract class LineRadarDataSet<T : EntryFloat>(yVals: MutableList<T>, label: String) : LineScatterCandleRadarDataSet<T>(yVals, label), ILineRadarDataSet<T> {
12+
abstract class LineRadarDataSet<T : BaseEntry<Float>>(yVals: MutableList<T>, label: String) : LineScatterCandleRadarDataSet<T>(yVals, label), ILineRadarDataSet<T> {
1313
// TODO: Move to using `Fill` class
1414
/**
1515
* the color that is used for filling the line surface

chartLib/src/main/kotlin/info/appdev/charting/data/LineScatterCandleRadarDataSet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import android.graphics.DashPathEffect
44
import info.appdev.charting.interfaces.datasets.ILineScatterCandleRadarDataSet
55
import info.appdev.charting.utils.convertDpToPixel
66

7-
abstract class LineScatterCandleRadarDataSet<T : EntryFloat>(yVals: MutableList<T>, label: String) : BarLineScatterCandleBubbleDataSet<T>(yVals, label),
7+
abstract class LineScatterCandleRadarDataSet<T : BaseEntry<Float>>(yVals: MutableList<T>, label: String) : BarLineScatterCandleBubbleDataSet<T>(yVals, label),
88
ILineScatterCandleRadarDataSet<T> {
99
override var isVerticalHighlightIndicator: Boolean = true
1010
override var isHorizontalHighlightIndicator: Boolean = true

0 commit comments

Comments
 (0)