From 3945f7c5f1c12e3bd433b6c466bf05218e2cbc4f Mon Sep 17 00:00:00 2001 From: Wenyin Wei Date: Wed, 22 May 2019 08:47:12 +0800 Subject: [PATCH 1/2] The code now supports node position specification --- springy.js | 47 ++++++++++++++++++++++++++++++++++------------- springyui.js | 6 +++--- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/springy.js b/springy.js index 0bf5ba4..60bc49f 100644 --- a/springy.js +++ b/springy.js @@ -90,9 +90,28 @@ // accepts variable number of arguments, where each argument // is a string that becomes both node identifier and label for (var i = 0; i < arguments.length; i++) { - var name = arguments[i]; - var node = new Node(name, {label:name}); - this.addNode(node); + var e = arguments[i]; + var name = e[0]; + var attr = e[1]; + this.newNode(name, attr); + } + }; + Graph.prototype.addEdges = function() { + // accepts variable number of arguments, where each argument + // is a triple [nodeid1, nodeid2, attributes] + for (var i = 0; i < arguments.length; i++) { + var e = arguments[i]; + var node1 = this.nodeSet[e[0]]; + if (node1 == undefined) { + throw new TypeError("invalid node name: " + e[0]); + } + var node2 = this.nodeSet[e[1]]; + if (node2 == undefined) { + throw new TypeError("invalid node name: " + e[1]); + } + var attr = e[2]; + + this.newEdge(node1, node2, attr); } }; @@ -145,8 +164,8 @@ } }; - Graph.prototype.newNode = function(data) { - var node = new Node(this.nextNodeId++, data); + Graph.prototype.newNode = function(name, data) { + var node = new Node(name, Object.assign( {label:name},data) ); this.addNode(node); return node; }; @@ -329,7 +348,7 @@ var Layout = Springy.Layout = {}; Layout.ForceDirected = function(graph, stiffness, repulsion, damping, minEnergyThreshold, maxSpeed) { this.graph = graph; - this.stiffness = stiffness; // spring stiffness constant + this.stiffness = stiffness ; // spring stiffness constant this.repulsion = repulsion; // repulsion constant this.damping = damping; // velocity damping factor this.minEnergyThreshold = minEnergyThreshold || 0.01; //threshold used to determine render stop @@ -342,7 +361,9 @@ Layout.ForceDirected.prototype.point = function(node) { if (!(node.id in this.nodePoints)) { var mass = (node.data.mass !== undefined) ? node.data.mass : 1.0; - this.nodePoints[node.id] = new Layout.ForceDirected.Point(Vector.random(), mass); + // this.nodePoints[node.id] = new Layout.ForceDirected.Point(Vector.random(), mass); + var vector = (node.data.xpos !== undefined) ? new Vector(node.data.xpos,-node.data.ypos) : Vector.random(); + this.nodePoints[node.id] = new Layout.ForceDirected.Point(vector, mass); } return this.nodePoints[node.id]; @@ -350,7 +371,8 @@ Layout.ForceDirected.prototype.spring = function(edge) { if (!(edge.id in this.edgeSprings)) { - var length = (edge.data.length !== undefined) ? edge.data.length : 1.0; + var d = this.point(edge.source).p.subtract(this.point(edge.target).p); + var length = (edge.data.length !== undefined) ? edge.data.length : d.magnitude(); var existingSpring = false; @@ -525,9 +547,9 @@ } Layout.ForceDirected.prototype.tick = function(timestep) { - this.applyCoulombsLaw(); + // this.applyCoulombsLaw(); this.applyHookesLaw(); - this.attractToCentre(); + // this.attractToCentre(); this.updateVelocity(timestep); this.updatePosition(timestep); }; @@ -550,9 +572,8 @@ // returns [bottomleft, topright] Layout.ForceDirected.prototype.getBoundingBox = function() { - var bottomleft = new Vector(-2,-2); - var topright = new Vector(2,2); - + var bottomleft = new Vector(Infinity,Infinity); + var topright = new Vector(-Infinity,-Infinity); this.eachNode(function(n, point) { if (point.p.x < bottomleft.x) { bottomleft.x = point.p.x; diff --git a/springyui.js b/springyui.js index acc35eb..1e34562 100755 --- a/springyui.js +++ b/springyui.js @@ -28,7 +28,7 @@ Copyright (c) 2010 Dennis Hotson jQuery.fn.springy = function(params) { var graph = this.graph = params.graph || new Springy.Graph(); var nodeFont = "16px Verdana, sans-serif"; - var edgeFont = "8px Verdana, sans-serif"; + var edgeFont = "14px Verdana, sans-serif"; var stiffness = params.stiffness || 400.0; var repulsion = params.repulsion || 400.0; var damping = params.damping || 0.5; @@ -44,7 +44,7 @@ jQuery.fn.springy = function(params) { // calculate bounding box of graph layout.. with ease-in var currentBB = layout.getBoundingBox(); - var targetBB = {bottomleft: new Springy.Vector(-2, -2), topright: new Springy.Vector(2, 2)}; + var targetBB = {bottomleft: new Springy.Vector(0, 0), topright: new Springy.Vector(1000, 1000)}; // auto adjusting bounding box Springy.requestAnimationFrame(function adjust() { @@ -287,7 +287,7 @@ jQuery.fn.springy = function(params) { var textPos = s1.add(s2).divide(2).add(normal.multiply(displacement)); ctx.translate(textPos.x, textPos.y); ctx.rotate(angle); - ctx.fillText(text, 0,-2); + ctx.fillText(text, 0, -8); ctx.restore(); } From 2c1c2351fcb4d8ea46101c566bf3b9a306077d28 Mon Sep 17 00:00:00 2001 From: Wenyin Wei Date: Wed, 22 May 2019 08:49:22 +0800 Subject: [PATCH 2/2] An abundant definition of addEdges is found and removed --- springy.js | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/springy.js b/springy.js index 60bc49f..f8c1553 100644 --- a/springy.js +++ b/springy.js @@ -96,24 +96,6 @@ this.newNode(name, attr); } }; - Graph.prototype.addEdges = function() { - // accepts variable number of arguments, where each argument - // is a triple [nodeid1, nodeid2, attributes] - for (var i = 0; i < arguments.length; i++) { - var e = arguments[i]; - var node1 = this.nodeSet[e[0]]; - if (node1 == undefined) { - throw new TypeError("invalid node name: " + e[0]); - } - var node2 = this.nodeSet[e[1]]; - if (node2 == undefined) { - throw new TypeError("invalid node name: " + e[1]); - } - var attr = e[2]; - - this.newEdge(node1, node2, attr); - } - }; Graph.prototype.addEdge = function(edge) { var exists = false;