我似乎不知道如何让我的图表发挥作用。我已经准备好了数据,但是每次添加标签(例如(labelDate))时,它只会在记录时给我一个 undefined variable 。
ngOnInit(): void {
let labelDate;
this._estimateService.getEstimates()
.subscribe(estimateData => {
const key = 'data';
const groupedEstimates = groupBy(estimateData[key], 'estimate_date');
// console.log(groupedEstimateData);
const groupedEstimatesFiltered = omit(groupedEstimates, 'null');
Object.entries(groupedEstimatesFiltered)
.map(([date, estimatedData]) => {
labelDate = moment(date).format('DD MMM'); // Change it here
console.log(labelDate, estimatedData.length + ' - this is the first log');
});
});
if (!this.data) {
const color = Chart.helpers.color;
this.data = {
labels: [labelDate], // Doesnt work - logs as undefined
datasets: [
{
data: [estimateData.length] // Same as above
},
数据集日志是什么样的:
最佳答案
您的 if block 可能会在订阅的 next
完成之前执行。为了避免这种情况,你可以调用 observable 的 toPromise
将其更改为 Promise 并等待它,或者将 if block 移动到订阅的 next
底部,这样你就可以确保你不会在那里举行比赛。
关于javascript - 在 Chart.js 中使用准备好的数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59751252/