Add "onHover" to the legend options that will hold a user defined function (default is null) and called when a "mousemove" event is registered on top of a label item, with same parameters as the "onClick" option.
Also introduced logic that determines if the type of event passed to the legend "handleEvent" function is one we can handle. Currently allowing "click" and "mousemove" events. If the event is not one of those we stop the function execution (this is for the sake of reusing the legend hitbox calculations).
display | Boolean | true | Is the legend displayed
position | String | 'top' | Position of the legend. Possible values are 'top', 'left', 'bottom' and 'right'.
fullWidth | Boolean | true | Marks that this box should take the full width of the canvas (pushing down other boxes)
-onClick | Function | `function(event, legendItem) {}` | A callback that is called when a click is registered on top of a label item
+onClick | Function | `function(event, legendItem) {}` | A callback that is called when a 'click' event is registered on top of a label item
+onHover | Function | `function(event, legendItem) {}` | A callback that is called when a 'mousemove' event is registered on top of a label item
labels |Object|-| See the [Legend Label Configuration](#chart-configuration-legend-label-configuration) section below.
#### Legend Label Configuration
hoverOptions.onHover.call(me, me.active);
}
+ if (me.legend && me.legend.handleEvent) {
+ me.legend.handleEvent(e);
+ }
+
if (e.type === 'mouseup' || e.type === 'click') {
if (options.onClick) {
options.onClick.call(me, e, me.active);
}
- if (me.legend && me.legend.handleEvent) {
- me.legend.handleEvent(e);
- }
}
// Remove styling for last active (even if it may still be active)
ci.update();
},
+ onHover: null,
+
labels: {
boxWidth: 40,
padding: 10,
// Handle an event
handleEvent: function(e) {
var me = this;
+ var opts = me.options;
+ var type = e.type === 'mouseup' ? 'click' : e.type;
+
+ if (type === 'mousemove') {
+ if (!opts.onHover) {
+ return;
+ }
+ } else if (type === 'click') {
+ if (!opts.onClick) {
+ return;
+ }
+ } else {
+ return;
+ }
+
var position = helpers.getRelativePosition(e, me.chart.chart),
x = position.x,
- y = position.y,
- opts = me.options;
+ y = position.y;
if (x >= me.left && x <= me.right && y >= me.top && y <= me.bottom) {
// See if we are touching one of the dataset boxes
if (x >= hitBox.left && x <= hitBox.left + hitBox.width && y >= hitBox.top && y <= hitBox.top + hitBox.height) {
// Touching an element
- if (opts.onClick) {
+ if (type === 'click') {
opts.onClick.call(me, e, me.legendItems[i]);
+ break;
+ } else if (type === 'mousemove') {
+ opts.onHover.call(me, e, me.legendItems[i]);
+ break;
}
- break;
}
}
}
// a callback that will handle
onClick: jasmine.any(Function),
+ onHover: null,
labels: {
boxWidth: 40,