patrykandpatrick / vico

A light and extensible chart library for Android.
https://patrykandpatrick.com/vico/wiki
Apache License 2.0
2.07k stars 125 forks source link

Chart does not correctly override axis values with `AxisValueOverrider` given two non-consecutive numbers #822

Closed LeeWeeder closed 1 month ago

LeeWeeder commented 1 month ago

How to reproduce

val modelProducer = remember { CartesianChartModelProducer() }
    LaunchedEffect(Unit) {
        val data = listOf(1, 3)

        modelProducer.runTransaction {
            lineSeries {
                series(
                    x = data,
                    y = data.indices.toList()
                )
            }
        }
    }

    CartesianChartHost(
        chart = rememberCartesianChart(
            rememberLineCartesianLayer(
                axisValueOverrider = remember {
                    // AxisValueOverrider
                    object : AxisValueOverrider {
                        override fun getMinX(minX: Double, maxX: Double, extraStore: ExtraStore): Double {
                            return 1.0
                        }

                        override fun getMaxX(minX: Double, maxX: Double, extraStore: ExtraStore): Double {
                             return 7.0
                        }
                    }
                }
            ),
            bottomAxis = rememberBottomAxis()
        ), modelProducer = modelProducer
    )

Observed behavior

Screenshot_20240731_155954

Given the default itemPlacer, some x values are missing.

In some cases, the max value will be missing.

Screenshot_20240731_161407

Expected behavior

Screenshot_20240731_160749

Given also the default itemPlacer, the x values were there, from 1 to 7.

[!NOTE] I am using two consecutive numbers here because, with that, I can show the expected behavior. It will also work on more than two entries.

Vico version(s)

2.0.0-alpha.25

Android version(s)

34

Gowsky commented 1 month ago

This behavior is expected and is related to the x step. To achieve the result you're looking for, please set the xStep to 1 via rememberCartesianChart(getXStep = { 1.0 }). We'll share a detailed explanation as soon as we can. We have a large volume of requests at the moment.