Click anchors the label to its respective section HTML - Javascript Chart.js

Javascript examples for Chart.js:Chart Label

Description

Click anchors the label to its respective section HTML

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script> 
      <style id="compiled-css" type="text/css">

div{//from  w  w  w  .  j  av a 2s  .c  om
   padding: 300px 0;
}
#chartDonnut{
   margin-bottom: 90px;
}
.redSection{
   background: #ad1f27;
}
.orangeSection{
   background: #c26828;
}
.yellowSection{
   background: #c3b830;
}
.greenSection{
   background: #14773c;
}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){
// Chart
var barData = {
    labels: ['Red', 'Orange', 'Yellow', 'Green']
};
var pieData = [{
    value: 40,
    color: "#ad1f27",
    highlight: "#ad474c",
    label: "Red"
}, {
    value: 40,
    color: "#c26828",
    highlight: "#c27e4d",
    label: "Orange"
}, {
    value: 10,
    color: "#c3b830",
    highlight: "#c3bc6b",
    label: "Yellow"
}, {
    value: 10,
    color: "#14773c",
    highlight: "#2da15b",
    label: "Green"
}];
var options = {
    segmentShowStroke: false
};
Chart.defaults.global.responsive = true;
var context = document.getElementById('chartDonnut').getContext('2d');
var skillsChart = new Chart(context).Doughnut(pieData, options);
$("#chartDonnut").click(function(evt) {
    var sector = skillsChart.getSegmentsAtEvent(evt)[0].label;
    document.getElementsByClassName(sector.toLowerCase()+'Section')[0].scrollIntoView();
});
    });

      </script> 
   </head> 
   <body> 
      <canvas id="chartDonnut"></canvas> 
      <div class="redSection"> 
         <p>Red Section</p> 
      </div> 
      <div class="orangeSection"> 
         <p>Orange Section</p> 
      </div> 
      <div class="yellowSection"> 
         <p>Yellow Section</p> 
      </div> 
      <div class="greenSection"> 
         <p>Green Section</p> 
      </div>  
   </body>
</html>

Related Tutorials