-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3-spiral-heatmap1.3.js
303 lines (265 loc) · 8.95 KB
/
d3-spiral-heatmap1.3.js
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
// A spiral heatmap
// The following options are available
// radius: radius of the overall plot, not including any labels. Default 250
// holeRadiusProportion: the proportion (0 to 1) of the radius (see above) that is left as a hole. Default 0.
// arcsPerCoil: a 'coil' is one revolution of the spiral. This sets how many arcs (or arcs) you want per coil. Typically this would
// be set according to the periodicity of the data. For example, 12 for months per year, 24 for hours per day, etc
// coilPadding: the proportion (0 to 1) of the coil width that is used for padding between coils. Useful for making the spiral very noticeable
// arcLabel: the field name to use for the labels around the circumference
// coilLabel: the field name to use for the labels at the beginning of each coil
function spiralHeatmap () {
// constants
const radians = 0.0174532925
// All options that are accessible to caller
// Default values
var radius = 250
var holeRadiusProportion = 0.3 // proportion of radius
var arcsPerCoil = 12 // assuming months per year
var coilPadding = 0 // no padding
var arcLabel = '' // no labels
var arcLabelOffset = 10 // option added by MN in v1.3
var coilLabel = '' // no labels
var coilLabelOffset = -4 // option added by MN in v1.2
var startAngle = 0 //starts at 12 o'clock
function chart (selection) {
selection.each(function (data) {
const arcAngle = 360 / arcsPerCoil
const labelRadius = radius + arcLabelOffset // changed from fixed by MN
var arcLabelsArray = []
for (var i = 0; i < arcsPerCoil; i++) {
arcLabelsArray.push(i)
}
// Create/update the x/y coordinates for the vertices and control points for the paths
// Stores the x/y coordinates on the data
updatePathData(data)
let thisSelection = d3
.select(this)
.append('g')
.attr('class', 'spiral-heatmap')
var arcLabelsG = thisSelection
.selectAll('.arc-label')
.data(arcLabelsArray)
.enter()
.append('g')
.attr('class', 'arc-label')
arcLabelsG
.append('text')
.text(function (d) {
return data[d][arcLabel]
})
.attr('x', function (d, i) {
let labelAngle = i * arcAngle + arcAngle / 2
return x(labelAngle, labelRadius)
})
.attr('y', function (d, i) {
let labelAngle = i * arcAngle + arcAngle / 2
return y(labelAngle, labelRadius)
})
.style('text-anchor', function (d, i) {
return i < arcLabelsArray.length / 2 ? 'start' : 'end'
})
arcLabelsG
.append('line')
.attr('x2', function (d, i) {
let lineAngle = i * arcAngle
let lineRadius = radius + 10
return x(lineAngle, lineRadius)
})
.attr('y2', function (d, i) {
let lineAngle = i * arcAngle
let lineRadius = radius + 10
return y(lineAngle, lineRadius)
})
var arcs = thisSelection
.selectAll('.arc')
.data(data)
.enter()
.append('g')
.attr('class', 'arc')
arcs.append('path').attr('d', function (d) {
// start at vertice 1
let start = 'M ' + d.x1 + ' ' + d.y1
// inner curve to vertice 2
let side1 =
' Q ' +
d.controlPoint1x +
' ' +
d.controlPoint1y +
' ' +
d.x2 +
' ' +
d.y2
// straight line to vertice 3
let side2 = 'L ' + d.x3 + ' ' + d.y3
// outer curve vertice 4
let side3 =
' Q ' +
d.controlPoint2x +
' ' +
d.controlPoint2y +
' ' +
d.x4 +
' ' +
d.y4
// combine into string, with closure (Z) to vertice 1
return start + ' ' + side1 + ' ' + side2 + ' ' + side3 + ' Z'
})
// create coil labels on the first arc of each coil
if (coilLabel != '') {
coilLabels = arcs
.filter(function (d) {
return d.arcNumber == 0
})
.raise()
coilLabels
.append('path')
.attr('id', function (d) {
return 'path-' + d[coilLabel]
})
.attr('d', function (d) {
// start at vertice 1
let start = 'M ' + d.x1 + ' ' + d.y1
// inner curve to vertice 2
let side1 =
' Q ' +
d.controlPoint1x +
' ' +
d.controlPoint1y +
' ' +
d.x2 +
' ' +
d.y2
return start + side1
})
.style('opacity', 0)
coilLabels
.append('text')
.attr('class', 'coil-label')
.attr('x', 3)
.attr('dy', coilLabelOffset)
.append('textPath')
.attr('xlink:href', function (d) {
return '#path-' + d[coilLabel]
})
.text(function (d) {
return d[coilLabel]
})
}
})
function updatePathData (data) {
let holeRadius = radius * holeRadiusProportion
let arcAngle = 360 / arcsPerCoil
let dataLength = data.length
let coils = Math.ceil(dataLength / arcsPerCoil) // number of coils, based on data.length / arcsPerCoil
let coilWidth = radius * (1 - holeRadiusProportion) / (coils + 1) // remaining radius (after holeRadius removed), divided by coils + 1. I add 1 as the end of the coil moves out by 1 each time
data.forEach(function (d, i) {
let coil = Math.floor(i / arcsPerCoil)
let position = i - coil * arcsPerCoil
let startAngle = position * arcAngle
let endAngle = (position + 1) * arcAngle
let startInnerRadius = holeRadius + i / arcsPerCoil * coilWidth
let startOuterRadius =
holeRadius +
i / arcsPerCoil * coilWidth +
coilWidth * (1 - coilPadding)
let endInnerRadius = holeRadius + (i + 1) / arcsPerCoil * coilWidth
let endOuterRadius =
holeRadius +
(i + 1) / arcsPerCoil * coilWidth +
coilWidth * (1 - coilPadding)
// vertices of each arc
d.x1 = x(startAngle, startInnerRadius)
d.y1 = y(startAngle, startInnerRadius)
d.x2 = x(endAngle, endInnerRadius)
d.y2 = y(endAngle, endInnerRadius)
d.x3 = x(endAngle, endOuterRadius)
d.y3 = y(endAngle, endOuterRadius)
d.x4 = x(startAngle, startOuterRadius)
d.y4 = y(startAngle, startOuterRadius)
// CURVE CONTROL POINTS
let midAngle = startAngle + arcAngle / 2
let midInnerRadius =
holeRadius + (i + 0.5) / arcsPerCoil * coilWidth
let midOuterRadius =
holeRadius +
(i + 0.5) / arcsPerCoil * coilWidth +
coilWidth * (1 - coilPadding)
// MID POINTS, WHERE THE CURVE WILL PASS THRU
d.mid1x = x(midAngle, midInnerRadius)
d.mid1y = y(midAngle, midInnerRadius)
d.mid2x = x(midAngle, midOuterRadius)
d.mid2y = y(midAngle, midOuterRadius)
d.controlPoint1x = (d.mid1x - 0.25 * d.x1 - 0.25 * d.x2) / 0.5
d.controlPoint1y = (d.mid1y - 0.25 * d.y1 - 0.25 * d.y2) / 0.5
d.controlPoint2x = (d.mid2x - 0.25 * d.x3 - 0.25 * d.x4) / 0.5
d.controlPoint2y = (d.mid2y - 0.25 * d.y3 - 0.25 * d.y4) / 0.5
d.arcNumber = position
d.coilNumber = coil
})
return data
}
function x (angle, radius) {
// change to clockwise
let a = 360 - angle
// start from 12 o'clock
a = a + 180 - startAngle;
return radius * Math.sin(a * radians)
}
function y (angle, radius) {
// change to clockwise
let a = 360 - angle
// start from 12 o'clock
a = a + 180 - startAngle;
return radius * Math.cos(a * radians)
}
function chartWH (r) {
return r * 2
}
}
chart.radius = function (value) {
if (!arguments.length) return radius
radius = value
return chart
}
chart.holeRadiusProportion = function (value) {
if (!arguments.length) return holeRadiusProportion
holeRadiusProportion = value
return chart
}
chart.arcsPerCoil = function (value) {
if (!arguments.length) return arcsPerCoil
arcsPerCoil = value
return chart
}
chart.coilPadding = function (value) {
if (!arguments.length) return coilPadding
coilPadding = value
return chart
}
chart.arcLabel = function (value) {
if (!arguments.length) return arcLabel
arcLabel = value
return chart
}
chart.arcLabelOffset = function (value) {
if (!arguments.length) return arcLabelOffset
arcLabelOffset = value
return chart
}
chart.coilLabel = function (value) {
if (!arguments.length) return coilLabel
coilLabel = value
return chart
}
chart.coilLabelOffset = function (value) {
if (!arguments.length) return coilLabelOffset
coilLabelOffset = value
return chart
}
chart.startAngle = function (value) {
if (!arguments.length) return startAngle
startAngle = value
return chart
}
return chart
}