Skip to content

Commit 3394ec5

Browse files
fix weighting
1 parent 27a7e65 commit 3394ec5

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

samples/twitterbuzz/src/twitterbuzz/layout.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
(defn radial
4545
[mentions-data]
4646
(let [mentions #(rad/get-mentions mentions-data %)
47-
weights (rad/branch-weights
47+
weights (rad/weights
4848
(into #{} (mapcat mentions (keys mentions-data)))
4949
mentions)]
5050
{:mentions mentions-data

samples/twitterbuzz/src/twitterbuzz/radial.cljs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,22 @@
2929
(into (disj check c) (remove kids (child-fn c))))
3030
kids))))
3131

32-
(defn branch-weights
33-
"Return a map of node to its weight (number of descendants),
34-
using child-fn to get the set of children for a node.
35-
Recursive, assumes no cycles."
32+
(defn weight
33+
"Weight of noce, given child-fn (mapping of node to set
34+
of kids)."
35+
[node child-fn]
36+
(if-let [kids (seq (child-fn node))]
37+
(reduce + (map #(weight % child-fn) kids))
38+
1))
39+
40+
(defn weights
41+
"Return a map of node to its weight,
42+
using child-fn to get the set of children for a node."
3643
[nodes child-fn]
37-
(loop [weights {}
38-
known #{}
39-
remaining (set nodes)]
40-
(let [nodes (remove (fn [n] (seq (set/difference (child-fn n) known))) remaining)]
41-
(if (seq nodes)
42-
(recur (into weights (map
43-
(fn [n] [n (+ 1
44-
#_(count (child-fn n))
45-
(reduce + (map weights (child-fn n))))])
46-
nodes))
47-
(into known nodes)
48-
(set/difference remaining nodes))
49-
weights))))
44+
(reduce
45+
(fn [m n] (assoc m n (weight n child-fn)))
46+
{}
47+
nodes))
5048

5149
(defn layout
5250
"Returns a map of node => :radius, :slice, :angle.
@@ -68,7 +66,7 @@
6866
(recur
6967
(merge
7068
m
71-
{node {:radius radius :slice s :angle (math/average c1 c2)}}
69+
{node {:radius radius :slice s :angle (/ (+ c1 c2) 2)}}
7270
(when-let [children (seq (remove seen (child-fn node)))]
7371
(layout children weight-fn child-fn (inc radius) c1 c2 seen)))
7472
c2

0 commit comments

Comments
 (0)