reactjs / react-chartjs

common react charting components using chart.js
MIT License
2.93k stars 300 forks source link

Get points on event returns empty array. #155

Open selimovicz opened 7 years ago

selimovicz commented 7 years ago

handleOnClick(event) { console.log(this.refs.lineChart.getPointsAtEvent(event)); }

As title states, this console log always returns empty array even though graph is showing a lot of data.

Jareechang commented 7 years ago

are you extending the class ? my this.refs only has { canvass: ... } I am not sure how you are able to do this otherwise.

It would help to debug it if you post more code.

Jareechang commented 7 years ago

You can make use of extensions to get a reference to this canvas.

const Chart = require('react-chartjs');

class YourChart extends Chart.Line {
    constructor(props) {
        super(props);        
    }   
    componentDidMount() {
        this.initializeChart(this.props);
        const chart = this;
        this.refs.canvass.onclick = function(e) {
            console.log(chart.getPointsAtEvent(e));
        }
    }   
};