|
1 | 1 | (ns birdwatch.timeseries
|
2 | 2 | (:require [birdwatch.util :as util]
|
| 3 | + [birdwatch.charts.ts-chart :as tsc] |
3 | 4 | [reagent.core :as r :refer [atom]]))
|
4 | 5 |
|
5 | 6 | (enable-console-print!)
|
6 | 7 |
|
7 |
| -(def bars (atom [])) |
8 |
| -(def label (atom {})) |
9 |
| - |
10 |
| -(def ts-elem (util/by-id "timeseries1")) |
11 |
| -(def ts-w (aget ts-elem "offsetWidth")) |
12 |
| -(def ts-h 100) |
13 |
| - |
14 |
| -(defn bar [x y h w idx] |
15 |
| - [:rect {:x x :y (- y h) :fill "steelblue" :width w :height h |
16 |
| - :on-mouse-enter #(reset! label {:idx idx}) |
17 |
| - :on-mouse-leave #(reset! label {})}]) |
18 |
| - |
19 |
| -(defn barchart [indexed mx cnt w] |
20 |
| - (let [gap (/ (/ ts-w 20) cnt)] |
21 |
| - [:svg {:width ts-w :height ts-h} |
22 |
| - [:g |
23 |
| - (doall (for [[idx [k v]] indexed] |
24 |
| - [bar (* idx w) ts-h (* (/ v mx) ts-h) (- w gap) idx]))]])) |
25 |
| - |
26 |
| -(defn labels [bars mx cnt w] |
27 |
| - (when-not (empty? @label) |
28 |
| - (let [idx (:idx @label) |
29 |
| - [k v] (get bars idx) |
30 |
| - top (- ts-h (* (/ v mx) ts-h)) |
31 |
| - lr (if (< (/ idx cnt) 0.6) "left" "right")] |
32 |
| - [:div.detail {:style {:left (* idx w)}} |
33 |
| - [:div.x_label {:class lr} (.toString (.unix js/moment k))] |
34 |
| - [:div.item.active {:class lr :style {:top top}} "Tweets: " v] |
35 |
| - [:div.dot.active {:style {:top top :border-color "steelblue"}}]]))) |
36 |
| - |
37 |
| -(defn ts-chart [] |
38 |
| - (let [bars @bars |
39 |
| - indexed (map-indexed vector bars) |
40 |
| - mx (apply max (map (fn [[k v]] v) bars)) |
41 |
| - cnt (count bars) |
42 |
| - w (/ ts-w cnt)] |
43 |
| - [:div.rickshaw_graph |
44 |
| - [barchart indexed mx cnt w] |
45 |
| - [labels bars mx cnt w]])) |
46 |
| - |
47 |
| -(r/render-component [ts-chart] ts-elem) |
48 |
| - |
49 | 8 | (defn date-round
|
50 | 9 | "return function that rounds the provided seconds since epoch down to the nearest time interval s
|
51 | 10 | e.g. (date-round 60) creates a function that takes seconds t and rounds them to the nearest minute"
|
|
102 | 61 | (defn update-ts
|
103 | 62 | "update time series chart"
|
104 | 63 | [app]
|
105 |
| - (reset! bars (vec (ts-data app)))) |
| 64 | + (reset! tsc/bars (vec (ts-data app)))) |
106 | 65 |
|
0 commit comments