Skip to content

Commit cb5a465

Browse files
authored
Merge pull request #2016 from plotly/sankey-default-link-label
Sankey default link label
2 parents 790d470 + 8715270 commit cb5a465

File tree

2 files changed

+89
-24
lines changed

2 files changed

+89
-24
lines changed

src/traces/sankey/plot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ module.exports = function plot(gd, calcData) {
145145
y: hoverCenterY - rootBBox.top,
146146
name: d3.format(d.valueFormat)(d.link.value) + d.valueSuffix,
147147
text: [
148-
d.link.label,
148+
d.link.label || '',
149149
['Source:', d.link.source.label].join(' '),
150150
['Target:', d.link.target.label].join(' ')
151151
].filter(renderableValuePresent).join('<br>'),

test/jasmine/tests/sankey_test.js

+88-23
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ describe('sankey tests', function() {
112112
expect(fullTrace.link.target)
113113
.toEqual([], 'presence of link target array is guaranteed');
114114

115+
expect(fullTrace.link.label)
116+
.toEqual([], 'presence of link target array is guaranteed');
115117
});
116118

117119
it('\'Sankey\' specification should have proper types',
@@ -179,6 +181,40 @@ describe('sankey tests', function() {
179181

180182
});
181183

184+
it('does not fill \'link\' labels even if not specified', function() {
185+
186+
var fullTrace = _supply({
187+
node: {
188+
label: ['a', 'b']
189+
},
190+
link: {
191+
source: [0, 1],
192+
target: [1, 0],
193+
value: [1, 2]
194+
}
195+
});
196+
197+
expect(Lib.isArray(fullTrace.link.label)).toBe(true, 'must be an array');
198+
expect(fullTrace.link.label).toEqual([], 'an array of empty strings');
199+
});
200+
201+
it('preserves \'link\' labels if specified', function() {
202+
203+
var fullTrace = _supply({
204+
node: {
205+
label: ['a', 'b']
206+
},
207+
link: {
208+
source: [0, 1],
209+
target: [1, 0],
210+
value: [1, 2],
211+
label: ['a', 'b']
212+
}
213+
});
214+
215+
expect(Lib.isArray(fullTrace.link.label)).toBe(true, 'must be an array');
216+
expect(fullTrace.link.label).toEqual(['a', 'b'], 'an array of the supplied values');
217+
});
182218
});
183219

184220
describe('sankey calc', function() {
@@ -316,29 +352,6 @@ describe('sankey tests', function() {
316352
describe('Test hover/click interactions:', function() {
317353
afterEach(destroyGraphDiv);
318354

319-
function assertLabel(content, style) {
320-
var g = d3.selectAll('.hovertext');
321-
var lines = g.selectAll('.nums .line');
322-
var name = g.selectAll('.name');
323-
324-
expect(lines.size()).toBe(content.length - 1);
325-
326-
lines.each(function(_, i) {
327-
expect(d3.select(this).text()).toBe(content[i]);
328-
});
329-
330-
expect(name.text()).toBe(content[content.length - 1]);
331-
332-
var path = g.select('path');
333-
expect(path.style('fill')).toEqual(style[0], 'bgcolor');
334-
expect(path.style('stroke')).toEqual(style[1], 'bordercolor');
335-
336-
var text = g.select('text.nums');
337-
expect(parseInt(text.style('font-size'))).toEqual(style[2], 'font.size');
338-
expect(text.style('font-family').split(',')[0]).toEqual(style[3], 'font.family');
339-
expect(text.style('fill')).toEqual(style[4], 'font.color');
340-
}
341-
342355
it('should shows the correct hover labels', function(done) {
343356
var gd = createGraphDiv();
344357
var mockCopy = Lib.extendDeep({}, mock);
@@ -409,6 +422,30 @@ describe('sankey tests', function() {
409422
.catch(fail)
410423
.then(done);
411424
});
425+
426+
it('should show correct hover labels even if there is no link.label supplied', function(done) {
427+
var gd = createGraphDiv();
428+
var mockCopy = Lib.extendDeep({}, mock);
429+
delete mockCopy.data[0].link.label;
430+
431+
function _hover(px, py) {
432+
mouseEvent('mousemove', px, py);
433+
mouseEvent('mouseover', px, py);
434+
delete gd._lastHoverTime;
435+
}
436+
437+
Plotly.plot(gd, mockCopy)
438+
.then(function() {
439+
_hover(450, 300);
440+
441+
assertLabel(
442+
['Source: Solid', 'Target: Industry', '46TWh'],
443+
['rgb(0, 0, 96)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
444+
);
445+
})
446+
.catch(fail)
447+
.then(done);
448+
});
412449
});
413450

414451
describe('Test hover/click event data:', function() {
@@ -520,3 +557,31 @@ describe('sankey tests', function() {
520557
});
521558
});
522559
});
560+
561+
function assertLabel(content, style) {
562+
var g = d3.selectAll('.hovertext');
563+
var lines = g.selectAll('.nums .line');
564+
var name = g.selectAll('.name');
565+
var tooltipBoundingBox = g.node().getBoundingClientRect();
566+
var nameBoundingBox = name.node().getBoundingClientRect();
567+
568+
expect(tooltipBoundingBox.top <= nameBoundingBox.top);
569+
expect(tooltipBoundingBox.bottom >= nameBoundingBox.bottom);
570+
571+
expect(lines.size()).toBe(content.length - 1);
572+
573+
lines.each(function(_, i) {
574+
expect(d3.select(this).text()).toBe(content[i]);
575+
});
576+
577+
expect(name.text()).toBe(content[content.length - 1]);
578+
579+
var path = g.select('path');
580+
expect(path.style('fill')).toEqual(style[0], 'bgcolor');
581+
expect(path.style('stroke')).toEqual(style[1], 'bordercolor');
582+
583+
var text = g.select('text.nums');
584+
expect(parseInt(text.style('font-size'))).toEqual(style[2], 'font.size');
585+
expect(text.style('font-family').split(',')[0]).toEqual(style[3], 'font.family');
586+
expect(text.style('fill')).toEqual(style[4], 'font.color');
587+
}

0 commit comments

Comments
 (0)