Skip to content

Instantly share code, notes, and snippets.

@jbilcke
Created November 15, 2012 09:33
Show Gist options
  • Save jbilcke/4077655 to your computer and use it in GitHub Desktop.
Save jbilcke/4077655 to your computer and use it in GitHub Desktop.
fix for the d3.js radial tree bug (when there is only 1 child on the root)
importScripts('../vendor/d3/d3.layout.redux.js')
@onmessage = (msg) ->
tree = msg.data
# the layout has a weird effect
if tree.children.length is 1
for n in tree.children[0].children
tree.children.push ghost: yes, children: []
d3_layout = d3.layout.tree()
d3_layout.separation (a, b) ->
(if a.parent is b.parent then 1 else 3) / a.depth
nodes = d3_layout.nodes tree
newNodes = for n in nodes
continue if n.ghost
angle = n.x * Math.PI * 2
n.x = 2000 * n.y * Math.cos angle
n.y = 2000 * n.y * Math.sin angle
n
nodes = newNodes
# sort elements by id
nodes.sort (a,b) -> a.id - b.id
postMessage nodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment