Skip to content

Instantly share code, notes, and snippets.

@haywoood
Created May 18, 2020 14:59
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 haywoood/112792fa0328e9c1ba26be79d5ac1222 to your computer and use it in GitHub Desktop.
Save haywoood/112792fa0328e9c1ba26be79d5ac1222 to your computer and use it in GitHub Desktop.
(defn mount-viz []
(let [svg (-> (d3/select "#chart")
(.append "svg")
(.attr "width" 800)
(.attr "height" 600)
(.append "g"))
x (-> (d3/scaleLinear)
(.domain #js [(d3/min data) (d3/max data)])
(.range #js [0 800]))
_ (-> (.append svg "g")
(.attr "transform" (str "translate(0," 600 ")"))
(.call (d3/axisBottom x)))
histogram (-> (d3/histogram)
(.value identity)
(.domain (.domain x))
(.thresholds (.ticks x 70)))
bins (histogram data)
y (-> (d3/scaleLinear)
(.range #js [600 0]))
_ (.domain y #js [0 (d3/max bins (fn [d] (.-length d)))])
_ (-> (.append svg "g")
(.call (d3/axisLeft y)))]
(-> (.selectAll svg "rect")
(.data bins)
(.enter)
(.append "rect")
(.attr "x" 1)
(.attr "transform"
(fn [d]
(str "translate(" (x (.-x0 d)) "," (y (.-length d)) ")")))
(.attr "width"
(fn [d]
(- (x (.-x1 d))
(x (.-x0 d))
-1)))
(.attr "height"
(fn [d]
(- 600 (y (.-length d)))))
(.style "fill" "#69b3a2"))))
(defn render []
(r/create-class
{:component-did-mount
(fn [this]
(mount-viz))
:render
(fn [this]
[:div#chart])}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment