From: Tanner Linsley Date: Tue, 12 May 2015 01:43:17 +0000 (-0600) Subject: hoverMode, onHover, key fixes X-Git-Tag: v2.0-alpha~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df91739b77b29dea65e148ecaa0fbdd762909091;p=thirdparty%2FChart.js.git hoverMode, onHover, key fixes --- diff --git a/src/Chart.Bar.js b/src/Chart.Bar.js index 351698823..66d9e1625 100644 --- a/src/Chart.Bar.js +++ b/src/Chart.Bar.js @@ -37,6 +37,12 @@ //Number - Spacing between data sets within X values barDatasetSpacing : 1, + //String - Hover mode for events + hoverMode : 'bars', // 'bar', 'dataset' + + //Function - Custom hover handler + onHover : null, + //String - A legend template legendTemplate : "" @@ -77,18 +83,27 @@ //Set up tooltip events on the chart if (this.options.showTooltips){ - helpers.bindEvents(this, this.options.tooltipEvents, function(evt){ - var activeBars = (evt.type !== 'mouseout') ? this.getBarsAtEvent(evt) : []; - - this.eachBars(function(bar){ - bar.restore(['fillColor', 'strokeColor']); - }); - - helpers.each(activeBars, function(activeBar){ - activeBar.fillColor = activeBar.highlightFill; - activeBar.strokeColor = activeBar.highlightStroke; - }); - this.showTooltip(activeBars); + helpers.bindEvents(this, this.options.tooltipEvents, function(e){ + var active; + if(e.type == 'mouseout'){ + return false; + } + if(this.options.hoverMode == 'bar'){ + active = this.getBarAtEvent(e); + // TODO: tooltips for single items + } + else if(this.options.hoverMode == 'bars'){ + active = this.getBarsAtEvent(e); + } + else { + // TODO: active = this.getDatasetAtEvent(e); + } + + if(this.options.onHover){ + this.options.onHover.call(this, active); + } + + this.showTooltip(active); }); } @@ -183,10 +198,10 @@ var eventPosition = helpers.getRelativePosition(e); for (var datasetIndex = 0; datasetIndex < this.datasets.length; ++datasetIndex) { - for (var barIndex = 0; barIndex < this.datasets[datasetIndex].bars.length; ++barIndex) { - if (this.datasets[datasetIndex].bars[barIndex].inRange(eventPosition.x, eventPosition.y)) { + for (var barIndex = 0; barIndex < this.datasets[datasetIndex].metaData.length; ++barIndex) { + if (this.datasets[datasetIndex].metaData[barIndex].inRange(eventPosition.x, eventPosition.y)) { bar = { - rectangle : this.datasets[datasetIndex].bars[barIndex], + rectangle : this.datasets[datasetIndex].metaData[barIndex], datasetIndex : datasetIndex, barIndex : barIndex, };