I'm trying to get the onclick function in chart.js to work, and found out about the getElementsAtEvent(event) function that should return an array containing the data of the part of the chart that I click, but it's constantly returning an empty array.
Here's my code:
var canvas = this.el; //this is an elementref
canvas.onclick = (event) => {
var data = this.chart.getElementsAtEvent(event)
console.log(data);
}
this.chart = new Chart('canvas', {
type: 'bar',
data: {
labels: ["Browser", "Game", "Word Processing", "Database", "Spreadsheet", "Multimedia"],
datasets: [{
label: 'number of applications related to',
data: [24, 10, 30, 20, 46, 78],
backgroundColor: 'rgba(54, 162, 235, 0.2',
borderColor: 'rgba(54, 162, 235, 1)',
pointHoverBackgroundColor: 'red',
borderWidth: 1
}]
},
options: {
title: {
text: "Application Logs",
display: true
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
})
Not sure if this is part of why it doesn't work but here's my canvas:
<canvas id="canvas" width="300" height="123" class="chartjs-render-monitor"></canvas>
Like I mentioned it just prints out an empty array [] in the console, and using console.log(data[0]) obviously just returns undefined.