gpt4 book ai didi

angularjs - 如何定义 chart.js 时间刻度日期格式

转载 作者:行者123 更新时间:2023-12-02 15:12:28 24 4
gpt4 key购买 nike

我正在使用 chart.js,我想为我的数据设置自定义日期格式。我该怎么做?

例如,这是我的 x 轴日期配置:

 scales: {
xAxes: [{
type: 'time',
position: 'bottom',
time: {
displayFormats: {
**'day': 'Y M D',
'week': 'Y M D',
'month': 'Y M D',
'year': 'Y M D',**
}
},

如何控制工具提示和轴刻度标签格式。

最佳答案

如果您尝试格式化数据 tbat 出现在点悬停工具提示中,那么在使用时间刻度时,您可以使用 tooltipFormat属性设置用于工具提示的 moment js 格式字符串。

scales: {
xAxes: [{
type: 'time',
position: 'bottom',
time: {
tooltipFormat: "MM-DD-YYYY",
},
}],
}

但是,如果您尝试设置 x 轴刻度标签的格式,使其只显示日期的日部分,那么您可以使用 unit属性(property)。

scales: {
xAxes: [{
type: 'time',
position: 'bottom',
time: {
unit: "day",
},
}],
}

以上示例使用为 day 定义的默认显示格式,即“ll”(例如,Sep 4 1986)。如果您想将此格式更改为其他格式(例如 09/04/1986),则可以像这样重新定义 day 显示格式。

scales: {
xAxes: [{
type: 'time',
position: 'bottom',
time: {
unit: "day",
displayFormats: {
day: "l",
},
},
}],
}

关于angularjs - 如何定义 chart.js 时间刻度日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42781645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com