Click events on Pie Charts in Chart.js

后端 未结 10 2021
失恋的感觉
失恋的感觉 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:55

    Update: As @Soham Shetty comments, getSegmentsAtEvent(event) only works for 1.x and for 2.x getElementsAtEvent should be used.

    .getElementsAtEvent(e)

    Looks for the element under the event point, then returns all elements at the same data index. This is used internally for 'label' mode highlighting.

    Calling getElementsAtEvent(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.getElementsAtEvent(evt);
        // => activePoints is an array of points on the canvas that are at the same position as the click event.
    };
    

    Example: https://jsfiddle.net/u1szh96g/208/


    Original answer (valid for Chart.js 1.x version):

    You can achieve this using getSegmentsAtEvent(event)

    Calling getSegmentsAtEvent(event) on your Chart instance passing an argument of an event, or jQuery event, will return the segment elements that are at that the same position of that event.

    From: Prototype Methods

    So you can do:

    $("#myChart").click( 
        function(evt){
            var activePoints = myNewChart.getSegmentsAtEvent(evt);           
            /* do something */
        }
    );  
    

    Here is a full working example:

    <html>
        <head>
            <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.2.js"></script>
            <script type="text/javascript" src="Chart.js"></script>
            <script type="text/javascript">
                var data = [
                    {
                        value: 300,
                        color:"#F7464A",
                        highlight: "#FF5A5E",
                        label: "Red"
                    },
                    {
                        value: 50,
                        color: "#46BFBD",
                        highlight: "#5AD3D1",
                        label: "Green"
                    },
                    {
                        value: 100,
                        color: "#FDB45C",
                        highlight: "#FFC870",
                        label: "Yellow"
                    }
                ];
    
                $(document).ready( 
                    function () {
                        var ctx = document.getElementById("myChart").getContext("2d");
                        var myNewChart = new Chart(ctx).Pie(data);
    
                        $("#myChart").click( 
                            function(evt){
                                var activePoints = myNewChart.getSegmentsAtEvent(evt);
                                var url = "http://example.com/?label=" + activePoints[0].label + "&value=" + activePoints[0].value;
                                alert(url);
                            }
                        );                  
                    }
                );
            </script>
        </head>
        <body>
            <canvas id="myChart" width="400" height="400"></canvas>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-11-28 03:55
      var ctx = document.getElementById('pie-chart').getContext('2d');
     var myPieChart = new Chart(ctx, {
                    // The type of chart we want to create
                    type: 'pie',
    
                });
    
                 //define click event
      $("#pie-chart").click(
                function (evt) {
                    var activePoints = myPieChart.getElementsAtEvent(evt);
                    var labeltag = activePoints[0]._view.label;
    
                          });
    
    0 讨论(0)
  • 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 ... */
        }
    };
    
    0 讨论(0)
  • 2020-11-28 04:00

    Working fine chartJs sector onclick

    ChartJS : pie Chart - Add options "onclick"

                  options: {
                        legend: {
                            display: false
                        },
                        'onClick' : function (evt, item) {
                            console.log ('legend onClick', evt);
                            console.log('legd item', item);
                        }
                    }
    
    0 讨论(0)
提交回复
热议问题