Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting which bars user is hovering #14

Open
tom10271 opened this issue Oct 5, 2015 · 0 comments
Open

Getting which bars user is hovering #14

tom10271 opened this issue Oct 5, 2015 · 0 comments

Comments

@tom10271
Copy link

tom10271 commented Oct 5, 2015


        getBarsAtEvent : function(e, withIndex){
            withIndex = withIndex || false;

            var barsArray = [],
                eventPosition = helpers.getRelativePosition(e),
                datasetIterator = function(dataset){
                    barsArray.push(dataset.bars[barIndex]);
                },
                barIndex;

            for (var datasetIndex = 0; datasetIndex < this.datasets.length; datasetIndex++) {
                for (barIndex = 0; barIndex < this.datasets[datasetIndex].bars.length; barIndex++) {
                    if (this.datasets[datasetIndex].bars[barIndex].inRange(eventPosition.x,eventPosition.y)){
                        helpers.each(this.datasets, datasetIterator);
                        return !withIndex ? barsArray : [barsArray, barIndex];
                    }
                }
            }

            return barsArray;
        },

In case anyone would like to get the index of bar they are hovering, replace the whole function with code above.

By calling:

                    $canvas.on('click', function(evt) {
                        var bar = chart.getBarsAtEvent(evt, true);

                        console.log(bar);
                    });

bar[0] would be the original bar data and bar[1] is the index. Noted that bars are drawn in reverse order. i.e. the first data you pushed will be the last one to be drawn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant