x 轴中的日期显示时间和时区。我只想显示月份和日期。
function dateSeries(startDate, endDate) {
let dates = [];
let currentDate = new Date(startDate);
while (currentDate <= endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
return dates;
};
let ctx = document.getElementById('myChart');
let goal = [5,4,3,2,1,0];
let dates = dateSeries(new Date('2024-12-03'), new Date('2024-12-08'));
let myChart = new Chart(ctx, {
type: 'line',
data: {
labels: dates,
datasets: [
{
label: 'Goal',
data: goal,
borderColor: 'rgba(142, 180, 227, 1)',
}
]
},
options: {
scales: {
x: {
type: 'time',
time: {
unit: 'day',
displayFormats: {
day: 'MMM DD'
}
}
}
}
}
});
它呈现为:
我觉得我的“选项”代码看起来像我找到的所有示例,但我无法正确设置轴格式。
您可以使用它,而不是(“fa-IR”)使用您的当地时间作为格式时间
day: (t) => new Date(t).toLocaleDateString('fa-IR').slice(5,10)