forcegraph.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. define(["d3"], function (d3) {
  2. return function (linkScale, sidebar, router) {
  3. var self = this
  4. var nodes, links
  5. var svg, vis, link, node
  6. var nodesDict, linksDict
  7. var zoomBehavior
  8. var force
  9. var el
  10. var doAnimation = false
  11. var LINK_DISTANCE = 70
  12. function savePositions() {
  13. var save = nodes.map( function (d) {
  14. return { id: d.id, x: d.x, y: d.y }
  15. })
  16. localStorage.setItem("graph/nodeposition", JSON.stringify(save))
  17. }
  18. function nodeName(d) {
  19. if (d.node && d.node.nodeinfo)
  20. return d.node.nodeinfo.hostname
  21. else
  22. return d.id
  23. }
  24. function dragstart(d) {
  25. d3.event.sourceEvent.stopPropagation()
  26. d.fixed |= 2
  27. }
  28. function dragmove(d) {
  29. d.px = d3.event.x
  30. d.py = d3.event.y
  31. force.resume()
  32. }
  33. function dragend(d) {
  34. d3.event.sourceEvent.stopPropagation()
  35. d.fixed &= 1
  36. }
  37. function animatePanzoom(translate, scale) {
  38. zoomBehavior.scale(scale)
  39. zoomBehavior.translate(translate)
  40. var el = vis
  41. if (doAnimation)
  42. el = el.transition().duration(500)
  43. el.attr("transform", "translate(" + translate + ") " +
  44. "scale(" + scale + ")")
  45. }
  46. function panzoom() {
  47. var translate = zoomBehavior.translate()
  48. var scale = zoomBehavior.scale()
  49. vis.attr("transform", "translate(" + translate + ") " +
  50. "scale(" + scale + ")")
  51. }
  52. function panzoomTo(a, b) {
  53. var sidebarWidth = sidebar.getWidth()
  54. var size = force.size()
  55. var targetWidth = Math.max(1, b[0] - a[0])
  56. var targetHeight = Math.max(1, b[1] - a[1])
  57. var scaleX = size[0] / targetWidth
  58. var scaleY = size[1] / targetHeight
  59. var scaleMax = zoomBehavior.scaleExtent()[1]
  60. var scale = 0.5 * Math.min(scaleMax, Math.min(scaleX, scaleY))
  61. var centroid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2]
  62. var x = -centroid[0] * scale + size[0] / 2
  63. var y = -centroid[1] * scale + size[1] / 2
  64. var translate = [x + sidebarWidth, y]
  65. animatePanzoom(translate, scale)
  66. }
  67. function resize() {
  68. var sidebarWidth = sidebar.getWidth()
  69. var width = el.offsetWidth - sidebarWidth
  70. var height = el.offsetHeight
  71. force.size([width, height])
  72. }
  73. function tickEvent() {
  74. link.selectAll("line")
  75. .attr("x1", function(d) { return d.source.x })
  76. .attr("y1", function(d) { return d.source.y })
  77. .attr("x2", function(d) { return d.target.x })
  78. .attr("y2", function(d) { return d.target.y })
  79. node
  80. .attr("cx", function(d) { return d.x })
  81. .attr("cy", function(d) { return d.y })
  82. }
  83. el = document.createElement("div")
  84. el.classList.add("graph")
  85. self.div = el
  86. zoomBehavior = d3.behavior.zoom()
  87. .scaleExtent([1 / 3, 3])
  88. .on("zoom", panzoom)
  89. .translate([sidebar.getWidth(), 0])
  90. svg = d3.select(el).append("svg")
  91. .attr("pointer-events", "all")
  92. .call(zoomBehavior)
  93. vis = svg.append("g")
  94. vis.append("g").attr("class", "links")
  95. vis.append("g").attr("class", "nodes")
  96. force = d3.layout.force()
  97. .charge(-70)
  98. .gravity(0.05)
  99. .linkDistance(LINK_DISTANCE)
  100. .linkStrength(function (d) {
  101. return 1 / d.tq
  102. })
  103. .on("tick", tickEvent)
  104. .on("end", savePositions)
  105. panzoom()
  106. var draggableNode = d3.behavior.drag()
  107. .on("dragstart", dragstart)
  108. .on("drag", dragmove)
  109. .on("dragend", dragend)
  110. window.addEventListener("resize", resize)
  111. self.setData = function (data) {
  112. var save = JSON.parse(localStorage.getItem("graph/nodeposition"))
  113. var nodePositions = {}
  114. if (save)
  115. save.forEach( function (d) {
  116. nodePositions[d.id] = d
  117. })
  118. links = data.graph.links.filter( function (d) {
  119. return !d.vpn
  120. })
  121. link = vis.select("g.links")
  122. .selectAll("g.link")
  123. .data(links, linkId)
  124. var linkEnter = link.enter().append("g")
  125. .attr("class", "link")
  126. .on("click", function (d) {
  127. if (!d3.event.defaultPrevented)
  128. router.link(d)()
  129. })
  130. linkEnter.append("line")
  131. .append("title")
  132. link.selectAll("line")
  133. .style("stroke", function (d) { return linkScale(d.tq) })
  134. link.selectAll("title").text(showTq)
  135. linksDict = {}
  136. link.each( function (d) {
  137. if (d.source.node && d.target.node)
  138. linksDict[linkId(d)] = d
  139. })
  140. nodes = data.graph.nodes
  141. node = vis.select("g.nodes")
  142. .selectAll(".node")
  143. .data(nodes,
  144. function(d) {
  145. return d.id
  146. }
  147. )
  148. var nodeEnter = node.enter().append("circle")
  149. .attr("r", 8)
  150. .on("click", function (d) {
  151. if (!d3.event.defaultPrevented)
  152. router.node(d.node)()
  153. })
  154. .call(draggableNode)
  155. node.attr("class", function (d) {
  156. var s = ["node"]
  157. if (!d.node)
  158. s.push("unknown")
  159. return s.join(" ")
  160. })
  161. nodesDict = {}
  162. node.each( function (d) {
  163. if (d.node)
  164. nodesDict[d.node.nodeinfo.node_id] = d
  165. })
  166. nodeEnter.append("title")
  167. nodeEnter.each( function (d) {
  168. if (nodePositions[d.id]) {
  169. d.x = nodePositions[d.id].x
  170. d.y = nodePositions[d.id].y
  171. }
  172. })
  173. node.selectAll("title").text(nodeName)
  174. force.nodes(nodes)
  175. .links(links)
  176. resize()
  177. force.start()
  178. }
  179. self.resetView = function () {
  180. node.classed("highlight", false)
  181. link.classed("highlight", false)
  182. var size = force.size()
  183. var diameter = Math.sqrt(nodes.length / Math.PI) * LINK_DISTANCE
  184. var x = (size[0] - diameter) / 2
  185. var y = (size[1] - diameter) / 2
  186. panzoomTo([x, y], [x + diameter, y + diameter])
  187. doAnimation = true
  188. }
  189. self.gotoNode = function (d) {
  190. link.classed("highlight", false)
  191. node.classed("highlight", function (e) {
  192. return e.node === d && d !== undefined
  193. })
  194. var n = nodesDict[d.nodeinfo.node_id]
  195. if (n)
  196. panzoomTo([n.x, n.y], [n.x, n.y])
  197. doAnimation = true
  198. }
  199. self.gotoLink = function (d) {
  200. node.classed("highlight", false)
  201. link.classed("highlight", function (e) {
  202. return e === d && d !== undefined
  203. })
  204. var l = linksDict[linkId(d)]
  205. if (l) {
  206. var x = d3.extent([l.source, l.target], function (d) { return d.x })
  207. var y = d3.extent([l.source, l.target], function (d) { return d.y })
  208. panzoomTo([x[0], y[0]], [x[1], y[1]])
  209. }
  210. doAnimation = true
  211. }
  212. self.destroy = function () {
  213. window.removeEventListener("resize", resize)
  214. force.stop()
  215. node.remove()
  216. link.remove()
  217. svg.remove()
  218. force = null
  219. svg = null
  220. vis = null
  221. link = null
  222. node = null
  223. }
  224. return self
  225. }
  226. })