我正在创建一个从变量public pieChartData:number[] = [300, this.Goal]
生成的饼图。我试图在构造函数中使用对象数组上的for
循环来获取正确的数据
data = [{
Id: 1,
YTDGoal: 125000
}]
但是当传入pieChartData
时,this.Goal
未定义。
我该如何解决这个问题?
我创建了一个Stackblitz for this issue。
答案 0 :(得分:1)
试试这个。
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
data = [{
Id: 1,
YTDGoal: 125000
}]
Goal;
public pieChartLabels:string[] = ['Download Sales', 'YTD Goal'];
public pieChartData:number[] = [300, this.Goal];
public pieChartType:string = 'pie';
public pieChartCustomColors = [{
backgroundColor: ['blue', 'red']
}]
constructor() {
for (let i = 0; i < this.data.length; i++) {
this.Goal = this.data[i].YTDGoal
}
this. pieChartData = [300, this.Goal];
}
public chartClicked(e:any):void {
console.log(e);
}
public chartHovered(e:any):void {
console.log(e);
}
}