score:0
Accepted answer
this is quite straightforward and is documented at the chart.js api page. below is a working snippet adapted from the official example:
let canvas = document.getelementbyid('canvas');
let mychart = new chart(canvas, {
type: 'bar',
data: {
labels: ['a', 'b', 'c', 'd'],
datasets: [{
data: [0, 2, 4, 8]
}]
}
});
canvas.addeventlistener('click', function(evt) {
var firstpoint = mychart.getelementatevent(evt)[0];
if (firstpoint) {
var label = mychart.data.labels[firstpoint._index];
var value = mychart.data.datasets[firstpoint._datasetindex].data[firstpoint._index];
alert('label: ' + label + "\nvalue: " + value);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.7.3/chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>
Source: stackoverflow.com
Related Query
- How can i launch a modal after clicking each bar in Bar chart in Chartjs and also how can i get its data?
- How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?
- how can i show labels and value in both on bar chart
- How can I reduce the spacing between my legend and chart proper in my Chart.JS bar chart, and increase it in the graph area?
- how to write labels along with data on top and bottom of each stack in bar chart
- How to show symbols after the label and before the numeric value using chart.js Bar chart in Angular
- How can I change the background colors of a bar chart after it has been created?
- How Can customize chartjs doughnut chart border and tooltip
- How Can I Get An Instance of a ChartJS Bar Chart Using Angular
- How can i get the Chart JS Bar Graph Bar Label and Value on click?
- How to add vertical line in bar chart using chartJs and ng2-charts?
- how to create bar chart with group and sam color for each group using chart.js?
- chartjs : how to set custom scale in bar chart
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- How to add second Y-axis for Bar and Line chart in Chart.js?
- ChartJS bar chart with legend which corresponds to each bar
- Can Chart.js combines Line Chart and Bar Chart in one canvas
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How to set a full length background color for each bar in chartjs bar
- How to draw Horizontal line on Bar Chart Chartjs
- Chart js: how can I align the legend and the title
- How can I display the xAxes and yAxes data in the tooltip, Chart JS?
- How to set single color on each bar in angular chart js
- How to show data values in top of bar chart and line chart in chart.js 3
- How can I datalabels and Sum display in the same time on a stacked bar
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Grouped Bar Chart from mySQL database using ChartJS and PHP
- Chartjs - Set start and end value into a Bar Chart
- ChartJS horizontal chart display legend on each bar
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
More Query from same tag
- Bubble getting cut off in Chart.js
- ChartJS and Flask, how to pass data?
- chart.js My DATA doesn't go along with the label,
- ChartJS bubble plot categorical y-labels with numbers in string plot issue
- Remove Decimal values in Yaxis Linechart -Primefaces
- vue-chart.js : Data available as computed properties, but not showing in chart
- Chart JS can't access data from array within an object
- protoype js 1.0.6 is conflicting with chart js
- Unable to load Chart.js data from getJSON / PHP?
- How to reorganize data to display how many times a string appears in an array object?
- How do I connect to a PostgreSQL database on Heroku server and use Chart.js?
- Chart.js yAxes Ticks stepSize not working (fiddle)
- How to fit Doughnut Chart JS into Bootstrap column?
- Plotting Dashed Vertical and Horizontal lines on line graph for single point
- angular 5 chart.js datalabels plugin
- Chart.js / Javascript How to Override Mouseout Event Listener
- Can't draw pie chart using chartjs - TypeError: Cannot create property '_meta' on number '16'
- ChartJS showing old values when mouse over on Doughnut after updating values
- Chartjs plugin label in Angular app moving unexpectedly at hover
- React-Chartjs-2 and Chartjs v3:Option Property
- ChartJS: Mapping Non numeric Y and X
- How to assign a logarithmic scale to y axis in chart.js?
- Use chartjs-plugin-annotation 0.5.7 in Chartjs 2.9.x with React
- Chartjs stacked bar separate tooltip for all stacked
- Complete border on ChartJS bars
- Draw a horizontal and vertical line on mouse hover in chart js
- Chartjs : data labels getting overlapped in smaller datasets using chartjs-plugin-datalabels
- Updating Chartjs Data with Response from POST Call?
- ng2-charts - bar chart, showing only one color
- Laravel determine the date from database if MONDAY-SUNDAY