ng2-charts
ng2-charts copied to clipboard
Chart title does not update
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()
.
ng2-charts
deep clones the options, so you can't just change nested properties like that.
You can either:
- call
this.chart.refresh()
which will destroy the chart and build it again (this will refresh the options clone) - 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 callthis.chart.update()
by hand
this.lineChartOptions.title.text += " and another one";
this.lineChartOptions = {...this.lineChartOptions};
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
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...
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!