Skip to content

Commit 0c9c75f

Browse files
committed
Bring back customHovers
1 parent 3e43ef3 commit 0c9c75f

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/components/fx/hover.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,82 @@ exports.loneHover = function loneHover(hoverItem, opts) {
153153
return hoverLabel.node();
154154
};
155155

156+
// TODO: replace loneHover?
157+
exports.customHovers = function customHovers(hoverItems, opts) {
158+
159+
if(!Array.isArray(hoverItems)) {
160+
hoverItems = [hoverItems];
161+
}
162+
163+
var pointsData = hoverItems.map(function(hoverItem) {
164+
return {
165+
color: hoverItem.color || Color.defaultLine,
166+
x0: hoverItem.x0 || hoverItem.x || 0,
167+
x1: hoverItem.x1 || hoverItem.x || 0,
168+
y0: hoverItem.y0 || hoverItem.y || 0,
169+
y1: hoverItem.y1 || hoverItem.y || 0,
170+
xLabel: hoverItem.xLabel,
171+
yLabel: hoverItem.yLabel,
172+
zLabel: hoverItem.zLabel,
173+
text: hoverItem.text,
174+
name: hoverItem.name,
175+
idealAlign: hoverItem.idealAlign,
176+
177+
// optional extra bits of styling
178+
borderColor: hoverItem.borderColor,
179+
fontFamily: hoverItem.fontFamily,
180+
fontSize: hoverItem.fontSize,
181+
fontColor: hoverItem.fontColor,
182+
183+
// filler to make createHoverText happy
184+
trace: {
185+
index: 0,
186+
hoverinfo: ''
187+
},
188+
xa: {_offset: 0},
189+
ya: {_offset: 0},
190+
index: 0
191+
};
192+
});
193+
194+
195+
var container3 = d3.select(opts.container),
196+
outerContainer3 = opts.outerContainer ?
197+
d3.select(opts.outerContainer) : container3;
198+
199+
var fullOpts = {
200+
hovermode: 'closest',
201+
rotateLabels: false,
202+
bgColor: opts.bgColor || Color.background,
203+
container: container3,
204+
outerContainer: outerContainer3
205+
};
206+
207+
var hoverLabel = createHoverText(pointsData, fullOpts, opts.gd);
208+
209+
// Fix vertical overlap
210+
var tooltipSpacing = 5;
211+
var lastBottomY = 0;
212+
hoverLabel
213+
.sort(function(a, b) {return a.y0 - b.y0;})
214+
.each(function(d) {
215+
var topY = d.y0 - d.by / 2;
216+
217+
if((topY - tooltipSpacing) < lastBottomY) {
218+
d.offset = (lastBottomY - topY) + tooltipSpacing;
219+
} else {
220+
d.offset = 0;
221+
}
222+
223+
lastBottomY = topY + d.by + d.offset;
224+
});
225+
226+
227+
alignHoverText(hoverLabel, fullOpts.rotateLabels);
228+
229+
return hoverLabel.node();
230+
};
231+
156232
// The actual implementation is here:
157233
function _hover(gd, evt, subplot, noHoverEvent) {
158234
if(!subplot) subplot = 'xy';

0 commit comments

Comments
 (0)