|
| 1 | +(ns twitterbuzz.showgraph |
| 2 | + (:require [twitterbuzz.core :as buzz] |
| 3 | + [goog.dom :as dom] |
| 4 | + [goog.graphics :as graphics])) |
| 5 | + |
| 6 | +; Drawing configuration |
| 7 | +(def avatar-size 0.15) ; used for both x and y dimensions of avatars |
| 8 | +(def edge-widths [0 0.001 0.005 0.010 0.020]) ; More mentions == thicker edges |
| 9 | + |
| 10 | +(def edge-strokes |
| 11 | + (vec (map #(graphics/Stroke. % "#009") edge-widths))) |
| 12 | + |
| 13 | +(def max-stroke (peek edge-strokes)) |
| 14 | + |
| 15 | +(def g |
| 16 | + (doto (graphics/createGraphics "100%" "100%" 1.0 1.0) |
| 17 | + (.render (dom/getElement "network")))) |
| 18 | + |
| 19 | +(defn draw-graph [users nodes] |
| 20 | + ; Draw mention edges |
| 21 | + (doseq [[username {:keys [mentions]}] users |
| 22 | + :when (get nodes username) |
| 23 | + [mention-name mention-count] mentions] |
| 24 | + (when-let [{x2 :x, y2 :y} (get nodes mention-name)] |
| 25 | + (let [{x1 :x, y1 :y} (get nodes username)] |
| 26 | + (.drawPath g |
| 27 | + (-> (. g (createPath)) (.moveTo x1 y1) (.lineTo x2 y2)) |
| 28 | + (get edge-strokes mention-count max-stroke) nil)))) |
| 29 | + |
| 30 | + ; Draw avatar nodes |
| 31 | + (let [offset (/ avatar-size 2)] |
| 32 | + (doseq [[username {:keys [x y]}] nodes] |
| 33 | + (.drawImage g (- x offset) (- y offset) avatar-size avatar-size |
| 34 | + (-> users (get username) :image-url))))) |
| 35 | + |
| 36 | +; This is temporary. The graph data should flow somehow from the |
| 37 | +; tweets. For now, just hardcode some: |
| 38 | +(defn update-graph [tweets] |
| 39 | + (draw-graph |
| 40 | + {"bob" |
| 41 | + {:image-url "http://a1.twimg.com/profile_images/1324487726/dr_bunsen_honeydew_normal.jpg" |
| 42 | + :mentions {"susan" 3 "joe" 2} |
| 43 | + :last-tweet "Clojure on JavaScript. Wow!"} |
| 44 | + "susan" |
| 45 | + {:image-url "http://a1.twimg.com/profile_images/1324487726/dr_bunsen_honeydew_normal.jpg" |
| 46 | + :mentions {"joe" 5} |
| 47 | + :last-tweet "ClojureScript doesn't have TCO! I'll never use it."}} |
| 48 | + {"bob" {:x 0.2 :y 0.7} "susan" {:x 0.4 :y 0.1}})) |
| 49 | + |
| 50 | +(buzz/register update-graph) |
0 commit comments