ChartJS:在鼠标悬停时在图表上的数据点绘制垂直线
ChartJS: Draw vertical line at data point on chart on mouseover
我不知道如何在使用 Chart JS 将鼠标悬停在数据点上时在折线图上的数据点上绘制垂直线。我希望这条线保持在图表矩形的边界内,这样它就不会延伸到刻度区域。如有任何帮助,我们将不胜感激!
目前还没有这方面的原生功能,您必须创建一个图表插件才能实现。
ᴘʟᴜɢɪɴ(ᴅʀᴀᴅʀᴀᴠᴇʀᴛɪᴄᴀʟᴠᴇʀᴛɪᴄᴀʟᴏɴᴀᴛ-ᴅᴀᴛᴀ-ᴘᴏɪɴᴛ):
Chart.plugins.register({
afterDatasetsDraw: function(chart) {
if (chart.tooltip._active && chart.tooltip._active.length) {
var activePoint = chart.tooltip._active[0],
ctx = chart.ctx,
y_axis = chart.scales['y-axis-0'],
x = activePoint.tooltipPosition().x,
topY = y_axis.top,
bottomY = y_axis.bottom;
// draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 2;
ctx.strokeStyle = '#07C';
ctx.stroke();
ctx.restore();
}
}
});
* 在脚本的开头添加这个插件
ᴅᴇᴍᴏ⧩
Chart.plugins.register({
afterDatasetsDraw: function(chart) {
if (chart.tooltip._active && chart.tooltip._active.length) {
var activePoint = chart.tooltip._active[0],
ctx = chart.ctx,
y_axis = chart.scales['y-axis-0'],
x = activePoint.tooltipPosition().x,
topY = y_axis.top,
bottomY = y_axis.bottom;
// draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 2;
ctx.strokeStyle = '#07C';
ctx.stroke();
ctx.restore();
}
}
});
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
label: 'LINE',
data: [3, 1, 4, 2, 5],
backgroundColor: 'rgba(0, 119, 290, 0.2)',
borderColor: 'rgba(0, 119, 290, 0.6)',
fill: false
}]
},
options: {
responsive: false,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx" height="200"></canvas>
即使有一个公认的答案,我想我可能会贡献一个我写的专门解决这个问题的插件。
chartjs 线高标注插件
https://www.npmjs.com/package/chartjs-plugin-lineheight-annotation
有一些可导出的 类 可以帮助您在需要时计算到数据点的顶部。此外,还有一个简单的 API,您可以将其添加到图表的选项对象
/// default values
lineHeightAnnotation: {
// defaults to have line to the highest data point on every tick
always: true,
// optionally, only have line draw to the highest datapoint nearest the user's hover position
hover: false,
// colors of the line
color: '#000',
// name of yAxis
yAxis: 'y-axis-0',
// weight of the line
lineWeight: 1.5,
/// sets shadow for ALL lines on the canvas
shadow: {
// color of the shadow
color: 'rgba(0,0,0,0.35)',
// blur of the shadow
blur: 10,
/// shadow offset
offset: {
// x offset
x: 0,
// y offset
y: 3
}
},
// dash defaults at [10, 10]
noDash: true,
}
我不知道如何在使用 Chart JS 将鼠标悬停在数据点上时在折线图上的数据点上绘制垂直线。我希望这条线保持在图表矩形的边界内,这样它就不会延伸到刻度区域。如有任何帮助,我们将不胜感激!
目前还没有这方面的原生功能,您必须创建一个图表插件才能实现。
ᴘʟᴜɢɪɴ(ᴅʀᴀᴅʀᴀᴠᴇʀᴛɪᴄᴀʟᴠᴇʀᴛɪᴄᴀʟᴏɴᴀᴛ-ᴅᴀᴛᴀ-ᴘᴏɪɴᴛ):
Chart.plugins.register({
afterDatasetsDraw: function(chart) {
if (chart.tooltip._active && chart.tooltip._active.length) {
var activePoint = chart.tooltip._active[0],
ctx = chart.ctx,
y_axis = chart.scales['y-axis-0'],
x = activePoint.tooltipPosition().x,
topY = y_axis.top,
bottomY = y_axis.bottom;
// draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 2;
ctx.strokeStyle = '#07C';
ctx.stroke();
ctx.restore();
}
}
});
* 在脚本的开头添加这个插件
ᴅᴇᴍᴏ⧩
Chart.plugins.register({
afterDatasetsDraw: function(chart) {
if (chart.tooltip._active && chart.tooltip._active.length) {
var activePoint = chart.tooltip._active[0],
ctx = chart.ctx,
y_axis = chart.scales['y-axis-0'],
x = activePoint.tooltipPosition().x,
topY = y_axis.top,
bottomY = y_axis.bottom;
// draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 2;
ctx.strokeStyle = '#07C';
ctx.stroke();
ctx.restore();
}
}
});
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
label: 'LINE',
data: [3, 1, 4, 2, 5],
backgroundColor: 'rgba(0, 119, 290, 0.2)',
borderColor: 'rgba(0, 119, 290, 0.6)',
fill: false
}]
},
options: {
responsive: false,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx" height="200"></canvas>
即使有一个公认的答案,我想我可能会贡献一个我写的专门解决这个问题的插件。
chartjs 线高标注插件
https://www.npmjs.com/package/chartjs-plugin-lineheight-annotation
有一些可导出的 类 可以帮助您在需要时计算到数据点的顶部。此外,还有一个简单的 API,您可以将其添加到图表的选项对象
/// default values
lineHeightAnnotation: {
// defaults to have line to the highest data point on every tick
always: true,
// optionally, only have line draw to the highest datapoint nearest the user's hover position
hover: false,
// colors of the line
color: '#000',
// name of yAxis
yAxis: 'y-axis-0',
// weight of the line
lineWeight: 1.5,
/// sets shadow for ALL lines on the canvas
shadow: {
// color of the shadow
color: 'rgba(0,0,0,0.35)',
// blur of the shadow
blur: 10,
/// shadow offset
offset: {
// x offset
x: 0,
// y offset
y: 3
}
},
// dash defaults at [10, 10]
noDash: true,
}