javascript - 如何使用 Chart.js 向我的折线图添加点击事件

标签 javascript charts onclick linechart chart.js

我正在尝试使用 Chart.js 将点击事件添加到我的折线图中.我已经启用了工具提示来显示折线图中的信息,但我还想添加一个单击方法,让我知道用户在 x 轴上单击的位置。现在我只想弹出一个警报,给我用户点击的 x 轴上的值。

研究:

我浏览了 documentation of 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?");
};

对于那些难以想象图表的人来说:

enter image description here

希望我提供了足够的信息以获得一些帮助。如果我需要解释一下,请告诉我。先感谢您!!! :)

最佳答案

如果您使用的是新的 ChartJs 版本,请使用:

canvas.onclick = function(evt){
   var activePoints = myLineChart.getElementsAtEvent(evt);
};

关于javascript - 如何使用 Chart.js 向我的折线图添加点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25514802/

相关文章:

android - Onclick用于在java中创建的imageview,而不是xml

javascript - 创建一个可以自行删除的元素?

php - 将 PHP 数组传递给 JavaScript 函数不起作用

javascript - 使用 Angular 4 创建将获取 URL 值的输入字段时出现错误

javascript - jQuery:无法使用 .load 缩放 img。它填满了整个页面。我想缩小它以适应

javascript - THREE.js vertexShader 基于高度的颜色混合

javascript - 如何将 'this' 与 onClick 结合使用以从正确的 href 获取属性

javascript - 重定向到 Highcharts 标记单击上的 URL

c# - 从资源文件向 Windows Chart 添加 BackImage

javascript - Google Material 折线图 - 刻度