-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpartition.html
executable file
·120 lines (100 loc) · 4.05 KB
/
partition.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
<html>
<head>
<meta charset="utf-8">
<title>Rect - Partition</title>
</head>
<style>
.node_text {
font-size: 10px;
text-anchor: middle;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="lib/GetScreenSize.js"></script>
<script>
size = new GSS();
var width = size.offsetWidth,
height = size.offsetHeight,
radius = (Math.min(width, height) / 2) - 10;
var formatNumber = d3.format(",d");
var x = d3.scale.linear()
.range([0, 2 * Math.PI]);
var y = d3.scale.sqrt()
.range([0, radius]);
var color = d3.scale.category20c();
var partition = d3.layout.partition()
.value(function (d) { return parseInt(d.value); });
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)); });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + (height / 2) + ")");
d3.json("Data/Data.json", function (error, root) {
if (error) throw error;
svg.selectAll("path")
.data(partition.nodes(root))
.enter().append("path")
.attr("d", arc)
.attr("id", function (d, i) {
return i;
})
.style("fill", function (d) { return color((d.children ? d : d.parent).name); })
.on("click", click)
.on("mouseover",function(d){
svg.append("text")
.attr('x',function(){
return d3.event.x - width / 2
}).attr('y',function(){
return d3.event.y - height / 2
}).text(d.name).attr('id',d.id);
})
.on("mouseout",function(d){
svg.select('text').remove();
})
.on('mousemove',function(d){
svg.select('text').attr('x',function(){
return d3.event.x - width / 2
}).attr('y',function(){
return d3.event.y - height / 2
});
});
// svg.selectAll('text')
// .data(partition.nodes(root).filter(function (d) {
// if (d.depth <= 2) {
// return d
// }
// }))
// .enter()
// .append('text')
// .attr('x', 0)
// .attr('y', -20)
// .append("textPath").attr('xlink:href',function (d, i) {
// return '#' + i;
// }).text(function (d) {
// if (d.depth <= 2) {
// return d.name
// }
// });
});
function click(d) {
svg.transition()
.duration(750)
.tween("scale", function () {
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 (t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); };
})
.selectAll("path")
.attrTween("d", function (d) { return function () { return arc(d); }; });
}
d3.select(self.frameElement).style("height", height + "px");
</script>
</body>
</html>