Skip to content

handle large circular sankey diagram #3535

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 6 commits into from
Feb 14, 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
17 changes: 13 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"country-regex": "^1.1.0",
"d3": "^3.5.12",
"@plotly/d3-sankey": "0.7.2",
"d3-sankey-circular": "0.30.0",
"d3-sankey-circular": "0.32.0",
"d3-force": "^1.0.6",
"d3-interpolate": "1",
"delaunay-triangulate": "^1.1.6",
Expand Down
2 changes: 1 addition & 1 deletion src/traces/sankey/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function sankeyModel(layout, d, traceIndex) {
if(circular) {
sankey = d3SankeyCircular
.sankeyCircular()
.circularLinkGap(2)
.circularLinkGap(0)
.nodeId(function(d) {
return d.pointNumber;
});
Expand Down
Binary file added test/image/baselines/sankey_circular_large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/sankey_circular_process.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/sankey_circular_simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/sankey_subplots_circular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
230 changes: 230 additions & 0 deletions test/image/mocks/sankey_circular_large.json

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions test/image/mocks/sankey_circular_simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"data": [
{
"type": "sankey",
"node": {
"pad": 5,
"label": ["0", "1", "2", "3", "sink", "source"]
},
"link": {
"source": [
0, 1, 2, 3,
0, 1, 2, 3,
5
],
"target": [
1, 2, 3, 0,
4, 4, 4, 4,
0
],
"value": [
1, 0.85, 0.7, 0.55,
0, 0.15, 0.15, 0.15,
0.45
]
}
}],
"layout": {
"width": 800,
"height": 800
}
}
183 changes: 112 additions & 71 deletions test/jasmine/tests/sankey_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var d3SankeyCircular = require('d3-sankey-circular');
var mock = require('@mocks/sankey_energy.json');
var mockDark = require('@mocks/sankey_energy_dark.json');
var mockCircular = require('@mocks/sankey_circular.json');
var mockCircularLarge = require('@mocks/sankey_circular_large.json');
var Sankey = require('@src/traces/sankey');

var createGraphDiv = require('../assets/create_graph_div');
Expand Down Expand Up @@ -1069,91 +1070,131 @@ describe('sankey layout generators', function() {
describe('d3-sankey-ciruclar', function() {
var data, sankey, graph;

function _calc(trace) {
var gd = { data: [trace] };

supplyAllDefaults(gd);
var fullTrace = gd._fullData[0];
return Sankey.calc(gd, fullTrace);
}
describe('implements d3-sankey compatible API', function() {
function _calc(trace) {
var gd = { data: [trace] };

supplyAllDefaults(gd);
var fullTrace = gd._fullData[0];
return Sankey.calc(gd, fullTrace);
}

beforeEach(function() {
data = _calc(mockCircular.data[0]);
data = {
nodes: data[0]._nodes,
links: data[0]._links
};
sankey = d3SankeyCircular
.sankeyCircular()
.iterations(32)
.circularLinkGap(2)
.nodePadding(10)
.size([500, 500])
.nodeId(function(d) {
return d.pointNumber;
})
.nodes(data.nodes)
.links(data.links);

graph = sankey();
});

beforeEach(function() {
data = _calc(mockCircular.data[0]);
data = {
nodes: data[0]._nodes,
links: data[0]._links
};
sankey = d3SankeyCircular
.sankeyCircular()
.iterations(32)
.circularLinkGap(2)
.nodePadding(10)
.size([500, 500])
.nodeId(function(d) {
return d.pointNumber;
})
.nodes(data.nodes)
.links(data.links);
it('creates a graph with circular links', function() {
expect(graph.nodes.length).toEqual(6, 'there are 6 nodes');
var circularLinks = graph.links.filter(function(link) {
return link.circular;
});
expect(circularLinks.length).toEqual(2, 'there are two circular links');
});

graph = sankey();
});
it('keep a list of nodes with positions in integer (depth, height)', function() {
checkArray(graph.nodes, 'depth', [0, 0, 2, 3, 1, 1]);
checkArray(graph.nodes, 'height', [1, 3, 1, 0, 2, 0]);
});

it('creates a graph with circular links', function() {
expect(graph.nodes.length).toEqual(6, 'there are 6 nodes');
var circularLinks = graph.links.filter(function(link) {
return link.circular;
it('keep a list of nodes with positions in x and y', function() {
checkRoundedArray(graph.nodes, 'x0', [72, 72, 267, 365, 169, 169]);
checkRoundedArray(graph.nodes, 'y0', [303, 86, 72, 109, 86, 359]);
});
expect(circularLinks.length).toEqual(2, 'there are two circular links');
});

it('keep a list of nodes with positions in integer (depth, height)', function() {
checkArray(graph.nodes, 'depth', [0, 0, 2, 3, 1, 1]);
checkArray(graph.nodes, 'height', [1, 3, 1, 0, 2, 0]);
});
it('supports column reordering', function() {
var reorder = [ 2, 2, 1, 1, 0, 0 ];

it('keep a list of nodes with positions in x and y', function() {
checkRoundedArray(graph.nodes, 'x0', [72, 72, 267, 365, 169, 169]);
checkRoundedArray(graph.nodes, 'y0', [303, 86, 72, 109, 86, 359]);
});
checkArray(graph.nodes, 'column', [0, 0, 2, 3, 1, 1]);

it('supports column reordering', function() {
var reorder = [ 2, 2, 1, 1, 0, 0 ];
var a = graph.nodes[0].x0;
sankey.nodeAlign(function(node) {
return reorder[node.pointNumber];
});
graph = sankey();
checkArray(graph.nodes, 'column', [2, 2, 1, 1, 0, 0]);
checkArray(graph.nodes, 'height', [1, 3, 1, 0, 2, 0]);
var b = graph.nodes[0].x0;
expect(a).not.toEqual(b);
});

checkArray(graph.nodes, 'column', [0, 0, 2, 3, 1, 1]);
it('updates links vertical position and circularLinkType upon moving nodes', function() {
var linkIndex = 6;
var nodeIndex = 2;
var delta = [0, 400];

var a = graph.nodes[0].x0;
sankey.nodeAlign(function(node) {
return reorder[node.pointNumber];
});
graph = sankey();
checkArray(graph.nodes, 'column', [2, 2, 1, 1, 0, 0]);
checkArray(graph.nodes, 'height', [1, 3, 1, 0, 2, 0]);
var b = graph.nodes[0].x0;
expect(a).not.toEqual(b);
});
var link = graph.links[linkIndex];
var linkY1 = link.y1;
var node = graph.nodes[nodeIndex];
var offsetTopToBottom = (node.y1 - node.y0) * link.value / node.value;

it('updates links vertical position and circularLinkType upon moving nodes', function() {
var linkIndex = 6;
var nodeIndex = 2;
var delta = [0, 400];
// Start with a circular link on top
expect(link.circular).toBeTruthy();
expect(link.circularLinkType).toEqual('top');

var link = graph.links[linkIndex];
var linkY1 = link.y1;
var node = graph.nodes[nodeIndex];
var offsetTopToBottom = (node.y1 - node.y0) * link.value / node.value;
// Update graph
var updatedGraph = moveNode(sankey, graph, nodeIndex, delta);
var updatedLink = updatedGraph.links[linkIndex];

// Start with a circular link on top
expect(link.circular).toBeTruthy();
expect(link.circularLinkType).toEqual('top');
// End up with a cirular link on bottom
expect(updatedLink.circular).toBeTruthy();
expect(updatedLink.circularLinkType).toEqual('bottom');
expect(updatedLink.y1).toBeCloseTo(linkY1 + delta[1] + offsetTopToBottom, 0);
});
});

// Update graph
var updatedGraph = moveNode(sankey, graph, nodeIndex, delta);
var updatedLink = updatedGraph.links[linkIndex];
describe('handles large number of links', function() {
function _calc(trace) {
var gd = { data: [trace] };

supplyAllDefaults(gd);
var fullTrace = gd._fullData[0];
return Sankey.calc(gd, fullTrace);
}

beforeEach(function() {
data = _calc(mockCircularLarge.data[0]);
data = {
nodes: data[0]._nodes,
links: data[0]._links
};
sankey = d3SankeyCircular
.sankeyCircular()
.iterations(32)
.nodePadding(10)
.size([500, 500])
.nodeId(function(d) {
return d.pointNumber;
})
.nodes(data.nodes)
.links(data.links);

graph = sankey();
});

// End up with a cirular link on bottom
expect(updatedLink.circular).toBeTruthy();
expect(updatedLink.circularLinkType).toEqual('bottom');
expect(updatedLink.y1).toBeCloseTo(linkY1 + delta[1] + offsetTopToBottom, 0);
it('creates a graph with circular links', function() {
expect(graph.nodes.length).toEqual(26, 'right number of nodes');
var circularLinks = graph.links.filter(function(link) {
return link.circular;
});
expect(circularLinks.length).toEqual(89, 'right number of circular links');
});
});
});
});