Skip to content

Commit fa2128d

Browse files
committed
Update existing graph, use data format from layout ns. Refs clojure#48
1 parent 0226e55 commit fa2128d

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

samples/twitterbuzz/src/twitterbuzz/showgraph.cljs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,35 @@
1616
(doto (graphics/createGraphics "100%" "100%" 1.0 1.0)
1717
(.render (dom/getElement "network"))))
1818

19-
(defn draw-graph [users nodes]
19+
(defn draw-graph [{:keys [locs mentions]}]
20+
(. g (clear))
21+
2022
; 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))))
23+
(doseq [[username {x1 :x, y1 :y}] locs
24+
[mention-name mention-count] (:mentions (get mentions username))]
25+
(when-let [{x2 :x, y2 :y} (get locs mention-name)]
26+
(.drawPath g
27+
(-> (. g (createPath)) (.moveTo x1 y1) (.lineTo x2 y2))
28+
(get edge-strokes mention-count max-stroke) nil)))
2929

3030
; Draw avatar nodes
3131
(let [offset (/ avatar-size 2)]
32-
(doseq [[username {:keys [x y]}] nodes]
32+
(doseq [[username {:keys [x y]}] locs]
3333
(.drawImage g (- x offset) (- y offset) avatar-size avatar-size
34-
(-> users (get username) :image-url)))))
34+
(:image-url (get mentions username))))))
3535

3636
; This is temporary. The graph data should flow somehow from the
3737
; tweets. For now, just hardcode some:
38+
(def test-users {"djspiewak" {:image-url "http://a0.twimg.com/profile_images/746976711/angular-final_normal.jpg", :last-tweet "Does Clojure have a Sinatra clone?", :mentions {}}, "tobsn" {:image-url "http://a2.twimg.com/profile_images/1364411587/yr40_normal.png", :last-tweet "Creating a Query DSL using Clojure and MongoDB http://tob.sn/qgCxkm #mongodb", :mentions {}}, "CzarneckiD" {:image-url "http://a3.twimg.com/profile_images/1156755747/head_trees_normal.jpg", :last-tweet "@greymouser I need to start writing some Clojure I guess :)", :mentions {"greymouser" 2}}, "sbtourist" {:image-url "http://a0.twimg.com/profile_images/72494229/Cryer_Black_normal.jpg", :last-tweet "Clooj, a lightweight Clojure IDE: http://t.co/OSCjr9X", :mentions {"djspiewak" 2}}, "jboner" {:image-url "http://a2.twimg.com/profile_images/1395654712/jonas_bw_small_normal.JPG", :last-tweet "RT @sbtourist: Clooj, a lightweight Clojure IDE: http://t.co/OSCjr9X", :mentions {"sbtourist" 2, "djspiewak" 2, "romanroe" 2}}})
39+
40+
(def test-graph (atom (list
41+
{"jboner" {:x 0.2 :y 0.8} "djspiewak" {:x 0.3 :y 0.2}}
42+
{"jboner" {:x 0.4 :y 0.7} "djspiewak" {:x 0.4 :y 0.3}}
43+
{"jboner" {:x 0.3 :y 0.8} "sbtourist" {:x 0.3 :y 0.2} "djspiewak" {:x 0.5 :y 0.5}}
44+
{"jboner" {:x 0.2 :y 0.7} "sbtourist" {:x 0.4 :y 0.1} "djspiewak" {:x 0.7 :y 0.4}})))
45+
3846
(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}}))
47+
(draw-graph {:locs (peek @test-graph), :mentions test-users})
48+
(swap! test-graph pop))
4949

5050
(buzz/register update-graph)

0 commit comments

Comments
 (0)