Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

hyperlink - 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>  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...