Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix maxBarValue not updated in a timely manner #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

junerver
Copy link

示例中添加新的柱状图数据时,没有更新最大值限定,导致require 代码块抛出异常

或者可以考虑将该字段直接移除

data class BarChartData(
    val bars: List<Bar>,
    val padBy: Float = 10F,
    val startAtZero: Boolean = true,
) {

    init {
        require(padBy in 0F..FLOAT_100) {
            "padBy must be between 0F and 100F, included"
        }
    }

    internal val maxBarValue: Float
        get() = bars.maxOf { it.value }

    private val yMinMaxValues: Pair<Float, Float>
        get() {
            val minValue = bars.minOf { it.value }
            val maxValue = maxBarValue
            return minValue to maxValue
        }

    val maxY: Float
        get() = yMinMaxValues.second + (yMinMaxValues.second - yMinMaxValues.first) * padBy / FLOAT_100
    val minY: Float
        get() = if (startAtZero) 0F
        else yMinMaxValues.first - (yMinMaxValues.second - yMinMaxValues.first) * padBy / FLOAT_100

    data class Bar(
        val value: Float,
        val color: Color,
        val label: String
    )
}

@junerver junerver force-pushed the master branch 2 times, most recently from cb5db48 to 64abab8 Compare November 15, 2024 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant