gpt4 book ai didi

chart.js - chartjs - 在整数 x 轴值上绘制垂直线

转载 作者:行者123 更新时间:2023-12-01 11:17:59 24 4
gpt4 key购买 nike

在下面的示例中,chartjs 注释适用于字符串值 ( "MAR" ),但不适用于整数值。如何在一些整数 x 轴值上绘制一条垂直线。

var chartData = {
labels: ["JAN", "FEB", "MAR"],
datasets: [
{
data: [12, 3, 2]
}
]
};

window.onload = function() {
var ctx = document.getElementById("canvas").getContext("2d");
new Chart(ctx, {
type: "line",
data: chartData,
options: {
annotation: {
annotations: [
{
type: "line",
mode: "vertical",
scaleID: "x-axis-0",
value: 2,
borderColor: "red",
label: {
content: "TODAY",
enabled: true,
position: "top"
}
}
]
}
}
});
};

见 fiddle : https://codepen.io/anon/pen/QaQWba

最佳答案

您可以通过将数据传递为 points 来实现您的目标,配置xAxeslinearcreating a custom tick format :

数据:

var chartData = {
labels: ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"],
datasets: [
{
data: [{x: 1, y: 12}, {x: 2, y: 3}, {x: 3, y: 2}, {x: 4, y: 1}, {x: 5, y: 8}, {x: 6, y: 8}, {x: 7, y: 2}, {x: 8, y: 2}, {x: 9, y: 3}, {x: 10, y: 5}, {x: 11, y: 11}, {x: 12, y: 1}];
}
]
};

xAxes 配置:
xAxes: [{
type: 'linear',
position: 'bottom',
ticks: {
max: 12,
min: 1,
stepSize: 1,
callback: function(value, index, values) {
return chartData.labels[index];
}
}
}]

检查更新的 CodePen: https://codepen.io/beaver71/pen/XVZXOM

关于chart.js - chartjs - 在整数 x 轴值上绘制垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48161695/

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