如何使用 Chart.js 向我的折线图添加点击事件
我正在尝试使用 Chart.js 向我的折线图添加点击事件。我已经启用了工具提示来显示折线图中的信息,但我还想添加一个点击方法,让我知道用户在 x 轴上点击的位置。现在,我只希望弹出一个警报,告诉我用户在 x 轴上点击的位置的值。
研究:
我浏览了 Chart.js 文档 ,然后发现了这种方法:.getPointsAtEvent(event)
Calling getPointsAtEvent(event) on your Chart instance passing an argument of an event, or jQuery event, will return the point elements that are at that the same position of that event.
canvas.onclick = function(evt){
var activePoints = myLineChart.getPointsAtEvent(evt);
// => activePoints is an array of points on the canvas that are at the same position as the click event. };
我只是想不通如何使用或者在代码中把这个函数放在哪里。如果有人能帮我弄清楚我可以在哪里将其添加到我的代码中,我将不胜感激!
我的代码:(在 javascript 中)
//NOTE: the div 'roomForChart' has been already declared as <div id="roomForChart"></div>
//creating html code inside of javascript to display the canvas used for the graph
htmlForGraph = "<canvas id='myChart' width ='500' height='400'>";
document.getElementById('roomForChart').innerHTML += htmlForGraph;
//NOW TO CREATE DATA
//the data for my line chart
var data = {
labels: ["Aug 1", "Aug 2", "Aug 3","Aug 4","Aug 5"], //the x axis
datasets: [
{ //my red line
label: "Usage Plan",
fillColor: "rgba(255,255,255,0.2)", //adds the color below the line
strokeColor: "rgba(224,0,0,1)",//creates the line
pointColor: "rgba(244,0,0,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [1024, 1024, 1024, 1024, 1024]
},
{ //my green line
label: "Overall Usage",
fillColor: "rgba(48,197,83,0.2)",
strokeColor: "rgba(48,197,83,1)",
pointColor: "rgba(48,197,83,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(48,197,83,1)",
data: [15, 25, 45, 45, 1500]
},
{ //my blue line
label: "Daily Usage",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [15, 10, 20, 0, 5]
}
] //ending the datasets
}; //ending data
//creating a variable for my chart
var ctx = document.getElementById("myChart").getContext("2d");
//code to create a maximum y value on the chart
var maxUsage = 1024;
var maxSteps = 5;
var myLineChart = new Chart(ctx).Line(data, {
pointDot: false,
scaleOverride: true,
scaleSteps: maxSteps,
scaleStepWidth: Math.ceil(maxUsage / maxSteps),
scaleStartValue: 0
});
//what I have tried but it doesn't show an alert message
ctx.onclick = function(evt){
var activePoints = myLineChart.getPointsAtEvent(evt);
// => activePoints is an array of points on the canvas that are at the same position as the click event.
alert("See me?");
};
对于那些难以想象图表的人,请看这里:
希望我提供的信息足以获得一些帮助。如果我需要解释自己,请告诉我。提前谢谢!!! :)
如果您使用的是新版 ChartJs,请使用以下命令:
canvas.onclick = function(evt){
var activePoints = myLineChart.getElementsAtEvent(evt);
};
将此行
document.getElementById('roomForChart').innerHTML += htmlForGraph;
更改为此
holder = document.getElementById('roomForChart');
holder.innerHTML += htmlForGraph;
然后,您将获得稍微修改过的代码片段
holder.onclick = function(evt){
var activePoints = myLineChart.getPointsAtEvent(evt);
// => activePoints is an array of points on the canvas that are at the same position as the click event.
alert("See me?");
};
在 onclick 处理程序中添加
console.log(activePoints);
以查看
activePoints
变量的内容。如我所见,有三个对象。例如,这些是
activePoints[0]
的值
datasetLabel: "Usage Plan"
fillColor: "rgba(244,0,0,1)"
highlightFill: "#fff"
highlightStroke: "rgba(220,220,220,1)"
label: "Aug 4"
strokeColor: "#fff"
value: 1024
x: 371
y: 12.356097560975627
并且可以按以下方式访问它们
activePoints[0].label
activePoints[0].x
activePoints[0].y
activePoints[0].value
最好先检查属性是否为
undefined
,因为每个点击事件背后都没有数据。
我无法让
onclick
方法工作。
但我最终设法使用
click
方法运行它:
$("#canvas_id").click(function(e) {
var activeBars = myBarChart.getBarsAtEvent(e);
console.log(activeBars[0]);
});
注意:此示例适用于条形图 - 其他图表有略有不同的方法来检索点(请参阅文档)。