score:34
Accepted answer
you could accomplish this using the following on-click event handler for your chart ...
canvas.onclick = function(evt) {
var activepoint = mychart.getelementatevent(evt)[0];
var data = activepoint._chart.data;
var datasetindex = activepoint._datasetindex;
var label = data.datasets[datasetindex].label;
var value = data.datasets[datasetindex].data[activepoint._index];
};
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩
var chartdata = {
type: 'horizontalbar',
data: {
labels: ['a0224', 'a3681', 'a3984', 'a4101', 'a4150', 'b0682', 'others'],
datasets: [{
label: "p1",
backgroundcolor: '#cc3399',
data: [6, 7, 6, 8, 6, 10, 3]
}, {
label: "p2",
backgroundcolor: '#0099ff',
data: [8, 9, 5, 8, 6, 10, 3]
}, {
label: "p3",
backgroundcolor: '#0022ff',
data: [6, 7, 6, 8, 6, 10, 3]
}]
},
options: {
responsive: false,
legend: {
display: false
},
scales: {
yaxes: [{
stacked: true
}],
xaxes: [{
stacked: true
}]
},
}
}
var canvas = document.getelementbyid('createcurryearhccgapchart');
var mychart = new chart(canvas, chartdata);
canvas.onclick = function(evt) {
var activepoint = mychart.getelementatevent(evt)[0];
var data = activepoint._chart.data;
var datasetindex = activepoint._datasetindex;
var label = data.datasets[datasetindex].label;
var value = data.datasets[datasetindex].data[activepoint._index];
console.log(label, value);
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.6.0/chart.min.js"></script>
<canvas id="createcurryearhccgapchart" height="170"></canvas>
score:0
in the new version of chart.js, the below code would work.
const ctx = $('#monitor');
monitorchart = new chart(ctx, config);
ctx.on('click', function(evt) {
const points = monitorchart.getelementsateventformode(evt, 'nearest', { intersect: true }, true);
if (points.length) {
const firstpoint = points[0];
const label = monitorchart.data.labels[firstpoint.index];
const slabel = monitorchart.data.datasets[firstpoint.datasetindex].label;
const value = monitorchart.data.datasets[firstpoint.datasetindex].data[firstpoint.index];
console.log(label, slabel, value);
}
});
you can check here!
Source: stackoverflow.com
Related Query
- Click event on stacked bar chart - ChartJs
- Add a click event to the bar chart of chartjs
- Chartjs v2.0: stacked bar chart
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Multiple stacked bar chart using ChartJs
- Stacked bar chart starting from 0 - ChartJS React
- Click event of stacked line chart not working
- ChartJs Stacked Bar Chart
- Stacked bar chart with chartjs with included values
- Chartjs Stacked bar chart not displaying correctly
- Stacked Bar chart fixed width bar chart issue - Chartjs
- Multi-colored legend stacked bar chart Chartjs
- ChartJS add custom tooltip to a stacked bar chart
- Chartjs - Stacked bar chart data order causes invisible other values
- ChartJS - Horizontal Stacked Bar Chart Tooltip being cut off by Bottom of Canvas
- Chartjs - Stacked bar chart blocking other values
- ChartJs - stacked bar chart with groups - how to create second x-axis with stack id
- ChartJS stacked bar chart not displaying as stacked
- how to insert dynamic data from sql into chartjs stacked bar chart javascript
- ChartJS 2.7.3 stacked bar chart with overlap
- Chartjs v3 overlap stacked bar chart with groups
- Draw stacked horizontal bar chart with Average line using ChartJS
- How to configure Chartjs Bar Stacked Chart in Laravel 8
- PrimeFaces: ChartJs stacked bar chart label removal
- Chartjs Bar Chart showing old data when hovering
- Chart.js: Bar Chart Click Events
- Horizontal stacked bar chart with chart.js
- How to add an on click event to my Line chart using Chart.js
- Vertical stacked bar chart with chart.js
- Changing fontFamily on ChartJS bar chart
More Query from same tag
- Chart.js pie chart overflowing div
- How to have custom colors in ng2-charts and chart.js according to data?
- How to generate chartjs charts to pdf using laravel?
- How to set percentage value by default on each bars in horizontal bar chart js
- Meteor with Chart js cannot read property ‘getContext’ of null only AFTER new document in collection?
- Using chartjs to limit data?
- How to add space between two scales in chart-js?
- Stack grouped xAxes in bar chart
- Chart.js line graph data not displaying
- Displaying mixed types of legends (bar and lines) with Chartjs
- How to customize the tooltip of a Chart.js Line Chart?
- Chart.js 3.6.2, Angular 10: Logarithmic Line chart Y-Axis problems. How to set and keep Y-Axis properties, even when data changes?
- How to use log scale with Chart Kick?
- Chartjs: dynamic data (array) not working
- Chart JS: Donut/Doughnut Chart: Tooltip to be shown always for all the data. All tooltip is not shown when multiple data are with 0 data
- How to use chartjs-plugin-datalabels for specific charts using vue-chart.js
- Chart.js bars have non uniform widths
- One common component for Chart.js to use with different y-axis With React.js
- How to display data values on bar graph (chart.js)?
- How to make integer scale in Chartjs
- Vertical stacked bar chart using SharePoint List
- Chart.js Plugin Not Recognized as Options Property in Angular
- Gradient line chart with ChartJS
- Chart.js - how to have statis lables and populate with dynamic data?
- I am Creating pie chart using Chartjs 2.6.0. I want to show label on Slices
- Chart.js How to actualize canvas due to new x-axis range typed by user
- Format x axis to show percent with Primefaces/Chartjs ScatterChart
- Want to draw data using chartjs in django
- Chart.js Line Graph: Start a line in the middle of a graph
- Dynamically updating Chart.js data in angular