未验证 提交 aaabdcc2 编写于 作者: A abigale Li 提交者: GitHub

refactor some part of sankey layout (#9995)

* fix(singleAxis): fix line width in singleAxis

* refactor(sankey): refactor some part of sankey layout
上级 a7e75ba3
...@@ -46,15 +46,15 @@ else { ...@@ -46,15 +46,15 @@ else {
} }
var MAGIC_DEPS = { var MAGIC_DEPS = {
'exports' : true, 'exports': true,
'module' : true, 'module': true,
'require' : true 'require': true
}; };
var SIMPLIFIED_CJS = ['require', 'exports', 'module']; var SIMPLIFIED_CJS = ['require', 'exports', 'module'];
// Convert AMD-style JavaScript string into node.js compatible module // Convert AMD-style JavaScript string into node.js compatible module
function parse (raw){ function parse(raw) {
var output = ''; var output = '';
var ast = esprima.parse(raw, { var ast = esprima.parse(raw, {
range: true, range: true,
...@@ -63,50 +63,52 @@ function parse (raw){ ...@@ -63,50 +63,52 @@ function parse (raw){
var defines = ast.body.filter(isDefine); var defines = ast.body.filter(isDefine);
if ( defines.length > 1 ){ if (defines.length > 1) {
throw new Error('Each file can have only a single define call. Found "'+ defines.length +'"'); throw new Error('Each file can have only a single define call. Found "' + defines.length + '"');
} else if (!defines.length){ }
else if (!defines.length) {
return raw; return raw;
} }
var def = defines[0]; var def = defines[0];
var args = def.expression['arguments']; var args = def.expression['arguments'];
var factory = getFactory( args ); var factory = getFactory(args);
var useStrict = getUseStrict( factory ); var useStrict = getUseStrict(factory);
// do replacements in-place to avoid modifying the code more than needed // do replacements in-place to avoid modifying the code more than needed
if (useStrict) { if (useStrict) {
output += useStrict.expression.raw +';\n'; output += useStrict.expression.raw + ';\n';
} }
output += raw.substring( 0, def.range[0] ); // anything before define output += raw.substring(0, def.range[0]); // anything before define
output += getRequires(args, factory); // add requires output += getRequires(args, factory); // add requires
output += getBody(raw, factory.body, useStrict); // module body output += getBody(raw, factory.body, useStrict); // module body
output += raw.substring( def.range[1], raw.length ); // anything after define output += raw.substring(def.range[1], raw.length); // anything after define
return output; return output;
} }
function getRequires(args, factory){ function getRequires(args, factory) {
var requires = []; var requires = [];
var deps = getDependenciesNames( args ); var deps = getDependenciesNames(args);
var params = factory.params.map(function(param, i){ var params = factory.params.map(function (param, i) {
return { return {
name : param.name, name: param.name,
// simplified cjs doesn't have deps // simplified cjs doesn't have deps
dep : (deps.length)? deps[i] : SIMPLIFIED_CJS[i] dep: (deps.length) ? deps[i] : SIMPLIFIED_CJS[i]
}; };
}); });
params.forEach(function(param){ params.forEach(function (param) {
if ( MAGIC_DEPS[param.dep] && !MAGIC_DEPS[param.name] ) { if (MAGIC_DEPS[param.dep] && !MAGIC_DEPS[param.name]) {
// if user remaped magic dependency we declare a var // if user remaped magic dependency we declare a var
requires.push( 'var '+ param.name +' = '+ param.dep +';' ); requires.push('var ' + param.name + ' = ' + param.dep + ';');
} else if ( param.dep && !MAGIC_DEPS[param.dep] ) { }
else if (param.dep && !MAGIC_DEPS[param.dep]) {
// only do require for params that have a matching dependency also // only do require for params that have a matching dependency also
// skip "magic" dependencies // skip "magic" dependencies
requires.push( 'var '+ param.name +' = require(\''+ param.dep +'\');' ); requires.push('var ' + param.name + ' = require(\'' + param.dep + '\');');
} }
}); });
...@@ -114,14 +116,14 @@ function getRequires(args, factory){ ...@@ -114,14 +116,14 @@ function getRequires(args, factory){
} }
function getDependenciesNames(args){ function getDependenciesNames(args) {
var deps = []; var deps = [];
var arr = args.filter(function(arg){ var arr = args.filter(function (arg) {
return arg.type === 'ArrayExpression'; return arg.type === 'ArrayExpression';
})[0]; })[0];
if (arr) { if (arr) {
deps = arr.elements.map(function(el){ deps = arr.elements.map(function (el) {
return el.value; return el.value;
}); });
} }
...@@ -130,23 +132,23 @@ function getDependenciesNames(args){ ...@@ -130,23 +132,23 @@ function getDependenciesNames(args){
} }
function isDefine(node){ function isDefine(node) {
return node.type === 'ExpressionStatement' && return node.type === 'ExpressionStatement'
node.expression.type === 'CallExpression' && && node.expression.type === 'CallExpression'
node.expression.callee.type === 'Identifier' && && node.expression.callee.type === 'Identifier'
node.expression.callee.name === 'define'; && node.expression.callee.name === 'define';
} }
function getFactory(args){ function getFactory(args) {
return args.filter(function(arg){ return args.filter(function (arg) {
return arg.type === 'FunctionExpression'; return arg.type === 'FunctionExpression';
})[0]; })[0];
} }
function getBody(raw, factoryBody, useStrict){ function getBody(raw, factoryBody, useStrict) {
var returnStatement = factoryBody.body.filter(function(node){ var returnStatement = factoryBody.body.filter(function (node) {
return node.type === 'ReturnStatement'; return node.type === 'ReturnStatement';
})[0]; })[0];
...@@ -154,26 +156,27 @@ function getBody(raw, factoryBody, useStrict){ ...@@ -154,26 +156,27 @@ function getBody(raw, factoryBody, useStrict){
var bodyStart = useStrict ? useStrict.expression.range[1] + 1 : factoryBody.range[0] + 1; var bodyStart = useStrict ? useStrict.expression.range[1] + 1 : factoryBody.range[0] + 1;
if (returnStatement) { if (returnStatement) {
body += raw.substring( bodyStart, returnStatement.range[0] ); body += raw.substring(bodyStart, returnStatement.range[0]);
// "return ".length === 7 so we add "6" to returnStatement start // "return ".length === 7 so we add "6" to returnStatement start
body += 'module.exports ='+ raw.substring( returnStatement.range[0] + 6, factoryBody.range[1] - 1 ); body += 'module.exports =' + raw.substring(returnStatement.range[0] + 6, factoryBody.range[1] - 1);
} else { }
else {
// if using exports or module.exports or just a private module we // if using exports or module.exports or just a private module we
// simply return the factoryBody content // simply return the factoryBody content
body = raw.substring( bodyStart, factoryBody.range[1] - 1 ); body = raw.substring(bodyStart, factoryBody.range[1] - 1);
} }
return body; return body;
} }
function getUseStrict(factory){ function getUseStrict(factory) {
return factory.body.body.filter(isUseStrict)[0]; return factory.body.body.filter(isUseStrict)[0];
} }
function isUseStrict(node){ function isUseStrict(node) {
return node.type === 'ExpressionStatement' && return node.type === 'ExpressionStatement'
node.expression.type === 'Literal' && && node.expression.type === 'Literal'
node.expression.value === 'use strict'; && node.expression.value === 'use strict';
} }
...@@ -18,14 +18,13 @@ ...@@ -18,14 +18,13 @@
*/ */
/** /**
* @file The layout algorithm of sankey view * @file The layout algorithm of sankey diagram.
* @author Deqing Li(annong035@gmail.com) * @author Deqing Li(annong035@gmail.com)
*/ */
import * as layout from '../../util/layout'; import * as layout from '../../util/layout';
import * as zrUtil from 'zrender/src/core/util'; import * as zrUtil from 'zrender/src/core/util';
import {groupData} from '../../util/model'; import {groupData} from '../../util/model';
import { __DEV__ } from '../../config';
export default function (ecModel, api, payload) { export default function (ecModel, api, payload) {
...@@ -135,6 +134,9 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod ...@@ -135,6 +134,9 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod
} }
} }
// Traversing nodes using topological sorting to calculate the
// horizontal(if orient === 'horizontal') or vertical(if orient === 'vertical')
// position of the nodes.
while (zeroIndegrees.length) { while (zeroIndegrees.length) {
for (var idx = 0; idx < zeroIndegrees.length; idx++) { for (var idx = 0; idx < zeroIndegrees.length; idx++) {
var node = zeroIndegrees[idx]; var node = zeroIndegrees[idx];
...@@ -162,30 +164,15 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod ...@@ -162,30 +164,15 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod
nextTargetNode = []; nextTargetNode = [];
} }
var nodesCopy = nodes.slice(0);
for (i = 0; i < remainEdges.length; i++) { for (i = 0; i < remainEdges.length; i++) {
if (remainEdges[i] === 1) { if (remainEdges[i] === 1) {
if (__DEV__) { throw new Error('Sankey is a DAG, the original data has cycle!');
throw new Error('Sankey is a DAG, the original data has cycle!');
}
if (nodeAlign === 'right') {
var edge = edges[i];
var index1 = nodesCopy.indexOf(edge.node1);
if (index1 > -1) {
nodesCopy.splice(index1, 1);
}
var index2 = nodesCopy.indexOf(edge.node2);
if (index2 > -1) {
nodesCopy.splice(index2, 1);
}
}
} }
} }
if (nodeAlign === 'right') { if (nodeAlign === 'right') {
var nextSourceNode = []; var nextSourceNode = [];
var remainNodes = nodesCopy; var remainNodes = nodes;
var nodeHeight = 0; var nodeHeight = 0;
while (remainNodes.length) { while (remainNodes.length) {
for (var i = 0; i < remainNodes.length; i++) { for (var i = 0; i < remainNodes.length; i++) {
...@@ -203,7 +190,7 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod ...@@ -203,7 +190,7 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod
++nodeHeight; ++nodeHeight;
} }
zrUtil.each(nodesCopy, function (node) { zrUtil.each(nodes, function (node) {
if (orient === 'vertical') { if (orient === 'vertical') {
node.setLayout({y: Math.max(0, x - 1 - node.getLayout().skNodeHeight)}, true); node.setLayout({y: Math.max(0, x - 1 - node.getLayout().skNodeHeight)}, true);
} }
...@@ -212,13 +199,12 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod ...@@ -212,13 +199,12 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod
} }
}); });
} }
else if (nodeAlign === 'justify') {
if (nodeAlign === 'justify') {
moveSinksRight(nodes, x, orient); moveSinksRight(nodes, x, orient);
} }
var maxDepth = x - 1; var maxDepth = x - 1;
zrUtil.each(nodesCopy, function (node) { zrUtil.each(nodes, function (node) {
var item = node.hostGraph.data.getRawDataItem(node.dataIndex); var item = node.hostGraph.data.getRawDataItem(node.dataIndex);
if (item.depth && !isNaN(item.depth) && item.depth >= 0) { if (item.depth && !isNaN(item.depth) && item.depth >= 0) {
if (item.depth > maxDepth) { if (item.depth > maxDepth) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册