Javascript examples for Chart.js:Bar Chart
Get clicked bar chart tooltip data
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script> <script type="text/javascript"> window.onload=function(){//w w w .j ava2s.co m var data = { labels: ['Jan', 'Feb', 'Mar'], datasets: [{ label: 'medium', data: [3, 1, 4], backgroundColor: 'rgba(0, 119, 220, 0.5)' }, { label: 'low', data: [2, 5, 1], backgroundColor: 'rgba(0, 119, 220, 0.2)' }] }; var title = 'BAR CHART'; var tool = new Chart(canvas, { type: 'bar', data: data, options: { legend: { display: true, labels: { fontColor: 'rgb(255, 99, 132)' }, position: 'bottom' }, title: { display: true, text: title }, scales: { xAxes: [{ stacked: true, barPercentage: 5.0, categoryPercentage: 0.2 }], yAxes: [{ stacked: true, ticks: { beginAtZero: true }, barPercentage: 0.3 }] }, events: ["click"], hover: { mode: 'nearest' }, onClick: function(event, element) { var activeElement = element[0]; var data = activeElement._chart.data; var barIndex = activeElement._index; var datasetIndex = activeElement._datasetIndex; var datasetLabel = data.datasets[datasetIndex].label; var xLabel = data.labels[barIndex]; var yLabel = data.datasets[datasetIndex].data[barIndex]; console.log(datasetLabel, xLabel, yLabel); } } }); } </script> </head> <body> <canvas id="canvas"></canvas> </body> </html>