Skip to content

Instantly share code, notes, and snippets.

@killercup
Created August 26, 2014 13:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killercup/71eee74725caf8e645f9 to your computer and use it in GitHub Desktop.
Save killercup/71eee74725caf8e645f9 to your computer and use it in GitHub Desktop.
yeah, donuts and coffee! #d3 #charts
make_pie = ($box, data, size) ->
if size?
w = h = size
r = Math.min(w, h) / 2
r2 = r - Math.floor(r/2)
else
w = h = 40
r = Math.min(w, h) / 2
r2 = r - 12
donut = d3.layout.pie().sort(null)
arc = d3.svg.arc().innerRadius(r2).outerRadius(r)
chart = d3.select('#'+$box.attr("id"))
.append("svg:svg").attr("width", w).attr("height", h)
.attr("class", "pie").append("svg:g")
.attr("transform", "translate(#{w/2},#{h/2})")
arcs = chart.selectAll("path")
.data(donut(data))
.enter().append("svg:path")
.attr("class", (d, i) -> "pie-#{i} make_tooltip")
.attr("title", (d) -> d.value)
.attr("d", arc)
.on("mouseover", (d) ->
$(this).tooltip('show')
).on("mouseout", (d) ->
$(this).tooltip('hide')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment