Angular 7-动态HighChart更新

问题描述 投票:0回答:1

在我的角度解决方案中,我需要使用Highcharts显示一些图表。在我的component.ts中,我有以下代码:

import * as Highcharts from "highcharts";

highcharts = Highcharts;
  chartOptions = {
    chart: {
       type: "spline",
       title: "Reach/TRP"
    },
    xAxis: {
      title: {
        text: this.xAxis.name
      }
    },
    yAxis: {
       title: {
          text: "Reach"
       }
    },
    series: [
       {
          name: this.xAxis.name,
          data: this.xAxis.data
       }
    ]
 };

然后在我的.html中,我像这样使用它:

<highcharts-chart
  [Highcharts] = "highcharts"
  [options] = "chartOptions"
  style="width: 100%; height: 400px; display: block;">
</highcharts-chart>

在我的html上,我还有以下下拉菜单:

<p-dropdown [showTransitionOptions]="'0ms'" [hideTransitionOptions]="'0ms'"
  dropdownIcon="fa fa-angle-down" class= "nextgen-dropdown" 
  [options]="optionsLookup"
  [(ngModel)]="selectedOption" (onChange)= "onOptionChange($event)">
</p-dropdown>

具有"OnOptionChange"方法的应该像这样更新Chart的方法:

 onOptionChange() {
    this.xAxis.name = this.selectedOption;
    this.xAxis.data = [100, 100, 200, 200, 350, 250];
    this.chartOptions.series[0] = this.xAxis;
  }

这里的问题是方法被正确调用,但是图表根本没有更新。

我在这里想念的是什么?

angular typescript highcharts angular-highcharts
1个回答
0
投票

您应该传递新的选项参考:

onOptionChange() {
  this.xAxis.name = this.selectedOption;
  this.xAxis.data = [100, 100, 200, 200, 350, 250];
  this.chartOptions.series[0] = this.xAxis;

  this.chartOptions = { ...this.chartOptions }; // this should do the trick
}
© www.soinside.com 2019 - 2024. All rights reserved.