Chartjs时间戳到日期时间标签



我使用时间戳格式创建散点图(日期时间不适用于散点图(。我需要帮助将X轴上的标签从时间戳更改为日期时间,以便用户可读。只是轴标签,而不是插入到图表中的实际时间戳值。

let ctx = (<HTMLCanvasElement>(
document.getElementById("measurementChart")
)).getContext("2d");
this.myChart = new Chart(ctx, {
type: "scatter",
data: {
datasets: [
{
label: name,
backgroundColor: "black",
borderColor: "blue",
showLine: true,
data: d,
}
]
},
options: {
maintainAspectRatio: false,
responsive: true,
tooltips: {
mode: "interpolate", //interpolate
enabled: true,
intersect: false,
backgroundColor: "black",
},
scales: {
yAxes: [
{
display: true,
scaleLabel: {
display: true,
},
}
],
xAxes: [
{
display: true,
scaleLabel: {
display: false,
labelString: "Date"
},
}
]
},
legend: {
reverse: true,
display: true,
},
}
});

您可以使用tick回调,如下所述:https://www.chartjs.org/docs/2.9.4/axes/labelling.html#creating-自定义刻度格式

scales: {
yAxes: [
{
ticks:{
callback: (val) => (new Date(val)) // or a different way to convert timestamp to date
},
display: true,
scaleLabel: {
display: true,
},
}
],
xAxes: [
{
ticks:{
callback: (val) => (new Date(val)) // or a different way to convert timestamp to date
}
display: true,
scaleLabel: {
display: false,
labelString: "Date"
},
}
]
}

相关内容

  • 没有找到相关文章

最新更新