Skip to content

Commit fd19907

Browse files
committed
move data conversion into its own function and run in calc() phase
1 parent 2504dc3 commit fd19907

File tree

3 files changed

+103
-89
lines changed

3 files changed

+103
-89
lines changed

src/traces/sankey/calc.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var tarjan = require('strongly-connected-components');
1212
var Lib = require('../../lib');
1313
var wrap = require('../../lib/gup').wrap;
1414

15+
var convertToD3Sankey = require('./convert-to-d3-sankey');
16+
1517
function circularityPresent(nodeList, sources, targets) {
1618

1719
var nodeLen = nodeList.length;
@@ -48,8 +50,10 @@ module.exports = function calc(gd, trace) {
4850
trace.node.color = [];
4951
}
5052

53+
var result = convertToD3Sankey(trace);
54+
5155
return wrap({
52-
link: trace.link,
53-
node: trace.node
56+
_nodes: result.nodes,
57+
_links: result.links
5458
});
5559
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright 2012-2018, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var Lib = require('../../lib');
12+
var isArrayOrTypedArray = Lib.isArrayOrTypedArray;
13+
var isIndex = Lib.isIndex;
14+
15+
module.exports = function(trace) {
16+
var nodeSpec = trace.node;
17+
var linkSpec = trace.link;
18+
19+
var links = [];
20+
var hasLinkColorArray = isArrayOrTypedArray(linkSpec.color);
21+
var linkedNodes = {};
22+
23+
var nodeCount = nodeSpec.label.length;
24+
var i;
25+
for(i = 0; i < linkSpec.value.length; i++) {
26+
var val = linkSpec.value[i];
27+
// remove negative values, but keep zeros with special treatment
28+
var source = linkSpec.source[i];
29+
var target = linkSpec.target[i];
30+
if(!(val > 0 && isIndex(source, nodeCount) && isIndex(target, nodeCount))) {
31+
continue;
32+
}
33+
34+
source = +source;
35+
target = +target;
36+
linkedNodes[source] = linkedNodes[target] = true;
37+
38+
links.push({
39+
pointNumber: i,
40+
label: linkSpec.label[i],
41+
color: hasLinkColorArray ? linkSpec.color[i] : linkSpec.color,
42+
source: source,
43+
target: target,
44+
value: +val
45+
});
46+
}
47+
48+
var hasNodeColorArray = isArrayOrTypedArray(nodeSpec.color);
49+
var nodes = [];
50+
var removedNodes = false;
51+
var nodeIndices = {};
52+
53+
for(i = 0; i < nodeCount; i++) {
54+
if(linkedNodes[i]) {
55+
var l = nodeSpec.label[i];
56+
nodeIndices[i] = nodes.length;
57+
nodes.push({
58+
pointNumber: i,
59+
label: l,
60+
color: hasNodeColorArray ? nodeSpec.color[i] : nodeSpec.color
61+
});
62+
} else removedNodes = true;
63+
}
64+
65+
// need to re-index links now, since we didn't put all the nodes in
66+
if(removedNodes) {
67+
for(i = 0; i < links.length; i++) {
68+
links[i].source = nodeIndices[links[i].source];
69+
links[i].target = nodeIndices[links[i].target];
70+
}
71+
}
72+
73+
return {
74+
links: links,
75+
nodes: nodes
76+
};
77+
};

src/traces/sankey/render.js

Lines changed: 20 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ var Drawing = require('../../components/drawing');
1616
var d3sankey = require('./d3-sankey.js');
1717
var d3Force = require('d3-force');
1818
var Lib = require('../../lib');
19-
var isArrayOrTypedArray = Lib.isArrayOrTypedArray;
20-
var isIndex = Lib.isIndex;
2119
var gup = require('../../lib/gup');
2220
var keyFun = gup.keyFun;
2321
var repeat = gup.repeat;
@@ -67,78 +65,18 @@ function switchToSankeyFormat(nodes) {
6765
// view models
6866

6967
function sankeyModel(layout, d, traceIndex) {
70-
var trace = unwrap(d).trace;
68+
var calcData = unwrap(d);
69+
var trace = calcData.trace;
7170
var domain = trace.domain;
72-
var nodeSpec = trace.node;
73-
var linkSpec = trace.link;
74-
var arrangement = trace.arrangement;
7571
var horizontal = trace.orientation === 'h';
7672
var nodePad = trace.node.pad;
7773
var nodeThickness = trace.node.thickness;
78-
var nodeLineColor = trace.node.line.color;
79-
var nodeLineWidth = trace.node.line.width;
80-
var linkLineColor = trace.link.line.color;
81-
var linkLineWidth = trace.link.line.width;
82-
var valueFormat = trace.valueformat;
83-
var valueSuffix = trace.valuesuffix;
84-
var textFont = trace.textfont;
8574

8675
var width = layout.width * (domain.x[1] - domain.x[0]);
8776
var height = layout.height * (domain.y[1] - domain.y[0]);
8877

89-
var links = [];
90-
var hasLinkColorArray = isArrayOrTypedArray(linkSpec.color);
91-
var linkedNodes = {};
92-
93-
var nodeCount = nodeSpec.label.length;
94-
var i;
95-
for(i = 0; i < linkSpec.value.length; i++) {
96-
var val = linkSpec.value[i];
97-
// remove negative values, but keep zeros with special treatment
98-
var source = linkSpec.source[i];
99-
var target = linkSpec.target[i];
100-
if(!(val > 0 && isIndex(source, nodeCount) && isIndex(target, nodeCount))) {
101-
continue;
102-
}
103-
104-
source = +source;
105-
target = +target;
106-
linkedNodes[source] = linkedNodes[target] = true;
107-
108-
links.push({
109-
pointNumber: i,
110-
label: linkSpec.label[i],
111-
color: hasLinkColorArray ? linkSpec.color[i] : linkSpec.color,
112-
source: source,
113-
target: target,
114-
value: +val
115-
});
116-
}
117-
118-
var hasNodeColorArray = isArrayOrTypedArray(nodeSpec.color);
119-
var nodes = [];
120-
var removedNodes = false;
121-
var nodeIndices = {};
122-
for(i = 0; i < nodeCount; i++) {
123-
if(linkedNodes[i]) {
124-
var l = nodeSpec.label[i];
125-
nodeIndices[i] = nodes.length;
126-
nodes.push({
127-
pointNumber: i,
128-
label: l,
129-
color: hasNodeColorArray ? nodeSpec.color[i] : nodeSpec.color
130-
});
131-
}
132-
else removedNodes = true;
133-
}
134-
135-
// need to re-index links now, since we didn't put all the nodes in
136-
if(removedNodes) {
137-
for(i = 0; i < links.length; i++) {
138-
links[i].source = nodeIndices[links[i].source];
139-
links[i].target = nodeIndices[links[i].target];
140-
}
141-
}
78+
var nodes = calcData._nodes;
79+
var links = calcData._links;
14280

14381
var sankey = d3sankey()
14482
.size(horizontal ? [width, height] : [height, width])
@@ -152,13 +90,6 @@ function sankeyModel(layout, d, traceIndex) {
15290
Lib.warn('node.pad was reduced to ', sankey.nodePadding(), ' to fit within the figure.');
15391
}
15492

155-
var node, sankeyNodes = sankey.nodes();
156-
for(var n = 0; n < sankeyNodes.length; n++) {
157-
node = sankeyNodes[n];
158-
node.width = width;
159-
node.height = height;
160-
}
161-
16293
switchToForceFormat(nodes);
16394

16495
return {
@@ -168,21 +99,21 @@ function sankeyModel(layout, d, traceIndex) {
16899
horizontal: horizontal,
169100
width: width,
170101
height: height,
171-
nodePad: nodePad,
172-
nodeLineColor: nodeLineColor,
173-
nodeLineWidth: nodeLineWidth,
174-
linkLineColor: linkLineColor,
175-
linkLineWidth: linkLineWidth,
176-
valueFormat: valueFormat,
177-
valueSuffix: valueSuffix,
178-
textFont: textFont,
102+
nodePad: trace.node.pad,
103+
nodeLineColor: trace.node.line.color,
104+
nodeLineWidth: trace.node.line.width,
105+
linkLineColor: trace.link.line.color,
106+
linkLineWidth: trace.link.line.width,
107+
valueFormat: trace.valueformat,
108+
valueSuffix: trace.valuesuffix,
109+
textFont: trace.textfont,
179110
translateX: domain.x[0] * layout.width + layout.margin.l,
180111
translateY: layout.height - domain.y[1] * layout.height + layout.margin.t,
181112
dragParallel: horizontal ? height : width,
182113
dragPerpendicular: horizontal ? width : height,
183114
nodes: nodes,
184115
links: links,
185-
arrangement: arrangement,
116+
arrangement: trace.arrangement,
186117
sankey: sankey,
187118
forceLayouts: {},
188119
interactionState: {
@@ -454,12 +385,14 @@ function snappingForce(sankeyNode, forceKey, nodes, d) {
454385
}
455386

456387
// scene graph
457-
module.exports = function(svg, styledData, layout, callbacks) {
388+
module.exports = function(svg, calcData, layout, callbacks) {
389+
390+
var styledData = calcData
391+
.filter(function(d) {return unwrap(d).trace.visible;})
392+
.map(sankeyModel.bind(null, layout));
393+
458394
var sankey = svg.selectAll('.' + c.cn.sankey)
459-
.data(styledData
460-
.filter(function(d) {return unwrap(d).trace.visible;})
461-
.map(sankeyModel.bind(null, layout)),
462-
keyFun);
395+
.data(styledData, keyFun);
463396

464397
sankey.exit()
465398
.remove();

0 commit comments

Comments
 (0)