-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdirectory_sizes.html
185 lines (160 loc) · 5.74 KB
/
directory_sizes.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
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
<html>
<head>
<title></title>
<script src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.3.1/require.min.js" type="text/javascript"></script>
</head>
<body>
<form>
<label><input type="radio" name="mode" value="size" checked="checked"> Size</label><br/>
<label><input type="radio" name="mode" value="count"> Count</label>
</form>
<div style="float: left;"><span id="output_d3_title"> </span></div>
<div style="float: right;"><span id="mouseover_d3_title"> </span></div>
<div style="clear: both;"></div>
<div style="width: 100%;">
<div id="output_d3" style="margin: 0 auto; width: 1000px;"></div>
</div>
<div style="clear: both;"></div>
<div style="width: 100%; color: #666666; font-size: 10px; bottom: 0;">
<div style="width: 50%; margin: auto; text-align: center;">
Adapted from <a style="color: gray;" href="https://bl.ocks.org/kerryrodden/477c1bfb081b783f80ad">Kerry Rodden's Zoomable sunburst with updating data</a>
</div>
</div>
<script type="text/javascript">
requirejs.config({
paths: {
'd3': '//cdnjs.cloudflare.com/ajax/libs/d3/3.4.8/d3.min',
'jquery': '//cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min',
}
});
require(["jquery", "d3"], function($, d3) {
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("#output_d3").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)); });
// Keep track of the node that is currently being displayed as the root.
var node;
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;
}
// When switching data: interpolate the arcs in data space.
function arcTweenData(a, i) {
var oi = d3.interpolate({x: a.x0, dx: a.dx0}, a);
function tween(t) {
var b = oi(t);
a.x0 = b.x;
a.dx0 = b.dx;
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;
}
}
// When zooming: interpolate the scales.
function arcTweenZoom(d) {
var xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),
yd = d3.interpolate(y.domain(), [d.y, 1]),
yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);
return function(d, i) {
return i
? function(t) { return arc(d); }
: function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };
};
}
function formatted_size(size) {
var post = "";
var posts = ['K', 'M', 'G', 'T', 'P', 'E'];
var outsize = size;
var i;
for (i = 0; i < posts.length; ++i) {
if (Math.trunc(outsize / 1024) > 0) {
outsize /= 1024.0;
} else {
break;
}
}
--i;
if (i >= 0 && i < posts.length) {
post = posts[i];
} else {
outsize = size;
post = "";
}
return outsize.toFixed(2) + post;
}
function node_text(node) {
return node["path"] + " - " + formatted_size(node["size"]);
}
d3.json("directory_sizes_d3tree.json", function(error, root) {
$("#output_d3_title").text(node_text(root));
node = root;
var path = svg.datum(root).selectAll("path")
.data(partition.value(function (d) { return d.size; }).nodes)
.enter().append("path")
.attr("d", arc)
.style("fill", function(d) {
var c = d.children ? d : d.parent;
this._color = color(c ? c.path : null);
return this._color;
})
.on("click", click)
.each(stash);
path.append("svg:title")
.text(function(d) { return d["name"]; });
path.on("mouseover", function(d) {
$("#mouseover_d3_title").text(node_text(d));
d3.select(this).transition().duration(200).style("fill", d3.rgb(this._color).brighter());
}).on("mouseout", function(d) {
$("#mouseover_d3_title").html(" ");
d3.select(this).transition().duration(200).style("fill", this._color);
});
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)
.transition()
.duration(1000)
.attrTween("d", arcTweenData);
});
function click(d) {
$("#output_d3_title").text(node_text(d));
node = d;
path.transition()
.duration(1000)
.attrTween("d", arcTweenZoom(d));
}
});
});
</script>
</body>
</html>