Skip to content

Seamless animation between changed data point values doesn't work #35

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

Closed
mekwall opened this issue Sep 29, 2016 · 25 comments
Closed

Seamless animation between changed data point values doesn't work #35

mekwall opened this issue Sep 29, 2016 · 25 comments

Comments

@mekwall
Copy link

mekwall commented Sep 29, 2016

When updating the data of the chart instance, you replace all of it which makes chart.js re-render the chart even though you've only updated the data points. This makes it impossible to animate between changed values.

It would be great if when we only update data points that it would update them them properly so that seamless animation between point values work as intended.

@mekwall
Copy link
Author

mekwall commented Sep 29, 2016

I got it to kind of work by doing the following inside of updateChart():

var currentData = this.chart_instance.config.data.datasets;
var nextData = data.datasets;

if (currentData.length == nextData.length) {
    var deleteDataSets = false;
    nextData.forEach(function (dataset, sid) {
        if (dataset.length === nextData[sid].length) {
            deleteDataSets = true;
            dataset.data.forEach(function (point, pid) {
                if (currentData[sid] && currentData[sid].data[pid]) {
                    currentData[sid].data[pid] = nextData[sid].data[pid];
                } else {
                    delete currentData[sid].data[pid];
                }
            });
        }
    });
    if (deleteDataSets) delete data.datasets;
    this.chart_instance.config.data = _extends([], this.chart_instance.config.data, data);
} else {
    this.chart_instance.config.data = _extends([], this.chart_instance.config.data, data);
}

this.chart_instance.config.data = _extends([], this.chart_instance.config.data, data);

this.chart_instance.update();

It doesn't seem to update the other options though, but it is animating between data point values.

@gor181
Copy link
Collaborator

gor181 commented Oct 3, 2016

Hey @mekwall ,

Thanks for reporting in. Seems your approach is fine, just we need to think and make sure all the options/datasets/configs are being updated.

I can take a look at this by end of this week, it's kinda hectic..

thanks,
Goran

@mekwall
Copy link
Author

mekwall commented Oct 3, 2016

No rush @gor181. Here's a revised version that I currently use:

const {data, options} = this.props;

if (!this.chart_instance) return;

if (options) {
    this.chart_instance.options = Chart.helpers.configMerge(this.chart_instance.options, options);
}

var currentData = this.chart_instance.config.data.datasets;
var nextData = data.datasets;

nextData.forEach(function (dataset, sid) {
    if (currentData[sid] && currentData[sid].data) {
        currentData[sid].data.splice(nextData[sid].data.length);
        dataset.data.forEach(function (point, pid) {
            currentData[sid].data[pid] = nextData[sid].data[pid];
        });
    } else {
        currentData[sid] = nextData[sid];
    }
});
delete data.datasets;

this.chart_instance.config.data = {
    ...this.chart_instance.config.data,
    ...data
};

this.chart_instance.update();

@varatep
Copy link

varatep commented Oct 12, 2016

@mekwall Are you currently extending the class and overriding the updateChart() prop?

@darkone23
Copy link

Just chiming in that I am also seeing this behavior.

👍

@mekwall
Copy link
Author

mekwall commented Oct 13, 2016

@varatep I've made a copy of Chart.js and modified that. We use it directly in the project and do not rely on this package as a dependency.

@jerairrest
Copy link
Collaborator

I've added @mekwall change to a fork here: https://github.com/jerairrest/react-chartjs-2. I've tested the change with around 1000 data points and it looks like its working great!

@Codelica
Copy link

I gave the @jerairrest fork with @mekwall change a try here and it's working well for me also. 👍

@jebarjonet
Copy link

What about a PR on this repo ? So that it could be profitable for everybody

@jerairrest
Copy link
Collaborator

@Codelica Absolutely! See #65

@jebarjonet
Copy link

@jerairrest very nice ! Don't know if the yarn file is necessary in this PR but that can not hurt 😄

@jerairrest
Copy link
Collaborator

I can remove it if need be. @gor181 your thoughts?

@mekwall
Copy link
Author

mekwall commented Dec 2, 2016 via email

@jebarjonet
Copy link

Note that yarn is still in version 0.x and has bugs and failures with some packages (not the case in the very repo apparently)

@jerairrest
Copy link
Collaborator

Welp, I'm leaving it up to @gor181 at this point. :) To yarn or not to yarn. That is the question!

@gor181
Copy link
Collaborator

gor181 commented Dec 3, 2016

heya @jerairrest ,

Can we make yarn compatibility with another PR?
Asap you remove it we can merge the changes.

It would be also awesome to have an example added to examples if you have the code still available.

Big thanks to your and everyone for making the effort.

Cheers,
Goran

@jerairrest
Copy link
Collaborator

built and pending merge. I'll add an example in a seperate pr.

@gor181 gor181 closed this as completed in 4bd7d79 Dec 3, 2016
@gor181
Copy link
Collaborator

gor181 commented Dec 3, 2016

Thanks @jerairrest and everyone, this is out in v1.5.5

@gor181
Copy link
Collaborator

gor181 commented Dec 3, 2016

@jerairrest also added an example
Live at http://gor181.github.io/react-chartjs-2/ (last)

@kvz
Copy link

kvz commented Jul 30, 2017

Hi! I'm rather new to React, Charts.js, and react-chartjs-2, so I hope I'm asking at the right place, but I'm still seeing the behavior as reported in this ticket (I think). Here's a gif:

redraw

The graph builds up from the bottom to the top at every datapoint added. I based the graph mostly on this example: https://github.com/jerairrest/react-chartjs-2/blob/master/example/src/components/randomizedLine.js

Who has a clue what I might be doing wrong?

I'm updating state via:

      let newState = _.clone(this.state)
      newState.labels.push(timestamp)
      newState.datasets[0].data.push(lastVal)
      this.setState(newState)

and calling the component like:

      <Line
        data={this.state}
        width={320}
        height={180}
        options={{
          animate   : false,
          responsive: false,
        }}
        redraw
      />

@jerairrest
Copy link
Collaborator

hmmm... can you reproduce this with sample data in a code pen? It looks like you're losing the last data point of your graph in the gif. Thankks!

@kvz
Copy link

kvz commented Aug 6, 2017 via email

@minusplusminus
Copy link

minusplusminus commented Oct 12, 2017

Had the same problem. Solved it with this Stack overflow post: https://stackoverflow.com/questions/21389341/disable-animation-with-charts-js

<Line data={data} options={{ animation: { duration: 0 }}}/>

@anvaymishra1
Copy link

Hi! I'm rather new to React, Charts.js, and react-chartjs-2, so I hope I'm asking at the right place, but I'm still seeing the behavior as reported in this ticket (I think). Here's a gif:

redraw

The graph builds up from the bottom to the top at every datapoint added. I based the graph mostly on this example: https://github.com/jerairrest/react-chartjs-2/blob/master/example/src/components/randomizedLine.js

Who has a clue what I might be doing wrong?

I'm updating state via:

      let newState = _.clone(this.state)
      newState.labels.push(timestamp)
      newState.datasets[0].data.push(lastVal)
      this.setState(newState)

and calling the component like:

      <Line
        data={this.state}
        width={320}
        height={180}
        options={{
          animate   : false,
          responsive: false,
        }}
        redraw
      />

I am stuck in a similar situation. I tried setting the animation to duration to 0 but still can't change the behaviour

@bogdanionitabp
Copy link

You can use the "mode" parameter for the chart.update() method, as documented here - this will skip animation https://www.chartjs.org/docs/latest/developers/api.html#updatemode

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

No branches or pull requests