Search code examples
androidscichart

Column chart not updating if a column value decreases


If the values in a Column chart increases the column chart updates and everything looks good. But when the value decreases columns are not updated and it still displays the old values.

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)
}
  • Example 1: DataSet (A= 50, B=60, C=20) these values are displayed correctly

  • Example 2: DataSet (A= 80, B=60, C=20) A increased from 50 to 80 in this DataSet: these values are displayed correctly as well.

  • Example 3: DataSet (A= 14, B=60, C=20) notice A decreased from 80 to 14 the graph is not refreshed and the old value of A= 80 is displayed

Update The values of Column chart is updated in the listener of donut chart

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()

        }
    })

Thanks for reading :)


Solution

  • The problem in your code is that you're adding new column series on top of the old one, because RenderableCollection isn't cleared before you add new column series. I would suggest to add clear() call for renderable series collection into 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()
        }
    }
    

    Also I would suggest to set theme and modifiers once, because in case if you leave that code in drawColumnChart() you'll need to clear modifier collection as well + you'll reset theme every time column is rerendered:

    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())
        }
    
    }