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