1

I have a chart and when clicked on each chart label the scroll move to their respective section in HTML. I'm using Chart.js, The only action I could do when I click the label is an alert.

Javascript

// 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 activePoints = skillsChart.getSegmentsAtEvent(evt);
    var url = activePoints[0].label + activePoints[0].value + activePoints[0].x + activePoints[0].y;
    alert(url);
});

jsfiddle

Cristiano Matos
  • 329
  • 1
  • 2
  • 10

2 Answers2

1

To access the selected field, just you need to use internal links.

You need to add modify the html to be like this :

<canvas id="chartDonnut"></canvas>
<div id="red" class="redSection">
    <p>Red Section</p>
</div>
<div id="orange" class="orangeSection">
    <p>Orange Section</p>
</div>
<div id="yellow" class="yellowSection">
    <p>Yellow Section</p>
</div>
<div id="green" class="greenSection">
    <p>Green Section</p>
</div>

And for the jquery code, it'll be like this :

// 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 activePoints = skillsChart.getSegmentsAtEvent(evt);
    var url = activePoints[0].label + activePoints[0].value + activePoints[0].x + activePoints[0].y;
    if(activePoints[0].label=="Red"){
       window.location="#red";
    }
    if(activePoints[0].label=="Orange"){
       window.location="#orange";
    }
    if(activePoints[0].label=="Yellow"){
       window.location="#yellow";
    }
    if(activePoints[0].label=="Green"){
       window.location="#green";
    }
});
BahaEddine Ayadi
  • 987
  • 9
  • 20
1

Here's the modified data that should be in the click handler:

$("#chartDonnut").click(function(evt) {
    var sector = skillsChart.getSegmentsAtEvent(evt)[0].label;
    document.getElementsByClassName(sector.toLowerCase()+'Section')[0].scrollIntoView();
});

Working jsfiddle: http://jsfiddle.net/1efr492j/1/

Note that it will jump to the other spot, not smoothly scroll there. If you want it to smoothly scroll, look here for some suggestions: scrollintoview animation

The other answer will work as well, but this is a bit less invasive and changes less code

Community
  • 1
  • 1
markasoftware
  • 12,292
  • 8
  • 41
  • 69