我正在尝试通过api调用加载图表数据,虽然数据已返回但未加载到图表系列中。
// Initialize data series
seriesData: any[] = [];
// Function to retrieve data from api
getSeriesData() {
this.uid.getSeriesData(this.auth.getCurrentUser()).then(
data => this.seriesData = data, // This is populated
err => console.log(err)
);
}
//ngInit
ngOnInit() {
this.getSeriesData();
// Chartist
const dataDailySalesChart: any = {
labels: ['M', 'T', 'W', 'T', 'F', 'S', 'S'],
series: [
this.seriesData // THIS IS ALWAYS EMPTY
]
};
}
答案 0 :(得分:1)
你试图在解析promise之前构建图表数据。因为getSeriesData是异步的,你应该这样做,
getSeriesData() {
this.uid.getSeriesData(this.auth.getCurrentUser()).then(
data => this.seriesData = data, // This is populated
this.generateChart(this.seriesData),
err => console.log(err)
);
}
ngOnInit() {
this.getSeriesData();
}
generateChart(chartData:any){
const dataDailySalesChart: any = {
labels: ['M', 'T', 'W', 'T', 'F', 'S', 'S'],
series: [
chartData
]
};
};