swimlane / ngx-charts

:bar_chart: Declarative Charting Framework for Angular
https://swimlane.github.io/ngx-charts/
MIT License
4.3k stars 1.15k forks source link

If use ChangeDetectionStrategy.OnPush + Observable, the chart never update #591

Open javico2609 opened 7 years ago

javico2609 commented 7 years ago

I'm submitting a ... (check one with "x")

[x ] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here

Current behavior If use ChangeDetectionStrategy.OnPush + Observable, the chart never update

Expected behavior Proper update of the chart

Reproduction of the problem

 public dataDemo$: BehaviorSubject<any[]> = new BehaviorSubject([]);
 Observable.interval(10000).subscribe( index => {
      const value =  {
        name: `Ramdon-${index}`,
        value: index * 100
      };

      this.dataDemo$.next( [...this.oldValues, value]  );
    })

 <ngx-charts-pie-chart
        [scheme]="colors"
        [results]="dataDemo$ | async"
        [labels]="true">
 </ngx-charts-pie-chart>

What is the motivation / use case for changing the behavior?

Please tell us about your environment:

WebStorm, Yarn v1.2.0

sandangel commented 7 years ago

you have to subscribe to the observable and add private cd: ChangeDetectorRef dependency in your component constructor. Then in the subscribe method when a change happen call this.cd.markForCheck().

martijnhiemstra commented 6 years ago

I have added the private cd: ChangeDetectorRef to the constructor and when I update the data for the pie/bar chart using this.cd.markForCheck() then it still doesn't work!