Skip to content
Permalink
Browse files
refactor some part of sankey layout (#9995)
* fix(singleAxis): fix line width in singleAxis

* refactor(sankey): refactor some part of sankey layout
  • Loading branch information
deqingli committed Feb 26, 2019
1 parent a7e75ba commit aaabdcc2764c035f9ee26149f1a7f9cd3309e164
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 66 deletions.
@@ -46,15 +46,15 @@ else {
}

var MAGIC_DEPS = {
'exports' : true,
'module' : true,
'require' : true
'exports': true,
'module': true,
'require': true
};

var SIMPLIFIED_CJS = ['require', 'exports', 'module'];

// Convert AMD-style JavaScript string into node.js compatible module
function parse (raw){
function parse(raw) {
var output = '';
var ast = esprima.parse(raw, {
range: true,
@@ -63,65 +63,67 @@ function parse (raw){

var defines = ast.body.filter(isDefine);

if ( defines.length > 1 ){
throw new Error('Each file can have only a single define call. Found "'+ defines.length +'"');
} else if (!defines.length){
if (defines.length > 1) {
throw new Error('Each file can have only a single define call. Found "' + defines.length + '"');
}
else if (!defines.length) {
return raw;
}

var def = defines[0];
var args = def.expression['arguments'];
var factory = getFactory( args );
var useStrict = getUseStrict( factory );
var factory = getFactory(args);
var useStrict = getUseStrict(factory);

// do replacements in-place to avoid modifying the code more than needed
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 += 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;
}


function getRequires(args, factory){
function getRequires(args, factory) {
var requires = [];
var deps = getDependenciesNames( args );
var params = factory.params.map(function(param, i){
var deps = getDependenciesNames(args);
var params = factory.params.map(function (param, i) {
return {
name : param.name,
name: param.name,
// 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){
if ( MAGIC_DEPS[param.dep] && !MAGIC_DEPS[param.name] ) {
params.forEach(function (param) {
if (MAGIC_DEPS[param.dep] && !MAGIC_DEPS[param.name]) {
// if user remaped magic dependency we declare a var
requires.push( 'var '+ param.name +' = '+ param.dep +';' );
} else if ( param.dep && !MAGIC_DEPS[param.dep] ) {
requires.push('var ' + param.name + ' = ' + param.dep + ';');
}
else if (param.dep && !MAGIC_DEPS[param.dep]) {
// only do require for params that have a matching dependency also
// skip "magic" dependencies
requires.push( 'var '+ param.name +' = require(\''+ param.dep +'\');' );
requires.push('var ' + param.name + ' = require(\'' + param.dep + '\');');
}
});

return requires.join('\n');
}


function getDependenciesNames(args){
function getDependenciesNames(args) {
var deps = [];
var arr = args.filter(function(arg){
var arr = args.filter(function (arg) {
return arg.type === 'ArrayExpression';
})[0];

if (arr) {
deps = arr.elements.map(function(el){
deps = arr.elements.map(function (el) {
return el.value;
});
}
@@ -130,50 +132,51 @@ function getDependenciesNames(args){
}


function isDefine(node){
return node.type === 'ExpressionStatement' &&
node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'Identifier' &&
node.expression.callee.name === 'define';
function isDefine(node) {
return node.type === 'ExpressionStatement'
&& node.expression.type === 'CallExpression'
&& node.expression.callee.type === 'Identifier'
&& node.expression.callee.name === 'define';
}


function getFactory(args){
return args.filter(function(arg){
function getFactory(args) {
return args.filter(function (arg) {
return arg.type === 'FunctionExpression';
})[0];
}


function getBody(raw, factoryBody, useStrict){
var returnStatement = factoryBody.body.filter(function(node){
function getBody(raw, factoryBody, useStrict) {
var returnStatement = factoryBody.body.filter(function (node) {
return node.type === 'ReturnStatement';
})[0];

var body = '';
var bodyStart = useStrict ? useStrict.expression.range[1] + 1 : factoryBody.range[0] + 1;

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
body += 'module.exports ='+ raw.substring( returnStatement.range[0] + 6, factoryBody.range[1] - 1 );
} else {
body += 'module.exports =' + raw.substring(returnStatement.range[0] + 6, factoryBody.range[1] - 1);
}
else {
// if using exports or module.exports or just a private module we
// simply return the factoryBody content
body = raw.substring( bodyStart, factoryBody.range[1] - 1 );
body = raw.substring(bodyStart, factoryBody.range[1] - 1);
}

return body;
}


function getUseStrict(factory){
function getUseStrict(factory) {
return factory.body.body.filter(isUseStrict)[0];
}


function isUseStrict(node){
return node.type === 'ExpressionStatement' &&
node.expression.type === 'Literal' &&
node.expression.value === 'use strict';
function isUseStrict(node) {
return node.type === 'ExpressionStatement'
&& node.expression.type === 'Literal'
&& node.expression.value === 'use strict';
}
@@ -18,14 +18,13 @@
*/

/**
* @file The layout algorithm of sankey view
* @file The layout algorithm of sankey diagram.
* @author Deqing Li(annong035@gmail.com)
*/

import * as layout from '../../util/layout';
import * as zrUtil from 'zrender/src/core/util';
import {groupData} from '../../util/model';
import { __DEV__ } from '../../config';

export default function (ecModel, api, payload) {

@@ -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) {
for (var idx = 0; idx < zeroIndegrees.length; idx++) {
var node = zeroIndegrees[idx];
@@ -162,30 +164,15 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod
nextTargetNode = [];
}

var nodesCopy = nodes.slice(0);

for (i = 0; i < remainEdges.length; i++) {
if (remainEdges[i] === 1) {
if (__DEV__) {
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);
}
}
throw new Error('Sankey is a DAG, the original data has cycle!');
}
}

if (nodeAlign === 'right') {
var nextSourceNode = [];
var remainNodes = nodesCopy;
var remainNodes = nodes;
var nodeHeight = 0;
while (remainNodes.length) {
for (var i = 0; i < remainNodes.length; i++) {
@@ -203,7 +190,7 @@ function computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nod
++nodeHeight;
}

zrUtil.each(nodesCopy, function (node) {
zrUtil.each(nodes, function (node) {
if (orient === 'vertical') {
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
}
});
}

if (nodeAlign === 'justify') {
else if (nodeAlign === 'justify') {
moveSinksRight(nodes, x, orient);
}

var maxDepth = x - 1;
zrUtil.each(nodesCopy, function (node) {
zrUtil.each(nodes, function (node) {
var item = node.hostGraph.data.getRawDataItem(node.dataIndex);
if (item.depth && !isNaN(item.depth) && item.depth >= 0) {
if (item.depth > maxDepth) {

0 comments on commit aaabdcc

Please sign in to comment.