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

how to update labels?

Open zardaloop opened this issue 12 years ago • 29 comments

Hi nick, I followed up this issue https://github.com/nnnick/Chart.js/issues/13 which at the end of it you have added a fantastic solution to be able to update the data series and then update the chart. However I can't figure out how can I update the labels if the length of the data series and label values are different from the original dataset?

Many thanks

zardaloop avatar Aug 31 '14 21:08 zardaloop

I'm not sure if I completely understand?

So you can change existing values by changing the .value property on the bar/point and then call .update, then you can call .addData or .removeData to add/remove new points?

nnnick avatar Sep 01 '14 09:09 nnnick

Hi Nick, Thanks for getting back to me. I don't want to remove only one point. I want to be able to replace the whole data in a dataset. for example : myLiveChart.datasets[1].data = [1,2,3,4,5]

However at the moment I cant do that, because in order to be able to replace the data I should replace the labels since the new dataset has different length of data and the labels are different values.

In your example in http://jsbin.com/yitep/4/edit, labels are "labels: [1, 2, 3, 4, 5, 6, 7]", and they never change, but my question is how can I change the labels array? Because if want to be able the correct data as well as replacing the dataset's data I should change the labels as well, because the length of both should be the same.

I would highly appreciate any suggestion on that.

zardaloop avatar Sep 01 '14 10:09 zardaloop

+1 I have the same wish: changing the labels when updating the data. (BTW: No need for me to change the nr of elements in the dataset)

willemx avatar Sep 06 '14 08:09 willemx

+1 Me too

pederan avatar Sep 10 '14 19:09 pederan

Hello, this issue looks like an ERROR REPORT, for something not correctly implemented in Chart.js. It would be very helpful if you could please create an example chart that would illustrate this missing feature using JSBIN and even attached a mockup here of what the result should look like.

To help, I have created a simple JSBIN which you could fork: http://jsbin.com/yozobahucero/1/edit?html,js,output

Sorry that this response is templated as we are triaging many issues and ones with test cases will be seen first.

fulldecent avatar Sep 25 '14 17:09 fulldecent

I did some quick investigation and found the workaround: myLineChart.scale.xLabels = ["label1", "label2", "label3"]; myLineChart.update();

tolhzar avatar Oct 02 '14 20:10 tolhzar

+1: Experiencing an issue where updating the labels updates them in the tooltips, but not on the axis. Unfortunately this issue is intermittent.

dja avatar Nov 10 '14 01:11 dja

+1 Has there been any update? I have not been able to get a workaround on this.

mpgarate avatar Dec 19 '14 17:12 mpgarate

+1 how to update labels

amazing-gao avatar Apr 13 '15 06:04 amazing-gao

I've now just changed my own use to just replace the chart with a fresh one, instead of trying to animate and append updates.

dja avatar Apr 14 '15 18:04 dja

+1 Does anyone have a workaround that doesn't include redrawing the entire chart? I can update the chart.datasets[0].bars[0].label value, but the canvas keeps the old label even after calling update().

dw2 avatar Apr 14 '15 21:04 dw2

I have a workaround, but it's cumbersome. We're working on a nice PR that will likely fix any issues anyone is having with updating an existing chart and likewise animating those changes.

tannerlinsley avatar Apr 15 '15 01:04 tannerlinsley

@tannerlinsley Thanks for the update. I've implemented a workaround that removes the animation only when I need updated labels so that users don't see the re-animation when I redraw the chart.

dw2 avatar Apr 15 '15 02:04 dw2

Still can not update the labels? Looks like have to redraw the whole thing.

leijing7 avatar Apr 23 '15 06:04 leijing7

Can someone post a simple workaround for this issue?

Ideally I'd like to be able to update multiple y-values and labels (x-values) at once.

roger- avatar Apr 24 '15 16:04 roger-

Doesn't my solution posted on Oct 2 work?

tolhzar avatar Apr 24 '15 16:04 tolhzar

@rocketblr

Thanks, missed that and it indeed seems to work!

Are there any gotchas when updating scale.xLabels and scale.yLabels this way?

roger- avatar Apr 24 '15 16:04 roger-

Be careful when you need to change number of labels. In that case you should combine updating labels with adding or removing data to chart.

tolhzar avatar Apr 24 '15 17:04 tolhzar

Ah, ok. I'll assume I need to call .addData() or .removeData() as many times as necessary to get the right number of labels. Would also be nice if there was a cleaner way to do this, but that works for now.

roger- avatar Apr 24 '15 17:04 roger-

+1

leewisestamp avatar May 25 '15 10:05 leewisestamp