Created
August 29, 2018 20:09
-
-
Save mgiacomini/98d2cd292c24383869441e42022c1489 to your computer and use it in GitHub Desktop.
How to drill down charts with ChartJS?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://stackoverflow.com/questions/51609922/how-to-drill-down-charts-with-chartjs | |
| // https://stackoverflow.com/questions/26257268/click-events-on-pie-charts-in-chart-js | |
| $(document).ready(function() { | |
| var canvas = document.getElementById("myChart"); | |
| var ctx = canvas.getContext("2d"); | |
| var myNewChart = new Chart(ctx, { | |
| type: 'pie', | |
| data: data | |
| }); | |
| canvas.onclick = function(evt) { | |
| var activePoints = myNewChart.getElementsAtEvent(evt); | |
| if (activePoints[0]) { | |
| var chartData = activePoints[0]['_chart'].config.data; | |
| var idx = activePoints[0]['_index']; | |
| var label = chartData.labels[idx]; | |
| var value = chartData.datasets[0].data[idx]; | |
| var color = chartData.datasets[0].backgroundColor[idx]; //Or any other data you wish to take from the clicked slice | |
| alert(label + ' ' + value + ' ' + color); //Or any other function you want to execute. I sent the data to the server, and used the response i got from the server to create a new chart in a Bootstrap modal. | |
| } | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment