|
| 1 | +(ns birdwatch.wordcount-barchart |
| 2 | + (:require [birdwatch.util :as util] |
| 3 | + [reagent.core :as r :refer [atom]])) |
| 4 | + |
| 5 | +(enable-console-print!) |
| 6 | + |
| 7 | +(def items (atom [])) |
| 8 | +(def ts-elem (util/by-id "wordcount-barchart")) |
| 9 | +(def ts-w (aget ts-elem "offsetWidth")) |
| 10 | +(def text-defaults {:stroke "none" :fill "#DDD" :fontWeight 500 :fontSize "0.8em" :dy ".35em" :textAnchor "end"}) |
| 11 | + |
| 12 | +(def opts1 [[10 "10 seconds"][30 "30 seconds"][60 "1 minute"][300 "5 minutes"][600 "10 minutes"]]) |
| 13 | +(def opts2 [[10 "10 tweets"][100 "100 tweets"][500 "500 tweets"][1000 "1000 tweets"]]) |
| 14 | + |
| 15 | +(defn bar [text cnt y h w idx] |
| 16 | + [:g |
| 17 | + [:text {:y (+ y 8) :x 138 :stroke "none" :fill "black" :dy ".35em" :textAnchor "end"} text] |
| 18 | + [:rect {:y y :x 168 :height 15 :width w :stroke "white" :fill "#428bca"}] |
| 19 | + (if (> w 50) |
| 20 | + [:text (merge text-defaults {:y (+ y 8) :x (+ w 160)}) cnt] |
| 21 | + [:text (merge text-defaults {:y (+ y 8) :x (+ w 171) :fill "#666" :textAnchor "start"}) cnt])]) |
| 22 | + |
| 23 | +(defn wordcount-barchart [] |
| 24 | + (let [items @items |
| 25 | + indexed (map-indexed vector items) |
| 26 | + mx (apply max (map (fn [[k v]] v) items)) |
| 27 | + cnt (count items)] |
| 28 | + [:div |
| 29 | + [:svg {:width ts-w :height (+ (* cnt 15) 5)} |
| 30 | + [:g |
| 31 | + (doall (for [[idx [text cnt]] indexed] |
| 32 | + [bar text cnt (* idx 15) 15 (* (- ts-w 190) (/ cnt mx)) idx])) |
| 33 | + [:line {:transform "translate(168, 0)" :y 0 :y2 (* cnt 15) :stroke "black"}]]] |
| 34 | + [:p.legend [:strong "1st trend indicator:"] " position changes in last " |
| 35 | + [:select {:defaultValue 300000} |
| 36 | + (for [[v t] opts1] [:option {:value (* v 1000)} t])]] |
| 37 | + [:p.legend [:strong "2nd trend indicator:"] " ratio change termCount / totalTermsCounted over last " |
| 38 | + [:select {:defaultValue 100} |
| 39 | + (for [[v t] opts2] [:option {:value v} t])]]])) |
| 40 | + |
| 41 | +(r/render-component [wordcount-barchart] ts-elem) |
| 42 | + |
| 43 | +(defn update-words |
| 44 | + "update wordcount chart" |
| 45 | + [words] |
| 46 | + (reset! items words)) |
| 47 | + |
0 commit comments