Created
August 19, 2015 17:09
-
-
Save haskellcamargo/6dd89dc02bdb8fa2ac5c to your computer and use it in GitHub Desktop.
Circle packing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns pack.core) | |
(enable-console-print!) | |
; Circle packing automator | |
; @author Marcelo Camargo | |
(defn translate [x y] | |
(str "translate(" x "," y ")")) | |
(def diameter 960) | |
(def format (.format js/d3 ",d")) | |
(def pack | |
(.. js/d3 | |
-layout | |
pack | |
(size (array (- diameter 4) (- diameter 4))) | |
(value (fn [d] | |
(.-size d))))) | |
(def svg | |
(.. js/d3 | |
(select "body") | |
(append "svg") | |
(attr "width" diameter) | |
(attr "height" diameter) | |
(append "g") | |
(attr "transform" (translate 2 2)))) | |
(.json js/d3 "flare.json" (fn [error root] | |
(if error (throw error)) | |
(def node | |
(.. svg | |
(datum root) | |
(selectAll ".node") | |
(data (.-nodes pack)) | |
enter | |
(append "g") | |
(attr "class" (fn [d] | |
(if (.-children d) | |
"node" | |
"leaf node"))) | |
(attr "transform" (fn [d] | |
(translate (.-x d) (.-y d)))))) | |
(.. node | |
(append "title") | |
(text (fn [d] | |
(str (.-name d) | |
(if (.-children d) | |
"" | |
(str ": " (format (.-size d)))))))) | |
(.. node | |
(append "circle") | |
(attr "r" (fn [d] | |
(.-r d)))) | |
(.. node | |
(filter (fn [d] | |
(not (.-children d)))) | |
(append "text") | |
(attr "dy" ".3em") | |
(style "text-anchor" "middle") | |
(text (fn [d] | |
(.. d | |
-name | |
(substring 0 (/ (.-r d) 3)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment