问题可能是由于加载JSON文件的方式不正确导致的。以下是一种可能的解决方法:
npm install ng2-charts chart.js --save
ChartsModule
和HttpClient
:import { Component, OnInit } from '@angular/core';
import { ChartsModule } from 'ng2-charts';
import { HttpClient } from '@angular/common/http';
data: any[] = [];
ngOnInit
生命周期钩子中,使用HttpClient
服务加载JSON文件:constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get('path/to/your/json/file.json').subscribe((res: any[]) => {
this.data = res;
});
}
ng2-charts
的堆叠柱状图组件来显示数据:
请确保将path/to/your/json/file.json
替换为你实际的JSON文件路径。另外,你也可以根据需要自定义堆叠柱状图的其他选项。
希望这个解决方法能帮助到你!