-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunburst.js
292 lines (224 loc) · 7.96 KB
/
sunburst.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
var b = {
w: 100, h: 30, s: 3, t: 10
};
const totalSize = 6820
const levels = ["All", "genre", "score", "country"]
const depth = levels.length - 1
const sWidth = 450,
sHeight = 300,
maxRadius = (Math.min(sWidth, sHeight) / 2) - 5;
const formatNumber = d3.format(',d');
const x = d3.scaleLinear()
.range([0, 2 * Math.PI])
.clamp(true);
const y = d3.scaleSqrt()
.range([maxRadius * .1, maxRadius]);
const color = d3.scaleOrdinal(d3.schemeSet3);
const partition = d3.partition()
const arc = d3.arc()
.startAngle(d => x(d.x0))
.endAngle(d => x(d.x1))
.innerRadius(d => Math.max(0, y(d.y0)))
.outerRadius(d => Math.max(0, y(d.y1)));
const middleArcLine = d => {
const halfPi = Math.PI / 2;
const angles = [x(d.x0) - halfPi, x(d.x1) - halfPi];
const r = Math.max(0, (y(d.y0) + y(d.y1)) / 2);
const middleAngle = (angles[1] + angles[0]) / 2;
const invertDirection = middleAngle > 0 && middleAngle < Math.PI; // On lower quadrants write text ccw
if (invertDirection) { angles.reverse(); }
const path = d3.path();
path.arc(0, 0, r, angles[0], angles[1], invertDirection);
return path.toString();
};
const textFits = d => {
const CHAR_SPACE = 6;
const deltaAngle = x(d.x1) - x(d.x0);
const r = Math.max(0, (y(d.y0) + y(d.y1)) / 2);
const perimeter = r * deltaAngle;
return d.data.name.length * CHAR_SPACE < perimeter;
};
const sunburstSVG = d3.select('#sunburst-chart').append('svg')
.style('width', sWidth)
.style('height', sHeight)
.attr('viewBox', `${-sWidth / 2} ${-sHeight / 2} ${sWidth} ${sHeight}`)
.on('click', () => focusOn()); // Reset zoom on canvas click
function createSunburstVisualization(data) {
let root = prepareSunburst(data);
initializeBreadcrumbTrail(root)
createVisualization(root);
}
function createVisualization(root) {
root.sum(d => d.size);
const slice = sunburstSVG.selectAll('g.slice')
.data(partition(root).descendants())
slice.exit().remove();
const newSlice = slice.enter()
.append('g').attr('class', 'slice')
.on('click', d => {
d3.event.stopPropagation();
focusOn(d);
});
newSlice.append('title')
.text(d => d.data.name + '\n' + formatNumber(d.value));
newSlice.append('path')
.attr('class', 'main-arc')
.style('fill', d => color(d.data.name))
.attr('d', arc);
newSlice.append('path')
.attr('class', 'hidden-arc')
.attr('id', (_, i) => `hiddenArc${i}`)
.attr('d', middleArcLine);
const text = newSlice.append('text')
.attr('display', d => textFits(d) ? null : 'none');
// Add white contour
text.append('textPath')
.attr('startOffset', '50%')
.attr('xlink:href', (_, i) => `#hiddenArc${i}`)
.text(d => d.data.name)
.style('fill', 'none')
.style('stroke', '#fff')
.style('stroke-width', 5)
.style('stroke-linejoin', 'round');
text.append('textPath')
.attr('startOffset', '50%')
.attr('xlink:href', (_, i) => `#hiddenArc${i}`)
.text(d => d.data.name);
}
function focusOn(d = { x0: 0, x1: 1, y0: 0, y1: 1 }) {
// Reset to top-level if no data point specified
var sequenceArray = d.ancestors().reverse();
//sequenceArray.shift(); // remove root node from the array
var percentage = (100 * d.value / totalSize).toPrecision(3);
var percentageString = percentage + "%";
if (percentage < 0.1) {
percentageString = "< 0.1%";
}
updateBreadcrumbs(sequenceArray, percentageString)
const transition = sunburstSVG.transition()
.duration(750)
.tween('scale', () => {
const xd = d3.interpolate(x.domain(), [d.x0, d.x1]),
yd = d3.interpolate(y.domain(), [d.y0, 1]);
return t => { x.domain(xd(t)); y.domain(yd(t)); };
});
transition.selectAll('path.main-arc')
.attrTween('d', d => () => arc(d));
transition.selectAll('path.hidden-arc')
.attrTween('d', d => () => middleArcLine(d));
transition.selectAll('text')
.attrTween('display', d => () => textFits(d) ? null : 'none');
moveStackToFront(d);
//
function moveStackToFront(elD) {
sunburstSVG.selectAll('.slice').filter(d => d === elD)
.each(function (d) {
this.parentNode.appendChild(this);
if (d.parent) { moveStackToFront(d.parent); }
})
}
}
function prepareSunburst(data) {
let nestedData = d3.nest()
.key(function (d) { return d.genre; })
.key(function (d) {
let roundedScore = Math.round(d.score)
if (roundedScore < 5) {
return "0-5"
} else if (roundedScore < 7) {
return "5-7"
} else return "8-10"
})
.key(function (d) { return d.country; })
.entries(data)
const newData = nest("Movies", nestedData, 0, depth);
let root = d3.hierarchy(newData);
return root
}
function nest(name, data, depth, maxDepth) {
if (depth < maxDepth)
return {
"name": name,
children: data.map((e) => nest(e.key, e.values, depth + 1, maxDepth))
};
else return {
"name": name,
"size": data.length
};
}
function initializeBreadcrumbTrail(root) {
var trail = d3.select("#sequence").append("svg:svg")
.attr("width", sWidth)
.attr("height", 50)
.attr("id", "trail");
// Add the label at the end, for the percentage.
trail.append("svg:text")
.attr("id", "endlabel")
.style("fill", "#000");
updateBreadcrumbs([root], "100%")
}
function updateBreadcrumbs(nodeArray, percentageString) {
let labels = []
let values = []
// Data join; key function combines name and depth (= position in sequence).
var trail = d3.select("#trail")
.selectAll("g")
.data(nodeArray, function (d) { return d.data.name + d.depth; });
// Remove exiting nodes.
trail.exit().remove();
// Add breadcrumb and label for entering nodes.
var entering = trail.enter().append("svg:g");
entering.append("svg:polygon")
.attr("points", breadcrumbPoints)
.style("fill", function (d) { return color(d.data.name); });
for (let i= 0; i<nodeArray.length; i++){
node = nodeArray[i]
let reverseDepth = depth - node.height
if (reverseDepth > 0) {
label = levels[reverseDepth]
labels.push(label)
values.push(node.data.name)
}
}
entering.append("svg:text")
.attr("x", (b.w + b.t) / 2)
.attr("y", b.h / 2)
.attr("dy", "0.35em")
.attr("text-anchor", "middle")
.text(function (d) {
let label
if (nodeArray.length > 1) {
let reverseDepth = depth - d.height
label = levels[reverseDepth]
label = label + ": "
} else label = ""
return label + d.data.name;
});
// Merge enter and update selections; set position for all nodes.
entering.merge(trail).attr("transform", function (d, i) {
return "translate(" + i * (b.w + b.s) + ", 0)";
});
// Now move and update the percentage at the end.
d3.select("#trail").select("#endlabel")
.attr("x", (nodeArray.length + 0.3) * (b.w + b.s))
.attr("y", b.h / 2)
.attr("dy", "0.35em")
.attr("text-anchor", "middle")
.text(percentageString);
// Make the breadcrumb trail visible, if it's hidden.
d3.select("#trail")
.style("visibility", "");
updateData(labels, values)
}
function breadcrumbPoints(d, i) {
var points = [];
points.push("0,0");
points.push(b.w + ",0");
points.push(b.w + b.t + "," + (b.h / 2));
points.push(b.w + "," + b.h);
points.push("0," + b.h);
if (i > 0) { // Leftmost breadcrumb; don't include 6th vertex.
points.push(b.t + "," + (b.h / 2));
}
return points.join(" ");
}