Skip to content

Commit 2868b25

Browse files
authoredDec 3, 2019
Merge pull request #4404 from plotly/sankey-v-tooltip
sankey: fix position of link hover labels in vertical orientation
2 parents fb8b28a + 1c29be9 commit 2868b25

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed
 

Diff for: ‎src/traces/sankey/plot.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,17 @@ module.exports = function plot(gd, calcData) {
178178
function hoverCenterPosition(link) {
179179
var hoverCenterX, hoverCenterY;
180180
if(link.circular) {
181-
hoverCenterX = (link.circularPathData.leftInnerExtent + link.circularPathData.rightInnerExtent) / 2 + d.parent.translateX;
182-
hoverCenterY = link.circularPathData.verticalFullExtent + d.parent.translateY;
181+
hoverCenterX = (link.circularPathData.leftInnerExtent + link.circularPathData.rightInnerExtent) / 2;
182+
hoverCenterY = link.circularPathData.verticalFullExtent;
183183
} else {
184-
hoverCenterX = (link.source.x1 + link.target.x0) / 2 + d.parent.translateX;
185-
hoverCenterY = (link.y0 + link.y1) / 2 + d.parent.translateY;
184+
hoverCenterX = (link.source.x1 + link.target.x0) / 2;
185+
hoverCenterY = (link.y0 + link.y1) / 2;
186186
}
187-
return [hoverCenterX, hoverCenterY];
187+
var center = [hoverCenterX, hoverCenterY];
188+
if(link.trace.orientation === 'v') center.reverse();
189+
center[0] += d.parent.translateX;
190+
center[1] += d.parent.translateY;
191+
return center;
188192
}
189193

190194
// For each related links, create a hoverItem

Diff for: ‎test/jasmine/tests/sankey_test.js

+36
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,42 @@ describe('sankey tests', function() {
777777
.then(done);
778778
});
779779

780+
it('should position hover labels correctly', function(done) {
781+
var gd = createGraphDiv();
782+
var mockCopy = Lib.extendDeep({}, mock);
783+
784+
Plotly.plot(gd, mockCopy)
785+
.then(function() {
786+
_hover(900, 230);
787+
788+
assertLabel(
789+
['source: Thermal generation', 'target: Losses', '787TWh'],
790+
['rgb(0, 0, 96)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
791+
);
792+
793+
var g = d3.select('.hovertext');
794+
var pos = g.node().getBoundingClientRect();
795+
expect(pos.x).toBeCloseTo(555, -1.5, 'it should have correct x position');
796+
expect(pos.y).toBeCloseTo(196, -1.5, 'it should have correct y position');
797+
return Plotly.restyle(gd, 'orientation', 'v');
798+
})
799+
.then(function() {
800+
_hover(520, 500);
801+
802+
assertLabel(
803+
['source: Thermal generation', 'target: Losses', '787TWh'],
804+
['rgb(0, 0, 96)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
805+
);
806+
807+
var g = d3.select('.hovertext');
808+
var pos = g.node().getBoundingClientRect();
809+
expect(pos.x).toBeCloseTo(279, -1.5, 'it should have correct x position');
810+
expect(pos.y).toBeCloseTo(500, -1.5, 'it should have correct y position');
811+
})
812+
.catch(failTest)
813+
.then(done);
814+
});
815+
780816
it('should show the correct hover labels when hovertemplate is specified', function(done) {
781817
var gd = createGraphDiv();
782818
var mockCopy = Lib.extendDeep({}, mock);

0 commit comments

Comments
 (0)