Is it possible to update any chart
from ng2-charts
dynamically? I know there are other libraries like angular2-highcharts
, but I would like to handle it using ng2-charts
. Main question is how can I redraw chart
, after button click? I can resize window and data will be updated, so it have to be any option to do it manually.
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- Mercurial Commit Charts / Graphs [closed]
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
Recently I had to use ng2-charts and i was having a very big issues with updating my data until I found this solution:
and here what I have in my component :
I'm sure this is the right way to do it.
Update any chart from
ng2-charts
dynamically is possible. Example: http://plnkr.co/edit/m3fBiHpKuDgYG6GVdJQD?p=previewThat Angular update chart, variable or reference must change. When you just change value of array element, variables and references not changed. See also Github.
A good way is to get a grip on the chart itself in order to redraw it using the API :
I figure it out, maybe its not the best existing option, but it works. We can't update existing
chart
, but we can create new one, using our existingchart
and add new points. We can even get better effect, turning animation of chart off.Function that solved problem:Live demo: https://plnkr.co/edit/fXQTIjONhzeMDYmAWTe1?p=preview
@UPDATE: as @Raydelto Hernandez mentioned in comment below, better solution is:
I have found this article after a searching a while. Then I tried every solution that is mentioned in this article but none was working properly. Finally I settled using a dynamic component.
I provided the dynamic data to component that is dynamically added to the parent component. I wrote about the whole process just so you don't go through the challenges.
**This worked for me - with pie-chart:*
Component.html:
Component.ts:
In the header section:
In the declaration section of export class:
After the block that updates your pie chart data (using services/sockets or any other means):