Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class BarEntry : Entry {
/**
* Returns the ranges of the individual stack-entries. Will return null if this entry is not stacked.
*/
var ranges: Array<Range?> = arrayOfNulls(0)
var ranges: Array<Range> = arrayOf()
private set

/**
Expand Down Expand Up @@ -196,7 +196,7 @@ open class BarEntry : Entry {

if (values == null || values.isEmpty()) return

this.ranges = arrayOfNulls(values.size)
this.ranges = arrayOf()

var negRemain = -this.negativeSum
var posRemain = 0f
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.github.mikephil.charting.highlight

import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.github.mikephil.charting.utils.MPPointD
import kotlin.math.abs
import kotlin.math.max

open class BarHighlighter(barDataProvider: BarDataProvider?) : ChartHighlighter<BarDataProvider?>(barDataProvider) {
override fun getHighlight(x: Float, y: Float): Highlight? {
val high = super.getHighlight(x, y) ?: return null

val pos = getValsForTouch(x, y)

val barData = provider!!.barData

val set = barData.getDataSetByIndex(high.dataSetIndex)
if (set.isStacked()) {
return getStackedHighlight(
high,
set,
pos.x.toFloat(),
pos.y.toFloat()
)
}

MPPointD.recycleInstance(pos)

return high
}

/**
* This method creates the Highlight object that also indicates which value of a stacked BarEntry has been
* selected.
*
* @param high the Highlight to work with looking for stacked values
*/
fun getStackedHighlight(high: Highlight, set: IBarDataSet, xVal: Float, yVal: Float): Highlight? {
set.getEntryForXValue(xVal, yVal)?.let { entry ->
// not stacked
if (entry.yVals == null) {
return high
} else {
val ranges: Array<Range> = entry.ranges

if (ranges.isNotEmpty()) {
val stackIndex = getClosestStackIndex(ranges, yVal)

val pixels = provider!!.getTransformer(set.axisDependency)!!.getPixelForValues(high.x, ranges[stackIndex].to)

val stackedHigh = Highlight(
entry.x,
entry.y,
pixels.x.toFloat(),
pixels.y.toFloat(),
high.dataSetIndex,
stackIndex,
high.axis
)

MPPointD.recycleInstance(pixels)

return stackedHigh
}
}
}
return null
}

/**
* Returns the index of the closest value inside the values array / ranges (stacked barchart) to the value given as a parameter.
*/
protected fun getClosestStackIndex(ranges: Array<Range>?, value: Float): Int {
if (ranges == null || ranges.isEmpty())
return 0

var stackIndex = 0

for (range in ranges) {
if (range.contains(value)) return stackIndex
else stackIndex++
}

val length = max(ranges.size - 1, 0)

return if (value > ranges[length].to) length else 0
}

override fun getDistance(x1: Float, y1: Float, x2: Float, y2: Float): Float {
return abs(x1 - x2)
}

}
Loading
Loading