Skip to content

Instantly share code, notes, and snippets.

@asavin
Created February 26, 2014 14:09
Show Gist options
  • Select an option

  • Save asavin/9230056 to your computer and use it in GitHub Desktop.

Select an option

Save asavin/9230056 to your computer and use it in GitHub Desktop.
buildTreeChart = (tags) ->
tag-index = {}
# Build tag index
for tag in tags
if tag-index[tag]
tag-index[tag] += 1
else
tag-index[tag] = 1
# Sort index
sortable = []
for tag of tag-index
sortable.push [tag, tag-index[tag]]
sortable.sort (a, b) ->
b[1] - a[1]
tree-chart = []
limit = void
if sortable.length > 10
limit = 9
else
limit = sortable.length - 1
# Construct proper object for D3 tree chart
for tag in sortable[0 to limit]
tree-chart.push {name: tag[0], size: tag[1]}
tree-chart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment