javascript - 如何在 Charts.js 3.x 中将“hh:mm:ss”字符串解析为时间
问题描述
我想使用 Charts.js 创建一个时间线。为此,我以小时:分钟:秒格式导入数据,例如“00:00:10”。
当使用 Charts.js 2.0 时,可以设置解析器,它允许将时间值作为字符串导入。
但升级到 Charts.js 3.2.1 后,解析器选项似乎不再起作用。
在文档中指出,仍然可以像以前一样使用解析器。
https://www.chartjs.org/docs/latest/axes/cartesian/time.html#parser
我发现了多个论坛帖子解释了如何执行此操作,但它们是在 Charts.js 2.x 中完成的,并且在我的示例中不再有效。
使用 Chart.js 从时间字符串中绘制单圈时间 Xaxes中的
Chartjs 时间
这是我的代码。
由于解析器的脚本失败,第一个示例无法正常工作。在第二个示例中,您可以看到完全相同的代码,但没有正在工作的解析器。
那么如何解析我的字符串数据,以便正确格式化并显示时间线?
非常感谢您的帮助。
var ctx = document.getElementById('chart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [
{
label: "Switched",
data: [{x:"00:00:07", y: 0}, {x:"00:00:14", y: 0}, {x:"00:05:24", y: 0}],
showLine: false,
fill: false,
tooltip: "Player switched",
borderColor: "#16a085",
backgroundColor: "#16a085"
}
]},
options: {
responsive: true,
scales: {
x: {
type: 'time',
time: {
parser: 'HH:mm:ss',
unit: "seconds",
tooltipFormat: 'HH:mm:ss',
displayFormats: {
'seconds': "HH:mm:ss"
},
unitStepSize: 30
}
},
y: {
min: -1,
max: 13,
stepSize: 1,
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.1/chart.min.js"></script>
<canvas id="chart" height= "80px" style="background-color: rgb(50, 50, 50);"></canvas>
var ctx = document.getElementById('chart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [
{
label: "Switched",
data: [{x:"00:00:07", y: 0}, {x:"00:00:14", y: 0}, {x:"00:05:24", y: 0}],
showLine: false,
fill: false,
tooltip: "Player switched",
borderColor: "#16a085",
backgroundColor: "#16a085"
}
]},
options: {
responsive: true,
scales: {
x: {},
y: {
min: -1,
max: 13,
stepSize: 1,
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.1/chart.min.js"></script>
<canvas id="chart" height= "80px" style="background-color: rgb(50, 50, 50);"></canvas>
解决方案
在 chart.js v3 中,他们删除了 moment 和适配器,因此您需要将两者都添加到脚本中,它应该可以工作
例子:
var ctx = document.getElementById('chart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [
{
label: "Switched",
data: [{x:"00:00:07", y: 0}, {x:"00:00:14", y: 0}, {x:"00:05:24", y: 0}],
showLine: false,
fill: false,
tooltip: "Player switched",
borderColor: "#16a085",
backgroundColor: "#16a085"
}
]},
options: {
responsive: true,
scales: {
x: {
type: 'time',
time: {
parser: 'HH:mm:ss',
unit: "seconds",
tooltipFormat: 'HH:mm:ss',
displayFormats: {
'seconds': "HH:mm:ss"
},
unitStepSize: 30
}
},
y: {
min: -1,
max: 13,
stepSize: 1,
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@0.1.1"></script>
<canvas id="chart" height= "80px" style="background-color: rgb(50, 50, 50);"></canvas>
推荐阅读
- ms-access - 错误:您的数据库或项目包含对文件 UTILITY.ACCDA 的缺失或损坏的引用
- java - 如何读取包含两列数字的文件,然后返回每列的平均值?
- android - 为什么使用android改造时没有响应,虽然我可以看到请求?
- node.js - 来自浏览器 Angular 5 控制台中的服务器的消息
- fiware - FiwareLab - accumulator-server.py ImportError:没有名为 OpenSSL 的模块
- matlab - matlab中随机均匀变量的期望值
- matlab - 如何在 Simulink 中以编程方式返回
- javascript - javascript:将Unix转换为长日期格式的最短方法是什么
- javascript - 在javascript中自动跳转,有可能吗?
- java - 应用程序未运行时的 Firebase 侦听器