ng2-charts: Cannot read property 'data' of undefined
After updating from 1.2 to 1.3.
This line of code
this.chart.data.datasets = this.getDatasets();
gives me error when I show chart:
Cannot read property ‘data’ of undefined
Tring to reproduce the issue in plunker, got no luck so far…
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 25 (3 by maintainers)
Commits related to this issue
- Workaround issue with Cannot read property data of undefined Fixes issue with: ``` error_handler.js:60 TypeError: Cannot read property 'data' of undefined at BaseChartDirective.updateChartDa... — committed to cody-c-lewis/ng2-charts by cody-c-lewis 7 years ago
- Prevent this error when updating dataset: Cannot read property 'data' of undefined Fixes #424 — committed to guaycuru/ng2-charts by guaycuru 6 years ago
- Prevent this error when updating dataset: Cannot read property 'data' of undefined Fixes #424 — committed to guaycuru/ng2-charts by guaycuru 6 years ago
- DEV: try and fix errors when updating some ng2-charts graphs See https://github.com/valor-software/ng2-charts/issues/424 — committed to Zero-to-Heroes/firestone by sebastientromp 3 years ago
+1 i run in the same issue.
I’m using rxjs observables with angular2 and even if i provide some default empty data fot chart.data i’m still facing errors.
code very basic:
Error: Cannot read property ‘data’ of undefined
UPDATE: okey i just solved it with technic of using *ngIf. I hide chart component till i get data and then show it.
Ok, I got some time to look into this.
The issue can be reproduced by adding a
setTimeout.The issue is because of lack of
[{ data: [] }];in the beginning.In 1.2, it works this way in a child component.
Then you can provide data later.
But 1.3, that way does not work any more. Since I am using ngrx/store, so I changed the way to give that default data in reducer:
At least, it works now, I will close now.
@blubberbo I am using ngrx/store, so I added it in the
initialState, which makes suredatasetsinitial value is[{ data: [] }]before getting data from server.I’m also having this issue, although not with using initial data. When updating the chart, data including label count, I’m using ViewChild / chart.ngOnChanges(). issue ref here: #692
This works when going from a set of series to higher count, but not the other way, it throws a data undefined error.
specifically here:
(current “datasets” array count differ from newDataValues)
I’m using ng2-charts 1.6.0 and chart.js 2.5.0
Any ideas of a workaround to this?
Hello! I was having the same problem (I guess). Dataset is refreshed from
ngOnChangesmethod. Dataset can be filtered and after reducing the number of data the errorCannot read property 'data' of undefinedwas thrown.I came to the @szykov solution
*ngif..as wellHowever that wasn’t enough for me. What I did as a workaround to properly refresh the chart is clear the barCharData and wait a tick to refresh dataset:
@blubberbo u need at least provide
[{ data: [] }], like thisWidget.chart.data = [{ data: [] }]@nponna Unfortunately I don’t have any solid workaround, although I did modify updateChartData() in ng2-charts.js
from
to
this seems to work without any errors and side effects, (so far…)
I have no experience in extending and modifying libraries, appreciate if anyone could guide me how to override this function so it builds with ng2-charts? Is it possible to implement this fix project-only wise without forking ng2-charts?
@valorkin Are you guys planning to handle dynamic labels and dynamic sizes of datasets anytime in the future for ng2-charts?
@szykov great idea! молодец