我正在尝试使用Chart.js将单击事件添加到我的折线图中。我已经启用了工具提示以显示折线图中的信息,但是我还想添加一个on click方法,该方法可以让我知道用户在x轴上的点击位置。现在,我只想弹出一个警报,向我提供用户单击的x轴上的值。
研究:
我浏览了Chart.js的文档,然后发现了以下方法:.getPointsAtEvent(event)
在Chart实例上调用getPointsAtEvent(event)并传递事件或jQuery事件的参数,将返回该事件在同一位置的点元素。
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?");
};
对于那些很难在此处可视化图表的人,您可以:
希望我提供了足够的信息以获取帮助。如果我需要自我解释,请告诉我。先感谢您!!!:)
更改此行
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?");
};
console.log(activePoints);
在onclick处理程序中添加以查看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
第一个,因为每个单击事件后面都没有数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句