Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Where is the documentation about adding and removing Data?

Open SonicTTH opened this issue 10 years ago • 16 comments

I cannot find it here: http://www.chartjs.org/docs

I would like to know exactly what parameters the addData and removeData functions have.

SonicTTH avatar Oct 24 '16 12:10 SonicTTH

@SonicTTH are you using v1? Thise functions existed then.

In v2, you can simply update the data object on the chart before calling update()

etimberg avatar Oct 24 '16 12:10 etimberg

Okay thank you so far. Figured out that the chart.js wrapper im using for my asp.net plattform uses chart.js v1.

Okay so when I use v2 now. what do you mean by update tha data object? How can I add values to that?

SonicTTH avatar Oct 24 '16 12:10 SonicTTH

The best way to describe it is to look at the sample files. https://github.com/chartjs/Chart.js/blob/master/samples/line/line.html#L115

etimberg avatar Oct 24 '16 13:10 etimberg

Okay awesome found out how to correctly add data to the charts, so that it comes in from the right side.

But the remove method there (using splice) removes entries at the end of the chart. I want to create a flowing line chart where new data comes from right and is removed at the left.

Using shift() on both the labels and the datasets removees the first element correctly:

            config.data.labels.shift(); // remove the label first

            config.data.datasets.forEach(function(dataset, datasetIndex) {
                dataset.data.shift();
            });

            window.myLine.update();

But the lines are flying around like hell!

In v1, the removeData() function correctly removed a datapoint at the left end of the chart (beginnig of the array I guess) and had all the lines move leftwards in a scrolling manner.

The animation of the chart in v2 it so that it looks very weird with all the lines flying around instead of simply moving to the left.

Is this achievable somehow?

SonicTTH avatar Oct 24 '16 16:10 SonicTTH

I presume you're using the latest release, 2.3.0? You could try the latest from the master branch. We made improvements since the last release that may fix your problem

etimberg avatar Oct 24 '16 16:10 etimberg

I would like to try that but I dont know how to get a working chart.js from the master branch. I cant compile or gulp it as ive read here somewhere since I have no tools for that. Can I get a compiled latest version somehow?

SonicTTH avatar Oct 24 '16 16:10 SonicTTH

Duplicates #1997, might be interesting for you to read this thread. As @etimberg said, you need the upcoming (very soon) version 2.4 which fixes exactly the issue you are experiencing. There is a built alpha / dev version attached to this comment that you can use for testing.

simonbrunel avatar Oct 24 '16 16:10 simonbrunel

@SonicTTH You can download built files from the releases page.

On a side note, v1 documentation is available here.

panzarino avatar Oct 24 '16 17:10 panzarino

How to get json data and render to chart?

wachara-yanu avatar Oct 31 '16 02:10 wachara-yanu

@vajrayanu That is something that you have to do on the client side. Charts can be created as shown in the docs.

panzarino avatar Oct 31 '16 03:10 panzarino

I have found a workaround for the gap animation issue when removing old data. By setting the value of the first point to the second, then removing the second. Its a bit dirty, but I don't know of any other way of doing this.

    if (this.chartData.datasets[0].data.length > 5) {
      this.chartData.labels.shift()
      this.chartData.datasets.forEach(dataset => {
        dataset.data[0] = dataset.data[1]
        dataset.data.splice(1, 1)
      })
    }

everystone avatar Mar 22 '17 09:03 everystone

Data update docs in #4170

etimberg avatar May 08 '17 22:05 etimberg

Is there any news on this issue with the gap animation after shift(). We are talking about this in https://github.com/valor-software/ng2-charts/issues/291 as well.

Would be great to find a proper solution for this.

mikezks avatar Aug 24 '17 15:08 mikezks

@everystone, thanks for this workaround. Not perfect, but the best I have seen so far.

mikezks avatar Aug 24 '17 15:08 mikezks

As this issue is closed I'll open a new one related to the animation gap.

See https://github.com/chartjs/Chart.js/issues/4695.

mikezks avatar Aug 24 '17 15:08 mikezks

Go to https://www.chartjs.org/docs/latest/developers/updates.html

mahbub-hussain avatar Mar 16 '22 13:03 mahbub-hussain