-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunburst.html
102 lines (93 loc) · 2.25 KB
/
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sunburst Example</title>
<script src="../lib/d3.v7.min.js"></script>
</head>
<body>
<svg></svg>
</body>
<script>
var vData = {
id: "TOPICS",
children: [
{
id: "Topic A",
children: [
{ id: "Sub A1", size: 4 },
{ id: "Sub A2", size: 4 },
],
},
{
id: "Topic B",
children: [
{ id: "Sub B1", size: 3 },
{ id: "Sub B2", size: 3 },
{ id: "Sub B3", size: 3 },
],
},
{
id: "Topic C",
children: [
{ id: "Sub C1", size: 4 },
{ id: "Sub C2", size: 4 },
],
},
],
};
var vWidth = 300;
var vHeight = 300;
var vRadius = Math.min(vWidth, vHeight) / 2;
var vColor = d3.scaleOrdinal(d3.schemeCategory20b);
var g = d3
.select("svg")
.attr("width", vWidth)
.attr("height", vHeight)
.append("g")
.attr("transform", "translate(" + vWidth / 2 + "," + vHeight / 2 + ")");
var vLayout = d3.partition().size([2 * Math.PI, vRadius]);
var vRoot = d3.hierarchy(vData).sum(function (d) {
return d.size;
});
vLayout(vRoot);
var vArc = d3
.arc()
.startAngle(function (d) {
return d.x0;
})
.endAngle(function (d) {
return d.x1;
})
.innerRadius(function (d) {
return d.y0;
})
.outerRadius(function (d) {
return d.y1;
});
/*
var vNodes = vRoot.descendants();
var vSlices = g.selectAll('path')
.data(vNodes)
.enter()
.append('path');
vSlices.filter(function(d) { return d.parent; })
.attr('d', vArc)
.style('stoke', '#fff')
.style('fill', function (d) {
return vColor((d.children ? d : d.parent).data.id);
});
*/
g.selectAll("path")
.data(vRoot.descendants())
.enter()
.append("path")
.attr("display", function (d) {
return d.depth ? null : "none";
})
.attr("d", arc)
.style("stroke", "#fff")
.style("fill", function (d) {
return color((d.children ? d : d.parent).data.name);
});
</script>
</html>