graphology-layout-forceatlas2
Advanced tools
Comparing version 0.8.1 to 0.8.2
@@ -175,19 +175,34 @@ /** | ||
* | ||
* @param {Graph} graph - Target graph. | ||
* @param {Float32Array} NodeMatrix - Node matrix. | ||
* @param {function|null} outputReducer - A node reducer. | ||
*/ | ||
exports.assignLayoutChanges = function (graph, NodeMatrix, outputReducer) { | ||
var i = 0; | ||
graph.updateEachNodeAttributes(function (node, attr) { | ||
attr.x = NodeMatrix[i]; | ||
attr.y = NodeMatrix[i + 1]; | ||
i += PPN; | ||
return outputReducer ? outputReducer(node, attr) : attr; | ||
}); | ||
}; | ||
/** | ||
* Function reading the positions (only) from the graph, to write them in the matrix. | ||
* | ||
* @param {Graph} graph - Target graph. | ||
* @param {Float32Array} NodeMatrix - Node matrix. | ||
*/ | ||
exports.assignLayoutChanges = function (graph, NodeMatrix) { | ||
exports.readGraphPositions = function (graph, NodeMatrix) { | ||
var i = 0; | ||
graph.updateEachNodeAttributes( | ||
function (node, attr) { | ||
attr.x = NodeMatrix[i]; | ||
attr.y = NodeMatrix[i + 1]; | ||
graph.forEachNode(function (node, attr) { | ||
NodeMatrix[i] = attr.x; | ||
NodeMatrix[i + 1] = attr.y; | ||
i += PPN; | ||
return attr; | ||
}, | ||
{attributes: ['x', 'y']} | ||
); | ||
i += PPN; | ||
}); | ||
}; | ||
@@ -198,7 +213,8 @@ | ||
* | ||
* @param {Graph} graph - Target graph. | ||
* @param {Float32Array} NodeMatrix - Node matrix. | ||
* @return {object} - Map to node positions. | ||
* @param {Graph} graph - Target graph. | ||
* @param {Float32Array} NodeMatrix - Node matrix. | ||
* @param {function|null} outputReducer - A nodes reducer. | ||
* @return {object} - Map to node positions. | ||
*/ | ||
exports.collectLayoutChanges = function (graph, NodeMatrix) { | ||
exports.collectLayoutChanges = function (graph, NodeMatrix, outputReducer) { | ||
var nodes = graph.nodes(), | ||
@@ -208,6 +224,17 @@ positions = {}; | ||
for (var i = 0, j = 0, l = NodeMatrix.length; i < l; i += PPN) { | ||
positions[nodes[j]] = { | ||
x: NodeMatrix[i], | ||
y: NodeMatrix[i + 1] | ||
}; | ||
if (outputReducer) { | ||
var newAttr = Object.assign({}, graph.getNodeAttributes(nodes[j])); | ||
newAttr.x = NodeMatrix[i]; | ||
newAttr.y = NodeMatrix[i + 1]; | ||
newAttr = outputReducer(nodes[j], newAttr); | ||
positions[nodes[j]] = { | ||
x: newAttr.x, | ||
y: newAttr.y | ||
}; | ||
} else { | ||
positions[nodes[j]] = { | ||
x: NodeMatrix[i], | ||
y: NodeMatrix[i + 1] | ||
}; | ||
} | ||
@@ -214,0 +241,0 @@ j++; |
@@ -25,2 +25,3 @@ import Graph from 'graphology-types'; | ||
weighted?: boolean; | ||
outputReducer?: (key: string, attributes: any) => any; | ||
}; | ||
@@ -27,0 +28,0 @@ |
22
index.js
@@ -16,10 +16,11 @@ /** | ||
* | ||
* @param {boolean} assign - Whether to assign positions. | ||
* @param {Graph} graph - Target graph. | ||
* @param {object|number} params - If number, params.iterations, else: | ||
* @param {object} attributes - Attribute names: | ||
* @param {string} weight - Name of the edge weight attribute. | ||
* @param {boolean} weighted - Whether to take edge weights into account. | ||
* @param {number} iterations - Number of iterations. | ||
* @param {object} [settings] - Settings. | ||
* @param {boolean} assign - Whether to assign positions. | ||
* @param {Graph} graph - Target graph. | ||
* @param {object|number} params - If number, params.iterations, else: | ||
* @param {object} attributes - Attribute names: | ||
* @param {string} weight - Name of the edge weight attribute. | ||
* @param {boolean} weighted - Whether to take edge weights into account. | ||
* @param {number} iterations - Number of iterations. | ||
* @param {function|null} outputReducer - A node reducer | ||
* @param {object} [settings] - Settings. | ||
* @return {object|undefined} | ||
@@ -50,2 +51,5 @@ */ | ||
var outputReducer = | ||
typeof params.outputReducer === 'function' ? params.outputReducer : null; | ||
// Validating settings | ||
@@ -71,3 +75,3 @@ var settings = helpers.assign({}, DEFAULT_SETTINGS, params.settings); | ||
if (assign) { | ||
helpers.assignLayoutChanges(graph, matrices.nodes); | ||
helpers.assignLayoutChanges(graph, matrices.nodes, outputReducer); | ||
return; | ||
@@ -74,0 +78,0 @@ } |
{ | ||
"name": "graphology-layout-forceatlas2", | ||
"version": "0.8.1", | ||
"version": "0.8.2", | ||
"description": "ForceAtlas 2 layout algorithm for graphology.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,2 +10,3 @@ import Graph from 'graphology-types'; | ||
weighted?: boolean; | ||
outputReducer?: (key: string, attributes: any) => any; | ||
}; | ||
@@ -12,0 +13,0 @@ |
@@ -51,2 +51,4 @@ /** | ||
this.killed = false; | ||
this.outputReducer = | ||
typeof params.outputReducer === 'function' ? params.outputReducer : null; | ||
@@ -108,3 +110,4 @@ // Binding listeners | ||
helpers.assignLayoutChanges(this.graph, matrix); | ||
helpers.assignLayoutChanges(this.graph, matrix, this.outputReducer); | ||
if (this.outputReducer) helpers.readGraphPositions(this.graph, matrix); | ||
this.matrices.nodes = matrix; | ||
@@ -111,0 +114,0 @@ |
77905
1937