Skip to content

Commit 60bd4fc

Browse files
committed
factor out gl2d / gl3d cast hover options logic into 1 Fx method
1 parent 409b771 commit 60bd4fc

File tree

3 files changed

+27
-41
lines changed

3 files changed

+27
-41
lines changed

src/components/fx/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
getDistanceFunction: helpers.getDistanceFunction,
3636
getClosest: helpers.getClosest,
3737
inbox: helpers.inbox,
38+
castHoverOption: castHoverOption,
3839

3940
hover: require('./hover').hover,
4041
unhover: dragElement.unhover,
@@ -55,3 +56,19 @@ function loneUnhover(containerOrSelection) {
5556
selection.selectAll('g.hovertext').remove();
5657
selection.selectAll('.spikeline').remove();
5758
}
59+
60+
// Handler for trace-wide vs per-point hover label options
61+
function castHoverOption(trace, ptNumber, attr) {
62+
var labelOpts = trace.hoverlabel || {};
63+
var val = Lib.nestedProperty(labelOpts, attr).get();
64+
65+
if(Array.isArray(val)) {
66+
if(Array.isArray(ptNumber) && Array.isArray(val[ptNumber[0]])) {
67+
return val[ptNumber[0]][ptNumber[1]];
68+
} else {
69+
return val[ptNumber];
70+
}
71+
} else {
72+
return val;
73+
}
74+
}

src/plots/gl2d/scene2d.js

+5-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
'use strict';
1111

12-
var Lib = require('../../lib');
1312
var Registry = require('../../registry');
1413
var Axes = require('../../plots/cartesian/axes');
1514
var Fx = require('../../components/fx');
@@ -645,11 +644,11 @@ proto.draw = function() {
645644
zLabel: selection.traceCoord[2],
646645
text: selection.textLabel,
647646
name: selection.name,
648-
color: this.castHoverOption(trace, ptNumber, 'bgcolor') || selection.color,
649-
borderColor: this.castHoverOption(trace, ptNumber, 'bordercolor'),
650-
fontFamily: this.castHoverOption(trace, ptNumber, 'font.family'),
651-
fontSize: this.castHoverOption(trace, ptNumber, 'font.size'),
652-
fontColor: this.castHoverOption(trace, ptNumber, 'font.color')
647+
color: Fx.castHoverOption(trace, ptNumber, 'bgcolor') || selection.color,
648+
borderColor: Fx.castHoverOption(trace, ptNumber, 'bordercolor'),
649+
fontFamily: Fx.castHoverOption(trace, ptNumber, 'font.family'),
650+
fontSize: Fx.castHoverOption(trace, ptNumber, 'font.size'),
651+
fontColor: Fx.castHoverOption(trace, ptNumber, 'font.color')
653652
}, {
654653
container: this.svgContainer
655654
});
@@ -675,18 +674,3 @@ proto.hoverFormatter = function(axisName, val) {
675674
var axis = this[axisName];
676675
return Axes.tickText(axis, axis.c2l(val), 'hover').text;
677676
};
678-
679-
proto.castHoverOption = function(trace, ptNumber, attr) {
680-
var labelOpts = trace.hoverlabel || {};
681-
var val = Lib.nestedProperty(labelOpts, attr).get();
682-
683-
if(Array.isArray(val)) {
684-
if(Array.isArray(ptNumber) && Array.isArray(val[ptNumber[0]])) {
685-
return val[ptNumber[0]][ptNumber[1]];
686-
} else {
687-
return val[ptNumber];
688-
}
689-
} else {
690-
return val;
691-
}
692-
};

src/plots/gl3d/scene.js

+5-20
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,6 @@ function render(scene) {
6262
return Axes.tickText(axis, axis.d2l(val), 'hover').text;
6363
}
6464

65-
function castHoverOption(attr, ptNumber) {
66-
var labelOpts = trace.hoverlabel || {};
67-
var val = Lib.nestedProperty(labelOpts, attr).get();
68-
69-
if(Array.isArray(val)) {
70-
if(Array.isArray(ptNumber) && Array.isArray(val[ptNumber[0]])) {
71-
return val[ptNumber[0]][ptNumber[1]];
72-
} else {
73-
return val[ptNumber];
74-
}
75-
} else {
76-
return val;
77-
}
78-
}
79-
8065
var oldEventData;
8166

8267
if(lastPicked !== null) {
@@ -107,11 +92,11 @@ function render(scene) {
10792
zLabel: zVal,
10893
text: selection.textLabel,
10994
name: lastPicked.name,
110-
color: castHoverOption('bgcolor', ptNumber) || lastPicked.color,
111-
borderColor: castHoverOption('bordercolor', ptNumber),
112-
fontFamily: castHoverOption('font.family', ptNumber),
113-
fontSize: castHoverOption('font.size', ptNumber),
114-
fontColor: castHoverOption('font.color', ptNumber)
95+
color: Fx.castHoverOption(trace, ptNumber, 'bgcolor') || lastPicked.color,
96+
borderColor: Fx.castHoverOption(trace, ptNumber, 'bordercolor'),
97+
fontFamily: Fx.castHoverOption(trace, ptNumber, 'font.family'),
98+
fontSize: Fx.castHoverOption(trace, ptNumber, 'font.size'),
99+
fontColor: Fx.castHoverOption(trace, ptNumber, 'font.color')
115100
}, {
116101
container: svgContainer
117102
});

0 commit comments

Comments
 (0)