forked from titarenko/bubble-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble-chart.iced
185 lines (154 loc) · 4.86 KB
/
bubble-chart.iced
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
define ["d3"], (d3) ->
class BubbleChart
# options:
# - data: array of bubble objects
# -- value: domain value
# -- description: domain value description
# - width: width of the chart
# - height: height of the chart
constructor: (options) ->
@bubbles = d3.shuffle options.data
@width = options.width
@height = options.height
@maxRadius = 70
@bubbleScale = d3.scale.sqrt().range([0, @maxRadius])
@bubbleScale.domain [0, d3.max(@bubbles, (item) -> item.value)]
@collisionPadding = 4
@minCollisionRadius = 12
@jitter = 0.5
@textWidthFactor = 1.9
@bubbles.forEach (bubble) =>
bubble.radius = @bubbleScale bubble.value
bubble.collisionRadius = Math.max @minCollisionRadius, bubble.radius
@force = d3.layout.force()
.gravity(0)
.charge(0)
.size([@width, @height])
.on("tick", @_calculateForceFactory())
# options:
# - selector: chart root element selector
plot: (options) ->
@selector = options.selector
@_constructCanvas()
@_constructBackground()
@_constructCircles()
@_constructCaptions()
@force.nodes(@bubbles).start()
# options:
# - width: new width of the chart
# - height: new height of the chart
resize: (options) ->
@width = options.width
@height = options.height
@force.stop()
@_resize @canvas
@_resize @background
@force.size [@width, @height]
@force.start()
_constructCanvas: ->
# canvas itself
@canvas = d3
.select(@selector)
.selectAll("svg")
.data([1]) # array with 1 element to simply ensure existence of 1 canvas
# canvas construction (if does not exist, will be created)
@canvas.enter()
.append("svg")
.attr("width", @width)
.attr("height", @height)
# in case if markup is broken, do cleanup by removing excessive canvases
@canvas.exit().remove()
_constructBackground: ->
# background itself
@background = @canvas
.selectAll("rect")
.data([1]) # array with 1 element to simply ensure existence of exactly 1 background
# construction
@background.enter()
.append("rect")
.attr("class", "bubble-chart-background")
.attr("width", @width)
.attr("height", @height)
# removal of extra backgrounds, if any
@background.exit().remove()
_constructCircles: ->
# circles themselves
@circles = @canvas
.selectAll("circle")
.data(@bubbles)
# circles construction (if not enough, new ones will be created)
@circles.enter()
.append("circle")
.attr("class", "bubble-chart-bubble")
.attr("r", (bubble) -> bubble.radius)
.call(@force.drag)
# remove any excessive circles
@circles.exit().remove()
_constructCaptions: ->
# hyphenation simulator
split = (phrase) ->
phrase
.replace(/\S{3}[ьаеоуюыиэeyuioa](?!$)/g, (g) -> g + "-")
.replace(/(-\S)$/, (g) -> g[1])
.replace("- ", " ")
# copying is needed to pass value to child function
textWidthFactor = @textWidthFactor
# captions themselves
@captions = d3
.select(@selector)
.selectAll("div")
.data(@bubbles)
# captions construction
@captions.enter()
.append("span")
.attr("class", "bubble-chart-caption")
.style("font-size", (bubble) => Math.max(8, @bubbleScale bubble.value/2) + "px")
.style("width", (bubble) -> "#{textWidthFactor*bubble.radius}px")
.text((bubble) -> split bubble.description)
.call(@force.drag)
.each (bubble) ->
rect = @getBoundingClientRect()
bubble.width = textWidthFactor*bubble.radius
bubble.height = rect.height
# removal of excessive captions, if any
@captions.exit().remove()
_resize: (element) ->
element
.attr("width", @width)
.attr("height", @height)
# pushes items towards center of chart
_applyGravityFactory: (alpha) ->
centerX = @width/2
centerY = @height/2
ax = alpha/8
ay = alpha
(bubble) ->
bubble.x += (centerX - bubble.x)*ax
bubble.y += (centerY - bubble.y)*ay
# pushes items away from each other
_collideFactory: ->
(bubble) =>
@bubbles.forEach (otherBubble) =>
return if bubble == otherBubble
dx = bubble.x - otherBubble.x
dy = bubble.y - otherBubble.y
distance = Math.sqrt(dx*dx + dy*dy)
minDistance = bubble.collisionRadius + otherBubble.collisionRadius + @collisionPadding
return if distance >= minDistance
distance = (distance - minDistance)/distance*@jitter
moveX = dx*distance
moveY = dy*distance
bubble.x -= moveX
bubble.y -= moveY
otherBubble.x += moveX
otherBubble.y += moveY
_calculateForceFactory: ->
(e) =>
alpha = e.alpha*0.1
@circles
.each(@_applyGravityFactory(alpha))
.each(@_collideFactory())
.attr("transform", (bubble) -> "translate(#{bubble.x}, #{bubble.y})")
@captions
.style("left", (bubble) -> "#{(bubble.x - bubble.width/2)}px")
.style("top", (bubble) -> "#{(bubble.y - bubble.height/2)}px")