Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animated update #189

Open
EvgeniyGT opened this issue Jul 7, 2015 · 21 comments
Open

Animated update #189

EvgeniyGT opened this issue Jul 7, 2015 · 21 comments
Labels

Comments

@EvgeniyGT
Copy link

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

@danielgindi
Copy link
Collaborator

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

@AlexEdunov
Copy link

Hi! Is it feature already implemented?

@AlexEdunov
Copy link

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

@apiejh
Copy link

apiejh commented Jan 11, 2016

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!

@mlorenzeCDS
Copy link

Hello! The feature is already implemented?

@Salvar
Copy link

Salvar commented Jan 21, 2016

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?

@apiejh
Copy link

apiejh commented Jan 21, 2016

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

@Salvar
Copy link

Salvar commented Jan 21, 2016

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.

@anfriis
Copy link

anfriis commented Apr 4, 2017

Any updates or possible solutions for this ?

@4np
Copy link
Contributor

4np commented Sep 22, 2017

+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.

@robotgray
Copy link

@kevinjtchen just put a PR out for this #2889

@kditraglia
Copy link

"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?

@delox
Copy link

delox commented Dec 25, 2017

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

@nathan-fiscaletti
Copy link

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

@nikoagge
Copy link

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

@MihaelIsaev
Copy link

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

@nathan-fiscaletti
Copy link

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

@mrjko
Copy link

mrjko commented Feb 22, 2020

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

@vittorionapoli
Copy link

vittorionapoli commented Apr 21, 2020

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

@SSenocico
Copy link

SSenocico commented Apr 24, 2020

@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])

@Skornos
Copy link

Skornos commented May 11, 2020

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
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests