Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chart.js - link to other page when click on specific section in chart

So I want to click on a slice in the pie chart. The slice should act as a normal link to another page in our system. Here is the HTML and JS for this. My chart works fine otherwise.

Also: I've tried solutions that i've found here but none of them has worked- For example onclick in options section with a corresponding function(alert) but this did not work and messed up the rest of the page. Sorry if this is a bit fuzzy, i'm kind of new to this.

<div class="row" id="chartrow">
    <div class="col-6" >
        <div class="card container-fixed" >
            <div class="row" >
                <div class="col-6">
                   <table class="table table-striped table-sm table-hover">
            <tbody>
                <tr>
                    <th>Answer option  </th>
                    <th>Answer </th>
                    <th>Percent </th>

                </tr>
                <tr>

                    <td>1</td>
                    <td>2</td>
                    <td>12</td>

                </tr>
                <tr>

                    <td>2</td>
                    <td>1</td>
                    <td>23</td>
                </tr>
                <tr>

                    <td>3</td>
                    <td>5</td>
                    <td>56</td>
                </tr>
                <tr>

                    <td>4</td>
                    <td>3</td>
                    <td>14</td>
                    <tr>

                        <td>5</td>
                        <td>6</td>
                        <td>89</td>
                    </tr>
                <tr>

                    <td>6</td>
                    <td>8</td>
                    <td>56</td>
                </tr>
            </tbody>
        </table>
               </div>   
            <div class="col-6 container-fixed">
            <canvas id="pieChart" height="200" data-toggle="tooltip" data-placement="top"></canvas>
            </div>
           </div>
       </div> 
   </div>  

var ctxP = document.getElementById("pieChart").getContext('2d');
var myPieChart = new Chart(ctxP, {
    showTooltips:true,
  type: 'pie',
  data: {
    labels: ["Värde 1", "Värde 2", "Värde 3", "Värde 4", "Värde 5", "Värde 6", "Värde 7"],
    datasets: [{
      data: [ 1, 5,10, 20,50,70,50],
      backgroundColor: ["#64B5F6", "#FFD54F", "#2196F3", "#FFC107", "#1976D2", "#FFA000", "#0D47A1"],
      hoverBackgroundColor: ["#B2EBF2", "#FFCCBC", "#4DD0E1", "#FF8A65", "#00BCD4", "#FF5722", "#0097A7"]
    }]
  },
  options:{
    
         legend:{ 
            display:true,
            position:"right"
               }
          }
});
like image 946
Henrik Engdahl Avatar asked Dec 19 '22 04:12

Henrik Engdahl


1 Answers

You can use getElementAtEvent() method to detect which section/slice of your pie chart was clicked on, and based on that open a link/page accordingly.

Here is a quick example :

var canvasP = document.getElementById("pieChart");
var ctxP = canvasP.getContext('2d');
var myPieChart = new Chart(ctxP, {
   type: 'pie',
   data: {
      labels: ["Värde 1", "Värde 2", "Värde 3", "Värde 4", "Värde 5", "Värde 6", "Värde 7"],
      datasets: [{
         data: [1, 5, 10, 20, 50, 70, 50],
         backgroundColor: ["#64B5F6", "#FFD54F", "#2196F3", "#FFC107", "#1976D2", "#FFA000", "#0D47A1"],
         hoverBackgroundColor: ["#B2EBF2", "#FFCCBC", "#4DD0E1", "#FF8A65", "#00BCD4", "#FF5722", "#0097A7"]
      }]
   },
   options: {
      legend: {
         display: true,
         position: "right"
      }
   }
});

canvasP.onclick = function(e) {
   var slice = myPieChart.getElementAtEvent(e);
   if (!slice.length) return; // return if not clicked on slice
   var label = slice[0]._model.label;
   switch (label) {
      // add case for each label/slice
      case 'Värde 5':
         alert('clicked on slice 5');
         window.open('www.example.com/foo');
         break;
      case 'Värde 6':
         alert('clicked on slice 6');
         window.open('www.example.com/bar');
         break;
      // add rests ...
   }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="pieChart"></canvas>
like image 165
ɢʀᴜɴᴛ Avatar answered Jan 30 '23 21:01

ɢʀᴜɴᴛ