-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpieChart.js
More file actions
209 lines (195 loc) · 3.93 KB
/
pieChart.js
File metadata and controls
209 lines (195 loc) · 3.93 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//18-24
var group1 = [{
title: "Strongly Like",
value: 22
},
{
title: "Somewhat Like",
value: 35
},
{
title: "Somewhat Dislike",
value: 15
},
{
title: "Strongly Dislike",
value: 16
},
{
title: "Other/Not Sure",
value: 12
}
];
//25-34
var group2 = [{
title: "Strongly Like",
value: 25
},
{
title: "Somewhat Like",
value: 39
},
{
title: "Somewhat Dislike",
value: 14
},
{
title: "Strongly Dislike",
value: 15
},
{
title: "Other/Not Sure",
value: 7
}
];
//35-44
var group3 = [{
title: "Strongly Like",
value: 30
},
{
title: "Somewhat Like",
value: 40
},
{
title: "Somewhat Dislike",
value: 12
},
{
title: "Strongly Dislike",
value: 12
},
{
title: "Other/Not Sure",
value: 6
}
];
//45-55
var group4 = [{
title: "Strongly Like",
value: 39
},
{
title: "Somewhat Like",
value: 39
},
{
title: "Somewhat Dislike",
value: 9
},
{
title: "Strongly Dislike",
value: 9
},
{
title: "Other/Not Sure",
value: 4
}
];
//55+
var group5 = [{
title: "Strongly Like",
value: 39
},
{
title: "Somewhat Like",
value: 42
},
{
title: "Somewhat Dislike",
value: 10
},
{
title: "Strongly Dislike",
value: 7
},
{
title: "Other/Not Sure",
value: 2
}
];
var width = 300,
height = 300,
radius = Math.min(width, height) / 2;
var color = d3.scaleOrdinal()
.range(["#5EC9A9", "#AFE4B8", "#539CC6", "#323595", "#C2D5EB"]);
var pie = d3.pie()
.value(function (d) {
return d.value;
})(group1);
var arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(0);
var svg = d3.select("#pie")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var g = svg.selectAll("arc")
.data(pie)
.enter().append("g")
.attr("class", "arc")
.on('mouseover', function (d, i) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '.95');
})
.on('mouseout', function (d, i) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '1');
})
.attr('transform', 'translate(0, 0)');
g.append("path")
.attr("d", arc)
.style("fill", function (d) {
return color(d.data.title);
});
//added this for a legend originally. now located in html
// svg.append('g')
// .attr('class', 'legend')
// .selectAll('text')
// .data(group1)
// .enter()
// .append('text')
// .text(function (d) {
// return '• ' + d.title;
// })
// .attr('fill', function (d) {
// return color(d.title);
// })
// .attr('y', function (d, i) {
// return 19 * (i - 2);
// })
// .attr('x', 55);
function changeData(data) {
var pie = d3.pie()
.value(function (d) {
return d.value;
})(data);
path = d3.select("#pie")
.selectAll("path")
.data(pie); // Compute the new angles
path.transition().duration(500).attr("d", arc); // redrawing the path with a smooth transition
}
d3.select("button#group1")
.on("click", function () {
changeData(group1);
})
d3.select("button#group2")
.on("click", function () {
changeData(group2);
})
d3.select("button#group3")
.on("click", function () {
changeData(group3)
})
d3.select("button#group4")
.on("click", function () {
changeData(group4)
})
d3.select("button#group5")
.on("click", function () {
changeData(group5)
})