Created
August 20, 2015 17:33
-
-
Save haskellcamargo/3074771ebe659326a405 to your computer and use it in GitHub Desktop.
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 json.core) | |
(def data JSONData) | |
(def format | |
(.. js/d3 | |
-time | |
(format "%a %b %d %Y"))) | |
(def amount-fn (fn [d] | |
(.-amount d))) | |
(def date-fn (fn [d] | |
(.parse format (.-created-at d)))) | |
(def x | |
(.. js/d3 | |
-scale | |
linear | |
(range (array 10 280)) | |
(domain (.extent js/d3 data amount-fn)))) | |
(def y | |
(.. js/d3 | |
-scale | |
linear | |
(range (array 180 10)) | |
(domain (.extent js/d3 data amount-fn)))) | |
(def svg | |
(.. js/d3 | |
(select "#demo") | |
(append "svg:svg") | |
(attr "width" 300) | |
(attr "height" 200))) | |
(def refresh-graph (fn [] | |
(.. svg | |
(selectAll "circle") | |
(data data) | |
enter | |
(append "svg:circle") | |
(attr "r" 4) | |
(attr "cx" (fn [d] | |
(x (date-fn d)))) | |
(attr "cy" (fn [d] | |
(y (amount-fn d))))))) | |
(.. js/d3 | |
(selectAll (".add-data")) | |
(on "click" (fn [] | |
(let [start (.min js/d3 data date-fn) | |
end (.max js/d3 data date-fn) | |
time (+ (.getTime start) | |
(* (.random js/Math) | |
(- (.getTime end) | |
(.getTime start)))) | |
date (Date. time) | |
obj (js-obj | |
"id" (.floor js/Math (* (.random js/Math) 70)) | |
"amount" (.floor js/Math | |
(+ 1000 (* (.random js/Math) 20001))) | |
"created_at" (.toDateString date))] | |
(.push data obj) | |
(refresh-graph))))) | |
(refresh-graph) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment