Skip to content

Sankey default link label #2016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/traces/sankey/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module.exports = function plot(gd, calcData) {
y: hoverCenterY - rootBBox.top,
name: d3.format(d.valueFormat)(d.link.value) + d.valueSuffix,
text: [
d.link.label,
d.link.label || '',
['Source:', d.link.source.label].join(' '),
['Target:', d.link.target.label].join(' ')
].filter(renderableValuePresent).join('<br>'),
Expand Down
111 changes: 88 additions & 23 deletions test/jasmine/tests/sankey_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ describe('sankey tests', function() {
expect(fullTrace.link.target)
.toEqual([], 'presence of link target array is guaranteed');

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

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

});

it('does not fill \'link\' labels even if not specified', function() {

var fullTrace = _supply({
node: {
label: ['a', 'b']
},
link: {
source: [0, 1],
target: [1, 0],
value: [1, 2]
}
});

expect(Lib.isArray(fullTrace.link.label)).toBe(true, 'must be an array');
expect(fullTrace.link.label).toEqual([], 'an array of empty strings');
});

it('preserves \'link\' labels if specified', function() {

var fullTrace = _supply({
node: {
label: ['a', 'b']
},
link: {
source: [0, 1],
target: [1, 0],
value: [1, 2],
label: ['a', 'b']
}
});

expect(Lib.isArray(fullTrace.link.label)).toBe(true, 'must be an array');
expect(fullTrace.link.label).toEqual(['a', 'b'], 'an array of the supplied values');
});
});

describe('sankey calc', function() {
Expand Down Expand Up @@ -316,29 +352,6 @@ describe('sankey tests', function() {
describe('Test hover/click interactions:', function() {
afterEach(destroyGraphDiv);

function assertLabel(content, style) {
var g = d3.selectAll('.hovertext');
var lines = g.selectAll('.nums .line');
var name = g.selectAll('.name');

expect(lines.size()).toBe(content.length - 1);

lines.each(function(_, i) {
expect(d3.select(this).text()).toBe(content[i]);
});

expect(name.text()).toBe(content[content.length - 1]);

var path = g.select('path');
expect(path.style('fill')).toEqual(style[0], 'bgcolor');
expect(path.style('stroke')).toEqual(style[1], 'bordercolor');

var text = g.select('text.nums');
expect(parseInt(text.style('font-size'))).toEqual(style[2], 'font.size');
expect(text.style('font-family').split(',')[0]).toEqual(style[3], 'font.family');
expect(text.style('fill')).toEqual(style[4], 'font.color');
}

it('should shows the correct hover labels', function(done) {
var gd = createGraphDiv();
var mockCopy = Lib.extendDeep({}, mock);
Expand Down Expand Up @@ -409,6 +422,30 @@ describe('sankey tests', function() {
.catch(fail)
.then(done);
});

it('should show correct hover labels even if there is no link.label supplied', function(done) {
var gd = createGraphDiv();
var mockCopy = Lib.extendDeep({}, mock);
delete mockCopy.data[0].link.label;

function _hover(px, py) {
mouseEvent('mousemove', px, py);
mouseEvent('mouseover', px, py);
delete gd._lastHoverTime;
}

Plotly.plot(gd, mockCopy)
.then(function() {
_hover(450, 300);

assertLabel(
['Source: Solid', 'Target: Industry', '46TWh'],
['rgb(0, 0, 96)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
);
})
.catch(fail)
.then(done);
});
});

describe('Test hover/click event data:', function() {
Expand Down Expand Up @@ -520,3 +557,31 @@ describe('sankey tests', function() {
});
});
});

function assertLabel(content, style) {
var g = d3.selectAll('.hovertext');
var lines = g.selectAll('.nums .line');
var name = g.selectAll('.name');
var tooltipBoundingBox = g.node().getBoundingClientRect();
var nameBoundingBox = name.node().getBoundingClientRect();

expect(tooltipBoundingBox.top <= nameBoundingBox.top);
expect(tooltipBoundingBox.bottom >= nameBoundingBox.bottom);

expect(lines.size()).toBe(content.length - 1);

lines.each(function(_, i) {
expect(d3.select(this).text()).toBe(content[i]);
});

expect(name.text()).toBe(content[content.length - 1]);

var path = g.select('path');
expect(path.style('fill')).toEqual(style[0], 'bgcolor');
expect(path.style('stroke')).toEqual(style[1], 'bordercolor');

var text = g.select('text.nums');
expect(parseInt(text.style('font-size'))).toEqual(style[2], 'font.size');
expect(text.style('font-family').split(',')[0]).toEqual(style[3], 'font.family');
expect(text.style('fill')).toEqual(style[4], 'font.color');
}