JetBrains / lets-plot-skia

Skia frontend for Lets-Plot multiplatform plotting library.
MIT License
132 stars 2 forks source link

Unexpected redraw #2

Closed SanmerDev closed 10 months ago

SanmerDev commented 10 months ago

Bug

Expected

Platform

Dependencies

Sample

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import org.jetbrains.letsPlot.geom.geomDensity
import org.jetbrains.letsPlot.letsPlot
import org.jetbrains.letsPlot.skia.compose.PlotPanel

@Composable
fun TestScreen() {
    // irrelevant value
    var enable by remember { mutableStateOf(false) }

    // remember data and figure
    val rand = java.util.Random()
    val data by remember {
        mutableStateOf(
            mapOf(
                "rating" to List(200) { rand.nextGaussian() } + List(200) { rand.nextGaussian() * 1.5 + 1.5 },
                "cond" to List(200) { "A" } + List(200) { "B" }
            )
        )
    }
    val plot by remember {
        mutableStateOf(letsPlot(data) +
                geomDensity(color = "dark_green", alpha = .3) {
                    x = "rating"; fill = "cond"
                }
        )
    }

    Scaffold { innerPadding ->
        Column(
            modifier = Modifier
                .verticalScroll(rememberScrollState())
                .padding(innerPadding)
                .padding(all = 16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = "Irrelevant Value",
                    style = MaterialTheme.typography.bodyLarge,
                    modifier = Modifier.weight(1f)
                )

                Switch(
                    checked = enable,
                    onCheckedChange = { enable = it }
                )
            }

            PlotPanel(
                figure = plot,
                preserveAspectRatio = false,
                modifier = Modifier
                    .requiredHeight(240.dp)
                    .fillMaxWidth(),
                computationMessagesHandler = { messages ->
                    messages.forEach {
                        Log.d("PlotPanel", it)
                    }
                }
            )
        }
    }
}
changhexuefei commented 10 months ago

This situation also occurs in Windows. When the page is updated, the PlotPanel flashes with a black background.

SanmerDev commented 10 months ago

https://github.com/JetBrains/lets-plot-skia/blob/14ca8b85b9de3708b1ad50c785314dac3a975717/lets-plot-compose/src/androidMain/kotlin/org/jetbrains/letsPlot/skia/compose/PlotPanel.kt#L30 I think this issue may come from here, the provider does not remember, which causes it to be recreated when recompose.

changhexuefei commented 10 months ago

Hello, please tell me how to update the code of this library. I use it in the following way.

image

alshan commented 10 months ago

@changhexuefei hi, your setup looks fine providing the variables used are defined correctly in your project.

You may want to specify versions directly as shown here: https://github.com/JetBrains/lets-plot-skia#compose-desktop

changhexuefei commented 10 months ago

@alshan Hello, I want to use the code after fixing the unexpected redraw problem, do I have to wait until a new version is released.

IKupriyanov-HORIS commented 10 months ago

@changhexuefei hi! Yes, the fix is not yet released.

alshan commented 10 months ago

That's correct, you will be able to upgrade as soon as new version is released.

changhexuefei commented 10 months ago

@alshan @IKupriyanov-HORIS ok,thanks.