ComposeCharts icon indicating copy to clipboard operation
ComposeCharts copied to clipboard

The application crashes when there are fewer than two data points in the line chart.

Open mrihsann opened this issue 1 year ago • 1 comments

When there are less than two data in the line chart, if we touch the chart, the application crashes. If we do not use the line list as remember, the line is constantly recomposed.

As a solution, I use a structure like the one below.

val lineValues = when (uiState.resultListLast30Days.size) {
    0 -> listOf(0.0, 0.0) // If there is no data (0.0, 0.0)
    1 -> listOf(0.0,uiState.resultListLast30Days[0]) // If there is only one data (first data, 0.0)
    else -> uiState.resultListLast30Days // If there are two or more data, use it as is.
}

val lines = remember(lineValues) {
    listOf(
        Line(
            label = "",
            values = lineValues,
            color = SolidColor(Color(0xFF23af92)),
            firstGradientFillColor = Color(0xFF2BC0A1).copy(alpha = .5f),
            secondGradientFillColor = Color.Transparent,
            strokeAnimationSpec = tween(2000, easing = EaseInOutCubic),
            gradientAnimationDelay = 1000,
            drawStyle = DrawStyle.Stroke(width = 2.dp),
        )
    )
}
LineChart(
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp),
            labelHelperProperties = LabelHelperProperties(
                enabled = false
            ),
            indicatorProperties =  HorizontalIndicatorProperties(
                textStyle = MaterialTheme.typography.bodySmall.copy(color = MaterialTheme.colorScheme.onSurface)
            ),
            gridProperties = GridProperties(
                enabled = false
            ),
            data = lines,
            animationMode = AnimationMode.Together(delayBuilder = {
                it * 500L
            }),
        )

mrihsann avatar Sep 21 '24 11:09 mrihsann

@mrihsann thank you for reporting the problem, ill check.

ehsannarmani avatar Sep 21 '24 13:09 ehsannarmani

Hi, in the 0.0.15 version this issue should resolved. Check and test, if issue still exist, reopen the issue.

Thank you for reporting the problem.

ehsannarmani avatar Nov 03 '24 12:11 ehsannarmani