Charts: Animated update

Created on 7 Jul 2015  路  21Comments  路  Source: danielgindi/Charts

Hi!
Is there a way to update LineChartView data (ChartData) and redraw LineChartView animated from current position.
Like in appFigures application.
Thanks.

idea

Most helpful comment

It's been over two years, does anyone know how to implement this?

All 21 comments

This is actually a feature that I thought about already, and planning to implement.
We have a lot on our plate right now - so in the meantime if this is really important to you - you could do this:

Create a function that takes dataset _A_, dataset _B_, and a _phase_ value between 0 and 1.
This function will:

  1. Define a new dataset _C_
  2. Iterate on each value in _A_, and set C[i] = (B[i] - A[i]) * phase + A[i]
  3. Return _C_ as the new dataset

Now you should animate a simple number from 0 to 1, and pass it to the function along with the "from" dataset and "to" dataset. The result should be passed to the chart view :-)

You can also use the animation util in Charts, that can animate a number for you already, including easing and stuff.

So it's pretty much a peach of cake, and as soon as I can I'm planning on adding this to the library. But at least you have a solution for the meanwhile ;-)

Hi! Is it feature already implemented?

I need to animate adding new value to data set and changing position of limit line.

Hi, I'm trying to apply the animation of data change in a pie chart. I already have the function you've mentioned but am struggling to find a way to "animate a simple number from 0 to 1". Any thoughts on this? Thanks!

Hello! The feature is already implemented?

Adding my support for this feature, and also looking for a good way to interpolate a simple value according to some easing function (for the workaround). All the animation libraries seem to operate on views or properties, but I just want to "animate" the value of a simple number. Feels like it should be simple, maybe I'm looking in the wrong places?

You might want to have a look at the post below where I've shared my solution. I'm aware that it is far from perfect but it seems to be working, which is nice. http://stackoverflow.com/questions/34730917/animating-a-double-from-0-to-1-in-swift-over-a-specified-interval/34774113#34774113

Thanks for the quick reply @apiejh, really appreciate it. I managed to get this working with a timer function. Hacky, but works! At least until a better approach is available.

Any updates or possible solutions for this ?

+1, I have a chart with data and when some asynchronous process completes I would like to add either one or more new datasets and/or extend a dataset's datapoints (e.g. _live data_). It would be much more eye-pleasing if the chart would be able to animate those new datasets instead of rebuilding from scratch.

@kevinjtchen just put a PR out for this https://github.com/danielgindi/Charts/pull/2889

"You can also use the animation util in Charts, that can animate a number for you already, including easing and stuff."

I don't see any option to animate a single data-set only the whole chart. Am I missing something?

It's been over two years, does anyone know how to implement this?

Is this ever going to be implemented? I would love to be able to do something as simple as

lineChart.animate(to: newDataSet, duration: 1.0);

Just clear the array of old values and fill it with the new ones before calling again the setupChart()

It is 2019 already and is there a solution for animate data changes without redrawing the whole chart from scratch? 馃檪

It's been over two years, does anyone know how to implement this?

It's been over four years now, something tells me we're never going to get this :(

not sure why the PR #2889 is closed, but for those that still want some quick code snippets for danielgindis implementation: https://github.com/mrjko/testCharts/blob/master/chartsTest/BarChartViewController/BarChartViewController.swift

Does anyone know a way to do this for lineChart, I have to change dot position smoothly without redraw the whole chart.

@vittorionapoli You can use the same as @mrjko posted above, but replace it with LineChartDataSet

func partialResults(setA: LineChartDataSet, setB: LineChartDataSet, phase: Double) -> LineChartDataSet {
    let newSet = LineChartDataSet()

    for index in 0..<setA.entries.count {
        let currA = setA.entries[index].y
        let currB = setB.entries[index].y

        let newValue = (currB - currA) * phase + currA
        newSet.append(BarChartDataEntry(x: Double(index), y: newValue))
    }

    return newSet
}

@mrjko How the above method will work for an array of yValues for Stacked Bar Chart?
BarChartDataEntry(x: Double(index), yValues: [anArrayOfyValues])

Updating the presented solution with LineChartDataSet and also it adds/removes new values from the dataSet if the size of the setA and setB is different.

    func partialResults(setA: LineChartDataSet, setB: LineChartDataSet, phase: Double) -> LineChartDataSet {
        let newSet = LineChartDataSet()
        let setACount = setA.entries.count
        let setBCount = setB.entries.count

        for index in 0..<min(setACount, setBCount) {
            let currA = setA.entries[index].y
            let currB = setB.entries[index].y

            let newValue = (currB - currA) * phase + currA
            newSet.append(ChartDataEntry(x: Double(index), y: newValue))
        }

        let diff = abs(setACount - setBCount)
        if setACount < setBCount {
            for index in setBCount - diff..<setBCount {
                let currB = setB.entries[index].y
                let newValue = currB
                newSet.append(ChartDataEntry(x: Double(index), y: newValue))
            }
        } else if setACount > setBCount {
            for index in setACount - diff..<setACount {
                let currA = setA.entries[index].y
                let newValue = currA
                newSet.append(ChartDataEntry(x: Double(index), y: newValue))
            }
        }

        return newSet
    }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anhltse03448 picture anhltse03448  路  3Comments

coop44483 picture coop44483  路  3Comments

Bharati555 picture Bharati555  路  4Comments

cilasgimenez picture cilasgimenez  路  4Comments

BrandonShega picture BrandonShega  路  4Comments