Database Reference
In-Depth Information
2.
We'll add that and the data URL to the routes:
(defroutes
site-routes
(GET "/histogram" [] (hist-plot))
(GET "/histogram/data.json" []
(redirect "/data/abalone.json"))
(route/resources "/")
(route/not-found "Page not found"))
3. Now, for the ClojureScript ile, we'll open src-cljs/webviz/histogram.cljs
and add the following namespace declaration:
(ns webviz.histogram
(:require [webviz.core :as webviz]))
4.
We'll need a function to group the data into buckets:
(defn get-bucket-number [mn size x]
(Math/round (/ (- x mn) size)))
(defn inc-bucket [mn size buckets x]
(let [b (get-bucket-number mn size x)]
(assoc buckets b (inc (buckets b)))))
(defn get-buckets [coll n]
(let [mn (reduce min coll)
mx (reduce max coll)
bucket-size (/ (- mx mn) n)
first-center (+ mn (/ bucket-size 2.0))
centers (map #(* (inc %) first-center)
(range n))
initial (reduce #(assoc %1 %2 0) {}
(range n))]
(->> coll
(reduce (partial inc-bucket mn bucket-size)
initial)
seq
(sort-by first)
(map second)
(map vector centers))))
5.
Here are two functions to take the JSON, put it into buckets, and convert it to points
and groups:
(defn ->point [pair]
(let [[bucket count] pair]
(webviz/Point. (inc bucket) count 1)))
(defn data->nv-groups [data]
 
Search WWH ::




Custom Search