Chart.js 中具有线性时间刻度的折线图
Line graph with linear timescale in Chart.js
我正在尝试使用 Chart.js 3.3.2 to display some a line graph with an equally spaced x date axis. Like the example they give here。
我无法使用此示例的简单版本(请参阅下面的代码段),因为它输出错误:
Error: This method is not implemented: Check that a complete date adapter is provided.
我没有尝试实现整个示例,因为它依赖于外部定义的函数和值。
我很久以前问过一个类似的问题 (),但重新阅读了那里的答案,这里对我没有帮助(值得注意的是,这里的大多数答案也集中在 Chart.js 2 而不是3).
非常感谢这里的任何帮助(我发现这里的文档很难理解)。
const data = {
labels: [
new Date(86400000), // Day 1
new Date(2*86400000), // Day 2
new Date(3*86400000), // Day 3
new Date(4*86400000), // Day 4
new Date(6*86400000), // Day 6
new Date(7*86400000), // Day 7
new Date(13*86400000) // Day 13
],
datasets: [{
label: 'My First dataset',
data: [1,3,4,5,6,7,8]
}]
};
const config = {
type: 'line',
data: data,
options: {
scales: {
x: {
type: 'time'
}
}
}
};
let ctx = document.querySelector("canvas").getContext("2d");
let chart = new Chart(ctx,config);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.2/chart.min.js"></script>
</head>
<body>
<canvas></canvas>
<script src="script.js"></script>
</body>
</html>
您需要添加 3.x 迁移指南中提供的日期适配器
(在页面中搜索“可用适配器”)
https://www.chartjs.org/docs/latest/getting-started/v3-migration.html
这是一个工作示例
const data = {
labels: [
new Date(86400000), // Day 1
new Date(2 * 86400000), // Day 2
new Date(3 * 86400000), // Day 3
new Date(4 * 86400000), // Day 4
new Date(6 * 86400000), // Day 6
new Date(7 * 86400000), // Day 7
new Date(13 * 86400000), // Day 13
],
datasets: [
{
label: "My First dataset",
data: [1, 3, 4, 5, 6, 7, 8],
},
],
};
let ctx = document.querySelector("canvas").getContext("2d");
let chart = new Chart(ctx, {
type: "line",
data: data,
options: {
scales: {
x: {
type: "time",
}
},
},
});
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.27.0"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@0.1.1"></script>
<title>repl.it</title>
</head>
<body>
<canvas></canvas>
<script src="new.ts"></script>
</body>
</html>
我正在尝试使用 Chart.js 3.3.2 to display some a line graph with an equally spaced x date axis. Like the example they give here。
我无法使用此示例的简单版本(请参阅下面的代码段),因为它输出错误:
Error: This method is not implemented: Check that a complete date adapter is provided.
我没有尝试实现整个示例,因为它依赖于外部定义的函数和值。
我很久以前问过一个类似的问题 (
非常感谢这里的任何帮助(我发现这里的文档很难理解)。
const data = {
labels: [
new Date(86400000), // Day 1
new Date(2*86400000), // Day 2
new Date(3*86400000), // Day 3
new Date(4*86400000), // Day 4
new Date(6*86400000), // Day 6
new Date(7*86400000), // Day 7
new Date(13*86400000) // Day 13
],
datasets: [{
label: 'My First dataset',
data: [1,3,4,5,6,7,8]
}]
};
const config = {
type: 'line',
data: data,
options: {
scales: {
x: {
type: 'time'
}
}
}
};
let ctx = document.querySelector("canvas").getContext("2d");
let chart = new Chart(ctx,config);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.2/chart.min.js"></script>
</head>
<body>
<canvas></canvas>
<script src="script.js"></script>
</body>
</html>
您需要添加 3.x 迁移指南中提供的日期适配器
(在页面中搜索“可用适配器”)
https://www.chartjs.org/docs/latest/getting-started/v3-migration.html
这是一个工作示例
const data = {
labels: [
new Date(86400000), // Day 1
new Date(2 * 86400000), // Day 2
new Date(3 * 86400000), // Day 3
new Date(4 * 86400000), // Day 4
new Date(6 * 86400000), // Day 6
new Date(7 * 86400000), // Day 7
new Date(13 * 86400000), // Day 13
],
datasets: [
{
label: "My First dataset",
data: [1, 3, 4, 5, 6, 7, 8],
},
],
};
let ctx = document.querySelector("canvas").getContext("2d");
let chart = new Chart(ctx, {
type: "line",
data: data,
options: {
scales: {
x: {
type: "time",
}
},
},
});
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.27.0"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@0.1.1"></script>
<title>repl.it</title>
</head>
<body>
<canvas></canvas>
<script src="new.ts"></script>
</body>
</html>