chart.js - 单击图表中的特定部分链接到其他页面

时间:2017-08-31 11:30:45

标签: hyperlink onclick chart.js

所以我想点击饼图中的切片。切片应该充当我们系统中另一个页面的普通链接。这是HTML和JS。我的图表工作正常。

另外:我尝试过我在这里找到的解决方案,但没有一个能够工作 - 例如,在选项部分中单击并显示相应的功能(警报),但这不起作用并搞砸了页面的其余部分。 对不起,如果这有点模糊,我对此有点新鲜。

<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"
               }
          }
});

1 个答案:

答案 0 :(得分:8)

您可以使用getElementAtEvent()方法检测点击饼图的哪个部分/切片,并根据该方法相应地打开链接/页面。

这是一个简单的例子:

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>