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 @@ -80,20 +80,22 @@ open class BarChart : BarLineChartBase<BarData>, BarDataProvider {
}

protected override fun calcMinMax() {
if (mFitBars) {
mXAxis.calculate(mData.xMin - mData.barWidth / 2f, mData.xMax + mData.barWidth / 2f)
} else {
mXAxis.calculate(mData.xMin, mData.xMax)
}

// calculate axis range (min / max) according to provided data
mAxisLeft.calculate(mData.getYMin(YAxis.AxisDependency.LEFT), mData.getYMax(YAxis.AxisDependency.LEFT))
mAxisRight.calculate(
mData.getYMin(YAxis.AxisDependency.RIGHT), mData.getYMax(
YAxis.AxisDependency
.RIGHT
mData?.let { barData ->
if (mFitBars) {
mXAxis.calculate(barData.xMin - barData.barWidth / 2f, barData.xMax + barData.barWidth / 2f)
} else {
mXAxis.calculate(barData.xMin, barData.xMax)
}

// calculate axis range (min / max) according to provided data
mAxisLeft.calculate(barData.getYMin(YAxis.AxisDependency.LEFT), barData.getYMax(YAxis.AxisDependency.LEFT))
mAxisRight.calculate(
barData.getYMin(YAxis.AxisDependency.RIGHT), barData.getYMax(
YAxis.AxisDependency
.RIGHT
)
)
)
}
}

/**
Expand Down Expand Up @@ -134,26 +136,28 @@ open class BarChart : BarLineChartBase<BarData>, BarDataProvider {
* The rect will be assigned Float.MIN_VALUE in all locations if the Entry could not be found in the charts data.
*/
open fun getBarBounds(barEntry: BarEntry, outputRect: RectF) {
val set = mData.getDataSetForEntry(barEntry)
mData?.let { barData ->
val set = barData.getDataSetForEntry(barEntry)

if (set == null) {
outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE)
return
}
if (set == null) {
outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE)
return
}

val y = barEntry.y
val x = barEntry.x
val y = barEntry.y
val x = barEntry.x

val barWidth = mData.barWidth
val barWidth = barData.barWidth

val left = x - barWidth / 2f
val right = x + barWidth / 2f
val top = if (y >= 0) y else 0f
val bottom = if (y <= 0) y else 0f
val left = x - barWidth / 2f
val right = x + barWidth / 2f
val top = if (y >= 0) y else 0f
val bottom = if (y <= 0) y else 0f

outputRect.set(left, top, right, bottom)
outputRect.set(left, top, right, bottom)

getTransformer(set.axisDependency)!!.rectValueToPixel(outputRect)
getTransformer(set.axisDependency)!!.rectValueToPixel(outputRect)
}
}

/**
Expand Down Expand Up @@ -182,7 +186,7 @@ open class BarChart : BarLineChartBase<BarData>, BarDataProvider {
highlightValue(Highlight(x, dataSetIndex, stackIndex), false)
}

override val barData: BarData
override val barData: BarData?
get() = mData

/**
Expand All @@ -206,7 +210,7 @@ open class BarChart : BarLineChartBase<BarData>, BarDataProvider {
* @param barSpace the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f
*/
fun groupBars(fromX: Float, groupSpace: Float, barSpace: Float) {
barData.groupBars(fromX, groupSpace, barSpace)
barData?.groupBars(fromX, groupSpace, barSpace)
notifyDataSetChanged()
}

Expand All @@ -222,28 +226,29 @@ open class BarChart : BarLineChartBase<BarData>, BarDataProvider {
}

override fun getAccessibilityDescription(): String {
val barData = barData

val entryCount = barData.entryCount

// Find the min and max index
val yAxisValueFormatter = axisLeft.valueFormatter
val minVal = yAxisValueFormatter!!.getFormattedValue(barData.yMin, null)
val maxVal = yAxisValueFormatter.getFormattedValue(barData.yMax, null)

// Data range...
val xAxisValueFormatter = xAxis.valueFormatter
val minRange = xAxisValueFormatter!!.getFormattedValue(barData.xMin, null)
val maxRange = xAxisValueFormatter.getFormattedValue(barData.xMax, null)

val entries = if (entryCount == 1) "entry" else "entries"

// Format the values of min and max; to recite them back
return String.format(
Locale.getDefault(), "The bar chart has %d %s. " +
"The minimum value is %s and maximum value is %s." +
"Data ranges from %s to %s.",
entryCount, entries, minVal, maxVal, minRange, maxRange
)
barData?.let { barData ->
val entryCount = barData.entryCount

// Find the min and max index
val yAxisValueFormatter = axisLeft.valueFormatter
val minVal = yAxisValueFormatter!!.getFormattedValue(barData.yMin, null)
val maxVal = yAxisValueFormatter.getFormattedValue(barData.yMax, null)

// Data range...
val xAxisValueFormatter = xAxis.valueFormatter
val minRange = xAxisValueFormatter!!.getFormattedValue(barData.xMin, null)
val maxRange = xAxisValueFormatter.getFormattedValue(barData.xMax, null)

val entries = if (entryCount == 1) "entry" else "entries"

// Format the values of min and max; to recite them back
return String.format(
Locale.getDefault(), "The bar chart has %d %s. " +
"The minimum value is %s and maximum value is %s." +
"Data ranges from %s to %s.",
entryCount, entries, minVal, maxVal, minRange, maxRange
)
}
return ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
* Baseclass of all Chart-Views.
Expand All @@ -68,6 +69,7 @@ public abstract class Chart<T extends ChartData<? extends IDataSet<? extends Ent
* object that holds all data that was originally set for the chart, before
* it was modified or any filtering algorithms had been applied
*/
@Nullable
protected T mData = null;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ import kotlin.math.min
/**
* BarChart with horizontal bar orientation. In this implementation, x- and y-axis are switched, meaning the YAxis class
* represents the horizontal values and the XAxis class represents the vertical values.
*
* @author Philipp Jahoda
*/
class HorizontalBarChart : BarChart {
open class HorizontalBarChart : BarChart {
constructor(context: Context?) : super(context)

constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
Expand Down Expand Up @@ -203,22 +201,23 @@ class HorizontalBarChart : BarChart {
}

override fun getBarBounds(barEntry: BarEntry, outputRect: RectF) {
val bounds = outputRect
val set = mData.getDataSetForEntry(barEntry)
mData?.let { data ->
val set = data.getDataSetForEntry(barEntry)

val y = barEntry.y
val x = barEntry.x
val y = barEntry.y
val x = barEntry.x

val barWidth = mData.barWidth
val barWidth = data.barWidth

val top = x - barWidth / 2f
val bottom = x + barWidth / 2f
val left = if (y >= 0) y else 0f
val right = if (y <= 0) y else 0f
val top = x - barWidth / 2f
val bottom = x + barWidth / 2f
val left = if (y >= 0) y else 0f
val right = if (y <= 0) y else 0f

bounds.set(left, top, right, bottom)
outputRect.set(left, top, right, bottom)

getTransformer(set!!.axisDependency)!!.rectValueToPixel(bounds)
getTransformer(set!!.axisDependency)!!.rectValueToPixel(outputRect)
}
}

protected var mGetPositionBuffer: FloatArray = FloatArray(2)
Expand Down
Loading
Loading