我正在使用 ng2-charts - https://github.com/valor-software/ng2-charts
我有一个饼图,如果我在我的 component.ts 文件顶部声明变量时对数据进行硬编码,就像我的饼图显示的示例一样。
但我显然想让饼图数据动态化。我可以通过一个服务(是一个数字)调用数据,将数字添加到数据数组中,饼图不起作用。但是,如果我执行控制台日志,则数组会打印出我添加到其中的新数据/数字。
我需要以某种方式重新绘制表格。想不通怎么弄。
public pieChartLabels:string[] = ['Red Flags','Green Flags'];
public pieChartData: number[] = [200, 400];
public chartType:string = 'pie';
public redFlagsTotal: any;
public greenFlagsTotal: any;
constructor(private dataService:flagService) {
let component = this;
this.redFlagsTotal = this.dataService.getRedFlags().then(function(result){
component.redFlagsTotal = result.length;
console.log(component.redFlagsTotal);
component.pieChartData.push(component.redFlagsTotal);
console.log(component.pieChartData);
});
this.greenFlagsTotal = this.dataService.getGreenFlags().then(function(result){
component.greenFlagsTotal = result.length;
console.log(component.greenFlagsTotal);
component.pieChartData.push(component.greenFlagsTotal);
console.log(component.pieChartData);
});
}