Click events on Pie Charts in Chart.js

后端 未结 10 2114
失恋的感觉
失恋的感觉 2020-11-28 03:29

I\'ve got a question regard Chart.js.

I\'ve drawn multiple piecharts using the documentation provided. I was wondering if on click of a certain slice of one of the

10条回答
  •  我在风中等你
    2020-11-28 03:56

    If using a Donught Chart, and you want to prevent user to trigger your event on click inside the empty space around your chart circles, you can use the following alternative :

    var myDoughnutChart = new Chart(ctx).Doughnut(data);
    
    document.getElementById("myChart").onclick = function(evt){
        var activePoints = myDoughnutChart.getSegmentsAtEvent(evt);
    
        /* this is where we check if event has keys which means is not empty space */       
        if(Object.keys(activePoints).length > 0)
        {
            var label = activePoints[0]["label"];
            var value = activePoints[0]["value"];
            var url = "http://example.com/?label=" + label + "&value=" + value
            /* process your url ... */
        }
    };
    

提交回复
热议问题