ComposeCharts icon indicating copy to clipboard operation
ComposeCharts copied to clipboard

Line's value changed the whole Line redraw

Open xufh2016 opened this issue 8 months ago • 2 comments

I met a problem ,and this is my code :

@Composable
fun HpageRealtimeCurveChartUi(
    modifier: Modifier = Modifier
        .fillMaxWidth()
        .height(180.dp),
    viewModel: BleViewModel
) {
    val curveLabel by viewModel.curveLabelText.collectAsState()
    val curveBackgroundList by viewModel.curveBackgroundCh4List.collectAsState()
    val curveMeasurementList by viewModel.curveMeasurementCh4List.collectAsState()

    // 使用可变状态列表保存累积数据点
    val accumulatedValues = remember { mutableStateListOf<Double>() }

    // 当曲线标签变化时,重置累积数据并加载当前数据源的现有数据
    LaunchedEffect(curveLabel) {
        val currentList = when (curveLabel) {
            AppConstants.BACKGROUND_CH4 -> curveBackgroundList
            else -> curveMeasurementList
        }
        accumulatedValues.addAll(currentList)
    }

    // 监听数据源变化,追加新增数据点
    LaunchedEffect(curveBackgroundList, curveMeasurementList) {
        val currentList = when (curveLabel) {
            AppConstants.BACKGROUND_CH4 -> curveBackgroundList
            else -> curveMeasurementList
        }
        val newData = currentList.drop(accumulatedValues.size)
        if (newData.isNotEmpty()) {
            accumulatedValues.addAll(newData)
        }
    }

    LineChart(
        modifier = modifier.padding(horizontal = 5.dp),
        data = listOf(
            Line(
                label = curveLabel,
                values = accumulatedValues,
                color = SolidColor(LaoyingRed),
                firstGradientFillColor = if ((curveLabel == AppConstants.BACKGROUND_CH4 && curveBackgroundList.isEmpty()) || (curveLabel == AppConstants.MEASUREMENT_CH4 && curveMeasurementList.isEmpty())) {
                    Color.Transparent
                } else {
                    LaoyingRed.copy(alpha = 0.5f)
                },
                secondGradientFillColor = Color.Transparent,
                strokeAnimationSpec = tween(2000, easing = EaseInOutCubic),
                gradientAnimationDelay = 1000,
                drawStyle = DrawStyle.Stroke(width = 2.dp)
            )
        ),
        animationMode = AnimationMode.OneByOne
    )
}

,when field curveBackgroundList or curveMeasurementList value changed,the whole Line will redraw

xufh2016 avatar Apr 21 '25 08:04 xufh2016

Hi @xufh2016 , put your data into remember with curveBackgroundList , accumulatedValues and curveLabel parameters, and let me notified if issue still exists

ehsannarmani avatar Apr 21 '25 17:04 ehsannarmani

Hi @xufh2016 , put your data into remember with curveBackgroundList , accumulatedValues and curveLabel parameters, and let me notified if issue still exists

it does not work

xufh2016 avatar May 27 '25 05:05 xufh2016