Ng2-charts: Data stacks when updating datasets

Created on 15 Jun 2018  路  6Comments  路  Source: valor-software/ng2-charts

I try to display data in accordance with selected option, but chart works incorrect on hover and I get the folowing behaviour.

peek 2018-06-15 13-06

I think the reason is in binding chart data to properties.

            <canvas baseChart class="chart"
                    [datasets]="mainChart.mainChartData"
                    [labels]="mainChart.xAxisLabels"
                    [options]="mainChart.chartOptions"
                    [colors]="mainChart.chartColors"
                    [legend]="mainChart.chartLegend"
                    [chartType]="mainChart.chartType">
            </canvas>

I simply update this data after select another option. Seems like the current data stacks somehow. I tried to refresh my chart

@ViewChild(BaseChartDirective) private chart: BaseChartDirective;

via ngOnInit and ngOnDestroy and so on, but I didn't get expected behaviour. How can I resolve this?

Angular version: 4.3.4
ng2-charts: 1.6.0

Most helpful comment

Hi @leshanative,
I resolved this issue by wrapping whole <canvas> into div and then i was showing this <div by using *ngIf when the data was ready

All 6 comments

Hi @leshanative,
I resolved this issue by wrapping whole <canvas> into div and then i was showing this <div by using *ngIf when the data was ready

@Joklogh I don't know how but it works so damn perfectly. Thanks a lot!

@leshanative you are welcome. I guess the ng-charts does not like to change data or dataset on already initialized graph, but when you reinitialize the graph using *ngIf then the data in graph will be updated.

Hi @Antonyselt
Here is my code.
https://stackblitz.com/edit/angular-2jqsrc
I found new solution which seems to be more fluid (it's in function updateData2).
Instead of hiding and showing graph you can create copy of old data, then add new data into this copy and at the end assign old data to be equal to copy.
If you encounter any error, please let me know, I hope this wil help you.

@Antonyselt
Did you also tried implementing second solution (updateData2)?

It appears this is resolved, if not, feel free to reopen and provide an example of the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

martinpinto picture martinpinto  路  3Comments

shenriksen4 picture shenriksen4  路  3Comments

grahammutter picture grahammutter  路  4Comments

mrpotato3 picture mrpotato3  路  5Comments

raychenfj picture raychenfj  路  3Comments