ng2-charts icon indicating copy to clipboard operation
ng2-charts copied to clipboard

Chart title does not update

Open Leblayd opened this issue 4 years ago • 1 comments

Hi, I ran into this problem using the latest version (2.3.2), but I also tried the beta, and it didn't help. Reproduction of the problem

When I try updating the chart's title in plain JS, it works like a charm. https://jsfiddle.net/xjwnm8ty/13/

But here, I modified the Line charts demo to have the same functionality: https://stackblitz.com/edit/ng2-charts-line-template-mkvox6 Yet it does not work.

I checked, and when I replace options.title.text with a getter function like this: title: { get text() { debugger; return ''; } } I can clearly see that it does not get called on this.chart.update().

Leblayd avatar May 28 '20 12:05 Leblayd

ng2-charts deep clones the options, so you can't just change nested properties like that.

You can either:

  1. call this.chart.refresh() which will destroy the chart and build it again (this will refresh the options clone)
  2. or change the reference of lineChartOptions which will trigger change detection in ng2-charts, and let it decide how to update or refresh the chart Note: if you change the reference like this you don't have to call this.chart.update() by hand
    this.lineChartOptions.title.text += " and another one";
    this.lineChartOptions = {...this.lineChartOptions};

See updated stackblitz

danmana avatar Sep 23 '20 08:09 danmana

It's a bummer that it has to re-render the entire chart, but that's what I did to solve it in the end. I was going to update just the title without touching the chart at all, but that just didn't work out

Leblayd avatar Dec 03 '22 12:12 Leblayd

Did you try with the newer versions? On v3 and v4 we removed a lot of the deep cloning, and it might remove the need for you to callrefresh in many instances...

santam85 avatar Dec 05 '22 10:12 santam85

I have not - since then I got moved from the project that was using this But I'll keep that in mind if we end up having to visualize data again, thanks!

Leblayd avatar Dec 06 '22 18:12 Leblayd