-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogue-year-graph.html
347 lines (299 loc) · 13.2 KB
/
dialogue-year-graph.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<html>
<head>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v6.js"></script>
<script src="https://rawgit.com/susielu/d3-annotation/master/d3-annotation.min.js"></script>
<link rel="stylesheet" href="web.css">
<h2> Average yearly distribution of the difference of male and female lines</h2>
<p style="width: 700px">See how the gender breakdown in movies has changed over the years. Important events that happened in the woman film industry are marked in the graph. Hit play/pause to start/stop the animation. Use the scroller to manually go to a year. </p>
</head>
<body>
<button name= "play-button" class="play-button" id="play-button">Play</button>
<output for="slider" id="year" >1935</output>
<input type="range" name="slider" id="slider" min="1935" max="2010" value="1935" style="width: 1000px;" step="5" class="slider"/>
<style>
</style>
<script>
d3.csv("data_cleaned_merged/movie_title_metadata.csv").then((movie_data) =>{d3.csv("data_cleaned_merged/line_percents_by_gender.csv").then((gender_data) => {
//var sliderInterval = setInterval(increaseSlider, 1000);
var currYear = document.getElementById('slider').value
function increaseSlider(){
currentVal = document.getElementById('slider').value
document.getElementById('slider').value = parseInt(currentVal)+5;
document.getElementById("year").value = document.getElementById('slider').value;
var year = parseInt(document.getElementById('slider').value)
t = update_year(year)
// console.log(t)
update(t, year)
}
d3.select("#slider").on("input", function (d) {
selectedValue = this.value;
document.getElementById("year").value = selectedValue;
currYear = this.value;
t = update_year(this.value)
// console.log(t)
update(t, this.value)
})
var pauseButton = document.getElementById('play-button');
var playing = false;
function playSlider(){
playing = true;
pauseButton.innerHTML = 'Pause';
sliderInterval = setInterval(increaseSlider, 1000);
}
function pauseSlider(){
playing = false;
pauseButton.innerHTML = 'Play';
clearInterval(sliderInterval);
}
d3.select("#play-button").on("click", function(){
if(playing){ pauseSlider(); }
else{ playSlider(); }
})
//set margin, width height
const margin = {top: 10, right: 10, bottom: 30, left: 50},
width = 1300 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
var parseTime = d3.timeParse("%Y");
//parse data from into movie->year
movie_dictionnary = new Map()
movie_data.forEach(function(d){
let year = d.year
if(year.includes("/I")){
year =year.slice(0, -2)
}
// if(parseInt(year) <= 2010){
movie_dictionnary.set(d.title,parseInt(year))
// }
})
//create data_f with year, avg female lines and data_m with year, avg male lines
year_count = new Map()
gender_data.forEach(function(d){
if(movie_dictionnary.has(d.Title)){
let year = movie_dictionnary.get(d.Title)
if(!year_count.has(year)){
year_count.set(year, {total_f: 0, total_m:0, count:0})
//console.log(year_count)
}
year_count.get(year)['count']+= 1
year_count.get(year)['total_m']+= parseFloat(d.Male_Percent)
year_count.get(year)['total_f']+= parseFloat(d.Female_Percent)
}else{
//console.log("error: movie not in genre dictionnary: " + d.Title);
}
})
data_m_unbinned = []
data_f_unbinned = []
year_count.forEach(function(v,k){
data_f_unbinned.push({year: parseInt(k), gender: 'f', value: (v['total_f']/v['count'])*100})
data_m_unbinned.push({year: parseInt(k), gender: 'm', value: (v['total_m']/v['count'])*100})
})
//bin year by 5 by 5
const xBin = d3.scaleLinear()
.domain([1934, 2010])
.range([margin.left, width - margin.right])
.nice();
var bin = d3.bin()
.value(d=>d.year)
.domain(xBin.domain())
.thresholds(xBin.ticks(15));
data_m_unbinned = bin(data_m_unbinned)
data_f_unbinned = bin(data_f_unbinned)
data_m = []
data_m_unbinned.forEach(function(d){
let sum = 0
let count = 0
d.forEach(function(v){
sum+= v.value
count+=1
})
data_m.push({year: parseTime(parseInt(d.x1)), value: sum/count})
})
data_f = []
data_f_unbinned.forEach(function(d){
let sum = 0
let count = 0
d.forEach(function(v){
sum+= v.value
count+=1
})
data_f.push({year: parseTime(parseInt(d.x1)), value: sum/count})
})
data_diff = []
for(let i = 0; i < data_f.length;i++){
data_diff.push({year: data_f[i].year, value: (data_f[i].value - data_m[i].value)})
}
const data_male = data_m
const data_female = data_f
const data_difference = data_diff
function update_year(year){
numPoints = ((year-1935)/5)+1
// return [data_male.slice(0, numPoints), data_female.slice(0, numPoints)]
return data_difference.slice(0, numPoints)
}
const tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
//create svg
const svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
`translate(${margin.left}, ${margin.top})`);
//setting x scale
const xScale = d3.scaleTime()
.domain([parseTime(1930), parseTime(2020)])
.range([margin.left, width - margin.right])
//.nice();
//setting y scale
const yScale = d3.scaleLinear()
.domain([-100, 0])
.range([height - margin.bottom, margin.top])
.nice();
//graphs annotations
const annotations = [{},
{},
{note:{label:"Rise of populatity of the woman's film genre during WW2 ", title: "1939-1945"}, x:xScale(parseTime(1942)), y: 10, dx: 0, dy: height-300},
{note:{label:"End of silent films, very few women are included behind the sets of talkies", title: "1950s"}, x:xScale(parseTime(1950)), y: 10, dx: 0, dy: height-100},
{},
{note:{label:"Start of the women's liberation movement", title: "1960s"}, x:xScale(parseTime(1960)), y: 10, dx: 0, dy: height-250},
{},
{},
{note:{label:"Women In Film Founded", title: "1973"}, x:xScale(parseTime(1973)), y: 10, dx: 0, dy: height-80},
{},
{note:{label:"Barbara Streisand is the first woman to win the Golden Globe for directing", title: "1984"}, x:xScale(parseTime(1984)), y: 10, dx: 0, dy: height-250},
{},
{note:{label:"Jeanne Campion is the first woman to win a Palme d'Or at the Cannes Film Festival", title: "1993"}, x:xScale(parseTime(1993)), y: 10, dx: 0, dy: height-100},
{},
{note:{label:" Martha Coolidge is elected as the first female president of the Director Guild of America Awards", title: "2003"}, x:xScale(parseTime(2003)), y: 10, dx: 0, dy: height-300},
{note:{label:"Kathryn Bigelow was the first woman to win an Academy Award for Best Director", title: "2009"}, x:xScale(parseTime(2009)), y: 10, dx: 0, dy: height-100}]
//adding x and y axis
svg.append('g')
.attr('transform', `translate(${margin.left}, 0)`)
.call(d3.axisLeft(yScale).tickFormat(d => d + "%"))
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - 10)
.attr("x", 0 - (height / 2))
.style("text-anchor", "middle")
.text("Male lines - Female line distribution");
svg.append('g')
.attr('transform', `translate(0, ${margin.top})`)
.call(d3.axisBottom(xScale).tickValues([parseTime(1935), parseTime(1940), parseTime(1945), parseTime(1950), parseTime(1955), parseTime(1960), parseTime(1965), parseTime(1970), parseTime(1975), parseTime(1980), parseTime(1985), parseTime(1990), parseTime(1995), parseTime(2000), parseTime(2005), parseTime(2010), parseTime(2015)]))
svg.append("text")
.attr("transform",
"translate(" + (width/2) + " ," +
(0) + ")")
.style("text-anchor", "middle")
.text("Year");
function update(data_d, year){
// console.log(data_m)
// console.log(data_f)
// create line for male
svg.selectAll("circle").remove()
svg.selectAll("path").remove()
svg.selectAll("g").remove()
//adding x and y axis
svg.append('g')
.attr('transform', `translate(${margin.left}, 0)`)
.call(d3.axisLeft(yScale).tickFormat(d => d + "%"))
svg.append('g')
.attr('transform', `translate(0, ${margin.top})`)
.call(d3.axisBottom(xScale).tickValues([parseTime(1935), parseTime(1940), parseTime(1945), parseTime(1950), parseTime(1955), parseTime(1960), parseTime(1965), parseTime(1970), parseTime(1975), parseTime(1980), parseTime(1985), parseTime(1990), parseTime(1995), parseTime(2000), parseTime(2005), parseTime(2010), parseTime(2015)]))
svg.append("path")
.datum(data_d)
.attr("fill", "none")
.attr("stroke", '#dd8c28')
.attr("stroke-width", 1.5)
.attr("d", d3.line()
.x(function(d) { return xScale(d.year) })
.y(function(d) { return yScale(d.value) })
)
//create dot for male
svg
.append("g")
.selectAll("dot")
.data(data_d)
.enter()
.append("circle")
.attr("cx", function(d) { return xScale(d.year) } )
.attr("cy", function(d) { return yScale(d.value) } )
.attr("r", 5)
.attr("fill", '#dd8c28')
.on("mouseover", function (event, d) {
tooltip.transition()
.duration(200)
.style("opacity", .9)
tooltip.html(function () {
return `Year: ${d.year.getFullYear()}<br/> Difference in lines: ${parseInt(d.value)}%`
})
.style("left", (event.pageX) + "px")
.style("top", (event.pageY - 28) + "px");
})
.on("mouseout", function (event, d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
})
var ann_update = []
for(let i = 0; i < ((year-1935)/5)+1; i++){
if(annotations[i]!= {}){
ann_update.push(annotations[i])
}
}
makeAnnotation = d3.annotation().annotations(ann_update)
svg.append("g")
.attr('class', 'annotation-group')
.call(makeAnnotation)
svg.selectAll(".connector").style("opacity", 0.4).style("stroke-dasharray", ("3, 3"))
d3.select('.annotation-group')
}
data_d = update_year(1935)
// data_m = t[0]
// data_f = t[1]
// create line for male
svg.append("path")
.datum(data_d)
.attr("fill", "none")
.attr("stroke", '#dd8c28')
.attr("stroke-width", 1.5)
.attr("d", d3.line()
.x(function(d) { return xScale(d.year) })
.y(function(d) { return yScale(d.value) })
)
//create dot for male
svg
.append("g")
.selectAll("dot")
.data(data_d)
.enter()
.append("circle")
.attr("cx", function(d) { return xScale(d.year) } )
.attr("cy", function(d) { return yScale(d.value) } )
.attr("r", 5)
.attr("fill", '#dd8c28')
.on("mouseover", function (event, d) {
tooltip.transition()
.duration(200)
.style("opacity", .9)
tooltip.html(function () {
return `Year: ${d.year.getFullYear()}<br/> Difference in lines: ${parseInt(d.value)}%`
})
.style("left", (event.pageX) + "px")
.style("top", (event.pageY - 28) + "px");
})
.on("mouseout", function (event, d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
})
});
});
</script>
</body>
<!-- This adapted from Arvind's observable from lecture. https://observablehq.com/d/4c93c3a516d35624 -->
<!-- Great D3 intro resource: https://observablehq.com/@d3/learn-d3?collection=@d3/learn-d3 -->
</html>