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;
  }

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

我在这里缺少什么?

4

1 回答 1

3

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

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
}
于 2019-11-27T19:04:11.013 回答