Skip to content

Commit f7348dd

Browse files
committed
sankey: multiple hover labels for links part of a flow
1 parent 5abfe58 commit f7348dd

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

src/traces/sankey/plot.js

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -156,42 +156,49 @@ module.exports = function plot(gd, calcData) {
156156
if(gd._fullLayout.hovermode === false) return;
157157
var obj = d.link.trace.link;
158158
if(obj.hoverinfo === 'none' || obj.hoverinfo === 'skip') return;
159-
var rootBBox = gd._fullLayout._paperdiv.node().getBoundingClientRect();
160-
var hoverCenterX;
161-
var hoverCenterY;
162-
if(d.link.circular) {
163-
hoverCenterX = (d.link.circularPathData.leftInnerExtent + d.link.circularPathData.rightInnerExtent) / 2 + d.parent.translateX;
164-
hoverCenterY = d.link.circularPathData.verticalFullExtent + d.parent.translateY;
165-
} else {
166-
var boundingBox = element.getBoundingClientRect();
167-
hoverCenterX = boundingBox.left + boundingBox.width / 2 - rootBBox.left;
168-
hoverCenterY = boundingBox.top + boundingBox.height / 2 - rootBBox.top;
169-
}
170159

171-
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(d.link.value) + d.valueSuffix};
172-
d.link.fullData = d.link.trace;
160+
var hoverItems = [];
161+
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+
obj = link;
166+
var hoverCenterX;
167+
var hoverCenterY;
168+
if(link.circular) {
169+
hoverCenterX = (link.circularPathData.leftInnerExtent + link.circularPathData.rightInnerExtent) / 2 + d.parent.translateX;
170+
hoverCenterY = link.circularPathData.verticalFullExtent + d.parent.translateY;
171+
} else {
172+
hoverCenterX = (link.source.x1 + link.target.x0) / 2 + d.parent.translateX;
173+
hoverCenterY = (link.y0 + link.y1) / 2 + d.parent.translateY;
174+
}
173175

174-
var tooltip = Fx.loneHover({
175-
x: hoverCenterX,
176-
y: hoverCenterY,
177-
name: hovertemplateLabels.valueLabel,
178-
text: [
179-
d.link.label || '',
180-
sourceLabel + d.link.source.label,
181-
targetLabel + d.link.target.label,
182-
d.link.concentrationscale ? concentrationLabel + d3.format('%0.2f')(d.link.flow.labelConcentration) : ''
183-
].filter(renderableValuePresent).join('<br>'),
184-
color: castHoverOption(obj, 'bgcolor') || Color.addOpacity(d.tinyColorHue, 1),
185-
borderColor: castHoverOption(obj, 'bordercolor'),
186-
fontFamily: castHoverOption(obj, 'font.family'),
187-
fontSize: castHoverOption(obj, 'font.size'),
188-
fontColor: castHoverOption(obj, 'font.color'),
189-
idealAlign: d3.event.x < hoverCenterX ? 'right' : 'left',
176+
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(link.value) + d.valueSuffix};
177+
178+
hoverItems.push({
179+
x: hoverCenterX,
180+
y: hoverCenterY,
181+
name: hovertemplateLabels.valueLabel,
182+
text: [
183+
link.label || '',
184+
sourceLabel + link.source.label,
185+
targetLabel + link.target.label,
186+
link.concentrationscale ? concentrationLabel + d3.format('%0.2f')(link.flow.labelConcentration) : ''
187+
].filter(renderableValuePresent).join('<br>'),
188+
color: castHoverOption(obj, 'bgcolor') || Color.addOpacity(link.color, 1),
189+
borderColor: castHoverOption(obj, 'bordercolor'),
190+
fontFamily: castHoverOption(obj, 'font.family'),
191+
fontSize: castHoverOption(obj, 'font.size'),
192+
fontColor: castHoverOption(obj, 'font.color'),
193+
idealAlign: d3.event.x < hoverCenterX ? 'right' : 'left',
194+
195+
hovertemplate: obj.hovertemplate,
196+
hovertemplateLabels: hovertemplateLabels,
197+
eventData: [link]
198+
});
199+
}
190200

191-
hovertemplate: obj.hovertemplate,
192-
hovertemplateLabels: hovertemplateLabels,
193-
eventData: [d.link]
194-
}, {
201+
var tooltip = Fx.multiHovers(hoverItems, {
195202
container: fullLayout._hoverlayer.node(),
196203
outerContainer: fullLayout._paper.node(),
197204
gd: gd

src/traces/sankey/render.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ function sankeyModel(layout, d, traceIndex) {
142142
concentration: link.value / total,
143143
links: flowLinks
144144
};
145+
if(link.concentrationscale) {
146+
link.color = tinycolor(link.concentrationscale(link.flow.labelConcentration));
147+
}
145148
}
146149
}
147150

@@ -287,9 +290,6 @@ function sankeyModel(layout, d, traceIndex) {
287290

288291
function linkModel(d, l, i) {
289292
var tc = tinycolor(l.color);
290-
if(l.concentrationscale) {
291-
tc = tinycolor(l.concentrationscale(l.flow.labelConcentration));
292-
}
293293
var basicKey = l.source.label + '|' + l.target.label;
294294
var key = basicKey + '__' + i;
295295

0 commit comments

Comments
 (0)