-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoomable-sunburst.html
142 lines (117 loc) · 5.94 KB
/
zoomable-sunburst.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>http://localhost:8080/iframe-with-redirects.html</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.14.2/d3.min.js"
integrity="sha256-M2M+sgC2bZ4r73FO1LV5JmHiS5COwEb2Uqw7EbsHmBY=" crossorigin="anonymous"></script>
</head>
<body>
<script type="text/javascript">
const tree = {"root":{"requestId":"83F8FAF1D237EE413AD7632FC4F59DBC","url":"http://localhost:8080/iframe-with-redirects.html","method":"GET","type":"Document","encodedBytes":292,"frameId":"803648FBC858FE6B364EAA5DD752207A","status":200,"time":0.015672000008635223,"children":[{"requestId":"18E99FF1BEC80B8BFAB5E585A8ECE885","url":"http://localhost:8080/redirect-asset.html","method":"GET","type":"Document","encodedBytes":292,"frameId":"26FAF10067DB819F9E75F3BBB9EDA259","status":200,"time":0.0052220000070519745,"children":[{"requestId":"39603.3","url":"http://localhost:8080/redirect-js","method":"GET","type":"Script","encodedBytes":305,"frameId":"26FAF10067DB819F9E75F3BBB9EDA259","status":200,"time":0.001999999978579581,"children":[{"requestId":"39603.3","url":"http://localhost:8080/redirect-two","method":"GET","type":"Script","encodedBytes":305,"frameId":"26FAF10067DB819F9E75F3BBB9EDA259","status":200,"time":0.001999999978579581,"children":[{"requestId":"39603.3","url":"http://localhost:8080/js/load-script.js","method":"GET","type":"Script","encodedBytes":305,"frameId":"26FAF10067DB819F9E75F3BBB9EDA259","status":200,"time":0.001999999978579581,"children":[{"requestId":"39603.5","url":"http://localhost:8080/js/load-other.js","method":"GET","type":"Script","encodedBytes":305,"frameId":"26FAF10067DB819F9E75F3BBB9EDA259","status":200,"time":0.0022720000124536455,"children":[{"requestId":"39603.6","url":"http://localhost:8080/assets/some-data-large.json","method":"GET","type":"Fetch","encodedBytes":302,"frameId":"26FAF10067DB819F9E75F3BBB9EDA259","status":200,"time":0.0599870000150986,"children":[]},{"requestId":"39603.7","url":"http://localhost:8080/assets/some-data-small.json","method":"GET","type":"Fetch","encodedBytes":298,"frameId":"26FAF10067DB819F9E75F3BBB9EDA259","status":200,"time":0.00317499996162951,"children":[]}]}]}]}]}]}]},"totalNodes":8,"frameIDs":["26FAF10067DB819F9E75F3BBB9EDA259"]};
const width = window.innerWidth;
const height = window.innerHeight - 30;
const radius = height / 6;
const format = d3.format(',d');
// const color = d3.scaleSequential([8, 0], d3.interpolateWarm);
const color = d3.scaleOrdinal(d3.quantize(d3.interpolateRainbow, tree.root.children.length + 1));
const frameColor = d3.scaleOrdinal()
.domain(tree.frameIDs)
.range(d3.schemeAccent);
const arc = d3.arc()
.startAngle(d => d.x0)
.endAngle(d => d.x1)
.padAngle(d => Math.min((d.x1 - d.x0) / 2, 0.005))
.padRadius(radius * 1.5)
.innerRadius(d => d.y0 * radius)
.outerRadius(d => Math.max(d.y0 * radius, d.y1 * radius - 1));
const partition = data => {
const root = d3.hierarchy(data)
.sum(d => d.time)
.sort((a, b) => b.value - a.value);
return d3.partition()
.size([2 * Math.PI, root.height + 1])
(root);
};
const root = partition(tree.root);
root.each(d => d.current = d);
const svg = d3.create('svg')
.attr('viewBox', [0, 0, width, width])
.style('font', '10px sans-serif');
const g = svg.append('g')
.attr('transform', `translate(${width / 2},${width / 2})`);
const path = g.append('g')
.selectAll('path')
.data(root.descendants().slice(1))
.join('path')
.attr('fill', d => {
while (d.depth > 1) d = d.parent;
return color(d.data.url);
})
.attr('fill-opacity', d => arcVisible(d.current) ? (d.children ? 0.6 : 0.4) : 0)
.attr('d', d => arc(d.current));
path.filter(d => d.children)
.style('cursor', 'pointer')
.on('click', clicked);
path.append('title')
.text(d => `${d.ancestors().map(d => d.data.url).reverse().join('/')}\n${format(d.value)}`);
const label = g.append('g')
.attr('pointer-events', 'none')
.attr('text-anchor', 'middle')
.style('user-select', 'none')
.selectAll('text')
.data(root.descendants().slice(1))
.join('text')
.attr('dy', '0.35em')
.attr('fill-opacity', d => +labelVisible(d.current))
.attr('transform', d => labelTransform(d.current))
.text(d => d.data.url);
const parent = g.append('circle')
.datum(root)
.attr('r', radius)
.attr('fill', 'none')
.attr('pointer-events', 'all')
.on('click', clicked);
function clicked(p) {
parent.datum(p.parent || root);
root.each(d => d.target = {
x0: Math.max(0, Math.min(1, (d.x0 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
x1: Math.max(0, Math.min(1, (d.x1 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
y0: Math.max(0, d.y0 - p.depth),
y1: Math.max(0, d.y1 - p.depth)
});
const t = g.transition().duration(750);
// Transition the data on all arcs, even the ones that aren’t visible,
// so that if this transition is interrupted, entering arcs will start
// the next transition from the desired position.
path.transition(t)
.tween('data', d => {
const i = d3.interpolate(d.current, d.target);
return t => d.current = i(t);
})
.filter(function (d) {
return +this.getAttribute('fill-opacity') || arcVisible(d.target);
})
.attr('fill-opacity', d => arcVisible(d.target) ? (d.children ? 0.6 : 0.4) : 0)
.attrTween('d', d => () => arc(d.current));
label.filter(function (d) {
return +this.getAttribute('fill-opacity') || labelVisible(d.target);
}).transition(t)
.attr('fill-opacity', d => +labelVisible(d.target))
.attrTween('transform', d => () => labelTransform(d.current));
}
function arcVisible(d) {
return d.y1 <= 3 && d.y0 >= 1 && d.x1 > d.x0;
}
function labelVisible(d) {
return d.y1 <= 3 && d.y0 >= 1 && (d.y1 - d.y0) * (d.x1 - d.x0) > 0.03;
}
function labelTransform(d) {
const x = (d.x0 + d.x1) / 2 * 180 / Math.PI;
const y = (d.y0 + d.y1) / 2 * radius;
return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
}
document.body.appendChild(svg.node());
</script>
</body>
</html>