-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpie-v5.html
141 lines (123 loc) · 3.58 KB
/
pie-v5.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Pie Chart v5</title>
<style>
body {
font: 10px sans-serif;
}
form {
left: 250px;
position: absolute;
}
label {
cursor: pointer;
}
.tooltip {
background: #eee;
box-shadow: 0 0 5px #999999;
color: #333;
padding: 8px;
position: absolute;
text-align: center;
visibility: hidden;
z-index: 10;
}
</style>
</head>
<body>
<form>
<h3>Include Ontario:</h3>
<label><input type="radio" name="ontario" value="yes" checked> Yes</label>
<label><input type="radio" name="ontario" value="no"> No</label>
</form>
<script src="d3.v3.min.js"></script>
<script>
(function(){
'use strict';
var width = 960,
height = 500,
radius = Math.min(width, height) / 2;
var color = d3.scale.category10();
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 110);
var pie = d3.layout.pie()
.value(function(d) { return d.count; })
.sort(null);
var tooltip = d3.select('body')
.append('div')
.attr('class', 'tooltip');
var tooltipOn = function(d, i) {
var content = '<div>' + d.data.province + '</div><div>' + d.data.count + '</div>';
tooltip.html(content)
.style('visibility', 'visible');
};
var tooltipMove = function(d, i) {
tooltip.style('top', (d3.event.pageY + 10) + 'px')
.style('left', (d3.event.pageX + 10) + 'px');
};
var tooltipOff = function() {
tooltip.style('visibility', 'hidden');
};
var svg = d3.select('body')
.append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');
var arcTween = function(a) {
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return arc(i(t));
};
};
d3.csv('provinces.csv', function(error, data) {
data.forEach(function(d) {
d.count = +d.count;
});
var path = svg.datum(data)
.selectAll('path')
.data(pie)
.enter()
.append('path')
.attr('d', arc)
.attr('fill', function(d, i) { return color(d.data.province); })
.each(function(d) { this._current = d; });
path.on('mouseover', tooltipOn)
.on('mousemove', tooltipMove)
.on('mouseout', tooltipOff);
var legend = svg.selectAll('.legend')
.data(color.domain())
.enter()
.append('g')
.attr('class', 'legend')
.attr('transform', function(d, i) {
return 'translate(0,' + (i * 20 - 100) + ')';
});
legend.append('rect')
.attr('width', 18)
.attr('height', 18)
.attr('x', -18)
.style('fill', color);
legend.append('text')
.attr('x', 4)
.attr('y', 9)
.text(function(d) { return d; });
var change = function() {
if (this.value === 'yes') {
pie.value(function(d) { return d.count; });
} else {
pie.value(function(d) { return (d.province === 'ON') ? 0 : d.count; });
}
path = path.data(pie);
path.transition().duration(750).attrTween('d', arcTween);
};
d3.selectAll('input').on('change', change);
});
})();
</script>
</body>
</html>