如果列值减少,则柱形图不会更新

Column chart not updating if a column value decreases

如果柱形图中的值增加,则柱形图更新并且一切看起来都不错。但是当值减少时,列不会更新,它仍然显示旧值。

private fun drawColumnChart() {
    UpdateSuspender.using(lineChart) {
        lineChart.theme = R.style.SciChart_Bright_Spark
        Collections.addAll(lineChart.renderableSeries, rSeries)
        Collections.addAll(lineChart.chartModifiers, sciChartBuilder
                .newModifierGroupWithDefaultModifiers().build())
        sciChartBuilder.newAnimator(rSeries)
                .withWaveTransformation()
                .withInterpolator(DecelerateInterpolator())
                .withDuration(3000)
                .withStartDelay(350)
                .start()
    }
}

private fun createDataSeries(values: Array<Int>): IXyDataSeries<Int, Int> {
    val dataSeries: IXyDataSeries<Int, Int> = sciChartBuilder
            .newXyDataSeries(Int::class.javaObjectType, Int::class.javaObjectType).build()
    for (i in values.indices) {
        dataSeries.append(i, values[i])
    }
    return dataSeries;
}

private fun createRSeries(values: Array<Int>): FastColumnRenderableSeries {

    val dataSeries: IXyDataSeries<Int, Int> = createDataSeries(values)

    return sciChartBuilder.newColumnSeries()
            .withStrokeStyle(-0xdcdcdd, 0.4f)
            .withDataPointWidth(0.5)
            .withDataSeries(dataSeries)
            .withPaletteProvider(ColumnsPaletteProvider())
            .build()
}

private fun createXandYAxis() {
    xAxis = sciChartBuilder.newNumericAxis()
            .withGrowBy(0.2, 0.2)
            .withLabelProvider(YearsLabelProvider())
            .build()
    yAxis = sciChartBuilder.newNumericAxis()
            .withAutoRangeMode(AutoRange.Always).build()
    Collections.addAll(lineChart.xAxes, xAxis)
    Collections.addAll(lineChart.yAxes, yAxis)
}

更新 柱形图的值在圆环图的监听器中更新

segments.forEach(Consumer { segment: PieSegment ->
        segment.addIsSelectedChangeListener {
            var chartData  = dataCollector(it.value.toLong())
            vehicleData[0] = chartData.fuels
            vehicleData[1] = chartData.repairs
            vehicleData[2] = chartData.tolls
            vehicleData[3] = chartData.mis

            rSeries = createRSeries(vehicleData)
            drawColumnChart()

        }
    })

感谢阅读:)

您的代码中的问题是您在旧列系列之上添加了新列系列,因为在您添加新列系列之前 RenderableCollection 没有被清除。我建议将可渲染系列集合的 clear() 调用添加到 drawColumnChart() 中。

private fun drawColumnChart() {
    UpdateSuspender.using(lineChart) {
        animator?.cancel() // cancel if there is animation in progress

        lineChart.renderableSeries.clear()
        Collections.addAll(lineChart.renderableSeries, rSeries)

        animator = sciChartBuilder.newAnimator(rSeries)
                    .withWaveTransformation()
                    .withInterpolator(DecelerateInterpolator())
                    .withDuration(3000)
                    .withStartDelay(350).build()

        animator.start()
    }
}

此外,我建议设置一次主题和修饰符,因为如果您将该代码留在 drawColumnChart() 中,您还需要清除修饰符集合 + 每次重新呈现列时都会重置主题:

private fun createXandYAxis() {
    xAxis = sciChartBuilder.newNumericAxis()
        .withGrowBy(0.2, 0.2)
        .withLabelProvider(YearsLabelProvider())
        .build()
    yAxis = sciChartBuilder.newNumericAxis()
        .withAutoRangeMode(AutoRange.Always).build()

    UpdateSuspender.using(lineChart) {
        lineChart.theme = R.style.SciChart_Bright_Spark

        Collections.addAll(lineChart.xAxes, xAxis)
        Collections.addAll(lineChart.yAxes, yAxis)
        Collections.addAll(lineChart.chartModifiers, sciChartBuilder
            .newModifierGroupWithDefaultModifiers().build())
    }

}