OnClick item has the incorrect datasetIndex for line graphs
See original GitHub issueHi,
I notice that when you associate an onclick event to a line chart, the item that is returned does not contain a validate dataset index. the index for the row is correct but the col value is not and always set to 0.
public render() {
const options = {
onClick: (e, item) => {
console.log(`Item with text ${item[0]._index} and index ${item[0]._datasetIndex} clicked`);
}
};
return (
<div>
<Line data={data}
options={options} />
</div>
);
}
How do we get the data point (x,y) where the user clicks if you don’t know what the _dataset index is. when i say x and y I refer to the actual data points and not the position
Thanks, Derek
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:9
Top Results From Across the Web
How to get onClick Event for a Label in ChartJS and React?
I've used a ref on the chart to get access to the label data. ... I do this, because if you were to...
Read more >How to add Data and Datasets Onclick in Chart JS - YouTube
How to add Data and Datasets Onclick in Chart JSAdding data and datasets with a button in Chart JS. This is closely related...
Read more >Legend | Chart.js
The chart legend displays data about the datasets that are appearing on ... that is called when a click event is registered on...
Read more >Present your data in a scatter chart or a line chart
Click the chart area of the chart. On the Design tab, click Add Chart Element > Axis Titles, and then do the following:...
Read more >Video: Customize charts - Microsoft Support
Training: After you create your chart, you can customize it to show additional chart elements, such as titles and data labels, or to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey, I don’t know if anyone is still looking for this answer but I came across a nice way to do it today.
I had a stacked line chart and I needed to get the information from the exact data point that was clicked. The charts have a prop called
getElementAtEvent
, to which you can pass a function that will take an array holding the single element you click on, not the array of every element in the chart like theonClick
handler does.The way I used it was like so:
LineChart Class:
Parent Class:
This will alert the associated label and the value of the data point that you click on in the chart, even with stacked line/bar charts. Then you can do whatever you want with that information!
Hope this helps!
DISCLAIMER: It definitely doesn’t have to be organized this way, I just extracted this from my project because I know for a fact it works just like this. You could totally put the
handleChartClick(element)
directly inside wherever you import theLine
class fromreact-chart-js2
.Need to grab the index in my dataset to link to specific URLs… Anyone solve this click event dilemma?