/
d3statebin.coffee
350 lines (297 loc) · 10.8 KB
/
d3statebin.coffee
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
root = exports ? this
createAccessors = (visExport) ->
for n of visExport.opts
continue unless visExport.opts.hasOwnProperty(n)
visExport[n] = ((n) ->
(v) ->
(if arguments.length then (visExport.opts[n] = v
this
) else visExport.opts[n])
)(n)
return
# http://gomakethings.com/vanilla-javascript-version-of-jquery-extend/
extend = (defaults, options) ->
extended = {}
prop = undefined
for prop of defaults
`prop = prop`
if Object::hasOwnProperty.call(defaults, prop)
extended[prop] = defaults[prop]
for prop of options
`prop = prop`
if Object::hasOwnProperty.call(options, prop)
extended[prop] = options[prop]
extended
d3.table = (data) ->
table = ["<b>", data.state, "</b>", "<table style='line-height:2em;'>"]
d3.entries(data).forEach (d) ->
table.push("<tr><td>" + d.key + "</td><td> </td><td>" + d.value + "</td></tr>")
table.join("") + "</table>"
d3.uniq = (data, fun) ->
if typeof fun is 'function'
v_ = data.map(fun)
else
v_ = data.map (d) -> d[fun]
d3.set(v_).values()
d3.selection.prototype.appendOnce = (name) ->
that = this.selectAll(name).data([{}])
that.enter().append(name)
return that
d3.selection.prototype.dataAppend = (data, name) ->
that = this.selectAll(name).data(data)
that.enter().append(name)
d3.ui = d3.ui || {}
d3.ui.panel = () ->
defaults = {panels: ["heading", "body"], type: "default"}
opts = arguments[0] || defaults
exports = (selection) ->
selection.each (data) ->
panel = d3.select(@).selectAll("div.panel.panel-#{opts.type}").data([{}])
panelEnter = panel.enter().append("div.panel.panel-#{opts.type}")
subPanels = panelEnter.selectAll("div").data(opts.panels)
subPanels.enter().append("div")
subPanels.attr
class: (d) -> "panel-#{d}"
###
d3.select(@)
.appendOnce("div.panel.panel-#{opts.type}")
.dataAppend(opts.panels, "div")
.attr
class: (d) -> "panel-#{d}"
###
panel
exports.opts = opts; createAccessors(exports)
exports
d3.ui.select = ->
opts = {text: "text", value: "value"}
event_handlers = {}
exports = (selection) ->
selection.each (data) ->
# Start of body
select = d3.select(@)
.appendOnce("select.form-control.col")
select.dataAppend(data, "option")
select.selectAll("option")
.attr
value: (d) -> d[opts.value] || d
.text (d) -> d[opts.text] || d
# End of body
for event of event_handlers
select.on event, event_handlers[event]
exports.on = (event, fun) ->
if arguments.length == 1
return event_handlers[event]
event_handlers[event] = fun
exports
exports.opts = opts; createAccessors(exports)
exports
d3.ui.paginate = ->
opts = {callback: (d, i) -> return d}
exports = (selection) ->
selection.each (data) ->
nav = d3.select(@)
nav.appendOnce("ul.pagination")
.dataAppend(data, "li")
.classed("active", (d, i) -> i is 0)
.append("a").text (d) -> d
li = nav.selectAll("li")
li.on "click", (d, i) ->
li.classed("active", false)
d3.select(@).classed("active", true)
opts.callback(d, i)
exports.opts = opts; createAccessors(exports)
exports
state_coords = (state) ->
{"AK":[{"row":7,"col":1}],"AL":[{"row":7,"col":8}],"AR":[{"row":6,"col":6}],"AZ":[{"row":6,"col":3}],"CA":[{"row":5,"col":2}],"CO":[{"row":5,"col":4}],"CT":[{"row":4,"col":11}],"DC":[{"row":6,"col":10}],"DE":[{"row":5,"col":11}],"FL":[{"row":8,"col":10}],"GA":[{"row":7,"col":9}],"HI":[{"row":8,"col":1}],"IA":[{"row":4,"col":6}],"ID":[{"row":3,"col":3}],"IL":[{"row":3,"col":7}],"IN":[{"row":4,"col":7}],"KS":[{"row":6,"col":5}],"KY":[{"row":5,"col":7}],"LA":[{"row":7,"col":6}],"MA":[{"row":3,"col":11}],"MD":[{"row":5,"col":10}],"ME":[{"row":1,"col":12}],"MI":[{"row":3,"col":8}],"MN":[{"row":3,"col":6}],"MO":[{"row":5,"col":6}],"MS":[{"row":7,"col":7}],"MT":[{"row":3,"col":4}],"NC":[{"row":6,"col":8}],"ND":[{"row":3,"col":5}],"NE":[{"row":5,"col":5}],"NH":[{"row":2,"col":12}],"NJ":[{"row":4,"col":10}],"NM":[{"row":6,"col":4}],"NV":[{"row":4,"col":3}],"NY":[{"row":3,"col":10}],"OH":[{"row":4,"col":8}],"OK":[{"row":7,"col":5}],"OR":[{"row":4,"col":2}],"PA":[{"row":4,"col":9}],"RI":[{"row":4,"col":12}],"SC":[{"row":6,"col":9}],"SD":[{"row":4,"col":5}],"TN":[{"row":6,"col":7}],"TX":[{"row":8,"col":5}],"UT":[{"row":5,"col":3}],"VA":[{"row":5,"col":9}],"VT":[{"row":2,"col":11}],"WA":[{"row":3,"col":2}],"WI":[{"row":2,"col":7}],"WV":[{"row":5,"col":8}],"WY":[{"row":4,"col":4}]}[state][0]
d3.heatmap = (S, A) ->
defaults = {}
opts = extend(defaults, arguments[0])
exports = (selection) ->
selection.each (data) ->
S.x.domain d3.uniq(data, A.x)
S.y.domain d3.uniq(data, A.y)
S.c.domain d3.extent(data, A.z)
cell = d3.select(@).selectAll(".cell").data(data, A.id)
cellEnter = cell.enter().append("g.cell")
cellEnter.append("rect.state")
cellEnter.append("text.state")
cell.attr
transform: (d) -> "translate(#{S.x A.x(d)}, #{S.y A.y(d)})"
cell.select("rect.state")
.attr
x: 0
y: 0
width: S.x.rangeBand()
height: S.y.rangeBand()
.style
fill: (d) -> S.c A.z(d)
cell
exports.opts = opts; createAccessors(exports)
exports
d3.hexmap = (S, A) ->
defaults = {}
hexbin = d3.hexbin().radius(18.5)
opts = extend(defaults, arguments[0])
exports = (selection) ->
selection.each (data) ->
S.x.domain d3.uniq(data, A.x)
S.y.domain d3.uniq(data, A.y)
S.c.domain d3.extent(data, A.z)
cell = d3.select(@).selectAll(".cell").data(data, A.id)
cellEnter = cell.enter().append("g.cell")
cellEnter.append("path.hexagon")
cellEnter.append("text.state")
cell.attr
transform: (d) ->
x = A.x(d)
y = A.y(d)
if (y % 2 is 0)
offset = S.x.rangeBand()/2
else
offset = 0
"translate(#{S.x(x) + offset}, #{S.y(y)})"
cell.select("path.hexagon")
.attr
transform: "translate(15, 15)"
d: (d) -> hexbin.hexagon(hexbin.radius())
.style
fill: (d) -> S.c A.z(d)
cell
exports.opts = opts; createAccessors(exports)
exports
d3.render = {} || d3.render
d3.render.legend = ->
mylegend = d3.svg.legend()
.units("")
.cellWidth(40)
.cellHeight(10)
exports = (selection) ->
selection.each (data) ->
legend = d3.select(this)
.appendOnce("g.legend")
.translate(5, 0)
# HACK: ensures that we dont end up with multiple
# legends on update
legend.select(".mutLegendGroup").remove()
legend.call(mylegend)
d3.rebind(exports, mylegend, "inputScale")
exports
d3.render.tip = (sel, html) ->
tip = d3.tip()
.attr("class", "d3-tip light")
.html html || d3.table
sel.call(tip)
sel.on "mouseover", tip.show
sel.on "mouseout", tip.hide
d3.statemap = (S, A) ->
opts = {}
exports = (selection) ->
selection.each (data) ->
cell = d3.select(this).call(d3.heatmap(S, A))
cell.selectAll("rect.state").call(d3.render.tip)
legend = d3.render.legend().inputScale(S.c)
d3.select(this).call(legend)
cell.selectAll("text.state")
.attr
dx: S.x.rangeBand()/2
dy: S.y.rangeBand()/2
.text (d) -> d.state
exports.opts = opts; createAccessors(exports)
exports
d3.hexstatemap = (S, A) ->
opts = {}
exports = (selection) ->
selection.each (data) ->
cell = d3.select(this).call(d3.hexmap(S, A))
cell.selectAll(".cell").call(d3.render.tip)
legend = d3.render.legend().inputScale(S.c)
d3.select(this).call(legend)
cell.selectAll("text.state")
.attr
dx: S.x.rangeBand()/2
dy: S.y.rangeBand()/2
.text (d) -> d.state
exports.opts = opts; createAccessors(exports)
exports
d3.container = (sel, o) ->
o.margin = o.margin || {top: 20, bottom: 40, left: 40, right: 20}
o.W = o.width + o.margin.left + o.margin.right
o.H = o.height + o.margin.top + o.margin.bottom
svg = sel.appendOnce("svg")
.attr({"width": o.W, "height": o.H})
.appendOnce("g.main")
.attr
transform: "translate(#{o.margin.left}, #{o.margin.top})"
svg
root.statemap = ->
defaults =
x: "state"
y: "count"
facet: null
width: 400
height: 400/1.7
margin: {top: 20, right: 80, bottom: 50, left: 50}
colors: colorbrewer.Blues[7]
heading: "Statebins"
footer: "This is a statebin chart"
units: ""
control: 'dropdown'
type: 'rect'
opts = extend(defaults, arguments[0])
exports = (selection) ->
selection.each (data) ->
A =
x: (d) -> state_coords(d[opts.x]).col
y: (d) -> state_coords(d[opts.x]).row
z: (d) -> d[opts.y]
id: (d) -> d[opts.x]
S =
x: d3.scale.ordinal().rangeRoundBands([0, opts.width], 0.02)
y: d3.scale.ordinal().rangeRoundBands([0, opts.height], 0.02)
c: d3.scale.quantize().range(opts.colors)
panel1 = d3.ui.panel()
.panels(['heading', 'body', 'footer'])
mypanel1 = d3.select(this).call(panel1)
#console.log mypanel1.data()
mypanel1.select('.panel-heading').html(opts.heading)
pbody = mypanel1.select(".panel-body")
if opts.facet
#console.log("Group is defined")
switch opts.control
when "steps"
choices = d3.ui.paginate()
.callback (d, i) -> update(d)
#active = d3.select(this)
# .select('.steps .active a')
# .text()
when "dropdown"
choices = d3.ui.select()
.text("description")
.value("code")
.on "change", (d, i) -> update(@value)
pbody
.appendOnce("div.#{opts.control}")
.datum(d3.uniq(data, opts.facet))
.call(choices)
mypanel1.select(".panel-footer").html(opts.footer)
update = (grp) ->
data_ = data.filter (d) ->
if opts.facet
return d[opts.facet] is grp
else
return true
if opts.type is 'rect'
d3.container(pbody, opts)
.datum(data_, A.id)
.call(d3.statemap(S, A))
else
d3.container(pbody, opts)
.datum(data_, A.id)
.call(d3.hexstatemap(S, A))
if opts.facet
update d3.uniq(data, opts.facet)[0]
else
update()
exports.opts = opts
createAccessors(exports)
exports