forcegraph.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. define(["d3"], function (d3) {
  2. var margin = 200
  3. var NODE_RADIUS = 15
  4. var LINE_RADIUS = 12
  5. return function (config, linkScale, sidebar, router) {
  6. var self = this
  7. var canvas, ctx, screenRect
  8. var nodesDict, linksDict
  9. var zoomBehavior
  10. var force
  11. var el
  12. var doAnimation = false
  13. var intNodes = []
  14. var intLinks = []
  15. var highlight
  16. var highlightedNodes = []
  17. var highlightedLinks = []
  18. var nodes = []
  19. var unknownNodes = []
  20. var savedPanZoom
  21. var draggedNode
  22. var LINK_DISTANCE = 70
  23. function graphDiameter(nodes) {
  24. return Math.sqrt(nodes.length / Math.PI) * LINK_DISTANCE * 1.41
  25. }
  26. function savePositions() {
  27. if (!localStorageTest())
  28. return
  29. var save = intNodes.map( function (d) {
  30. return { id: d.o.id, x: d.x, y: d.y }
  31. })
  32. localStorage.setItem("graph/nodeposition", JSON.stringify(save))
  33. }
  34. function nodeName(d) {
  35. if (d.o.node && d.o.node.nodeinfo)
  36. return d.o.node.nodeinfo.hostname
  37. else
  38. return d.o.id
  39. }
  40. function dragstart() {
  41. var e = translateXY(d3.mouse(el))
  42. var nodes = intNodes.filter(function (d) {
  43. return distancePoint(e, d) < NODE_RADIUS
  44. })
  45. if (nodes.length === 0)
  46. return
  47. draggedNode = nodes[0]
  48. d3.event.sourceEvent.stopPropagation()
  49. d3.event.sourceEvent.preventDefault()
  50. draggedNode.fixed |= 2
  51. draggedNode.px = draggedNode.x
  52. draggedNode.py = draggedNode.y
  53. }
  54. function dragmove() {
  55. if (draggedNode) {
  56. var e = translateXY(d3.mouse(el))
  57. draggedNode.px = e.x
  58. draggedNode.py = e.y
  59. force.resume()
  60. }
  61. }
  62. function dragend() {
  63. if (draggedNode) {
  64. d3.event.sourceEvent.stopPropagation()
  65. d3.event.sourceEvent.preventDefault()
  66. draggedNode.fixed &= ~2
  67. draggedNode = undefined
  68. }
  69. }
  70. var draggableNode = d3.behavior.drag()
  71. .on("dragstart", dragstart)
  72. .on("drag", dragmove)
  73. .on("dragend", dragend)
  74. function animatePanzoom(translate, scale) {
  75. var translateP = zoomBehavior.translate()
  76. var scaleP = zoomBehavior.scale()
  77. if (!doAnimation) {
  78. zoomBehavior.translate(translate)
  79. zoomBehavior.scale(scale)
  80. panzoom()
  81. } else {
  82. var start = {x: translateP[0], y: translateP[1], scale: scaleP}
  83. var end = {x: translate[0], y: translate[1], scale: scale}
  84. var interpolate = d3.interpolateObject(start, end)
  85. var duration = 500
  86. var ease = d3.ease("cubic-in-out")
  87. d3.timer(function (t) {
  88. if (t >= duration)
  89. return true
  90. var v = interpolate(ease(t / duration))
  91. zoomBehavior.translate([v.x, v.y])
  92. zoomBehavior.scale(v.scale)
  93. panzoom()
  94. return false
  95. })
  96. }
  97. }
  98. function onPanZoom() {
  99. savedPanZoom = {translate: zoomBehavior.translate(),
  100. scale: zoomBehavior.scale()}
  101. panzoom()
  102. }
  103. function panzoom() {
  104. var translate = zoomBehavior.translate()
  105. var scale = zoomBehavior.scale()
  106. panzoomReal(translate, scale)
  107. }
  108. function panzoomReal(translate, scale) {
  109. screenRect = {left: -translate[0] / scale, top: -translate[1] / scale,
  110. right: (canvas.width - translate[0]) / scale,
  111. bottom: (canvas.height - translate[1]) / scale}
  112. requestAnimationFrame(redraw)
  113. }
  114. function getSize() {
  115. var sidebarWidth = sidebar()
  116. var width = el.offsetWidth - sidebarWidth
  117. var height = el.offsetHeight
  118. return [width, height]
  119. }
  120. function panzoomTo(a, b) {
  121. var sidebarWidth = sidebar()
  122. var size = getSize()
  123. var targetWidth = Math.max(1, b[0] - a[0])
  124. var targetHeight = Math.max(1, b[1] - a[1])
  125. var scaleX = size[0] / targetWidth
  126. var scaleY = size[1] / targetHeight
  127. var scaleMax = zoomBehavior.scaleExtent()[1]
  128. var scale = 0.5 * Math.min(scaleMax, Math.min(scaleX, scaleY))
  129. var centroid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2]
  130. var x = -centroid[0] * scale + size[0] / 2
  131. var y = -centroid[1] * scale + size[1] / 2
  132. var translate = [x + sidebarWidth, y]
  133. animatePanzoom(translate, scale)
  134. }
  135. function updateHighlight(nopanzoom) {
  136. highlightedNodes = []
  137. highlightedLinks = []
  138. if (highlight !== undefined)
  139. if (highlight.type === "node") {
  140. var n = nodesDict[highlight.o.nodeinfo.node_id]
  141. if (n) {
  142. highlightedNodes = [n]
  143. if (!nopanzoom)
  144. panzoomTo([n.x, n.y], [n.x, n.y])
  145. }
  146. return
  147. } else if (highlight.type === "link") {
  148. var l = linksDict[highlight.o.id]
  149. if (l) {
  150. highlightedLinks = [l]
  151. if (!nopanzoom) {
  152. var x = d3.extent([l.source, l.target], function (d) { return d.x })
  153. var y = d3.extent([l.source, l.target], function (d) { return d.y })
  154. panzoomTo([x[0], y[0]], [x[1], y[1]])
  155. }
  156. }
  157. return
  158. }
  159. if (!nopanzoom)
  160. if (!savedPanZoom)
  161. panzoomTo([0, 0], force.size())
  162. else
  163. animatePanzoom(savedPanZoom.translate, savedPanZoom.scale)
  164. }
  165. function drawLabel(d) {
  166. var neighbours = d.neighbours.filter(function (d) {
  167. return !d.link.o.vpn
  168. })
  169. var sum = neighbours.reduce(function (a, b) {
  170. return [a[0] + b.node.x, a[1] + b.node.y]
  171. }, [0, 0])
  172. var sumCos = sum[0] - d.x * neighbours.length
  173. var sumSin = sum[1] - d.y * neighbours.length
  174. var angle = Math.PI / 2
  175. if (neighbours.length > 0)
  176. angle = Math.PI + Math.atan2(sumSin, sumCos)
  177. var cos = Math.cos(angle)
  178. var sin = Math.sin(angle)
  179. var width = d.labelWidth
  180. var height = d.labelHeight
  181. var x = d.x + d.labelA * Math.pow(Math.abs(cos), 2 / 5) * Math.sign(cos) - width / 2
  182. var y = d.y + d.labelB * Math.pow(Math.abs(sin), 2 / 5) * Math.sign(sin) - height / 2
  183. ctx.drawImage(d.label, x, y, width, height)
  184. }
  185. function visibleLinks(d) {
  186. return (d.source.x > screenRect.left && d.source.x < screenRect.right &&
  187. d.source.y > screenRect.top && d.source.y < screenRect.bottom) ||
  188. (d.target.x > screenRect.left && d.target.x < screenRect.right &&
  189. d.target.y > screenRect.top && d.target.y < screenRect.bottom) ||
  190. d.o.vpn
  191. }
  192. function visibleNodes(d) {
  193. return d.x + margin > screenRect.left && d.x - margin < screenRect.right &&
  194. d.y + margin > screenRect.top && d.y - margin < screenRect.bottom
  195. }
  196. function redraw() {
  197. var translate = zoomBehavior.translate()
  198. var scale = zoomBehavior.scale()
  199. var links = intLinks.filter(visibleLinks)
  200. ctx.save()
  201. ctx.setTransform(1, 0, 0, 1, 0, 0)
  202. ctx.clearRect(0, 0, canvas.width, canvas.height)
  203. ctx.restore()
  204. ctx.save()
  205. ctx.translate(translate[0], translate[1])
  206. ctx.scale(scale, scale)
  207. var clientColor = "rgba(230, 50, 75, 1.0)"
  208. var unknownColor = "#D10E2A"
  209. var nodeColor = "#F2E3C6"
  210. var highlightColor = "rgba(252, 227, 198, 0.15)"
  211. var nodeRadius = 6
  212. // -- draw links --
  213. ctx.save()
  214. links.forEach(function (d) {
  215. var dx = d.target.x - d.source.x
  216. var dy = d.target.y - d.source.y
  217. var a = Math.sqrt(dx * dx + dy * dy)
  218. dx /= a
  219. dy /= a
  220. ctx.beginPath()
  221. ctx.moveTo(d.source.x + dx * nodeRadius, d.source.y + dy * nodeRadius)
  222. ctx.lineTo(d.target.x - dx * nodeRadius, d.target.y - dy * nodeRadius)
  223. ctx.strokeStyle = d.color
  224. ctx.globalAlpha = d.o.vpn ? 0.1 : 0.8
  225. ctx.lineWidth = d.o.vpn ? 1.5 : 2.5
  226. ctx.stroke()
  227. })
  228. ctx.restore()
  229. // -- draw unknown nodes --
  230. ctx.beginPath()
  231. unknownNodes.filter(visibleNodes).forEach(function (d) {
  232. ctx.moveTo(d.x + nodeRadius, d.y)
  233. ctx.arc(d.x, d.y, nodeRadius, 0, 2 * Math.PI)
  234. })
  235. ctx.strokeStyle = unknownColor
  236. ctx.lineWidth = nodeRadius
  237. ctx.stroke()
  238. // -- draw nodes --
  239. var node = document.createElement("canvas")
  240. node.width = scale * nodeRadius * 8
  241. node.height = node.width
  242. var nctx = node.getContext("2d")
  243. nctx.scale(scale, scale)
  244. nctx.save()
  245. nctx.translate(-node.width / scale, -node.height / scale)
  246. nctx.lineWidth = nodeRadius
  247. nctx.beginPath()
  248. nctx.moveTo(nodeRadius, 0)
  249. nctx.arc(0, 0, nodeRadius, 0, 2 * Math.PI)
  250. nctx.strokeStyle = "rgba(255, 0, 0, 1)"
  251. nctx.shadowOffsetX = node.width * 1.5 + 0
  252. nctx.shadowOffsetY = node.height * 1.5 + 3
  253. nctx.shadowBlur = 6
  254. nctx.shadowColor = "rgba(0, 0, 0, 0.32)"
  255. nctx.stroke()
  256. nctx.shadowOffsetX = node.width * 1.5 + 0
  257. nctx.shadowOffsetY = node.height * 1.5 + 3
  258. nctx.shadowBlur = 6
  259. nctx.shadowColor = "rgba(0, 0, 0, 0.46)"
  260. nctx.stroke()
  261. nctx.restore()
  262. nctx.translate(node.width / 2 / scale, node.height / 2 / scale)
  263. nctx.beginPath()
  264. nctx.moveTo(nodeRadius, 0)
  265. nctx.arc(0, 0, nodeRadius, 0, 2 * Math.PI)
  266. nctx.strokeStyle = nodeColor
  267. nctx.lineWidth = nodeRadius
  268. nctx.stroke()
  269. ctx.save()
  270. ctx.scale(1 / scale, 1 / scale)
  271. nodes.filter(visibleNodes).forEach(function (d) {
  272. ctx.drawImage(node, scale * d.x - node.width / 2, scale * d.y - node.height / 2)
  273. })
  274. ctx.restore()
  275. // -- draw clients --
  276. ctx.save()
  277. ctx.beginPath()
  278. nodes.filter(visibleNodes).forEach(function (d) {
  279. var clients = d.o.node.statistics.clients
  280. if (clients === 0)
  281. return
  282. var startDistance = 16
  283. var radius = 3
  284. var a = 1.2
  285. var startAngle = Math.PI
  286. for (var orbit = 0, i = 0; i < clients; orbit++) {
  287. var distance = startDistance + orbit * 2 * radius * a
  288. var n = Math.floor((Math.PI * distance) / (a * radius))
  289. var delta = clients - i
  290. for (var j = 0; j < Math.min(delta, n); i++, j++) {
  291. var angle = 2 * Math.PI / n * j
  292. var x = d.x + distance * Math.cos(angle + startAngle)
  293. var y = d.y + distance * Math.sin(angle + startAngle)
  294. ctx.moveTo(x, y)
  295. ctx.arc(x, y, radius, 0, 2 * Math.PI)
  296. }
  297. }
  298. })
  299. ctx.fillStyle = clientColor
  300. ctx.fill()
  301. ctx.restore()
  302. // -- draw node highlights --
  303. if (highlightedNodes.length) {
  304. ctx.save()
  305. ctx.shadowColor = "rgba(255, 255, 255, 1.0)"
  306. ctx.shadowBlur = 10 * nodeRadius
  307. ctx.shadowOffsetX = 0
  308. ctx.shadowOffsetY = 0
  309. ctx.globalCompositeOperation = "lighten"
  310. ctx.fillStyle = highlightColor
  311. ctx.beginPath()
  312. highlightedNodes.forEach(function (d) {
  313. ctx.moveTo(d.x + 5 * nodeRadius, d.y)
  314. ctx.arc(d.x, d.y, 5 * nodeRadius, 0, 2 * Math.PI)
  315. })
  316. ctx.fill()
  317. ctx.restore()
  318. }
  319. // -- draw link highlights --
  320. if (highlightedLinks.length) {
  321. ctx.save()
  322. ctx.lineWidth = 2 * 5 * nodeRadius
  323. ctx.shadowColor = "rgba(255, 255, 255, 1.0)"
  324. ctx.shadowBlur = 10 * nodeRadius
  325. ctx.shadowOffsetX = 0
  326. ctx.shadowOffsetY = 0
  327. ctx.globalCompositeOperation = "lighten"
  328. ctx.strokeStyle = highlightColor
  329. ctx.lineCap = "round"
  330. ctx.beginPath()
  331. highlightedLinks.forEach(function (d) {
  332. ctx.moveTo(d.source.x, d.source.y)
  333. ctx.lineTo(d.target.x, d.target.y)
  334. })
  335. ctx.stroke()
  336. ctx.restore()
  337. }
  338. // -- draw labels --
  339. if (scale > 0.9)
  340. intNodes.filter(visibleNodes).forEach(drawLabel, scale)
  341. ctx.restore()
  342. }
  343. function tickEvent() {
  344. redraw()
  345. }
  346. function resizeCanvas() {
  347. var r = window.devicePixelRatio
  348. canvas.width = el.offsetWidth * r
  349. canvas.height = el.offsetHeight * r
  350. canvas.style.width = el.offsetWidth + "px"
  351. canvas.style.height = el.offsetHeight + "px"
  352. ctx.setTransform(1, 0, 0, 1, 0, 0)
  353. ctx.scale(r, r)
  354. requestAnimationFrame(redraw)
  355. }
  356. function distance(a, b) {
  357. return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2)
  358. }
  359. function distancePoint(a, b) {
  360. return Math.sqrt(distance(a, b))
  361. }
  362. function distanceLink(p, a, b) {
  363. /* http://stackoverflow.com/questions/849211 */
  364. var l2 = distance(a, b)
  365. if (l2 === 0)
  366. return distance(p, a)
  367. var t = ((p.x - a.x) * (b.x - a.x) + (p.y - a.y) * (b.y - a.y)) / l2
  368. if (t < 0)
  369. return distance(p, a)
  370. if (t > 1)
  371. return distance(p, b)
  372. return Math.sqrt(distance(p, { x: a.x + t * (b.x - a.x),
  373. y: a.y + t * (b.y - a.y) }))
  374. }
  375. function translateXY(d) {
  376. var translate = zoomBehavior.translate()
  377. var scale = zoomBehavior.scale()
  378. return {x: (d[0] - translate[0]) / scale,
  379. y: (d[1] - translate[1]) / scale
  380. }
  381. }
  382. function onClick() {
  383. if (d3.event.defaultPrevented)
  384. return
  385. var e = translateXY(d3.mouse(el))
  386. var nodes = intNodes.filter(function (d) {
  387. return distancePoint(e, d) < NODE_RADIUS
  388. })
  389. if (nodes.length > 0) {
  390. router.node(nodes[0].o.node)()
  391. return
  392. }
  393. var links = intLinks.filter(function (d) {
  394. return !d.o.vpn
  395. }).filter(function (d) {
  396. return distanceLink(e, d.source, d.target) < LINE_RADIUS
  397. })
  398. if (links.length > 0) {
  399. router.link(links[0].o)()
  400. return
  401. }
  402. }
  403. function zoom(z, scale) {
  404. var size = getSize()
  405. var newSize = [size[0] / scale, size[1] / scale]
  406. var sidebarWidth = sidebar()
  407. var delta = [size[0] - newSize[0], size[1] - newSize[1]]
  408. var translate = z.translate()
  409. var translateNew = [sidebarWidth + (translate[0] - sidebarWidth - delta[0] / 2) * scale, (translate[1] - delta[1] / 2) * scale]
  410. animatePanzoom(translateNew, z.scale() * scale)
  411. }
  412. function keyboardZoom(z) {
  413. return function () {
  414. var e = d3.event
  415. if (e.altKey || e.ctrlKey || e.metaKey)
  416. return
  417. if (e.keyCode === 43)
  418. zoom(z, 1.41)
  419. if (e.keyCode === 45)
  420. zoom(z, 1 / 1.41)
  421. }
  422. }
  423. el = document.createElement("div")
  424. el.classList.add("graph")
  425. zoomBehavior = d3.behavior.zoom()
  426. .scaleExtent([1 / 3, 3])
  427. .on("zoom", onPanZoom)
  428. .translate([sidebar(), 0])
  429. canvas = d3.select(el)
  430. .attr("tabindex", 1)
  431. .on("keypress", keyboardZoom(zoomBehavior))
  432. .call(zoomBehavior)
  433. .append("canvas")
  434. .on("click", onClick)
  435. .call(draggableNode)
  436. .node()
  437. ctx = canvas.getContext("2d")
  438. force = d3.layout.force()
  439. .charge(-250)
  440. .gravity(0.1)
  441. .linkDistance(function (d) {
  442. if (d.o.vpn)
  443. return 0
  444. else
  445. return LINK_DISTANCE
  446. })
  447. .linkStrength(function (d) {
  448. if (d.o.vpn)
  449. return 0
  450. else
  451. return Math.max(0.5, 1 / d.o.tq)
  452. })
  453. .on("tick", tickEvent)
  454. .on("end", savePositions)
  455. window.addEventListener("resize", resizeCanvas)
  456. panzoom()
  457. self.setData = function (data) {
  458. var oldNodes = {}
  459. intNodes.forEach( function (d) {
  460. oldNodes[d.o.id] = d
  461. })
  462. intNodes = data.graph.nodes.map( function (d) {
  463. var e
  464. if (d.id in oldNodes)
  465. e = oldNodes[d.id]
  466. else
  467. e = {}
  468. e.o = d
  469. return e
  470. })
  471. var newNodesDict = {}
  472. intNodes.forEach( function (d) {
  473. newNodesDict[d.o.id] = d
  474. })
  475. var oldLinks = {}
  476. intLinks.forEach( function (d) {
  477. oldLinks[d.o.id] = d
  478. })
  479. intLinks = data.graph.links.map( function (d) {
  480. var e
  481. if (d.id in oldLinks)
  482. e = oldLinks[d.id]
  483. else
  484. e = {}
  485. e.o = d
  486. e.source = newNodesDict[d.source.id]
  487. e.target = newNodesDict[d.target.id]
  488. if (d.vpn)
  489. e.color = "rgba(255, 255, 255, " + (0.6 / d.tq) + ")"
  490. else
  491. e.color = linkScale(d.tq).hex()
  492. return e
  493. })
  494. linksDict = {}
  495. nodesDict = {}
  496. intNodes.forEach(function (d) {
  497. d.neighbours = {}
  498. if (d.o.node)
  499. nodesDict[d.o.node.nodeinfo.node_id] = d
  500. var name = nodeName(d)
  501. var offset = 5
  502. var lineWidth = 3
  503. var buffer = document.createElement("canvas")
  504. var r = window.devicePixelRatio
  505. var bctx = buffer.getContext("2d")
  506. bctx.font = "11px Roboto"
  507. var width = bctx.measureText(name).width
  508. var scale = zoomBehavior.scaleExtent()[1] * r
  509. buffer.width = (width + 2 * lineWidth) * scale
  510. buffer.height = (16 + 2 * lineWidth) * scale
  511. bctx.scale(scale, scale)
  512. bctx.textBaseline = "middle"
  513. bctx.textAlign = "center"
  514. bctx.fillStyle = "rgba(242, 227, 198, 1.0)"
  515. bctx.shadowColor = "rgba(0, 0, 0, 1)"
  516. bctx.shadowBlur = 5
  517. bctx.fillText(name, buffer.width / (2 * scale), buffer.height / (2 * scale))
  518. d.label = buffer
  519. d.labelWidth = buffer.width / scale
  520. d.labelHeight = buffer.height / scale
  521. d.labelA = offset + buffer.width / (2 * scale)
  522. d.labelB = offset + buffer.height / (2 * scale)
  523. })
  524. intLinks.forEach(function (d) {
  525. d.source.neighbours[d.target.o.id] = {node: d.target, link: d}
  526. d.target.neighbours[d.source.o.id] = {node: d.source, link: d}
  527. if (d.o.source.node && d.o.target.node)
  528. linksDict[d.o.id] = d
  529. })
  530. intNodes.forEach(function (d) {
  531. d.neighbours = Object.keys(d.neighbours).map(function (k) {
  532. return d.neighbours[k]
  533. })
  534. })
  535. nodes = intNodes.filter(function (d) { return d.o.node })
  536. unknownNodes = intNodes.filter(function (d) { return !d.o.node })
  537. if (localStorageTest()) {
  538. var save = JSON.parse(localStorage.getItem("graph/nodeposition"))
  539. if (save) {
  540. var nodePositions = {}
  541. save.forEach( function (d) {
  542. nodePositions[d.id] = d
  543. })
  544. intNodes.forEach( function (d) {
  545. if (nodePositions[d.o.id] && (d.x === undefined || d.y === undefined)) {
  546. d.x = nodePositions[d.o.id].x
  547. d.y = nodePositions[d.o.id].y
  548. }
  549. })
  550. }
  551. }
  552. var diameter = graphDiameter(intNodes)
  553. force.nodes(intNodes)
  554. .links(intLinks)
  555. .size([diameter, diameter])
  556. updateHighlight(true)
  557. force.start()
  558. resizeCanvas()
  559. }
  560. self.resetView = function () {
  561. highlight = undefined
  562. updateHighlight()
  563. doAnimation = true
  564. }
  565. self.gotoNode = function (d) {
  566. highlight = {type: "node", o: d}
  567. updateHighlight()
  568. doAnimation = true
  569. }
  570. self.gotoLink = function (d) {
  571. highlight = {type: "link", o: d}
  572. updateHighlight()
  573. doAnimation = true
  574. }
  575. self.destroy = function () {
  576. force.stop()
  577. canvas.remove()
  578. force = null
  579. if (el.parentNode)
  580. el.parentNode.removeChild(el)
  581. }
  582. self.render = function (d) {
  583. d.appendChild(el)
  584. resizeCanvas()
  585. updateHighlight()
  586. }
  587. return self
  588. }
  589. })