chart.js - chart.js 和时间戳作为可读日期
问题描述
我有一些带有时间戳和整数值的数据。现在我创建了一个散点图,因为我的时间戳不在同一频率,但值是在随机时间获取的。时间戳是 x 轴,值是 y 轴。现在我想将时间戳转换为可读的时间格式。所以我包括了时刻。但我就是无法让它显示我格式化的时间字符串。即使我更改代码,它也只是显示一些默认格式。这是如何运作的?标签在此处输入图像描述
这是我的代码:
<script src="open/moment.js"></script>
<script src="open/Chart.min.js"></script>
<canvas id="hum1" style="width:1000px; height:500px;"></canvas>
<script>
var x = new Chart(document.getElementById("hum1"), {
type: 'scatter',
data: {
datasets: [{
label: "Test",
pointStyle: 'line',
showLine: true,
fill: false,
borderColor: '#00ff00',
pointRadius: 0,
data: [],
},{
label: "Humidity 1",
showLine: true,
fill: true,
borderColor: '#0000FF',
pointRadius: 0,
data: [
{x:1588353429.6027,y:400},
{x:1588353430.6634,y:0},
{x:1588353431.7241,y:0},
{x:1588353432.7848,y:0},
{x:1588353433.8455,y:400},
{x:1588353434.9062,y:400},
{x:1588353435.9668,y:400},
{x:1588353437.0274,y:400},
{x:1588353438.0881,y:400},
{x:1588353439.1487,y:400},
{x:1588353440.2093,y:400},
{x:1588353441.27,y:400},
{x:1588353442.3307,y:400},
{x:1588353443.3929,y:400},
{x:1588353444.4537,y:400},
{x:1588353445.5142,y:400},
{x:1588353723.1543,y:415},
{x:1588353724.216,y:0},
{x:1588353725.2782,y:0},
{x:1588353726.3402,y:0},
{x:1588353727.402,y:400},
{x:1588353728.463,y:400},
{x:1588353793.2418,y:415},
{x:1588353794.304,y:0},
{x:1588353795.3658,y:0},
{x:1588353796.4265,y:0},
{x:1588353797.4882,y:400},
{x:1588353798.5495,y:400},
{x:1588353799.6102,y:400},
{x:1588353800.6715,y:400},
{x:1588353801.7327,y:405},
{x:1588353802.7933,y:400},
{x:1588353803.8564,y:405},
{x:1588353804.9154,y:405},
{x:1588353805.9779,y:405},
{x:1588353807.0411,y:411},
],
}
]
},
options: {
responsive: false,
legend: {
position: 'bottom',
labels: {
usePointStyle: true
}
},
scales: {
xAxes: [{
type: 'time',
unit: 'minute',
time:{
displayFormats: {
minute: 'HH:mm'
}
},
ticks: {
source: 'data',
autoSkip: true
},
display: true,
scaleLabel: {
display: true,
labelString: 'Timestamp'
}
}],
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
steps: 10,
stepValue: 5,
max: 36282.75,
min: 0
},
scaleLabel: {
display: true,
labelString: 'Humidity [%]'
}
}]
},
title: {
display: true,
text: 'Filament hub 1'
}
}
});
</script>
解决方案
主要问题是您的时间戳是 Unix 时间戳(从 开始的秒数1 January 1970 UTC
)。然而, JavaScriptDate
对象使用毫秒,因为1 January 1970 UTC
. data
因此,在分配给图表配置之前,您应该将时间戳乘以 1000 。
data.forEach((o) => o.x *= 1000);
我也稍微改变了xAxes
如下的定义:
xAxes: [{
type: 'time',
time: {
unit: 'minute',
displayFormats: {
minute: 'HH:mm'
},
tooltipFormat: 'HH:mm'
},
scaleLabel: {
labelString: 'Timestamp'
}
}],
请在下面查看您修改后的代码。
const data = [
{x:1588353429.6027,y:400},
{x:1588353430.6634,y:0},
{x:1588353431.7241,y:0},
{x:1588353432.7848,y:0},
{x:1588353433.8455,y:400},
{x:1588353434.9062,y:400},
{x:1588353435.9668,y:400},
{x:1588353437.0274,y:400},
{x:1588353438.0881,y:400},
{x:1588353439.1487,y:400},
{x:1588353440.2093,y:400},
{x:1588353441.27,y:400},
{x:1588353442.3307,y:400},
{x:1588353443.3929,y:400},
{x:1588353444.4537,y:400},
{x:1588353445.5142,y:400},
{x:1588353723.1543,y:415},
{x:1588353724.216,y:0},
{x:1588353725.2782,y:0},
{x:1588353726.3402,y:0},
{x:1588353727.402,y:400},
{x:1588353728.463,y:400},
{x:1588353793.2418,y:415},
{x:1588353794.304,y:0},
{x:1588353795.3658,y:0},
{x:1588353796.4265,y:0},
{x:1588353797.4882,y:400},
{x:1588353798.5495,y:400},
{x:1588353799.6102,y:400},
{x:1588353800.6715,y:400},
{x:1588353801.7327,y:405},
{x:1588353802.7933,y:400},
{x:1588353803.8564,y:405},
{x:1588353804.9154,y:405},
{x:1588353805.9779,y:405},
{x:1588353807.0411,y:411}
];
data.forEach((o) => o.x *= 1000);
new Chart(document.getElementById("hum1"), {
type: 'scatter',
data: {
datasets: [{
label: "Humidity 1",
pointStyle: 'line',
showLine: true,
fill: false,
borderColor: '#0000FF',
pointRadius: 0,
data: data
}]
},
options: {
responsive: false,
legend: {
position: 'bottom',
labels: {
usePointStyle: true
}
},
scales: {
xAxes: [{
type: 'time',
time: {
unit: 'minute',
displayFormats: {
minute: 'HH:mm'
},
tooltipFormat: 'HH:mm'
},
scaleLabel: {
display: true,
labelString: 'Timestamp'
}
}],
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
steps: 10,
stepValue: 5,
min: 0
},
scaleLabel: {
display: true,
labelString: 'Humidity [%]'
}
}]
},
title: {
display: true,
text: 'Filament hub 1'
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="hum1" style="width:600px; height:230px;"></canvas>
推荐阅读
- selenium-webdriver - 将产品添加到购物篮时,如何使用 selenium -java 显式等待购物篮的计数从零变为一
- java - 如何在非静态服务类中使用广播接收器
- react-native - react-native-svg-uri 不能在 svg 文件中应用翻译(地图中的 svg 作为反应原生的标记)
- javascript - Exit from function after resolving protractor promise
- vb.net - 如何保存从相机加载的 Picturebox 中的图像?
- python - 大约 6 小时后,序列号停止读取
- c - C中的stdout和stderr有什么区别?
- c# - 是否可以使用来自 TCP 套接字的套接字信息创建 P2P UDP 打孔器?
- javascript - 我收到此错误“'npx' 未被识别为内部或外部命令,”
- python - 在 Python/Gurobi 中向模型添加新约束