Skip to content

Commit

Permalink
force代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
pissang committed Jan 12, 2015
1 parent 93c73cd commit 4afec3a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 86 deletions.
62 changes: 19 additions & 43 deletions src/chart/force.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,12 @@ define(function (require) {
|| this.deepQuery(styleQueryTarget, 'borderWidth')
},
highlightStyle: {
color: this.deepQuery(emphasisStyleQueryTarget, 'color', 'emphasis'),
color: this.deepQuery(emphasisStyleQueryTarget, 'color'),
// 兼容原有写法
strokeColor: this.deepQuery(emphasisStyleQueryTarget, 'strokeColor', 'emphasis')
|| this.deepQuery(emphasisStyleQueryTarget, 'borderColor', 'emphasis'),
lineWidth: this.deepQuery(emphasisStyleQueryTarget, 'lineWidth', 'emphasis')
|| this.deepQuery(emphasisStyleQueryTarget, 'borderWidth', 'emphasis')
strokeColor: this.deepQuery(emphasisStyleQueryTarget, 'strokeColor')
|| this.deepQuery(emphasisStyleQueryTarget, 'borderColor'),
lineWidth: this.deepQuery(emphasisStyleQueryTarget, 'lineWidth')
|| this.deepQuery(emphasisStyleQueryTarget, 'borderWidth')
},
clickable: serie.clickable,
zlevel: this.getZlevelBase(),
Expand Down Expand Up @@ -592,11 +592,6 @@ define(function (require) {
brushType: 'fill',
// Use same style with link shape
color: linkShape.style.strokeColor,
opacity: linkShape.style.opacity,
shadowBlur: linkShape.style.shadowBlur,
shadowColor: linkShape.style.shadowColor,
shadowOffsetX: linkShape.style.shadowOffsetX,
shadowOffsetY: linkShape.style.shadowOffsetY
},
highlightStyle: {
brushType: 'fill'
Expand Down Expand Up @@ -656,43 +651,27 @@ define(function (require) {

_syncNodePositions: function() {
var graph = this._graph;
// var delta = 0;
for (var i = 0; i < graph.nodes.length; i++) {
var gNode = graph.nodes[i];
var position = gNode.layout.position;
var node = gNode.data;
var shape = gNode.shape;
// delta += vec2.len(shape.position, position);
if (shape.fixed || (node.fixX && node.fixY)) {
vec2.copy(position, shape.position);
var fixX = shape.fixed || node.fixX;
var fixY = shape.fixed || node.fixY;
if (fixX === true) {
fixX = 1;
} else if (isNaN(fixX)) {
fixX = 0;
}
else if (node.fixX) {
position[0] = shape.position[0];
shape.position[1] = position[1];
}
else if (node.fixY) {
position[1] = shape.position[1];
shape.position[0] = position[0];
}
else if (isNaN(node.fixX - 0) == false && isNaN(node.fixY - 0) == false) {
shape.position[0] += (position[0] - shape.position[0]) * node.fixX;
position[0] = shape.position[0];
shape.position[1] += (position[1] - shape.position[1]) * node.fixY;
position[1] = shape.position[1];
}
else if (isNaN(node.fixX - 0) == false) {
shape.position[0] += (position[0] - shape.position[0]) * node.fixX;
position[0] = shape.position[0];
shape.position[1] = position[1];
}
else if (isNaN(node.fixY - 0) == false) {
shape.position[1] += (position[1] - shape.position[1]) * node.fixY;
position[1] = shape.position[1];
shape.position[0] = position[0];
}
else {
vec2.copy(shape.position, position);
if (fixY === true) {
fixY = 1;
} else if (isNaN(fixY)) {
fixY = 0;
}
shape.position[0] += (position[0] - shape.position[0]) * (1 - fixX);
shape.position[1] += (position[1] - shape.position[1]) * (1 - fixY);

vec2.copy(position, shape.position);

var nodeName = node.name;
if (nodeName) {
Expand All @@ -705,9 +684,6 @@ define(function (require) {

shape.modSelf();
}
// if (delta < 1) { // All shape stopped moving
// this._layout.temperature = 0;
// }
},

_step: function(e) {
Expand Down
39 changes: 13 additions & 26 deletions src/layout/Force.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ define(function(require) {
this._layout = null;
this._layoutWorker = null;

this._token = 0;

var self = this;
var _$onupdate = this._$onupdate;
this._$onupdate = function(e) {
Expand Down Expand Up @@ -162,8 +160,6 @@ define(function(require) {
edgeWeightArr[i] = edge.layout.weight || 1;
}

this._token = getToken();

if (this._layoutWorker) {

this._layoutWorker.postMessage({
Expand All @@ -172,12 +168,10 @@ define(function(require) {
nodesMass: massArr,
nodesSize: sizeArr,
edges: edgeArr,
edgesWeight: edgeWeightArr,
token: this._token
edgesWeight: edgeWeightArr
});
}
else {
this._layout.setToken(this._token);
this._layout.initNodes(positionArr, massArr, sizeArr);
this._layout.initEdges(edgeArr, edgeWeightArr);
}
Expand All @@ -189,11 +183,11 @@ define(function(require) {
var nodes = this.graph.nodes;
if (this._layoutWorker) {
// Sync back
var positionArr = new ArrayCtor(nodes.length * 2 + 1);
var positionArr = new ArrayCtor(nodes.length * 2);
for (var i = 0; i < nodes.length; i++) {
var n = nodes[i];
positionArr[i * 2 + 1] = n.layout.position[0];
positionArr[i * 2 + 2] = n.layout.position[1];
positionArr[i * 2] = n.layout.position[0];
positionArr[i * 2 + 1] = n.layout.position[1];
}
this._layoutWorker.postMessage(positionArr.buffer, [positionArr.buffer]);

Expand Down Expand Up @@ -226,25 +220,19 @@ define(function(require) {
ForceLayout.prototype._$onupdate = function (e) {
if (this._layoutWorker) {
var positionArr = new Float32Array(e.data);
var token = positionArr[0];
// If token is from current layout instance
if (token === this._token) {
for (var i = 0; i < this.graph.nodes.length; i++) {
var n = this.graph.nodes[i];
n.layout.position[0] = positionArr[i * 2 + 1];
n.layout.position[1] = positionArr[i * 2 + 2];
}
this.onupdate && this.onupdate();
for (var i = 0; i < this.graph.nodes.length; i++) {
var n = this.graph.nodes[i];
n.layout.position[0] = positionArr[i * 2];
n.layout.position[1] = positionArr[i * 2 + 1];
}
this.onupdate && this.onupdate();
}
else if (this._layout) {
if (this._layout.tokenMatch(this._token)) {
for (var i = 0; i < this.graph.nodes.length; i++) {
var n = this.graph.nodes[i];
vec2.copy(n.layout.position, this._layout.nodes[i].position);
}
this.onupdate && this.onupdate();
for (var i = 0; i < this.graph.nodes.length; i++) {
var n = this.graph.nodes[i];
vec2.copy(n.layout.position, this._layout.nodes[i].position);
}
this.onupdate && this.onupdate();
}
};

Expand All @@ -254,7 +242,6 @@ define(function(require) {
}
this._layoutWorker = null;
this._layout = null;
this._token = 0;
};

return ForceLayout;
Expand Down
23 changes: 6 additions & 17 deletions src/layout/forceLayoutWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,14 +705,6 @@ define(function __echartsForceLayoutWorker(require) {
return str.slice(str.indexOf('{') + 1, str.lastIndexOf('return'));
};

ForceLayout.prototype.setToken = function(token) {
this._token = token;
};

ForceLayout.prototype.tokenMatch = function(token) {
return token === this._token;
};

/****************************
* Main process
***************************/
Expand All @@ -727,11 +719,11 @@ define(function __echartsForceLayoutWorker(require) {
if (!forceLayout) return;

var positionArr = new Float32Array(e.data);
var nNodes = (positionArr.length - 1) / 2;
var nNodes = positionArr.length / 2;
for (var i = 0; i < nNodes; i++) {
var node = forceLayout.nodes[i];
node.position[0] = positionArr[i * 2 + 1];
node.position[1] = positionArr[i * 2 + 2];
node.position[0] = positionArr[i * 2];
node.position[1] = positionArr[i * 2 + 1];
}
return;
}
Expand All @@ -743,7 +735,6 @@ define(function __echartsForceLayoutWorker(require) {
}
forceLayout.initNodes(e.data.nodesPosition, e.data.nodesMass, e.data.nodesSize);
forceLayout.initEdges(e.data.edges, e.data.edgesWeight);
forceLayout._token = e.data.token;
break;
case 'updateConfig':
if (forceLayout) {
Expand All @@ -757,7 +748,7 @@ define(function __echartsForceLayoutWorker(require) {

if (forceLayout) {
var nNodes = forceLayout.nodes.length;
var positionArr = new Float32Array(nNodes * 2 + 1);
var positionArr = new Float32Array(nNodes * 2);

forceLayout.temperature = e.data.temperature;

Expand All @@ -768,12 +759,10 @@ define(function __echartsForceLayoutWorker(require) {
// Callback
for (var i = 0; i < nNodes; i++) {
var node = forceLayout.nodes[i];
positionArr[i * 2 + 1] = node.position[0];
positionArr[i * 2 + 2] = node.position[1];
positionArr[i * 2] = node.position[0];
positionArr[i * 2 + 1] = node.position[1];
}

positionArr[0] = forceLayout._token;

self.postMessage(positionArr.buffer, [positionArr.buffer]);
}
else {
Expand Down

0 comments on commit 4afec3a

Please sign in to comment.