-
Notifications
You must be signed in to change notification settings - Fork 0
/
piechart.html
162 lines (138 loc) · 5.12 KB
/
piechart.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
<!DOCTYPE html>
<meta charset="utf-8">
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<!-- Color scale -->
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<!-- Add 2 buttons -->
<button style="float:left" onclick="update(data1)">Total</button>
<button style="float:left" onclick="update(data2)">By Category</button>
<!-- Create a div where the graph will take place -->
<div id="piechart" style="width:50%; float:left"></div></div>
<div id="description" style="width:30%; float:left"> <p>At a very high level, we can see that of this collection of films, more than half fail the Bechdel test. Drilling down further into the types of classification, the highest number of movies fail the test because the women - although present - don't speak to each other. <br>
<br>
Hover over the fields to get more info.
</p></div>
<script>
// set the dimensions and margins of the graph
var width = 600
height = 450
margin = 40
// The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.
var radius = Math.min(width, height) / 2 - margin
// append the svg object to the div called 'piechart'
var svg = d3.select("#piechart")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
// Create the data
var data1 = [{name: 'Pass', count:803, percentage: 45, color:'#b3cde3'},
{name: 'Fail', count: 991, percentage: 55, color: '#fbb4ae'}]
var data2 = [{name: 'Pass', count:803, percentage: 45, color: '#b3cde3'},
{name: 'Dubious', count: 142, percentage: 8, color: '#ccebc5'},
{name: 'Talk About Men', count: 194, percentage: 11, color:'#decbe4'},
{name: 'No Talk', count: 514, percentage: 29, color: '#fed9a6'},
{name: 'No Women', count: 141, percentage: 8, color:'#fddaec'}]
// A function that creates / updates the plot depending on the selection
function update(data) {
// remove previous plots before loading new one
svg.selectAll("text").remove()
svg.selectAll("circle").remove()
svg.selectAll("path").remove()
d3.selectAll("g.tooltip").remove()
// Compute the position of each group on the pie:
var pie = d3.pie()
.value(function(d) {return d.count; })
var data_ready = pie(data)
// shape helper to build arcs:
var arcGenerator = d3.arc()
.innerRadius(0)
.outerRadius(radius)
// -1- Create a tooltip div that is hidden by default:
var tooltip = d3.select("#piechart")
.append("g")
.attr("class", "tooltip")
.style("background-color", "white")
.style("border", "solid")
.style("border-width", "1px")
.style("position", "absolute")
.style("width", "100px")
.style("border-radius", "5px")
.style("padding", "10px")
.style("opacity", 0)
// Create functions to show / update (when mouse move but stay on same circle) / hide the tooltip
var showTooltip = function(d) {
tooltip
.transition()
.duration(200)
tooltip
.style("opacity", 1)
.html("<b>Name:</b> " + d.data.name + "<br>" + "<b> Count: </b>" + d.data.count + "<br>" )
.style("left", (d3.mouse(this)[0]+300) + "px")
.style("top", (d3.mouse(this)[1]+300) + "px")
}
var moveTooltip = function(d) {
tooltip
.style("left", (d3.mouse(this)[0]+300) + "px")
.style("top", (d3.mouse(this)[1]+300) + "px")
}
var hideTooltip = function(d) {
tooltip
.transition()
.duration(200)
.style("opacity", 0)
}
var u = svg.selectAll("path")
.data(data_ready).enter()
.append('path')
// Build the pie chart. Each part of the pie is a path that we build using the arc function.
u
.merge(u)
.transition()
.duration(1000)
.attr('d', arcGenerator)
.attr('fill', function(d){ return(d.data.color) })
.attr("stroke", "black")
.style("stroke-width", "2px")
.style("opacity", 1)
u
.on("mouseover", showTooltip )
.on("mousemove", moveTooltip )
.on("mouseleave", hideTooltip )
// remove the group that is not present anymore
// u.exit().remove();
// Now add the annotation. Use the centroid method to get the best coordinates
var v = svg.selectAll('mySlices')
.data(data_ready)
.enter()
.append('text')
.transition()
.duration(1000)
.text(function(d){ return d.data.percentage + '%'})
.attr("transform", function(d) { return "translate(" + arcGenerator.centroid(d) + ")"; })
.style("text-anchor", "middle")
.style("font-size", 17)
// create a legend
svg.selectAll("mydots")
.data(data_ready)
.enter()
.append("circle")
.attr("cx", -280)
.attr("cy", function(d,i){ return 100 + i*25}) // 100 is where the first dot appears. 25 is the distance between dots
.attr("r", 7)
.style("fill", function(d){ return d.data.color})
// Add text to the legend
legend = svg.selectAll("mylabels")
.data(data_ready);
legend
.enter()
.append("text").transition().duration(1000)
.attr("x", -260)
.attr("y", function(d,i){ return 100 + i*25}) // 100 is where the first dot appears. 25 is the distance between dots
.text(function(d){ return d.data.name})
.attr("text-anchor", "left")
.style("alignment-baseline", "middle")
}
</script>