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

Cannot view chart in Jetpack Compose Preview #804

Closed anchithAcharya closed 1 month ago

anchithAcharya commented 1 month ago

How to reproduce

Run the following code in an android project with Jetpack Compose set up:

@Preview
@Composable
fun ChartComposable() {
    val chart = rememberCartesianChart(
        rememberLineCartesianLayer(),
        startAxis = rememberStartAxis(),
        bottomAxis = rememberBottomAxis(),
    )
    val cartesianChartModelProducer = remember { CartesianChartModelProducer() }
    LaunchedEffect(Unit) {
        cartesianChartModelProducer.runTransaction {
            lineSeries {
                series(listOf(1, 2, 3, 4))
            }
        }
    }
    CartesianChartHost(
        chart = chart,
        modelProducer = cartesianChartModelProducer
    )
}

Observed behavior

Preview fails to load, giving the following exception: java.lang.ClassNotFoundException: com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer

Expected behavior

Preview should be visible

Vico version(s)

2.0.0-alpha.23

Android version(s)

SDK version 34, minSDK 30

Additional information

No response

Gowsky commented 1 month ago

Hi @anchithAcharya, we ran your code and we didn't experience the exception. It seems to be a build cache issue. It should disappear when you rebuild the project. However, the preview will remain blank, as Compose previews don't support asynchronous execution (CartesianChartModelProducer runs off the main thread). We'll check if we can move the execution to the main thread for Compose previews. In the meantime, you can create the model synchronously for the preview.

Since this is not entirely a bug, I'm converting this to a discussion.