meshviewer/lib/forcegraph.js

508 lines
12 KiB
JavaScript
Raw Normal View History

2015-03-31 15:22:36 +00:00
define(["d3"], function (d3) {
return function (config, linkScale, sidebar, router) {
2015-03-31 15:22:36 +00:00
var self = this
2015-04-10 18:47:11 +00:00
var svg, canvas, ctx
var svgNodes, svgLinks
2015-03-31 15:22:36 +00:00
var nodesDict, linksDict
2015-03-31 19:18:06 +00:00
var zoomBehavior
2015-03-31 15:22:36 +00:00
var force
2015-03-31 19:31:06 +00:00
var el
var doAnimation = false
2015-04-03 00:32:32 +00:00
var intNodes = []
2015-04-07 23:21:52 +00:00
var intLinks = []
var highlight
2015-04-10 18:47:11 +00:00
var highlightedNodes = []
var highlightedLinks = []
2015-03-31 15:22:36 +00:00
2015-03-31 22:06:17 +00:00
var LINK_DISTANCE = 70
2015-04-01 16:21:54 +00:00
function graphDiameter(nodes) {
2015-04-10 11:50:39 +00:00
return Math.sqrt(nodes.length / Math.PI) * LINK_DISTANCE * 1.41
2015-04-01 16:21:54 +00:00
}
function savePositions() {
if (!localStorageTest())
return
2015-04-03 00:32:32 +00:00
var save = intNodes.map( function (d) {
return { id: d.o.id, x: d.x, y: d.y }
})
localStorage.setItem("graph/nodeposition", JSON.stringify(save))
}
2015-03-31 15:22:36 +00:00
function nodeName(d) {
2015-04-03 00:32:32 +00:00
if (d.o.node && d.o.node.nodeinfo)
return d.o.node.nodeinfo.hostname
2015-03-31 15:22:36 +00:00
else
2015-04-03 00:32:32 +00:00
return d.o.id
2015-03-31 15:22:36 +00:00
}
function dragstart(d) {
d3.event.sourceEvent.stopPropagation()
d.fixed |= 2
}
function dragmove(d) {
d.px = d3.event.x
d.py = d3.event.y
force.resume()
}
function dragend(d) {
d3.event.sourceEvent.stopPropagation()
d.fixed &= 1
}
2015-04-10 11:50:39 +00:00
var draggableNode = d3.behavior.drag()
.on("dragstart", dragstart)
.on("drag", dragmove)
.on("dragend", dragend)
2015-03-31 19:31:06 +00:00
function animatePanzoom(translate, scale) {
2015-04-10 18:47:11 +00:00
var translateP = zoomBehavior.translate()
var scaleP = zoomBehavior.scale()
2015-03-31 19:31:06 +00:00
2015-04-10 18:47:11 +00:00
if (!doAnimation) {
zoomBehavior.translate(translate)
zoomBehavior.scale(scale)
panzoom()
} else {
var start = {x: translateP[0], y: translateP[1], scale: scaleP}
var end = {x: translate[0], y: translate[1], scale: scale}
2015-04-10 18:47:11 +00:00
var interpolate = d3.interpolateObject(start, end)
var duration = 500
2015-04-10 18:47:11 +00:00
var ease = d3.ease("cubic-in-out")
d3.timer(function (t) {
if (t >= duration)
return true
var v = interpolate(ease(t / duration))
zoomBehavior.translate([v.x, v.y])
zoomBehavior.scale(v.scale)
panzoom()
return false
})
}
2015-03-31 19:31:06 +00:00
}
2015-03-31 15:22:36 +00:00
function panzoom() {
2015-03-31 19:18:06 +00:00
var translate = zoomBehavior.translate()
var scale = zoomBehavior.scale()
2015-04-10 18:47:11 +00:00
panzoomReal(translate, scale)
}
function panzoomReal(translate, scale) {
svg.attr("transform", "translate(" + translate + ") " +
2015-03-31 19:18:06 +00:00
"scale(" + scale + ")")
2015-04-10 18:47:11 +00:00
redraw()
2015-03-31 19:18:06 +00:00
}
2015-04-01 16:21:54 +00:00
function getSize() {
var sidebarWidth = sidebar.getWidth()
var width = el.offsetWidth - sidebarWidth
var height = el.offsetHeight
return [width, height]
}
2015-03-31 19:18:06 +00:00
function panzoomTo(a, b) {
var sidebarWidth = sidebar.getWidth()
2015-04-01 16:21:54 +00:00
var size = getSize()
2015-03-31 19:18:06 +00:00
var targetWidth = Math.max(1, b[0] - a[0])
var targetHeight = Math.max(1, b[1] - a[1])
var scaleX = size[0] / targetWidth
var scaleY = size[1] / targetHeight
var scaleMax = zoomBehavior.scaleExtent()[1]
var scale = 0.5 * Math.min(scaleMax, Math.min(scaleX, scaleY))
var centroid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2]
var x = -centroid[0] * scale + size[0] / 2
var y = -centroid[1] * scale + size[1] / 2
var translate = [x + sidebarWidth, y]
2015-03-31 19:29:47 +00:00
animatePanzoom(translate, scale)
}
function updateHighlight(nopanzoom) {
2015-04-10 18:47:11 +00:00
highlightedNodes = []
highlightedLinks = []
if (highlight !== undefined)
if (highlight.type === "node") {
var n = nodesDict[highlight.o.nodeinfo.node_id]
if (n) {
2015-04-10 18:47:11 +00:00
highlightedNodes = [n]
if (!nopanzoom)
panzoomTo([n.x, n.y], [n.x, n.y])
}
return
} else if (highlight.type === "link") {
2015-04-06 21:14:24 +00:00
var l = linksDict[highlight.o.id]
if (l) {
2015-04-10 18:47:11 +00:00
highlightedLinks = [l]
if (!nopanzoom) {
var x = d3.extent([l.source, l.target], function (d) { return d.x })
var y = d3.extent([l.source, l.target], function (d) { return d.y })
panzoomTo([x[0], y[0]], [x[1], y[1]])
}
}
return
}
if (!nopanzoom)
panzoomTo([0, 0], force.size())
}
2015-04-10 11:50:39 +00:00
function updateLinks(vis, data) {
2015-04-10 18:47:11 +00:00
var link = vis.selectAll("line")
.data(data, function (d) { return d.o.id })
2015-04-10 11:50:39 +00:00
link.exit().remove()
2015-04-10 18:47:11 +00:00
link.enter().append("line")
.on("click", function (d) {
if (!d3.event.defaultPrevented)
router.link(d.o)()
})
2015-04-10 11:50:39 +00:00
return link
}
function updateNodes(vis, data) {
2015-04-10 18:47:11 +00:00
var node = vis.selectAll("circle")
2015-04-10 11:50:39 +00:00
.data(data, function(d) { return d.o.id })
node.exit().remove()
node.enter().append("circle")
2015-04-10 18:47:11 +00:00
.attr("r", 12)
2015-04-10 11:50:39 +00:00
.on("click", function (d) {
if (!d3.event.defaultPrevented)
router.node(d.o.node)()
})
.call(draggableNode)
return node
}
2015-04-10 18:47:11 +00:00
function drawLabel(d) {
var sum = d.neighbours.reduce(function (a, b) {
return [a[0] + b.x, a[1] + b.y]
}, [0, 0])
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
var sumCos = sum[0] - d.x * d.neighbours.length
var sumSin = sum[1] - d.y * d.neighbours.length
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
var angle = Math.PI / 2
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
if (d.neighbours.length > 0)
angle = Math.PI + Math.atan2(sumSin, sumCos)
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
var cos = Math.cos(angle)
var sin = Math.sin(angle)
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
var x = d.x + d.labelA * Math.pow(Math.abs(cos), 2 / 5) * Math.sign(cos)
var y = d.y + d.labelB * Math.pow(Math.abs(sin), 2 / 5) * Math.sign(sin)
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
ctx.fillText(d.label, x, y)
}
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
function redraw() {
var translate = zoomBehavior.translate()
var scale = zoomBehavior.scale()
var nodes = intNodes.filter(function (d) { return d.o.node })
var unknownNodes = intNodes.filter(function (d) { return !d.o.node })
var links = intLinks
ctx.save()
ctx.font = "11px Roboto"
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.translate(translate[0], translate[1])
ctx.scale(scale, scale)
if (highlightedLinks.length) {
ctx.save()
ctx.lineWidth = 16
ctx.strokeStyle = "#FFD486"
highlightedLinks.forEach(function (d) {
ctx.beginPath()
ctx.moveTo(d.source.x, d.source.y)
ctx.lineTo(d.target.x, d.target.y)
ctx.stroke()
2015-04-10 11:50:39 +00:00
})
2015-04-10 18:47:11 +00:00
ctx.restore()
}
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
ctx.lineWidth = 2.5
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
links.forEach(function (d) {
ctx.beginPath()
ctx.moveTo(d.source.x, d.source.y)
ctx.lineTo(d.target.x, d.target.y)
ctx.strokeStyle = d.color
ctx.stroke()
})
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
ctx.textAlign = "center"
ctx.textBaseline = "middle"
ctx.fillStyle = "rgba(0, 0, 0, 0.6)"
intNodes.forEach(drawLabel)
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
ctx.beginPath()
unknownNodes.forEach(function (d) {
ctx.moveTo(d.x + 8, d.y)
ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI)
})
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
ctx.strokeStyle = "#d00000"
ctx.fillStyle = "#ffffff"
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
ctx.fill()
ctx.stroke()
2015-04-10 11:50:39 +00:00
2015-04-10 18:47:11 +00:00
ctx.beginPath()
nodes.forEach(function (d) {
ctx.moveTo(d.x + 8, d.y)
ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI)
2015-04-10 11:50:39 +00:00
})
2015-04-10 18:47:11 +00:00
ctx.strokeStyle = "#AEC7E8"
ctx.fillStyle = "#ffffff"
ctx.fill()
ctx.stroke()
if (highlightedNodes.length) {
ctx.save()
ctx.strokeStyle = "#FFD486"
ctx.fillStyle = "orange"
ctx.lineWidth = 6
highlightedNodes.forEach(function (d) {
ctx.beginPath()
ctx.moveTo(d.x + 8, d.y)
ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI)
ctx.fill()
ctx.stroke()
})
ctx.restore()
}
ctx.restore()
2015-04-10 11:50:39 +00:00
}
2015-03-31 15:22:36 +00:00
function tickEvent() {
2015-04-10 18:47:11 +00:00
redraw()
svgLinks.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 })
svgNodes.attr("cx", function (d) { return d.x })
.attr("cy", function (d) { return d.y })
}
function resizeCanvas() {
canvas.width = el.offsetWidth
canvas.height = el.offsetHeight
redraw()
2015-03-31 15:22:36 +00:00
}
2015-03-31 19:31:06 +00:00
el = document.createElement("div")
2015-03-31 15:22:36 +00:00
el.classList.add("graph")
self.div = el
2015-03-31 19:18:06 +00:00
zoomBehavior = d3.behavior.zoom()
2015-03-31 19:31:06 +00:00
.scaleExtent([1 / 3, 3])
2015-03-31 19:18:06 +00:00
.on("zoom", panzoom)
.translate([sidebar.getWidth(), 0])
2015-04-10 18:47:11 +00:00
canvas = d3.select(el).append("canvas").node()
svg = d3.select(el).append("svg")
.attr("pointer-events", "all")
.call(zoomBehavior)
2015-04-10 18:47:11 +00:00
.append("g")
2015-03-31 19:18:06 +00:00
2015-04-10 18:47:11 +00:00
var visLinks = svg.append("g")
var visNodes = svg.append("g")
2015-03-31 15:22:36 +00:00
2015-04-10 18:47:11 +00:00
ctx = canvas.getContext("2d")
2015-03-31 15:22:36 +00:00
force = d3.layout.force()
2015-04-10 11:50:39 +00:00
.charge(-80)
.gravity(0.01)
.chargeDistance(8 * LINK_DISTANCE)
2015-03-31 22:06:17 +00:00
.linkDistance(LINK_DISTANCE)
2015-03-31 23:07:40 +00:00
.linkStrength(function (d) {
2015-04-10 11:50:39 +00:00
return Math.max(0.5, 1 / d.o.tq)
2015-03-31 23:07:40 +00:00
})
2015-03-31 15:22:36 +00:00
.on("tick", tickEvent)
.on("end", savePositions)
2015-03-31 15:22:36 +00:00
2015-04-10 18:47:11 +00:00
window.addEventListener("resize", resizeCanvas)
panzoom()
2015-03-31 15:22:36 +00:00
self.setData = function (data) {
2015-04-03 00:32:32 +00:00
var oldNodes = {}
2015-04-03 00:32:32 +00:00
intNodes.forEach( function (d) {
oldNodes[d.o.id] = d
})
2015-04-03 00:32:32 +00:00
intNodes = data.graph.nodes.map( function (d) {
var e
if (d.id in oldNodes)
e = oldNodes[d.id]
else
e = {}
e.o = d
2015-04-03 00:32:32 +00:00
return e
})
var newNodesDict = {}
intNodes.forEach( function (d) {
newNodesDict[d.o.id] = d
})
2015-04-07 23:21:52 +00:00
var oldLinks = {}
intLinks.forEach( function (d) {
oldLinks[d.o.id] = d
})
intLinks = data.graph.links.filter( function (d) {
2015-03-31 15:22:36 +00:00
return !d.vpn
2015-04-03 00:32:32 +00:00
}).map( function (d) {
2015-04-07 23:21:52 +00:00
var e
if (d.id in oldLinks)
e = oldLinks[d.id]
else
e = {}
e.o = d
e.source = newNodesDict[d.source.id]
e.target = newNodesDict[d.target.id]
2015-04-10 18:47:11 +00:00
e.color = linkScale(d.tq).hex()
2015-04-03 00:32:32 +00:00
2015-04-07 23:21:52 +00:00
return e
2015-03-31 15:22:36 +00:00
})
linksDict = {}
nodesDict = {}
2015-04-10 11:50:39 +00:00
intNodes.forEach(function (d) {
d.neighbours = {}
if (d.o.node)
nodesDict[d.o.node.nodeinfo.node_id] = d
2015-04-10 18:47:11 +00:00
d.label = nodeName(d)
ctx.font = "11px Roboto"
var offset = 10
var width = ctx.measureText(d.label).width
d.labelA = offset + width / 2
d.labelB = offset + 11 / 2
2015-04-10 11:50:39 +00:00
})
2015-03-31 15:22:36 +00:00
2015-04-10 11:50:39 +00:00
intLinks.forEach(function (d) {
d.source.neighbours[d.target.o.id] = d.target
d.target.neighbours[d.source.o.id] = d.source
if (d.o.source.node && d.o.target.node)
linksDict[d.o.id] = d
2015-04-10 11:50:39 +00:00
})
2015-03-31 15:22:36 +00:00
2015-04-10 11:50:39 +00:00
intNodes.forEach(function (d) {
d.neighbours = Object.keys(d.neighbours).map(function (k) {
return d.neighbours[k]
})
})
2015-03-31 15:22:36 +00:00
2015-04-10 18:47:11 +00:00
svgLinks = updateLinks(visLinks, intLinks)
svgNodes = updateNodes(visNodes, intNodes)
2015-03-31 15:22:36 +00:00
2015-04-03 00:32:32 +00:00
if (localStorageTest()) {
var save = JSON.parse(localStorage.getItem("graph/nodeposition"))
if (save) {
var nodePositions = {}
save.forEach( function (d) {
nodePositions[d.id] = d
})
2015-04-10 18:47:11 +00:00
intNodes.forEach( function (d) {
2015-04-10 11:50:39 +00:00
if (nodePositions[d.o.id] && (d.x === undefined || d.y === undefined)) {
2015-04-03 00:32:32 +00:00
d.x = nodePositions[d.o.id].x
d.y = nodePositions[d.o.id].y
}
})
}
2015-04-03 00:32:32 +00:00
}
2015-03-31 15:22:36 +00:00
2015-04-03 00:32:32 +00:00
var diameter = graphDiameter(intNodes)
2015-04-01 16:21:54 +00:00
2015-04-03 00:32:32 +00:00
force.nodes(intNodes)
.links(intLinks)
2015-04-01 16:21:54 +00:00
.size([diameter, diameter])
2015-04-03 00:32:32 +00:00
updateHighlight(true)
2015-04-10 18:47:11 +00:00
force.start()
resizeCanvas()
2015-03-31 15:22:36 +00:00
}
self.resetView = function () {
highlight = undefined
updateHighlight()
doAnimation = true
2015-03-31 15:22:36 +00:00
}
self.gotoNode = function (d) {
highlight = {type: "node", o: d}
updateHighlight()
doAnimation = true
2015-03-31 15:22:36 +00:00
}
self.gotoLink = function (d) {
highlight = {type: "link", o: d}
updateHighlight()
doAnimation = true
2015-03-31 15:22:36 +00:00
}
self.destroy = function () {
force.stop()
2015-04-10 18:47:11 +00:00
canvas.remove()
force = null
svg = null
}
2015-03-31 15:22:36 +00:00
return self
}
})