-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransition.html
More file actions
195 lines (157 loc) · 4.62 KB
/
transition.html
File metadata and controls
195 lines (157 loc) · 4.62 KB
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: auto;
position: relative;
width: 960px;
}
form {
position: absolute;
right: 10px;
top: 10px;
}
</style>
<body>
<form>
<label><input type="radio" name="mode" value="size"> Size</label>
<label><input type="radio" name="mode" value="count" checked> Count</label>
</form>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 700,
radius = Math.min(width, height) / 2;
var x = d3.scale.linear()
.range([0, 2 * Math.PI]);
var y = d3.scale.sqrt()
.range([0, radius]);
var color = d3.scale.category20c();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + (height / 2 + 10) + ")");
var partition = d3.layout.partition()
.sort(null)
.value(function(d) { return 1; });
var arc = d3.svg.arc()
.startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })
.endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })
.innerRadius(function(d) { return Math.max(0, y(d.y)); })
.outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });
function walkTree(d, f) {
if (d.children)
d.children.forEach(function (x) { walkTree(x, f); });
f(d);
}
// Keep track of the node that is currently being displayed as the root.
var node;
// Used to assign id numbers to nodes
var i = 0;
d3.json("/data/flare.json", function(error, root) {
node = root;
var nodes = partition.nodes(root);
walkTree(nodes[0], function(d) {
// give everyone a color
color((d.children ? d : d.parent).name);
if (d.depth > 0) {
d._children = d.children;
d.children = null;
}
if (d.depth > 1) {
d.dx = 0;
}
});
partition.nodes(root);
var path = svg.selectAll("path")
.data(nodes, genId)
.enter().append("path")
.attr("d", arc)
.attr("fill", function(d) { return color((d.children || d._children ? d : d.parent).name); })
.on("click", click)
.attr("stroke", "#fff")
.attr("stroke-opacity", function (d) { return d.dx == 0 ? "0" : "1"} )
.each(stash);
d3.selectAll("input").on("change", function change() {
var value = this.value === "count"
? function() { return 1; }
: function(d) { return d.size; };
path
.data(partition.value(value).nodes(root), genId)
.transition()
.duration(1000)
.attrTween("d", arcTweenData);
});
function click(d) {
// remove children of clicked node
var buf = d._children;
d._children = d.children;
d.children = buf;
// compute the new layout
var old_d = {x: d.x, dx: d.dx, y: d.y, dy: d.dy};
var new_nodes = partition.nodes(root);
// find the new position of the clicked node
// update the removed children to have 0 area put occupy the correct position
// add the children back into the nodes of the computed layout
if (d._children)
d._children.forEach(function (node) { walkTree(node, function (x) {
x.x = d.x + d.dx / 2.;
x.dx = 0;
x.y = d.y + d.dy;
new_nodes.push(x);
}); });
if (d.children)
d.children.forEach(function (node) { walkTree(node, function(x) {
x.x0 = old_d.x + old_d.dx / 2.;
x.dx0 = 0;
x.y0 = old_d.y + old_d.dy;
})});
// apply transition
path
.data(new_nodes, genId)
.transition()
.duration(1000)
.attrTween("d", arcTweenData)
.attrTween("stroke-opacity", function (d, i) {
return d.dx == 0
? function (t) { return 1; }
: function (t) { return 1;};
});
}
});
d3.select(self.frameElement).style("height", height + "px");
// Setup for switching data: stash the old values for transition.
function stash(d) {
d.x0 = d.x;
d.dx0 = d.dx;
d.y0 = d.y;
d.dy0 = d.dy;
}
// When switching data: interpolate the arcs in data space.
function arcTweenData(a, i) {
var oi = d3.interpolate({x: a.x0, dx: a.dx0, y: a.y0, dy: a.dy0}, a);
function tween(t) {
var b = oi(t);
a.x0 = b.x;
a.dx0 = b.dx;
a.y0 = b.y;
a.dy0 = b.dy;
return arc(b);
}
if (i == 0) {
// If we are on the first arc, adjust the x domain to match the root node
// at the current zoom level. (We only need to do this once.)
var xd = d3.interpolate(x.domain(), [node.x, node.x + node.dx]);
return function(t) {
x.domain(xd(t));
return tween(t);
};
} else {
return tween;
}
}
</script>
</body>
</html>