Skip to content

Commit 3c883ff

Browse files
committed
refactor check for unified hover modes
1 parent 2c76d79 commit 3c883ff

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

src/components/fx/helpers.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ var Lib = require('../../lib');
1212

1313
// look for either subplot or xaxis and yaxis attributes
1414
// does not handle splom case
15-
exports.getSubplot = function getSubplot(trace) {
15+
exports.getSubplot = function(trace) {
1616
return trace.subplot || (trace.xaxis + trace.yaxis) || trace.geo;
1717
};
1818

1919
// is trace in given list of subplots?
2020
// does handle splom case
21-
exports.isTraceInSubplots = function isTraceInSubplots(trace, subplots) {
21+
exports.isTraceInSubplots = function(trace, subplots) {
2222
if(trace.type === 'splom') {
2323
var xaxes = trace.xaxes || [];
2424
var yaxes = trace.yaxes || [];
@@ -36,28 +36,28 @@ exports.isTraceInSubplots = function isTraceInSubplots(trace, subplots) {
3636
};
3737

3838
// convenience functions for mapping all relevant axes
39-
exports.flat = function flat(subplots, v) {
39+
exports.flat = function(subplots, v) {
4040
var out = new Array(subplots.length);
4141
for(var i = 0; i < subplots.length; i++) {
4242
out[i] = v;
4343
}
4444
return out;
4545
};
4646

47-
exports.p2c = function p2c(axArray, v) {
47+
exports.p2c = function(axArray, v) {
4848
var out = new Array(axArray.length);
4949
for(var i = 0; i < axArray.length; i++) {
5050
out[i] = axArray[i].p2c(v);
5151
}
5252
return out;
5353
};
5454

55-
exports.getDistanceFunction = function getDistanceFunction(mode, dx, dy, dxy) {
55+
exports.getDistanceFunction = function(mode, dx, dy, dxy) {
5656
if(mode === 'closest') return dxy || exports.quadrature(dx, dy);
5757
return mode.charAt(0) === 'x' ? dx : dy;
5858
};
5959

60-
exports.getClosest = function getClosest(cd, distfn, pointData) {
60+
exports.getClosest = function(cd, distfn, pointData) {
6161
// do we already have a point number? (array mode only)
6262
if(pointData.index !== false) {
6363
if(pointData.index >= 0 && pointData.index < cd.length) {
@@ -87,11 +87,11 @@ exports.getClosest = function getClosest(cd, distfn, pointData) {
8787
* @param {number} v1: signed difference between the current position and the right edge
8888
* @param {number} passVal: the value to return on success
8989
*/
90-
exports.inbox = function inbox(v0, v1, passVal) {
90+
exports.inbox = function(v0, v1, passVal) {
9191
return (v0 * v1 < 0 || v0 === 0) ? passVal : Infinity;
9292
};
9393

94-
exports.quadrature = function quadrature(dx, dy) {
94+
exports.quadrature = function(dx, dy) {
9595
return function(di) {
9696
var x = dx(di);
9797
var y = dy(di);
@@ -114,7 +114,7 @@ exports.quadrature = function quadrature(dx, dy) {
114114
* @param {object} cd
115115
* @return {object}
116116
*/
117-
exports.makeEventData = function makeEventData(pt, trace, cd) {
117+
exports.makeEventData = function(pt, trace, cd) {
118118
// hover uses 'index', select uses 'pointNumber'
119119
var pointNumber = 'index' in pt ? pt.index : pt.pointNumber;
120120

@@ -238,3 +238,13 @@ function getPointData(val, pointNumber) {
238238
return val[pointNumber];
239239
}
240240
}
241+
242+
var unifiedHoverMode = {
243+
'x unified': true,
244+
'y unified': true
245+
};
246+
247+
exports.isUnifiedHover = function(hovermode) {
248+
if(typeof hovermode !== 'string') return false;
249+
return !!unifiedHoverMode[hovermode];
250+
};

src/components/fx/hover.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ function _hover(gd, evt, subplot, noHoverEvent) {
394394

395395
// within one trace mode can sometimes be overridden
396396
mode = hovermode;
397-
if(['x unified', 'y unified'].indexOf(mode) !== -1) {
397+
if(helpers.isUnifiedHover(mode)) {
398398
mode = mode.charAt(0);
399399
}
400400

@@ -715,7 +715,7 @@ function _hover(gd, evt, subplot, noHoverEvent) {
715715

716716
var hoverLabels = createHoverText(hoverData, labelOpts, gd);
717717

718-
if(['x unified', 'y unified'].indexOf(hovermode) === -1) {
718+
if(!helpers.isUnifiedHover(hovermode)) {
719719
hoverAvoidOverlaps(hoverLabels, rotateLabels ? 'xa' : 'ya', fullLayout);
720720
alignHoverText(hoverLabels, rotateLabels);
721721
}
@@ -976,7 +976,7 @@ function createHoverText(hoverData, opts, gd) {
976976
}
977977

978978
// Show a single hover label
979-
if(['x unified', 'y unified'].indexOf(hovermode) !== -1) {
979+
if(helpers.isUnifiedHover(hovermode)) {
980980
// Delete leftover hover labels from other hovermodes
981981
container.selectAll('g.hovertext').remove();
982982

src/components/fx/layout_defaults.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var Lib = require('../../lib');
12+
var isUnifiedHover = require('./helpers').isUnifiedHover;
1213
var layoutAttributes = require('./layout_attributes');
1314

1415
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
@@ -35,12 +36,8 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
3536

3637
var hoverMode = coerce('hovermode', hovermodeDflt);
3738
if(hoverMode) {
38-
var dflt;
39-
if(['x unified', 'y unified'].indexOf(hoverMode) !== -1) {
40-
dflt = -1;
41-
}
4239
coerce('hoverdistance');
43-
coerce('spikedistance', dflt);
40+
coerce('spikedistance', isUnifiedHover(hoverMode) ? -1 : undefined);
4441
}
4542

4643
// if only mapbox or geo subplots is present on graph,

src/components/modebar/manage.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var axisIds = require('../../plots/cartesian/axis_ids');
1313
var scatterSubTypes = require('../../traces/scatter/subtypes');
1414
var Registry = require('../../registry');
15+
var isUnifiedHover = require('../fx/helpers').isUnifiedHover;
1516

1617
var createModeBar = require('./modebar');
1718
var modeBarButtons = require('./buttons');
@@ -85,7 +86,7 @@ function getButtonGroups(gd) {
8586
var hasPolar = fullLayout._has('polar');
8687
var hasSankey = fullLayout._has('sankey');
8788
var allAxesFixed = areAllAxesFixed(fullLayout);
88-
var hasUnifiedHoverLabel = ['x unified', 'y unified'].indexOf(fullLayout.hovermode) !== -1;
89+
var hasUnifiedHoverLabel = isUnifiedHover(fullLayout.hovermode);
8990

9091
var groups = [];
9192

src/plots/cartesian/layout_defaults.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var Lib = require('../../lib');
1313
var Color = require('../../components/color');
14+
var isUnifiedHover = require('../../components/fx/helpers').isUnifiedHover;
1415
var Template = require('../../plot_api/plot_template');
1516
var basePlotLayoutAttributes = require('../layout_attributes');
1617

@@ -250,7 +251,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
250251
handleAxisDefaults(axLayoutIn, axLayoutOut, coerce, defaultOptions, layoutOut);
251252

252253
var hovermode = layoutOut.hovermode || layoutIn.hovermode;
253-
var unifiedHover = hovermode && ['x unified', 'y unified'].indexOf(hovermode) !== -1;
254+
var unifiedHover = isUnifiedHover(hovermode);
254255
var unifiedSpike = unifiedHover && axLetter === hovermode.charAt(0);
255256
var spikecolor = coerce2('spikecolor', unifiedHover ? axLayoutOut.color : undefined);
256257
var spikethickness = coerce2('spikethickness', unifiedHover ? 1.5 : undefined);

0 commit comments

Comments
 (0)