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

Chart not updating Angular15, Chart4.1 #63635

Open
vze23c3q opened this issue Dec 15, 2022 · 1 comment
Open

Chart not updating Angular15, Chart4.1 #63635

vze23c3q opened this issue Dec 15, 2022 · 1 comment

Comments

@vze23c3q
Copy link

"@angular/core": "^15.0.0",
"chart.js": "^4.0.1",
"ng2-charts": "^4.1.1"

Replacing data in lineChartData does not refresh chart.

  public lineChartData: ChartConfiguration<'line'>['data'] = {
    labels: [],
    datasets: []
  };

  public lineChartOptions: ChartOptions<'line'> = {
    responsive: true
  };

  public lineChartLegend = true;

It correctly display data the first time, but not subsequent. Logging labels and data show they are updated.

  @Input()
  public set performanceResults(value: PortfolioPerformance[]) {
    this._performanceResults = value;
    this.setupChartData();
  }

  setupChartData() {
    this.removeData();

    let labels = this.generateLabels();
    labels.forEach(element => {
      this.lineChartData.labels.push(element);
    });

    let data = this.generateDatesets();
    data.forEach(element => {
      this.lineChartData.datasets.push(element);
    });
  }

<div style="display: block">
  <canvas
    baseChart
    width="800"
    height="400"
    [type]="'line'"
    [data]="lineChartData"
    [options]="lineChartOptions"
    [legend]="lineChartLegend"
  >
  </canvas>
</div>

I was hoping lineChartData.update() would fix but that does not exist.

Any help would be much appreciated!

@vze23c3q
Copy link
Author

Just fixed it by reinitializing lineChartData

  setupChartData() {
    this.lineChartData = {
      labels: [],
      datasets: []
    };

    let labels = this.generateLabels();
    labels.forEach(element => {
      this.lineChartData.labels.push(element);
    });

    let data = this.generateDatesets();
    data.forEach(element => {
      this.lineChartData.datasets.push(element);
    });
  }

If this is correct, please close.

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

1 participant