score:0
I believe you are looking for
.getElementAtEvent
and .getElementsAtEvent
(https://www.chartjs.org/docs/latest/developers/api.html#getelementsatevente)
What you can do - is set up a clickHandler
function that will grab the event from onClick
and use it to return the chart element under it.
Here is an example from the official docs:
function clickHandler(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];
}
}
After, just add it to your chart canvas (again, from the official docs):
canvas.onclick = clickHandler;
score:4
I think you will find getElementsAtXAxis
very helpful.
Basically, getElementsAtXAxis
has a very similar behaviour to getElementsAtEvent
although when the click event doesn't intersect a point of the chart, it will return the nearest ChartElement.
The only downside is that it will return all the ChartElement on the nearest x-axis index.
So, if you use multiple datasets, this might not be the best choice for you.
Code example:
canvas.onclick = function(e) {
const elts = chart.getElementsAtXAxis(simulatedEvent);
if (elts && elts.length) {
// Do something with elts[0]
}
};
If anyone is looking for the related piece of code, it's in core.controller.js
:
getElementAtEvent: function(e) {
return Interaction.modes.nearest(this, e, {intersect: true});
},
getElementsAtEvent: function(e) {
return Interaction.modes.index(this, e, {intersect: true});
},
getElementsAtXAxis: function(e) {
return Interaction.modes.index(this, e, {intersect: false});
},
Source: stackoverflow.com
Related Query
- Chart.js get the nearest point when clicked on canvas
- Get Value for point clicked on Pie Chart in Chart.js 3
- How to clear/destroy Chart.js canvas when the chart is represented as a react JSX element
- How to get the data attribute of the canvas chart created using chartjs
- How to get rid of the line on top of every point on a line chart (Chart.js)
- ChartJs (ver: 2.8.0): Custom tooltip does not hide if clicked (or mouse pointer is moved) outside the chart canvas area
- Drawing to canvas on click clears text when mouse leaves the chart
- When updating Chart.js chart the data is updated but canvas is emptied
- Moving vertical line when hovering over the chart using chart.js
- Why can I not see a data value when hovering over a point on the radar chart?
- Angular chart.js onClick is not working - how to get all elements of a bar when it is clicked on
- React ChartJS 2 : Get data on clicking the chart
- Chart.js is it possible to fire javascript when clicking on point on the graph?
- How to get the actual chart width and height in chart.js
- Can I use a gradient in Chart.js without accessing the chart context when the chart is created?
- How do I keep chart.js charts in one JS file, and not get errors when the ID from the JS file don't exist on one specific html page?
- Chart.js tooltip at any point on the chart
- Canvas is already in use. Chart with ID '0' must be destroyed before the canvas can be reused. chat.JS
- canvas fill text vanishes when hovering over chartjs pie chart
- Modify the labels in chart.js when I update the chart
- Chart js 2.x renders the canvas invisible
- ChartJS v2 - Keep tooltip open when user click on a point of a multiple lines chart
- I am using chartjs node canvas to render a chart however the graph options are being ignored
- ChartJS bar chart - trigger hover even when the cursor is outside of the bar
- React - Display Chart when button is clicked
- How to reuse a Chartjs Chart component in with different Data and get past the **Canvas is already in use** error?
- Chart.js 3.5: linear gradient doesn't apply properly when multiple bars in the same chart
- Chart.js - disable tooltip when cursor moves outside the main canvas
- Chart.js: get chart data from canvas
- Determining which chart the user has clicked on
More Query from same tag
- How do I create multiple charts at the same page with angular?
- Can I bind an onclick event and edit a point in chartjs
- In react.js with chart.js, I can't access to my data in a componant
- How do I use Chart.js and Bootstrap at same time? (various jQuery versions)
- Chart not displaying from JSON data
- how to create line chart using chart.js in angular 2+
- In Chart.js >3.0, on axis of type time, how to show labels and ticks only for existing data points (make labels reflect data)?
- chartjs - how to set the order in which the different charts are displayed
- ChartJS fails to render one of the lines in cartesian chart following update after change to max Y axis label value
- Mapping data (from getJSON response) in Chart.js
- Chart.JS: How to add Data to a specific dataset
- Chart.js 2.9.4 - error using bundle from CDN - "chart is not defined"
- Automatic colors assignment in chart.js 2.x doesn't work anymore, used to work in v. 1.x
- how to label axis within radar chart with chart.js
- ChartJS : It's not showing in html on Django
- Why Chart.js v2 time based data not aligning with X axis correctly?
- how can I show only 2 numbers on the xAxe in Chartjs having more than two numbers on the chart? (line chart)
- chart.js filltext object moved when tooltip is generated because of legend not displaying
- Chart.js - how to have statis lables and populate with dynamic data?
- how to show REALTIME chartjs of specific ID in mysql when click on corresponding marker on googlemap javascript
- How to make custom text appear underneath each bar in chartjs bar chart?
- Is there a way to change part of the title to another font style or set a custom title format in Chart.js?
- Show 'No data' in Pie when there is no data using VueJS and ChartJS?
- Change Chartjs Legend Icon Style
- Yii2 unable to view chart
- ChartJS - Labels background
- chart labels didn't shows in chartjs
- Limit labels number on Chart.js line chart
- Catch event of show and hide dataset at chart.js
- ChartJS and require: Chart is undefined