1

我正在使用 ng2-charts v1.5.0,我正在尝试让图表在单击后使用新数据进行更新。我正在关注其他人发布的示例。

在我看来,我有:

<div style="display: block">

  <canvas #chart baseChart
              [data]="dChartData"
              [labels]="dChartLabels"
              [chartType]="dChartType"
              (chartClick)="chartClicked($event)">
  </canvas>

</div>

如果我写:

@ViewChild( BaseChartDirective ) chart: BaseChartDirective;

然后我得到:

__zone_symbol__error : Error: Can't construct a query for the property "chart" of "ChartTest1WidgetComponent" since the query selector wasn't defined.

如果我写:

@ViewChild( 'chart' ) chart: BaseChartDirective;

然后我打电话this.chart.ngOnChanges({});,但我得到:_this.chart.ngOnChanges is not a function

这对我来说没有意义。如何获得作为参考的正确视图子项BaseChartDirective

4

1 回答 1

1

这对我有用: @ViewChild(BaseChartDirective) private Chart: BaseChartDirective; 然后this.Chart.ngOnChanges({} as SimpleChanges).

但是,更简单的方法是用新数据实际更新变量 dChartData,它会自动用新数据更新您的图表。它比使用 ngOnChanges 更快。希望这可以帮助!

于 2017-02-07T00:17:47.423 回答