Skip to content

Instantly share code, notes, and snippets.

@jcorbin
Created November 19, 2012 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcorbin/4108928 to your computer and use it in GitHub Desktop.
Save jcorbin/4108928 to your computer and use it in GitHub Desktop.
First pass at some convenience for turning d3.nest into tags with less boilerplate...
basicGroupTag = (tag) ->
[name, klass] = tag.split('.')
(sel, data) ->
sel = sel.selectAll(tag).data(data)
enter = sel.enter().append(name)
enter.attr('class', klass) if klass?
sel.exit().remove()
sel
ensureTag = (tag) ->
[name, klass] = tag.split('.')
(cont) ->
sel = cont.select(tag)
if sel.empty()
sel = cont.append(name)
sel.attr('class', klass) if klass?
sel
basicKeyTag = (tag) ->
ensure = ensureTag tag
(sel) ->
ensure(sel).text((d) -> d.key)
d3.nest.tags = ->
nest = d3.nest()
n = 0
key = nest.key
build = []
nest.key = (f, tag, render) ->
tag ?= 'fieldset'
render ?= 'legend'
tag = basicGroupTag tag if typeof tag == 'string'
render = basicKeyTag render if typeof render == 'string'
build.push (sel, data) ->
sel = tag(sel, data)
render(sel)
sel
n += 1
key(f)
nest.build = (sel, data) ->
for build_level, i in build
d = if i == 0 then nest.entries(data) else (d) -> d.values
sel = build_level sel, d
sel
nest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment