Skip to content

Commit 75fd925

Browse files
committed
trend arrow for recent position changes
1 parent 2811f88 commit 75fd925

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
(ns birdwatch.wordcount-barchart
22
(:require [birdwatch.util :as util]
3+
[birdwatch.stats.regression :as reg]
34
[reagent.core :as r :refer [atom]]))
45

56
(enable-console-print!)
67

78
(def items (atom []))
9+
(def pos-trends (atom {}))
10+
(def pos-items (atom {}))
11+
(def ratio-trends (atom {}))
12+
(def ratio-items (atom {}))
13+
814
(def ts-elem (util/by-id "wordcount-barchart"))
915
(def ts-w (aget ts-elem "offsetWidth"))
1016
(def text-defaults {:stroke "none" :fill "#DDD" :fontWeight 500 :fontSize "0.8em" :dy ".35em" :textAnchor "end"})
@@ -25,37 +31,44 @@
2531
[:polygon {:transform arrowTrans :stroke "none" :fill color :points points}]))
2632

2733
(defn bar [text cnt y h w idx]
28-
[:g
29-
[:text {:y (+ y 8) :x 138 :stroke "none" :fill "black" :dy ".35em" :textAnchor "end"} text]
30-
[arrow 146 y :RIGHT-UP]
31-
[arrow 160 y :RIGHT]
32-
[:rect {:y y :x 168 :height 15 :width w :stroke "white" :fill "#428bca"}]
33-
(if (> w 50)
34-
[:text (merge text-defaults {:y (+ y 8) :x (+ w 160)}) cnt]
35-
[:text (merge text-defaults {:y (+ y 8) :x (+ w 171) :fill "#666" :textAnchor "start"}) cnt])])
34+
(let [pos-slope (get @pos-trends text)]
35+
[:g
36+
[:text {:y (+ y 8) :x 138 :stroke "none" :fill "black" :dy ".35em" :textAnchor "end"} text]
37+
[arrow 146 y (cond
38+
(pos? pos-slope) :UP
39+
(neg? pos-slope ) :DOWN
40+
:else :RIGHT)]
41+
[arrow 160 y :RIGHT]
42+
[:rect {:y y :x 168 :height 15 :width w :stroke "white" :fill "#428bca"}]
43+
(if (> w 50)
44+
[:text (merge text-defaults {:y (+ y 8) :x (+ w 160)}) cnt]
45+
[:text (merge text-defaults {:y (+ y 8) :x (+ w 171) :fill "#666" :textAnchor "start"}) cnt])]))
3646

3747
(defn wordcount-barchart []
38-
(let [items @items
39-
indexed (map-indexed vector items)
40-
mx (apply max (map (fn [[k v]] v) items))
41-
cnt (count items)]
48+
(let [indexed @items
49+
mx (apply max (map (fn [[idx [k v]]] v) indexed))
50+
cnt (count indexed)]
4251
[:div
4352
[:svg {:width ts-w :height (+ (* cnt 15) 5)}
4453
[:g
45-
(doall (for [[idx [text cnt]] indexed]
46-
[bar text cnt (* idx 15) 15 (* (- ts-w 190) (/ cnt mx)) idx]))
54+
(for [[idx [text cnt]] indexed]
55+
^{:key text}[bar text cnt (* idx 15) 15 (* (- ts-w 190) (/ cnt mx)) idx])
4756
[:line {:transform "translate(168, 0)" :y 0 :y2 (* cnt 15) :stroke "black"}]]]
4857
[:p.legend [:strong "1st trend indicator:"] " position changes in last "
4958
[:select {:defaultValue 300000}
50-
(for [[v t] opts1] [:option {:value (* v 1000)} t])]]
59+
(for [[v t] opts1] ^{:key v} [:option {:value (* v 1000)} t])]]
5160
[:p.legend [:strong "2nd trend indicator:"] " ratio change termCount / totalTermsCounted over last "
5261
[:select {:defaultValue 100}
53-
(for [[v t] opts2] [:option {:value v} t])]]]))
62+
(for [[v t] opts2] ^{:key v} [:option {:value v} t])]]]))
5463

5564
(r/render-component [wordcount-barchart] ts-elem)
5665

5766
(defn update-words
5867
"update wordcount chart"
5968
[words]
60-
(reset! items words))
69+
(reset! items (vec (map-indexed vector words)))
70+
(doseq [[idx [text cnt]] @items]
71+
(let [slope (get (reg/linear-regression (take 3 (get @pos-items text))) 1)]
72+
(swap! pos-items update-in [text] conj idx)
73+
(swap! pos-trends assoc-in [text] slope))))
6174

0 commit comments

Comments
 (0)