Skip to content

Line's value changed the whole Line redraw #92

@xufh2016

Description

@xufh2016

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions