From 484f2bef20b9fdad6d8e6db0bc4c15cee81115f4 Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Thu, 13 Sep 2012 02:48:58 +0200 Subject: [PATCH] position nodes according to the geographic position initially and skip the early force ticks --- html/force.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/html/force.js b/html/force.js index ff3862a..d2be866 100644 --- a/html/force.js +++ b/html/force.js @@ -267,7 +267,7 @@ var force = d3.layout.force() } }) -force.on("tick", function(e) { +function tick_event(e) { var size = force.size() var nodes = force.nodes() var nl = nodes.length @@ -296,7 +296,7 @@ force.on("tick", function(e) { vis.selectAll(".label").attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) -}) +} var data @@ -373,10 +373,51 @@ function reload() { d3.select("#clientcount") .text("ungefähr " + (nClients - nNodes) + " Clients") + data = wilder_scheiß(data) + update() }) } +function fix_geonodes(nodes, x) { + nodes.filter(function(d) { + return d.geo !== null + }).forEach(function(d) { + d.fixed = x + }) +} + +function wilder_scheiß(data) { + var nodes = data.nodes.filter(function(d) { + return d.geo !== null + }) + + var lat = nodes.map(function(d) { return d.geo[0] }) + var lon = nodes.map(function(d) { return d.geo[1] }) + + var max_lat = Math.min.apply(null, lat) + var min_lat = Math.max.apply(null, lat) + + var min_lon = Math.min.apply(null, lon) + var max_lon = Math.max.apply(null, lon) + + var width = force.size()[0] + var height = force.size()[1] + + var scale_x = width / (max_lon - min_lon) + var scale_y = height / (max_lat - min_lat) + + nodes.forEach(function(d) { + if (d.x || d.y) + return + + d.x = (d.geo[1] - min_lon) * scale_x + d.y = (d.geo[0] - min_lat) * scale_y + }) + + return data +} + var dragging = false var node_drag = d3.behavior.drag() @@ -607,6 +648,25 @@ function update() { .alpha(0.1) .start() + if (initial == 1) { + fix_geonodes(data.nodes, true) + + force.alpha(0.1) + while(force.alpha() > 0.05) + force.tick() + + fix_geonodes(data.nodes, false) + + force.alpha(0.1) + while(force.alpha() > 0.05) + force.tick() + + force.on("tick", tick_event) + force.start() + } + + initial = 0 + linkedByIndex = {} links.forEach(function(d) { @@ -617,6 +677,8 @@ function update() { show_node(hashstr) } +var initial = 1 + reload() var timer = window.setInterval(reload, 30000)