无法使用来自 http get 的数据填充 Chartist (Angular 4)
Unable to populate Chartist with data from http get (Angular 4)
我正在尝试通过 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
]
};
}
您正在尝试在承诺之前构建图表数据 resolved.Since 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
]
};
};
我正在尝试通过 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
]
};
}
您正在尝试在承诺之前构建图表数据 resolved.Since 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
]
};
};