Skip to content

Sankey: properly set coordinates of node's origin prior to drag operation #3564

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 2 commits into from
Feb 21, 2019
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
4 changes: 2 additions & 2 deletions src/traces/sankey/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ function attachDragHandler(sankeyNode, sankeyLink, callbacks) {
var dragBehavior = d3.behavior.drag()
.origin(function(d) {
return {
x: d.node.x0,
y: d.node.y0
x: d.node.x0 + d.visibleWidth / 2,
y: d.node.y0 + d.visibleHeight / 2
};
})

Expand Down
52 changes: 52 additions & 0 deletions test/jasmine/tests/sankey_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var failTest = require('../assets/fail_test');
var mouseEvent = require('../assets/mouse_event');
var getNodeCoords = require('../assets/get_node_coords');
var assertHoverLabelStyle = require('../assets/custom_assertions').assertHoverLabelStyle;
var supplyAllDefaults = require('../assets/supply_defaults');
var defaultColors = require('@src/components/color/attributes').defaults;
Expand Down Expand Up @@ -905,6 +906,57 @@ describe('sankey tests', function() {
});
});

describe('Test drag interactions:', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! Fantastic test! Thanks!

var gd;

beforeEach(function() {
gd = createGraphDiv();
});

afterEach(destroyGraphDiv);

function _drag(fromX, fromY, dX, dY, delay) {
var toX = fromX + dX;
var toY = fromY + dY;

return new Promise(function(resolve) {
mouseEvent('mousemove', fromX, fromY);
mouseEvent('mousedown', fromX, fromY);
mouseEvent('mousemove', toX, toY);

setTimeout(function() {
mouseEvent('mouseup', toX, toY);
resolve();
}, delay);
});
}

it('should change the position of a node', function(done) {
var fig = Lib.extendDeep({}, mock);
var nodes;
var node;
var position;
var nodePos = [404, 302];
var move = [0, -100];

Plotly.plot(gd, fig)
.then(function() {
nodes = document.getElementsByClassName('sankey-node');
node = nodes.item(4); // Selecting node with label 'Solid'
position = getNodeCoords(node);
return _drag(nodePos[0], nodePos[1], move[0], move[1], 500);
})
.then(function() {
nodes = document.getElementsByClassName('sankey-node');
node = nodes.item(nodes.length - 1); // Dragged node is now the last one
var newPosition = getNodeCoords(node);
expect(newPosition.x).toBeCloseTo(position.x + move[0]);
expect(newPosition.y).toBeCloseTo(position.y + move[1]);
})
.catch(failTest)
.then(done);
});
});
it('emits a warning if node.pad is too large', function(done) {
var gd = createGraphDiv();
var mockCopy = Lib.extendDeep({}, mock);
Expand Down