ffmap-backend/html/force.js

320 lines
7.9 KiB
JavaScript
Raw Normal View History

2012-05-12 17:06:00 +02:00
function getOffset( el ) {
var _x = 0, _y = 0
2012-06-03 18:57:36 +02:00
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
_x += el.offsetLeft - el.scrollLeft
_y += el.offsetTop - el.scrollTop
el = el.offsetParent
2012-06-03 18:57:36 +02:00
}
return { top: _y, left: _x }
2012-05-12 17:06:00 +02:00
}
var offset = getOffset(document.getElementById('chart'))
2012-05-12 17:06:00 +02:00
var w = window.innerWidth - offset.left,
h = window.innerHeight - offset.top,
fill = d3.scale.category20()
2012-02-17 02:09:21 +01:00
2012-06-03 18:57:36 +02:00
var cp = d3.select("#chart").append("div")
.attr("id", "controlpanel")
2012-06-03 18:57:36 +02:00
var btns = cp.append("div")
.attr("class", "btn-group")
.attr("data-toggle", "buttons-radio")
2012-06-03 18:57:36 +02:00
btns.append("button")
.attr("class", "btn active left")
.attr("value", "all")
.text("Alles")
.on("click", update_graph)
2012-06-03 18:57:36 +02:00
btns.append("button")
.attr("class", "btn right")
.attr("value", "mesh")
.text("nur Mesh")
.on("click", update_graph)
2012-06-03 18:57:36 +02:00
2012-06-04 18:23:39 +02:00
cp.append("label")
.text("Knoten hervorheben:")
2012-06-04 18:23:39 +02:00
cp.append("br")
2012-06-04 18:23:39 +02:00
cp.append("input")
.on("keyup", function(){show_node(this.value)})
.on("change", function(){show_node(this.value)})
2012-06-04 18:23:39 +02:00
function show_node(mac) {
d3.selectAll("#chart .node")
.classed("marked", false)
2012-06-04 18:27:57 +02:00
if (mac.length == 0)
return
2012-06-04 18:27:57 +02:00
d3.selectAll("#chart .node")
.each( function(d) {
if (d.id == mac)
d3.select(this)
.classed("marked", true)
})
2012-06-04 18:23:39 +02:00
}
var hashstr = window.location.hash.substring(1)
function isConnected(a, b) {
return linkedByIndex[a.index + "," + b.index] ||
linkedByIndex[b.index + "," + a.index] ||
a.index == b.index
}
function fade(opacity) {
return function(d) {
vis.selectAll("g.node")
  .style("stroke-opacity", function(o) {
var connected = isConnected(d, o)
if (connected && opacity != 1)
d3.select(this)
.classed("highlight", true)
else
d3.select(this)
.classed("highlight", false)
        thisOpacity = connected?1:opacity
   this.setAttribute('fill-opacity', thisOpacity)
        return thisOpacity
 })
vis.selectAll(".link *")
  .style("stroke-opacity", function(o) {
 return o.source === d || o.target === d ? 1 : opacity
 })
  }
}
function show_node_info(d) {
if (typeof nodeinfo !== 'undefined')
nodeinfo.remove();
nodeinfo = d3.select("#chart")
.append("div")
.attr("id", "nodeinfo")
nodeinfo.append("h1")
.text(d.name)
nodeinfo.append("p")
.text("primary: " + d.id)
nodeinfo.append("p")
.text("macs: " + d.macs)
nodeinfo.append("p")
.text(d.gps)
}
2012-06-04 18:23:39 +02:00
2012-06-03 18:57:36 +02:00
function update_graph() {
var value = jQuery(this).val()
update(data, value)
2012-06-03 18:57:36 +02:00
}
var vis = d3.select("#chart").append("svg")
.attr("width", w)
.attr("height", h)
vis.append("g").attr("class", "links")
vis.append("g").attr("class", "nodes")
var linkedByIndex
var force = d3.layout.force()
.charge(-100)
.gravity(0.02)
.friction(0.75)
.theta(0.1)
.size([w, h])
force.on("tick", function() {
var link = vis.selectAll(".link")
link.selectAll("line")
.attr("x1", function(d) { return d.source.x })
.attr("y1", function(d) { return d.source.y })
.attr("x2", function(d) { return d.target.x })
.attr("y2", function(d) { return d.target.y })
link.selectAll(".label")
.attr("transform", function(d) {
π = Math.PI
Δx = d.source.x - d.target.x
Δy = d.source.y - d.target.y
m = Δy/Δx
α = Math.atan(m)
α += Δx<0?π:0
sin = Math.sin(α)
cos = Math.cos(α)
x = (Math.min(d.source.x, d.target.x) + Math.abs(Δx) / 2)
y = (Math.min(d.source.y, d.target.y) + Math.abs(Δy) / 2)
return "matrix(" + [cos, sin, -sin, cos, x, y].join(",") + ")"
})
vis.selectAll(".node").attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
})
})
var data
d3.json("nodes.json", function(json) {
force.linkDistance(function (d) { return d.distance; })
.linkStrength(function (d) { return d.strength; })
json.links.forEach(function(d) {
var node, other
if (d.source.group == 2) {
node = d.target;
other = d.source;
}
if (d.target.group == 2) {
node = d.source;
other = d.target;
}
if (node) {
if (node.uplinks === undefined)
node.uplinks = new Array();
node.uplinks.push(other);
}
})
data = json;
update(data, "all")
})
function update(src, type) {
var linkdata = data.links;
var links = force.links(data.links
.filter(function (d) {
return type != "mesh" ||
d.source.group != 2 &&
d.source.group != 3 &&
d.target.group != 2 &&
d.target.group != 3
})
).links()
var link = vis.select("g.links")
.selectAll("g.link")
.data(links, function(d) {
2012-06-06 01:26:38 +02:00
return d.id
})
2012-06-03 14:05:44 +02:00
var linkEnter = link.enter().append("g")
.attr("class", "link")
linkEnter.append("line")
.style("stroke-width", function(d) {
return Math.min(1, d.strength * 2)
})
linkEnter.filter(function (d) {
return d.quality != "TT" && d.quality != "1.000"
})
.append("path")
.attr("class", "label")
.attr("d", d3.svg.zigzag()
.amplitude(function (d) {
return Math.pow((1 - 1/d.quality), 0.5) * 8;
})
.len(30)
.angularFrequency(4)
)
link.exit().remove()
var nodes = force.nodes(data.nodes
.filter(function (d) {
return type != "mesh" ||
(d.group != 2 && d.group != 3)
})
).nodes()
var node = vis.select("g.nodes")
.selectAll("g.node")
.data(nodes,
function(d) {
2012-06-06 01:26:38 +02:00
return d.id
}
)
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.on("mouseover", fade(.2))
.on("mouseout", fade(1))
.on("click", show_node_info)
.call(force.drag)
nodeEnter.append("ellipse")
.attr("rx", function(d) {
if (d.group == 3) return 4
else return Math.max(10, d.name.length * 5)
})
.attr("ry", function(d) {
if (d.group == 3) return 4
else return 10
})
.style("fill", function(d) {
if (d.group == 3) return fill(d.group)
else return ""
})
.style("stroke", function(d) {
return fill(d.group)
})
nodeEnter.filter(function(d) {
return d.group != 3
})
.append("text")
2012-06-03 18:57:36 +02:00
.attr("text-anchor", "middle")
.attr("y", "4px")
.text(function(d) { return d.name })
2012-02-17 02:09:21 +01:00
nodeEnter.append("title")
.text(function(d) { return d.macs })
2012-06-03 18:57:36 +02:00
if (type == "mesh") {
2012-06-03 19:05:48 +02:00
var uplink_info = node.filter(function (d) {
if (d.uplinks !== undefined)
return d.uplinks.length > 0;
2012-06-03 19:05:48 +02:00
else
return false;
2012-06-03 19:05:48 +02:00
})
.append("g");
2012-06-03 21:08:48 +02:00
uplink_info.append("path")
.attr("d","m -2.8850049,-13.182327"
+ "c 7.5369165,0.200772 12.1529864,-1.294922 12.3338513,-10.639456"
+ "l 2.2140476,1.018191 -3.3137621,-5.293097 -3.2945999,5.20893 2.4339957,-0.995747"
+ "c -0.4041883,5.76426 -1.1549641,10.561363 -10.3735326,10.701179 z")
2012-06-03 21:08:48 +02:00
.style("fill", "#333");
2012-06-03 18:57:36 +02:00
uplink_info.append("text")
.attr("text-anchor", "middle")
2012-06-03 21:08:48 +02:00
.attr("y", 3 - 20)
2012-06-03 19:05:48 +02:00
.text(function (d) {return d.uplinks.length});
2012-06-03 18:57:36 +02:00
}
2012-02-17 02:09:21 +01:00
node.exit().remove()
2012-06-04 18:25:58 +02:00
force.start()
linkedByIndex = {}
2012-06-04 18:23:39 +02:00
links.forEach(function(d) {
linkedByIndex[d.source.index + "," + d.target.index] = 1
})
if (hashstr.length != 0)
show_node(hashstr)
2012-06-03 18:57:36 +02:00
}