gpt4 book ai didi

javascript - 在 v2 上的 chart.js 中的图表上绘制水平线

转载 作者:可可西里 更新时间:2023-11-01 01:47:07 24 4
gpt4 key购买 nike

我使用 chart.js 绘制了折线图。对于标签和数据集,我从数据库中获取值。我是 chart.js 及其非常强大的库的新手,但我无法完全理解它。我想画多条水平线。就像数据集的平均值、标准差以及最小值和最大值在哪里。我已经在 stackoverflow 中尝试过这个问题,但这些都给出了错误,或者可能是我无法理解工作原理。这是我的 chart.js 代码

function display_graph(id, label, data) {
var ctx = document.getElementById(id);
var data = {
labels: data.labels,
datasets: [
{
label: label,
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderWidth: 1,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: data.assay_value,
spanGaps: false
}
]
};

//options
var options = {
responsive: true,
title: {
display: true,
position: "top",
text: label,
fontSize: 18,
fontColor: "#111"
},
legend: {
display: true,
position: "bottom",
labels: {
fontColor: "#333",
fontSize: 16
}
}
};
var Blanks_Chart=null;
Blanks_Chart = new Chart(ctx, {
type: 'line',
data: data,
options: options
});}

最佳答案

您可以使用 chart.js annotation plugin轻松地在图表上绘制线条,而不必手动处理 Canvas 中的渲染像素(旧方法会给您带来错误)。请注意,该插件由与 chart.js 相同的团队创建/支持,并在 chart.js docs 中提到。 .

这是一个 example codepen演示如何在图表上创建线条。

添加插件后,您只需在图表配置中设置 annotation 属性即可。这是一个例子。

var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["January", "February"],
datasets: [{
label: 'Dataset 1',
borderColor: window.chartColors.blue,
borderWidth: 2,
fill: false,
data: [2, 10]
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js Draw Line On Chart'
},
tooltips: {
mode: 'index',
intersect: true
},
annotation: {
annotations: [{
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: 5,
borderColor: 'rgb(75, 192, 192)',
borderWidth: 4,
label: {
enabled: false,
content: 'Test label'
}
}]
}
}
});

关于javascript - 在 v2 上的 chart.js 中的图表上绘制水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42691873/

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