Skip to content

Commit

Permalink
Fix bullet graph exception when using more than 4 qualitative ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteckman committed Dec 29, 2024
1 parent 4193511 commit bb68f66
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Upgrade Kotlin to 2.1.0
- Android Gradle Plugin to 8.7.2
- Android compileSDK to 34 and minSDK to 23
- BulletGraph using default qualitative range indicators will accept a max of 5 values when being configured rather
than throwing a null pointer exception when Composing.

### Fixed

- Panning support for Int and Long linear axis models by introducing ContinuousLinearAxisModel and
DiscreteLinearAxisModel interfaces
- BulletGraph null pointer exception if more than 4 qualitative range indicators are used.
- BulletGraph qualitative range default shading not according to spec under some conditions.

## [0.7.1]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ private val DefaultRangeShades = listOf(
listOf(0.5f, 0.65f, 0.8f, 0.9f, 0.97f),
)

// Max number of qualitative ranges supported by the DefaultRangeShades above
private const val MaxQualitativeRanges = 5

private const val MinRangeShade = 0.99f

/**
Expand Down Expand Up @@ -174,6 +177,11 @@ public class BulletBuilderScope<T>(private val axisModel: LinearAxisModel<T>) wh
* to be customized then use the overloaded version of this function.
*/
public fun ranges(vararg values: T) {
// Subtract 1 because first value is the start of first range, not a range itself
require(values.size - 1 <= MaxQualitativeRanges) {
"A maximum of $MaxQualitativeRanges qualitative ranges is supported by this function. If more are " +
"required, use the overloaded version of this function."
}
rangesScope = RangesScope<T>().apply {
values.forEach {
ranges.add(Range<T>(it, null))
Expand Down Expand Up @@ -358,11 +366,12 @@ public class BulletBuilderScope<T>(private val axisModel: LinearAxisModel<T>) wh
@OptIn(ExperimentalKoalaPlotApi::class)
@Composable
private fun <T> RangeIndicators(builtScope: BulletBuilderScope<T>) where T : Comparable<T>, T : Number {
val shadeIndex = max(builtScope.rangesScope.ranges.size - 1, DefaultRangeShades.lastIndex)
val numShades = DefaultRangeShades[shadeIndex].size
for (rangeIndex in 1 until builtScope.rangesScope.ranges.size) {
val range = builtScope.rangesScope.ranges[rangeIndex]
if (range.indicator == null) {
val shadeIndex = min(builtScope.rangesScope.ranges.size - 2, DefaultRangeShades.lastIndex)
.coerceIn(0..DefaultRangeShades.lastIndex)
val numShades = DefaultRangeShades[shadeIndex].size
val shade = if (rangeIndex - 1 >= numShades) {
MinRangeShade
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.github.koalaplot.core.bar

import androidx.compose.ui.test.junit4.createComposeRule
import io.github.koalaplot.core.util.ExperimentalKoalaPlotApi
import io.github.koalaplot.core.xygraph.IntLinearAxisModel
import org.junit.Rule
import org.junit.Test

class BulletGraphTest {
@get:Rule
val composeTestRule = createComposeRule()

/**
* 5 default qualitative ranges allowed.
*/
@OptIn(ExperimentalKoalaPlotApi::class)
@Suppress("MagicNumber")
@Test
fun fiveDefaultRangesTest() {
composeTestRule.setContent {
BulletGraphs {
bullet(
IntLinearAxisModel(0..100)
) {
ranges(0, 10, 20, 30, 40, 50)

}
}
}
}

/**
* Trying to use more than 5 qualitative ranges should fail with an IllegalArgumentException.
*/
@OptIn(ExperimentalKoalaPlotApi::class)
@Suppress("MagicNumber")
@Test
fun moreThanFiveDefaultRangesFailsTest() {
composeTestRule.setContent {
var caught = false

BulletGraphs {
bullet(
IntLinearAxisModel(0..100)
) {
try {
ranges(0, 10, 20, 30, 40, 50, 60)
} catch (_: IllegalArgumentException) {
caught = true
}
}
}

assert(caught)
}
}

/**
*
*/
@OptIn(ExperimentalKoalaPlotApi::class)
@Suppress("MagicNumber")
@Test
fun moreThanFiveCustomRangesTest() {
composeTestRule.setContent {
BulletGraphs {
bullet(
IntLinearAxisModel(0..100)
) {

ranges(0) {
range(10)
range(20)
range(30)
range(40)
range(50)
range(60)
}
}
}
}
}
}

0 comments on commit bb68f66

Please sign in to comment.