Skip to content

Instantly share code, notes, and snippets.

@danielneal
Created March 26, 2015 18:00
Show Gist options
  • Save danielneal/2e2db7a6783d35fc0071 to your computer and use it in GitHub Desktop.
Save danielneal/2e2db7a6783d35fc0071 to your computer and use it in GitHub Desktop.
(ns hoplon.dimple.chart
(:require-macros [tailrecursion.javelin :refer [cell=]])
(:require [cljsjs.dimple]))
(def chart-types
{:line js/dimple.plot.line
:area js/dimple.plot.area
:bubble js/dimple.plot.bubble
:bar js/dimple.plot.bar})
(defelem chart-basic
"Construct a basic dimple chart.
Required parameters:
data: a cell with the data to plot
width: the width of the chart - can be a %
height: the height of the chart - can be a %
custom-setup: a custom setup function. Takes the chart as a parameter.
Optional parameters:
id: the id to give the containing div (defaults to autogenerated)
custom-draw: a custom draw function. The results of custom-setup are passed as opts to custom-draw.
Notes:
The chart will redraw whenever the data cell changes, or on screen resize.
The initial draw will only take place once the window has loaded"
[{:keys [data width height id custom-setup custom-draw]
:or {id (str (gensym))}}]
(with-let [container (div :width width :height height :id id)]
(with-init!
(let [Chart (.-chart js/dimple)
svg (.newSvg js/dimple (str "#" id) width height)
data-js (clj->js @data)
chart (Chart. svg data-js)
custom-draw-opts (when custom-setup (custom-setup chart))
draw (fn [resize-only?]
(if custom-draw
(custom-draw chart resize-only? custom-draw-opts)
(.draw chart 0 resize-only?)))]
(.resize (js/jQuery js/window) (partial draw true))
(.load (js/jQuery js/window) (partial draw false))
(cell= (do (set! (.-data chart) (clj->js data))
(draw false)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment