如何更新Angular 2和Node JS中的ng2-chartjs2图表中的数据

我正在使用NodeJS,Angular2和ng2-chartjs2。 下面我列出了我的代码渲染图表的相关部分。 数据从使用固定date范围的API加载到this.data中。 我想让用户select一个date范围,然后更新图表。 从这里我知道你可以调用图表对象的update()来更新数据,但是我不知道如何获取图表对象,因为组件代码实际上并没有引用它 -当模板被渲染时,它是自动完成的。 查看源代码 (第13行),我看到作者打算让对象可用。 我联系了作者,但还没有收到答复,需要搬家。 我已经学到了很多关于Angular2的知识,但是我还没有专家,所以对Angular2的更深入的理解使得这一点变得很明显。 我怎样才能访问对象来调用它的update(),或者做一些其他干净的方式?

该模板包含

<chart [options]="simple.options"></chart> 

和组件打印脚本代码包含

 import { ChartComponent } from 'ng2-chartjs2'; ... @Component({ selector: 'home', templateUrl: 'client/components/home/home.component.html', styleUrls: ['client/components/home/home.component.css'], directives: [DashboardLayoutComponent, CORE_DIRECTIVES, ChartComponent], pipes: [AddCommasPipe], }) ... setCurrentSimpleChart = (simpleType: number): void => { this.simple.options = { type: 'line', options: this.globalOptions, data: { labels: this.data[simpleType].labels, datasets: [{ label: this.titles[simpleType], data: this.data[simpleType].data, backgroundColor: 'rgba(255, 99, 132, 0.2)', borderColor: 'rgba(255,99,132,1)', borderWidth: 1 }], }, }; ... } 

更新:如果这有助于任何人:我实际上有两个不同的图表在页面上,所以我根据接受的答案search,并findViewChildren,并映射到不同的variables,所以我可以分别更新他们

 [this.simpleChart, this.liftChart] = this.chartComponents.toArray().map(component => component.chart); 

(还要注意,这是使用rc的angular2 – 因为指令等已经被移出了组件本身。)

您可以通过使用ViewChild保持对组件的ViewChild

 @ViewChild(ChartComponent) chartComp; 

然后你可以得到chart对象:

 let chart = this.chartComp.chart; 

这是相应的重击者