Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ChartJs how to get from mulitiple dateset which dataset bar is clicked

Tags:

chart.js

I want add a unique url to each bar and open that url on click one of bar. But in my case i have two datasets. I am using onClick event which return active record of both dataset. enter image description here

I am using following code.. It will return both dataset object...it should return only one dataset.

<script>
        var barChartData = {
            labels: ["Jan","Feb","March"],
            datasets: [{
                label : "Quoted",
                backgroundColor : "#FF7228",
                data : [50,20,70],
            },
            {
                label : "Accepted",
                backgroundColor : "#FFCC8C",
                data :  [30,10,20],
            }, 
            ]

        };

        window.onload = function() {
            var ctx = document.getElementById("canvas").getContext("2d");
            window.myBar = new Chart(ctx, {
                type: 'bar',
                data: barChartData,
                options: {
                    events:['click'],
                    onClick:function(c,i){
                            console.log(this.active);
                            }
                        },
                    responsive: true,
                    legend: {
                        position: 'top',
                    },
                }
            });
        };
 </script>
like image 346
Prahlad Avatar asked Aug 02 '17 06:08

Prahlad


People also ask

How do I delete a chart in Chartjs?

clear() Will clear the chart canvas.

What is CTX in chart JS?

js convention is to call it ctx . An object literal containing the data and the configuration options that Chart. js will use to build your chart. The required properties are type and data . In our example type is 'line' because we want a line chart.

Does Chartjs use canvas?

For Chart. js you do this by adding a canvas element, and setting width and height to define the proportions of your graph.


1 Answers

You can use getElementAtEvent() method to get only one dataset returned. Then you can apply further logic to detect which bar is clicked on.

ᴅᴇᴍᴏ

var barChartData = {
   labels: ["Jan", "Feb", "March"],
   datasets: [{
      label: "Quoted",
      backgroundColor: "#FF7228",
      data: [50, 20, 70],
   }, {
      label: "Accepted",
      backgroundColor: "#FFCC8C",
      data: [30, 10, 20],
   }]
};

var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx, {
   type: 'bar',
   data: barChartData,
   options: {
      onClick: function(e) {
         var bar = this.getElementAtEvent(e)[0];
         var index = bar._index;
         var datasetIndex = bar._datasetIndex;

         // check which bar is clicked
         if (index == 0 && datasetIndex == 0) alert('First BAR Clicked!');
         else if (index == 0 && datasetIndex == 1) alert('Second BAR Clicked!');
         else if (index == 1 && datasetIndex == 0) alert('Third BAR Clicked!');
         else if (index == 1 && datasetIndex == 1) alert('Fourth BAR Clicked!');
         else if (index == 2 && datasetIndex == 0) alert('Fifth BAR Clicked!');
         else if (index == 2 && datasetIndex == 1) alert('Sixth BAR Clicked!');
      }
   },
   responsive: true,
   legend: {
      position: 'top',
   },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="canvas"></canvas>
like image 117
ɢʀᴜɴᴛ Avatar answered Sep 28 '22 09:09

ɢʀᴜɴᴛ