question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OnClick item has the incorrect datasetIndex for line graphs

See original GitHub issue

Hi,

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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

9reactions
ahoopes16commented, Aug 15, 2019

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 the onClick handler does.

The way I used it was like so:

LineChart Class:

import { Line } from 'react-chart-js2'

function LineChart(props) {
   return (
      <div className="line-chart">
         <Line
             getElementAtEvent={props.handleChartClick}
             ...
          />
      </div>
   )
}

export default LineChart

Parent Class:

import LineChart from './LineChart'

class Parent extends Component {
   handleChartClick(element) {
      const { datasets } = element[0]._chart.tooltip._data
      const datasetIndex = element[0]._datasetIndex
      const dataIndex = element[0]._index

      alert(`${datasets[datasetIndex].label}: ${datasets[datasetIndex].data[dataIndex]}`) 
   }

   render() {
      return (
         <LineChart
            handleChartClick={element => this.handleChartClick(element)}
            ...
         />
      )
   }
}

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 the Line class from react-chart-js2.

1reaction
austinderekcommented, Nov 7, 2018

Need to grab the index in my dataset to link to specific URLs… Anyone solve this click event dilemma?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found