This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
sunburst.js
414 lines (352 loc) · 14.1 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import d3 from 'd3';
const dflts = {
width: 600,
height: 500,
padding: 10,
innerRadius: 20,
transitionDuration: 750
};
const numFormat = d3.format(',.3g');
const Tau = 2 * Math.PI;
function constrain(v, vMin, vMax) {
return Math.max(vMin, Math.min(vMax, v));
}
const textStyle = {
fill: '#444',
'text-anchor': 'middle',
'font-size': '10px',
'font-family': 'Arial',
'text-shadow': 'white -1px 0px 0.5px, white 0px -1px 0.5px, white 0px 1px 0.5px, white 1px 0px 0.5px'
};
export default class SunburstD3 {
constructor(el, figure, onChange) {
const self = this;
self.update = self.update.bind(self);
self._update = self._update.bind(self);
self.svg = d3.select(el).append('svg');
self.pathGroup = self.svg.append('g');
self.textGroup = self.svg.append('g')
.style('pointer-events', 'none');
self.angularScale = d3.scale.linear().range([0, Tau]);
self.radialScale = d3.scale.sqrt();
self.colorScale = d3.scale.category20();
self.partition = d3.layout.partition()
.value(d => !d.children && d.size)
.sort((a, b) => a.i - b.i);
self.arc = d3.svg.arc()
.startAngle(d => constrain(self.angularScale(d.x), 0, Tau))
.endAngle(d => constrain(self.angularScale(d.x + d.dx), 0, Tau))
.innerRadius(d => Math.max(0, self.radialScale(d.y)))
.outerRadius(d => Math.max(0, self.radialScale(d.y + d.dy)));
self.figure = {};
self.onChange = onChange;
self.initialized = false;
self._promise = Promise.resolve();
self.update(figure);
}
update(figure) {
const self = this;
// ensure any previous transition is complete before we start
self._promise = self._promise.then(() => self._update(figure));
}
_update(figure) {
const self = this;
const oldFigure = self.figure;
// fill defaults in the new figure
const width = figure.width || dflts.width;
const height = figure.height || dflts.height;
// interactive: undefined defaults to true
const interactive = figure.interactive !== false;
const padding = figure.padding || dflts.padding;
const innerRadius = figure.innerRadius || dflts.innerRadius;
const transitionDuration = figure.transitionDuration || dflts.transitionDuration;
const {data, dataVersion} = figure;
const selectedPath = figure.selectedPath || [];
const newFigure = self.figure = {
width,
height,
interactive,
padding,
innerRadius,
transitionDuration,
data,
dataVersion,
selectedPath
};
/*
* Definitions
*/
const selectedX = node => [node.x, node.x + node.dx];
const selectedY = node => [node.y, 1];
const selectedRadius = node => [node.y ? self.figure.innerRadius : 0, self.radius];
const rCenter = node => self.radialScale(node.y + node.dy / 2);
const angleCenter = node => self.angularScale(node.x + node.dx / 2);
const xCenter = node => rCenter(node) * Math.sin(angleCenter(node));
const yCenter = node => -rCenter(node) * Math.cos(angleCenter(node));
const skinny = node => {
const dtheta = self.angularScale(node.x + node.dx) - self.angularScale(node.x);
const r0 = self.radialScale(node.y);
const dr = self.radialScale(node.y + node.dy) / r0 - 1;
return r0 && (dr / dtheta > 1);
};
const textTrans = node => {
const rot = (angleCenter(node) * 360 / Tau + (skinny(node) ? 0 : 90)) % 180 - 90;
return 'rotate(' + rot + ',' + xCenter(node) + ',' + yCenter(node) + ')';
};
const hideText = node => {
return (
angleCenter(node) > 0 && angleCenter(node) < Tau &&
rCenter(node) > 0 && rCenter(node) < self.radius
) ? 1 : 0;
}
const posOnly = (d) => {
const {x, dx, y, dy} = d;
return {x, dx, y, dy};
}
function wrap(accessor) {
return d => {
return t => {
const d0 = self.oldDataMap[getPathStr(d)];
if(d0 && d0 !== d) {
const interpolator = d3.interpolateObject(posOnly(d0), posOnly(d));
return accessor(interpolator(t));
}
return accessor(d);
}
};
}
const transitionToNode = node => {
// simultaneous transitions can cause infinite loops in some cases
// mostly self._promise takes care of this, we want to avoid clicks
// during transitions.
self.transitioning = true;
const transition = self.svg.transition()
.duration(self.figure.transitionDuration)
.tween('scale', () => {
const angularDomain = d3.interpolate(
self.angularScale.domain(),
selectedX(node)
);
const radialDomain = d3.interpolate(
self.radialScale.domain(),
selectedY(node)
);
const radialRange = d3.interpolate(
self.radialScale.range(),
selectedRadius(node)
);
return function(t) {
self.angularScale
.domain(angularDomain(t));
self.radialScale
.domain(radialDomain(t))
.range(radialRange(t));
};
});
transition.selectAll('path')
.attrTween('d', wrap(self.arc));
transition.selectAll('text')
.attrTween('x', wrap(xCenter))
.attrTween('y', wrap(yCenter))
.attrTween('transform', wrap(textTrans))
.attrTween('opacity', wrap(hideText));
if(self.onChange) {
self.figure.selectedPath = getPath(node);
self.onChange(self.figure);
}
return transition;
};
const updatePaths = (_paths, _texts, _dataChange) => {
if(_dataChange) {
const enteringPaths = _paths.enter().append('path')
.style({stroke: '#fff', strokeWidth: 1})
.on('click', node => {
if(self.transitioning) { return; }
self._promise = self._promise.then(() => {
return new Promise(resolve => {
if(self.figure.interactive) {
transitionToNode(node)
.each('end', () => {
self.transitioning = false;
resolve();
});
}
else {
resolve();
}
});
});
});
enteringPaths.append('title');
_texts.enter().append('text')
.style(textStyle)
.text(d => d.name);
}
/*
* Updates to attributes, that we need to do regardless of what changed
*/
_paths
.attr('d', self.arc)
// coloring this way will be history-dependent: if you insert a
// new item in the middle it will get the next color, and existing
// items will keep their colors. But if you later redraw this
// component straight from the final data you'll get different colors
.style('fill', d => (
// first look for an explicit color (or explicit parent color)
d.color ||
(!d.children && d.parent.color) ||
self.colorScale(getPathStr(d.children ? d : d.parent))
));
// title is a cheap solution for tooltips; better is to call
// `d3.on('mouse(over|out)')` and draw a tooltip.
// That requires a bit of extra logic if you want these tooltips
// to work correctly across state updates
_paths.select('title').text(d => d.name + '\n' + numFormat(d.value));
_texts
.attr('x', xCenter)
.attr('y', yCenter)
.attr('transform', textTrans)
.attr('opacity', hideText);
const dataMap = self.oldDataMap = {};
_paths.each(d => {
dataMap[getPathStr(d)] = posOnly(d);
});
};
const setSize = () => {
self.radius = (Math.min(height, width) / 2) - padding;
self.svg.attr({width, height});
const centered = 'translate(' + (width / 2) + ',' + (height / 2) + ')';
self.pathGroup.attr('transform', centered);
self.textGroup.attr('transform', centered);
};
/*
* Diffing
*/
let retVal = Promise.resolve();
const change = diff(oldFigure, newFigure);
if(!change) { return retVal; }
const sizeChange = change.width || change.height || change.padding;
const dataChange = change.data;
const oldRootName = self.rootName;
const newRootName = self.rootName = data.name;
const oldSelectedPath = self.selectedPath;
const newSelectedPath = self.selectedPath = selectedPath.slice();
/*
* Drawing
*/
if(sizeChange) { setSize(); }
let paths = self.pathGroup.selectAll('path');
let texts = self.textGroup.selectAll('text');
if(dataChange) {
// clone data before partitioning, since this mutates the data
self.nodes = self.partition.nodes(addIndices(JSON.parse(JSON.stringify(data))));
paths = paths.data(self.nodes, getPathStr);
texts = texts.data(self.nodes, getPathStr);
// exit paths at the beginning of the transition
// enters will happen at the end
paths.exit().remove();
texts.exit().remove();
}
const selectedNode = getNode(self.nodes[0], selectedPath);
// no node: path is wrong, probably because we received a new selectedPath
// before the data it belongs with
if(!selectedNode) { return retVal; }
// immediate redraw rather than transition if:
const shouldAnimate =
// first draw
self.initialized &&
// new root node
(newRootName === oldRootName) &&
// not a pure up/down transition
sameHead(oldSelectedPath, newSelectedPath) &&
// the previous data didn't contain the new selected node
// this can happen if we transition selectedPath first, then data
(!dataChange || getNode(oldFigure.data, newSelectedPath));
console.log(shouldAnimate, oldSelectedPath, newSelectedPath);
if(shouldAnimate) {
retVal = new Promise(resolve => {
transitionToNode(selectedNode)
.each('end', () => {
updatePaths(paths, texts, dataChange);
self.transitioning = false;
resolve();
});
});
}
else {
// first draw has no animation, and initializes the scales
self.angularScale.domain(selectedX(selectedNode));
self.radialScale.domain(selectedY(selectedNode))
self.radialScale.range(selectedRadius(selectedNode));
updatePaths(paths, texts, dataChange);
self.initialized = true;
}
return retVal;
}
};
function sameHead(array1, array2) {
const len = Math.min(array1.length, array2.length);
for(let i = 0; i < len; i++) {
if(array1[i] !== array2[i]) { return false; }
}
return true;
}
// so we can sort by index, not by size as partition does by default.
function addIndices(node) {
if(node.children) {
node.children.forEach((child, i) => {
child.i = i;
addIndices(child);
});
}
return node;
}
function getPath(d) {
return d.parent ? getPath(d.parent).concat([d.name]) : [];
}
function getPathStr(d) {
return getPath(d).join(',') || d.name;
}
function getNode(node, path) {
if(!path.length) { return node; }
if(!node.children) { return false; }
let childi;
for(var i = 0; i < node.children.length; i++) {
childi = node.children[i];
if(childi.name === path[0]) {
return getNode(childi, path.slice(1));
}
}
return false;
}
/**
* Very simple diff - assumes newObj is flat and has all the possible keys from oldObj
* uses a "dataVersion" key to avoid diffing the full data object.
* In fact, this way we can avoid copying data (ie treating it immutably),
* and just use dataVersion to track mutations.
*/
function diff(oldObj, newObj) {
const V = 'Version';
const out = {};
let hasChange = false;
for(const key in newObj) {
if(key.substr(key.length - V.length) === V) { continue; }
if(typeof newObj[key] === 'object') {
if(newObj[key + V]) {
if(newObj[key + V] !== oldObj[key + V]) {
out[key] = 1;
hasChange = true;
}
}
else if(JSON.stringify(oldObj[key]) !== JSON.stringify(newObj[key])) {
out[key] = 1;
hasChange = true;
}
}
else if(oldObj[key] !== newObj[key]) {
out[key] = 1;
hasChange = true;
}
}
return hasChange && out;
}