用户从 mat datepicker 添加日期并存储在 firestore 中(例如 2023 年 10 月 15 日,时间戳类型)。这应该以 15/10/2023 的格式显示在图表的 x 轴上。我用过
const dateLabels = this.data.map((item) => item.weightDate).sort();
const monthLabels = dateLabels.map(a=>this.datePipe.transform(a.toDate(),
'dd/MM/YYYY'));
this.chart = new Chart('MyChart', {
type: 'line',
data: {
labels: monthLabels,
datasets: [
{
label: 'Weight',
data: weightLabels,
backgroundColor: 'blue',
},
],
},
options: {
aspectRatio: 2.5,
},
出现错误 TypeError:无法读取 null 的属性(读取“toDate”)
在您的代码中使用它:
import { DateAdapter } from '@angular/material/core';
data.dateAdapter.setLocale('en-GB');