作者:权利阳成_278 | 来源:互联网 | 2023-01-02 11:50
在下面的示例中,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"
}
}
]
}
}
});
};
参见小提琴:https : //codepen.io/anon/pen/QaQWba
1> beaver..:
您可以实现将数据作为点传递,配置xAxes
为线性并创建自定义刻度格式的目标:
数据:
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