Skip to content

Commit f00b542

Browse files
committedApr 5, 2019
sankey: center hoverlabel associated with hovered link
1 parent 5c6ef80 commit f00b542

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed
 

‎src/components/fx/hover.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,10 @@ exports.multiHovers = function multiHovers(hoverItems, opts) {
213213
// Fix vertical overlap
214214
var tooltipSpacing = 5;
215215
var lastBottomY = 0;
216+
var anchor = 0;
216217
hoverLabel
217218
.sort(function(a, b) {return a.y0 - b.y0;})
218-
.each(function(d) {
219+
.each(function(d, i) {
219220
var topY = d.y0 - d.by / 2;
220221

221222
if((topY - tooltipSpacing) < lastBottomY) {
@@ -225,8 +226,12 @@ exports.multiHovers = function multiHovers(hoverItems, opts) {
225226
}
226227

227228
lastBottomY = topY + d.by + d.offset;
228-
});
229229

230+
if(i === opts.anchorIndex || 0) anchor = d.offset;
231+
})
232+
.each(function(d) {
233+
d.offset -= anchor;
234+
});
230235

231236
alignHoverText(hoverLabel, fullOpts.rotateLabels);
232237

‎src/traces/sankey/plot.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,32 @@ module.exports = function plot(gd, calcData) {
159159

160160
var hoverItems = [];
161161

162-
// For each related links, create a hoverItem
163-
for(var i = 0; i < d.flow.links.length; i++) {
164-
var link = d.flow.links[i];
165-
if(gd._fullLayout.hovermode === 'closest' && d.link.pointNumber !== link.pointNumber) continue;
166-
link.fullData = link.trace;
167-
obj = d.link.trace.link;
168-
var hoverCenterX;
169-
var hoverCenterY;
162+
function hoverCenterPosition(link) {
163+
var hoverCenterX, hoverCenterY;
170164
if(link.circular) {
171165
hoverCenterX = (link.circularPathData.leftInnerExtent + link.circularPathData.rightInnerExtent) / 2 + d.parent.translateX;
172166
hoverCenterY = link.circularPathData.verticalFullExtent + d.parent.translateY;
173167
} else {
174168
hoverCenterX = (link.source.x1 + link.target.x0) / 2 + d.parent.translateX;
175169
hoverCenterY = (link.y0 + link.y1) / 2 + d.parent.translateY;
176170
}
171+
return [hoverCenterX, hoverCenterY];
172+
}
177173

174+
// For each related links, create a hoverItem
175+
var anchorIndex = 0;
176+
for(var i = 0; i < d.flow.links.length; i++) {
177+
var link = d.flow.links[i];
178+
if(gd._fullLayout.hovermode === 'closest' && d.link.pointNumber !== link.pointNumber) continue;
179+
if(d.link.pointNumber === link.pointNumber) anchorIndex = i;
180+
link.fullData = link.trace;
181+
obj = d.link.trace.link;
182+
var hoverCenter = hoverCenterPosition(link);
178183
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(link.value) + d.valueSuffix};
179184

180185
hoverItems.push({
181-
x: hoverCenterX,
182-
y: hoverCenterY,
186+
x: hoverCenter[0],
187+
y: hoverCenter[1],
183188
name: hovertemplateLabels.valueLabel,
184189
text: [
185190
link.label || '',
@@ -192,7 +197,7 @@ module.exports = function plot(gd, calcData) {
192197
fontFamily: castHoverOption(obj, 'font.family'),
193198
fontSize: castHoverOption(obj, 'font.size'),
194199
fontColor: castHoverOption(obj, 'font.color'),
195-
idealAlign: d3.event.x < hoverCenterX ? 'right' : 'left',
200+
idealAlign: d3.event.x < hoverCenter[0] ? 'right' : 'left',
196201

197202
hovertemplate: obj.hovertemplate,
198203
hovertemplateLabels: hovertemplateLabels,
@@ -203,7 +208,8 @@ module.exports = function plot(gd, calcData) {
203208
var tooltips = Fx.multiHovers(hoverItems, {
204209
container: fullLayout._hoverlayer.node(),
205210
outerContainer: fullLayout._paper.node(),
206-
gd: gd
211+
gd: gd,
212+
anchorIndex: anchorIndex
207213
});
208214

209215
tooltips.each(function() {

‎test/image/mocks/sankey_circular_large.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,11 @@
220220
[1, "#9467bd"]
221221
]
222222
}],
223-
"hovertemplate": "<b>%{label}</b><br>%{flow.labelConcentration:%0.2f}<br>%{flow.value}"
223+
"hovertemplate": "%{label}<br><b>flow.labelConcentration</b>: %{flow.labelConcentration:0.2%}<br><b>flow.concentration</b>: %{flow.concentration:0.2%}<br><b>flow.value</b>: %{flow.value}"
224224
}
225225
}],
226226
"layout": {
227+
"hovermode": "x",
227228
"width": 800,
228229
"height": 800
229230
}

0 commit comments

Comments
 (0)
Please sign in to comment.