Database Reference
In-Depth Information
"This takes a function and a map associating a label
with a sequence of values. It replaces those values
with their sums after passing them through the function."
(reduce
(fn [m [k vs]] (assoc m k (sum-by key-fn vs)))
{}
coll))
(defn sum-data-fields [json]
"This generates by-state sums for different groups."
(let [by-state (group-by #(.-state_name %) json)
white-by-state (sum-values #(.-white %) by-state)
afam-by-state (sum-values #(.-black %) by-state)
total-by-state (sum-values #(.-total %) by-state)]
(map #(hash-map :state %
:white (white-by-state %)
:black (afam-by-state %)
:total (total-by-state %))
(keys by-state))))
6. NVD3 expects the data to be in points and groups. We'll use the types we deined in
webviz.core . These functions will convert the summarized data into the objects
that NVD3 expects, as shown here:
(defn ->nv [item]
(let [{:keys [white black]} item]
(webviz/Point. (/ white 1000) (/ black 1000) 1)))
(defn ->nv-data [key-name data]
(->> data
sum-data-fields
(map ->nv)
(apply array)
(webviz/Group. key-name)
(array)))
7.
This function will actually create the chart and set the options and formatting for it:
(defn make-chart []
(let [c (-> (.scatterChart js/nv.models)
(.showDistX true)
(.showDistY true)
(.useVoronoi false)
(.color (.. js/d3 -scale category10 range)))]
(.tickFormat (.-xAxis c) (.format js/d3 "d"))
(.tickFormat (.-yAxis c) (.format js/d3 "d"))
c))
 
Search WWH ::




Custom Search