我目前有一个组件,我将在一个页面上显示多个图形,每个图形来自一个单独的 json 文件。我正在使用 ng2-charts 和 angular2,我不确定如何根据 json 数据加载我的图形,以及在一个组件中设置多个图形数据的最佳方法是什么。
这是我的 get 调用,它在我的组件文件中返回一个对象:
dataType: any=[];
getData(){
this.dataService.getDataType().subscribe((response) => {this.dataType = response;
for(let item of this.dataType){
this.barChartLabels.push(this.dataType.name);
}
console.log(this.itemNames);
});
}
这是我在组件文件中加载图表的代码:
public barChartOptions: any = {
scaleShowVerticalLines: false,
responsive: true
};
public barChartLabels: any =[]; //LOAD from dataType.label
public barChartType: string = 'horizontalBar';
public barChartLegend: boolean = true;
public barChartData: any[] = //LOAD from dataType.data
示例 json 数据:
[
{
"id": 1,
"name": "Test 1",
"display": "Test 1",
"score": 45
}, ...
]
我的 html - 使用 ng2-charts:
<canvas baseChart [datasets]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChartType" (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)">
</canvas>
目前 - 我能够在控制台中看到我有一个标签数组,但由于某种原因,即使我已将返回的标签推送到barChartLabels
html 中使用的数组中,它们也没有显示在图表上。