提交 d73a4200 编写于 作者: D deqingli

refactor(sankey): change code style to reduce the size of code in sankeyLayout.js

上级 c8f5205b
...@@ -142,12 +142,9 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod ...@@ -142,12 +142,9 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod
maxNodeDepth = item.depth; maxNodeDepth = item.depth;
} }
node.setLayout({depth: isItemDepth ? item.depth : x}, true); node.setLayout({depth: isItemDepth ? item.depth : x}, true);
if (orient === 'vertical') { orient === 'vertical' ? node.setLayout({dy: nodeWidth}, true)
node.setLayout({dy: nodeWidth}, true); : node.setLayout({dx: nodeWidth}, true);
}
else {
node.setLayout({dx: nodeWidth}, true);
}
for (var edgeIdx = 0; edgeIdx < node.outEdges.length; edgeIdx++) { for (var edgeIdx = 0; edgeIdx < node.outEdges.length; edgeIdx++) {
var edge = node.outEdges[edgeIdx]; var edge = node.outEdges[edgeIdx];
var indexEdge = edges.indexOf(edge); var indexEdge = edges.indexOf(edge);
...@@ -174,7 +171,8 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod ...@@ -174,7 +171,8 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod
if (nodeAlign && nodeAlign !== 'left') { if (nodeAlign && nodeAlign !== 'left') {
adjustNodeWithNodeAlign(nodes, nodeAlign, orient, maxDepth); adjustNodeWithNodeAlign(nodes, nodeAlign, orient, maxDepth);
} }
var kx = orient === 'vertical' ? (height - nodeWidth) / maxDepth : (width - nodeWidth) / maxDepth; var kx = orient === 'vertical' ? (height - nodeWidth) / maxDepth
: (width - nodeWidth) / maxDepth;
scaleNodeBreadths(nodes, kx, orient); scaleNodeBreadths(nodes, kx, orient);
} }
...@@ -211,7 +209,7 @@ function adjustNodeWithNodeAlign(nodes, nodeAlign, orient, maxDepth) { ...@@ -211,7 +209,7 @@ function adjustNodeWithNodeAlign(nodes, nodeAlign, orient, maxDepth) {
}); });
} }
else if (nodeAlign === 'justify') { else if (nodeAlign === 'justify') {
moveSinksRight(nodes, maxDepth, orient); moveSinksRight(nodes, maxDepth);
} }
} }
...@@ -219,11 +217,10 @@ function adjustNodeWithNodeAlign(nodes, nodeAlign, orient, maxDepth) { ...@@ -219,11 +217,10 @@ function adjustNodeWithNodeAlign(nodes, nodeAlign, orient, maxDepth) {
* All the node without outEgdes are assigned maximum x-position and * All the node without outEgdes are assigned maximum x-position and
* be aligned in the last column. * be aligned in the last column.
* *
* @param {module:echarts/data/Graph~Node} nodes node of sankey view * @param {module:echarts/data/Graph~Node} nodes. node of sankey view.
* @param {number} x value (x-1) use to assign to node without outEdges * @param {number} maxDepth. use to assign to node without outEdges as x-position.
* as x-position
*/ */
function moveSinksRight(nodes, maxDepth, orient) { function moveSinksRight(nodes, maxDepth) {
zrUtil.each(nodes, function (node) { zrUtil.each(nodes, function (node) {
if (!isNodeDepth(node) && !node.outEdges.length) { if (!isNodeDepth(node) && !node.outEdges.length) {
node.setLayout({depth: maxDepth}, true); node.setLayout({depth: maxDepth}, true);
...@@ -240,12 +237,8 @@ function moveSinksRight(nodes, maxDepth, orient) { ...@@ -240,12 +237,8 @@ function moveSinksRight(nodes, maxDepth, orient) {
function scaleNodeBreadths(nodes, kx, orient) { function scaleNodeBreadths(nodes, kx, orient) {
zrUtil.each(nodes, function (node) { zrUtil.each(nodes, function (node) {
var nodeDepth = node.getLayout().depth * kx; var nodeDepth = node.getLayout().depth * kx;
if (orient === 'vertical') { orient === 'vertical' ? node.setLayout({y: nodeDepth}, true)
node.setLayout({y: nodeDepth}, true); : node.setLayout({x: nodeDepth}, true);
}
else {
node.setLayout({x: nodeDepth}, true);
}
}); });
} }
...@@ -304,31 +297,23 @@ function prepareNodesByBreadth(nodes, orient) { ...@@ -304,31 +297,23 @@ function prepareNodesByBreadth(nodes, orient) {
* @param {number} nodeGap the vertical distance between two nodes * @param {number} nodeGap the vertical distance between two nodes
*/ */
function initializeNodeDepth(nodesByBreadth, edges, height, width, nodeGap, orient) { function initializeNodeDepth(nodesByBreadth, edges, height, width, nodeGap, orient) {
var kyArray = []; var minKy = Infinity;
zrUtil.each(nodesByBreadth, function (nodes) { zrUtil.each(nodesByBreadth, function (nodes) {
var n = nodes.length; var n = nodes.length;
var sum = 0; var sum = 0;
var ky = 0;
zrUtil.each(nodes, function (node) { zrUtil.each(nodes, function (node) {
sum += node.getLayout().value; sum += node.getLayout().value;
}); });
if (orient === 'vertical') { var ky = orient === 'vertical' ? (width - (n - 1) * nodeGap) / sum
ky = (width - (n - 1) * nodeGap) / sum; : (height - (n - 1) * nodeGap) / sum;
if (ky < minKy) {
minKy = ky;
} }
else {
ky = (height - (n - 1) * nodeGap) / sum;
}
kyArray.push(ky);
});
kyArray.sort(function (a, b) {
return a - b;
}); });
var ky0 = kyArray[0];
zrUtil.each(nodesByBreadth, function (nodes) { zrUtil.each(nodesByBreadth, function (nodes) {
zrUtil.each(nodes, function (node, i) { zrUtil.each(nodes, function (node, i) {
var nodeDy = node.getLayout().value * ky0; var nodeDy = node.getLayout().value * minKy;
if (orient === 'vertical') { if (orient === 'vertical') {
node.setLayout({x: i}, true); node.setLayout({x: i}, true);
node.setLayout({dx: nodeDy}, true); node.setLayout({dx: nodeDy}, true);
...@@ -337,12 +322,11 @@ function initializeNodeDepth(nodesByBreadth, edges, height, width, nodeGap, orie ...@@ -337,12 +322,11 @@ function initializeNodeDepth(nodesByBreadth, edges, height, width, nodeGap, orie
node.setLayout({y: i}, true); node.setLayout({y: i}, true);
node.setLayout({dy: nodeDy}, true); node.setLayout({dy: nodeDy}, true);
} }
}); });
}); });
zrUtil.each(edges, function (edge) { zrUtil.each(edges, function (edge) {
var edgeDy = +edge.getValue() * ky0; var edgeDy = +edge.getValue() * minKy;
edge.setLayout({dy: edgeDy}, true); edge.setLayout({dy: edgeDy}, true);
}); });
} }
...@@ -356,73 +340,44 @@ function initializeNodeDepth(nodesByBreadth, edges, height, width, nodeGap, orie ...@@ -356,73 +340,44 @@ function initializeNodeDepth(nodesByBreadth, edges, height, width, nodeGap, orie
* @param {number} height the whole height of the area to draw the view * @param {number} height the whole height of the area to draw the view
*/ */
function resolveCollisions(nodesByBreadth, nodeGap, height, width, orient) { function resolveCollisions(nodesByBreadth, nodeGap, height, width, orient) {
var keyAttr = orient === 'vertical' ? 'x' : 'y';
zrUtil.each(nodesByBreadth, function (nodes) { zrUtil.each(nodesByBreadth, function (nodes) {
nodes.sort(function (a, b) {
return a.getLayout()[keyAttr] - b.getLayout()[keyAttr];
});
var nodeX;
var node; var node;
var dy; var dy;
var y0 = 0; var y0 = 0;
var n = nodes.length; var n = nodes.length;
var i; var nodeDyAttr = orient === 'vertical' ? 'dx' : 'dy';
for (var i = 0; i < n; i++) {
if (orient === 'vertical') { node = nodes[i];
var nodeX; dy = y0 - node.getLayout()[keyAttr];
nodes.sort(function (a, b) {
return a.getLayout().x - b.getLayout().x;
});
for (i = 0; i < n; i++) {
node = nodes[i];
dy = y0 - node.getLayout().x;
if (dy > 0) {
nodeX = node.getLayout().x + dy;
node.setLayout({x: nodeX}, true);
}
y0 = node.getLayout().x + node.getLayout().dx + nodeGap;
}
// If the bottommost node goes outside the bounds, push it back up
dy = y0 - nodeGap - width;
if (dy > 0) { if (dy > 0) {
nodeX = node.getLayout().x - dy; nodeX = node.getLayout()[keyAttr] + dy;
node.setLayout({x: nodeX}, true); orient === 'vertical' ? node.setLayout({x: nodeX}, true)
y0 = nodeX; : node.setLayout({y: nodeX}, true);
for (i = n - 2; i >= 0; --i) {
node = nodes[i];
dy = node.getLayout().x + node.getLayout().dx + nodeGap - y0;
if (dy > 0) {
nodeX = node.getLayout().x - dy;
node.setLayout({x: nodeX}, true);
}
y0 = node.getLayout().x;
}
} }
y0 = node.getLayout()[keyAttr] + node.getLayout()[nodeDyAttr] + nodeGap;
} }
else { var viewWidth = orient === 'vertical' ? width : height;
var nodeY; // If the bottommost node goes outside the bounds, push it back up
nodes.sort(function (a, b) { dy = y0 - nodeGap - viewWidth;
return a.getLayout().y - b.getLayout().y; if (dy > 0) {
}); nodeX = node.getLayout()[keyAttr] - dy;
for (i = 0; i < n; i++) { orient === 'vertical' ? node.setLayout({x: nodeX}, true)
: node.setLayout({y: nodeX}, true);
y0 = nodeX;
for (i = n - 2; i >= 0; --i) {
node = nodes[i]; node = nodes[i];
dy = y0 - node.getLayout().y; dy = node.getLayout()[keyAttr] + node.getLayout()[nodeDyAttr] + nodeGap - y0;
if (dy > 0) { if (dy > 0) {
nodeY = node.getLayout().y + dy; nodeX = node.getLayout()[keyAttr] - dy;
node.setLayout({y: nodeY}, true); orient === 'vertical' ? node.setLayout({x: nodeX}, true)
} : node.setLayout({y: nodeX}, true);
y0 = node.getLayout().y + node.getLayout().dy + nodeGap;
}
// If the bottommost node goes outside the bounds, push it back up
dy = y0 - nodeGap - height;
if (dy > 0) {
nodeY = node.getLayout().y - dy;
node.setLayout({y: nodeY}, true);
y0 = nodeY;
for (i = n - 2; i >= 0; --i) {
node = nodes[i];
dy = node.getLayout().y + node.getLayout().dy + nodeGap - y0;
if (dy > 0) {
nodeY = node.getLayout().y - dy;
node.setLayout({y: nodeY}, true);
}
y0 = node.getLayout().y;
} }
y0 = node.getLayout()[keyAttr];
} }
} }
}); });
...@@ -439,7 +394,8 @@ function relaxRightToLeft(nodesByBreadth, alpha, orient) { ...@@ -439,7 +394,8 @@ function relaxRightToLeft(nodesByBreadth, alpha, orient) {
zrUtil.each(nodesByBreadth.slice().reverse(), function (nodes) { zrUtil.each(nodesByBreadth.slice().reverse(), function (nodes) {
zrUtil.each(nodes, function (node) { zrUtil.each(nodes, function (node) {
if (node.outEdges.length) { if (node.outEdges.length) {
var y = sum(node.outEdges, weightedTarget, orient) / sum(node.outEdges, getEdgeValue, orient); var y = sum(node.outEdges, weightedTarget, orient)
/ sum(node.outEdges, getEdgeValue, orient);
if (orient === 'vertical') { if (orient === 'vertical') {
var nodeX = node.getLayout().x + (y - center(node, orient)) * alpha; var nodeX = node.getLayout().x + (y - center(node, orient)) * alpha;
node.setLayout({x: nodeX}, true); node.setLayout({x: nodeX}, true);
...@@ -462,10 +418,8 @@ function weightedSource(edge, orient) { ...@@ -462,10 +418,8 @@ function weightedSource(edge, orient) {
} }
function center(node, orient) { function center(node, orient) {
if (orient === 'vertical') { return orient === 'vertical' ? node.getLayout().x + node.getLayout().dx / 2
return node.getLayout().x + node.getLayout().dx / 2; : node.getLayout().y + node.getLayout().dy / 2;
}
return node.getLayout().y + node.getLayout().dy / 2;
} }
function getEdgeValue(edge) { function getEdgeValue(edge) {
...@@ -496,7 +450,8 @@ function relaxLeftToRight(nodesByBreadth, alpha, orient) { ...@@ -496,7 +450,8 @@ function relaxLeftToRight(nodesByBreadth, alpha, orient) {
zrUtil.each(nodesByBreadth, function (nodes) { zrUtil.each(nodesByBreadth, function (nodes) {
zrUtil.each(nodes, function (node) { zrUtil.each(nodes, function (node) {
if (node.inEdges.length) { if (node.inEdges.length) {
var y = sum(node.inEdges, weightedSource, orient) / sum(node.inEdges, getEdgeValue, orient); var y = sum(node.inEdges, weightedSource, orient)
/ sum(node.inEdges, getEdgeValue, orient);
if (orient === 'vertical') { if (orient === 'vertical') {
var nodeX = node.getLayout().x + (y - center(node, orient)) * alpha; var nodeX = node.getLayout().x + (y - center(node, orient)) * alpha;
node.setLayout({x: nodeX}, true); node.setLayout({x: nodeX}, true);
...@@ -516,23 +471,14 @@ function relaxLeftToRight(nodesByBreadth, alpha, orient) { ...@@ -516,23 +471,14 @@ function relaxLeftToRight(nodesByBreadth, alpha, orient) {
* @param {module:echarts/data/Graph~Node} nodes node of sankey view * @param {module:echarts/data/Graph~Node} nodes node of sankey view
*/ */
function computeEdgeDepths(nodes, orient) { function computeEdgeDepths(nodes, orient) {
var keyAttr = orient === 'vertical' ? 'x' : 'y';
zrUtil.each(nodes, function (node) { zrUtil.each(nodes, function (node) {
if (orient === 'vertical') { node.outEdges.sort(function (a, b) {
node.outEdges.sort(function (a, b) { return a.node2.getLayout()[keyAttr] - b.node2.getLayout()[keyAttr];
return a.node2.getLayout().x - b.node2.getLayout().x; });
}); node.inEdges.sort(function (a, b) {
node.inEdges.sort(function (a, b) { return a.node1.getLayout()[keyAttr] - b.node1.getLayout()[keyAttr];
return a.node1.getLayout().x - b.node1.getLayout().x; });
});
}
else {
node.outEdges.sort(function (a, b) {
return a.node2.getLayout().y - b.node2.getLayout().y;
});
node.inEdges.sort(function (a, b) {
return a.node1.getLayout().y - b.node1.getLayout().y;
});
}
}); });
zrUtil.each(nodes, function (node) { zrUtil.each(nodes, function (node) {
var sy = 0; var sy = 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册