Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Charts initialized with an empty dataset cause errors on change detection #1265

Closed
danmana opened this issue Sep 16, 2020 · 2 comments
Closed

Comments

@danmana
Copy link
Contributor

danmana commented Sep 16, 2020

Reproduction of the problem

  1. Initialize a chart with datasets=[] empty array (ex: when the user has to select the datasets and there are none selected by default)
  2. Trigger change detection (ex: hover over chart)

Result: Errors show up in the console.

ERROR TypeError: Cannot set property 'data' of undefined
    at BaseChartDirective.propagateDataToDatasets (valor-software-ng2-charts.umd.js? [sm]:1138)
    at BaseChartDirective.ngDoCheck (valor-software-ng2-charts.umd.js? [sm]:652)

If later a dataset is added, the errors go away.
And if we remove again all datasets the errors don't come back. Only when the chart is initialized with an empty datasets array.

Demo: https://stackblitz.com/edit/angular-ivy-c8oztr?file=src%2Fapp%2Fapp.component.ts

The problem is caused by this code from propagateDataToDatasets, which assumes that if datasets is defined, there is at least one dataset (see else block).

      if (!this.datasets) {
        this.datasets = [{ data: newDataValues }];
        if (this.chart) {
          this.chart.data.datasets = this.datasets;
        }
      } else {
        this.datasets[0].data = newDataValues;
        this.datasets.splice(1); // Remove all elements but the first
      }
santam85 added a commit that referenced this issue Sep 18, 2020
@paviad
Copy link
Contributor

paviad commented Sep 23, 2020

Fix incorporated into version 2.4.2

@paviad paviad closed this as completed Sep 23, 2020
@hudzenko
Copy link

Hi there, we experience now weird behaviour when [datasets]="[]". baseChart code somehow replaces objects and inputs in just totally other part of applications. Mostly it somehow replaces angular Inputs with the chart related object for totally different components of our app

This is super weird..

santam85 added a commit that referenced this issue Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants