-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogue-graph.html
363 lines (316 loc) · 13.7 KB
/
dialogue-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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<html>
<head>
<!-- Load d3.js -->
<link rel="stylesheet" href="web.css">
<script src="https://d3js.org/d3.v6.js"></script>
</head>
<div >
<body>
<h2> Female Character Lines Broken Down by Movies</h2>
<p style="width: 80%">Each circle represents a movie with the colors representing the gender with more dominant lines in the movie. Hover, on the circles to see what movie it is and the percentage breakdown.</p>
<div class="tabs" id="tabs" style="margin: auto;">
<button class="tablinks" id ="adventure">Adventure</button>
<button class="tablinks" id ="action">Action</button>
<button class="tablinks" id ="drama">Drama</button>
<button class="tablinks" id ="comedy">Comedy</button>
<button class="tablinks" id ="romance">Romance</button>
<button class="tablinks" id ="thriller">Thriller</button>
<button class="tablinks" id ="all">All Genres</button>
<div style="font-weight: 400; display:flex" >
<span class="dot" style = "background-color:#619ba5; "></span>
<span style="margin-top: 7;">Female</span>
<span class="dot" style = "background-color:#f5d6b0;"> </span>
<span style="margin-top: 7;">Male</span>
</div>
</div>
<div class="loader" id="loader1" style="margin-top: 120;"></div>
</div>
<!-- <div id="chart" style="width:420px;height:420px;"> -->
<!-- This is where we will put our chart. -->
<!-- </div> -->
<!-- <div style = "float: right;">
<h2> Average lines by gender for each credit's position</h2>
<p style="width: 700px">In movies, the higher the credit's position of an actor the higher the pay is (1 being high - 8 being low). This visualization depicts what percentage of lines a male/female character has given their credit's position in the film. Clicking on a genre will also change this graph to reflect that selected genre.</p>
<div class="loader" id="loader2" style="margin-top: 120;"></div>
<div id="chart" style="width:420px;height:420px;">
</div>
</div> -->
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
<script>
function doneLoading() {
document.getElementById("loader1").style.display = "none";
// document.getElementById("loader2").style.display = "none";
};
d3.csv("data_cleaned_merged/movie_title_metadata.csv").then((movie_data) =>{d3.csv("data_cleaned_merged/line_percents_by_gender.csv").then((data) => { d3.csv("data_cleaned_merged/line_percents_by_character.csv").then((data_position) => {
movie_dictionnary = new Map()
movie_data.forEach(function(d){
genres = d.genres.slice(1, -1)
genres = new Set(genres.split("'"))
movie_dictionnary.set(d.title,genres)
})
data.forEach(function(d){
if(movie_dictionnary.has(d.Title)){
let g = ""
movie_dictionnary.get(d.Title).forEach(function(m){
g+= m
if(m.length >= 2){
g+= ", "
}
})
g = g.slice(0,-2)
d.genre =g
}
})
function filterDataPos(all_data){
data_know_gender = all_data.filter(d => {
if ((d.Gender === 'm' || d.Gender === 'f') && parseInt(d.Position_in_Credits) <= 10){
return d}
})
data_pos = []
count = []
for(let i =1; i<=10;i++){
let temp_count = {male_count:0, female_count:0, male_sum:0, female_sum:0}
let temp = {position:i, male_lines_avg:0, female_lines_avg:0}
data_pos.push(temp)
count.push(temp_count)
}
data_know_gender.forEach(function(d, index) {
if(d.Gender === 'm'){
count[parseInt(d.Position_in_Credits)-1].male_count++
count[parseInt(d.Position_in_Credits)-1].male_sum += parseFloat(d.Character_Line_Percent)
}
if(d.Gender === 'f'){
count[parseInt(d.Position_in_Credits)-1].female_count++
count[parseInt(d.Position_in_Credits)-1].female_sum += parseFloat(d.Character_Line_Percent)
}
})
for(let i = 0; i < 10; i++){
data_pos[i].male_lines_avg = (count[i].male_sum/count[i].male_count)*100
data_pos[i].female_lines_avg = (count[i].female_sum/count[i].female_count)*100
}
return data_pos
}
const all_data = data
const all_data_pos = data_position
data_pos = filterDataPos(all_data_pos)
document.getElementById("all").classList.toggle("active");
//genre tabbing
document.getElementById("action").onclick = function(){selectGenre("action")};
document.getElementById("adventure").onclick = function(){selectGenre("adventure")};
document.getElementById("drama").onclick = function(){selectGenre("drama")};
document.getElementById("comedy").onclick = function(){selectGenre("comedy")};
document.getElementById("romance").onclick = function(){selectGenre("romance")};
// Experimenting with other genres
document.getElementById("thriller").onclick = function(){selectGenre("thriller")};
document.getElementById("all").onclick = function(){selectGenre("all")};
window.addEventListener('storage', function(e) {
if(e.key == "genreCredits"){
selectGenre(sessionStorage["genreCredits"]);
}
});
function selectGenre(g){
document.getElementById("action").classList.remove("active");
document.getElementById("adventure").classList.remove("active");
document.getElementById("drama").classList.remove("active");
document.getElementById("comedy").classList.remove("active");
document.getElementById("romance").classList.remove("active");
// Experimenting with other genres
document.getElementById("thriller").classList.remove("active");
document.getElementById("all").classList.remove("active");
document.getElementById(g).classList.toggle("active");
sessionStorage["genreLine"] = g;
if(g == "all"){
data = all_data
}else{
data = all_data.filter(d=>{
if(movie_dictionnary.has(d.Title)){
if(movie_dictionnary.get(d.Title).has(g)){
return d;
}
}else{
// console.log("error: movie not in genre dictionnary: " + d.Title);
}
})
// data_position = all_data_pos.filter(d=>{
// if(movie_dictionnary.has(d.Title)){
// if(movie_dictionnary.get(d.Title).has(g)){
// return d;
// }
// }else{
// console.log("error: movie not in genre dictionnary: " + d.Title);
// }
// })
}
// data_pos = filterDataPos(data_position)
update();
// updateDataPos();
}
// SVG setup
const margin = {top: 10, right: 10, bottom: 50, left: 10},
width = 1400 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
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})`);
// Setup scales
const x = d3.scaleLinear()
.domain([0.0, 100.0])
.range([margin.left, width - margin.right])
.nice();
const nbins = 100;
const tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
const t = d3.transition()
.duration(1000);
var color = d3.scaleLinear().domain([1,100]).range(["#f5d6b0", "#619ba5"])
// Function for graph
function update(){
//histogram binning
const histogram = d3.histogram()
.domain(x.domain())
.thresholds(x.ticks(nbins))
.value(function(d) { return (d.Female_Percent)*100;} )
//binning data and filtering out empty bins
const bins = histogram(data).filter(d => d.length>0)
//g container for each bin
let binContainer = svg.selectAll(".gBin")
.data(bins);
binContainer.exit().remove()
let binContainerEnter = binContainer.enter()
.append("g")
.attr("class", "gBin")
.attr("transform", d => `translate(${x(d.x0)}, ${height})`)
//need to populate the bin containers with data the first time
binContainerEnter.selectAll("circle")
.data(d => d.map((p, i) => {
return {idx: i,
name: p.Title,
value_f: parseInt((p.Female_Percent)*100),
value: parseInt((p.Male_Percent)*100),
radius: ((x(d.x1)-x(d.x0))/2),
genre: p.genre
}
}))
.enter()
.append("circle")
.attr("fill", function(d){
return color(d.value_f)
})
.attr("class", "enter")
.attr("cx", 0) //g element already at correct x pos
.attr("cy", function(d) {
return - d.idx * 2 * d.radius - d.radius; })
.attr("r", 0)
.on("click", function(d){
sessionStorage["movie"] = d.target.__data__.name
})
.on("mouseover", tooltipOn)
.on("mouseout", tooltipOff)
.transition()
.duration(500)
.attr("r", function(d) {
return (d.length==0) ? 0 : d.radius; })
binContainerEnter.merge(binContainer)
.attr("transform", d => `translate(${x(d.x0)}, ${height})`)
//enter/update/exit for circles, inside each container
let dots = binContainer.selectAll("circle")
.data(d => d.map((p, i) => {
return {idx: i,
name: p.Title,
value_f: parseInt((p.Female_Percent)*100),
value: parseInt((p.Male_Percent)*100),
radius: (x(d.x1)-x(d.x0))/2,
genre: p.genre
}
}))
// //EXIT old elements not present in data
dots.exit()
.attr("class", "exit")
.transition()
.duration(1000)
.attr("r", 0)
.remove();
//UPDATE old elements present in new data.
dots.attr("class", "update");
// ENTER new elements present in new data.
dots.enter()
.append("circle")
.attr("class", "enter")
.attr("fill", function(d){
return color(d.value_f)
})
.attr("cx", 0) //g element already at correct x pos
.attr("cy", function(d) {
return - d.idx * 2 * d.radius - d.radius; })
.attr("r", 0)
.merge(dots)
.on("click", function(d){
sessionStorage["movie"] = d.target.__data__.name
})
.on("mouseover", tooltipOn)
.on("mouseout", tooltipOff)
.transition()
.duration(500)
.attr("r", function(d) {
return (d.length==0) ? 0 : d.radius; })
};
function tooltipOn(d) {
//x position of parent g element
let gParent = d3.select(this.parentElement)
let translateValue = gParent.attr("transform")
let gX = translateValue.split(",")[0].split("(")[1]
let gY = height + (+d3.select(this).attr("cy")-50)
d3.select(this)
.classed("selected", true)
// .attr("r", d=>{
// return d.radius*1.5
// })
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(d.target.__data__.name + "<br/> Male Lines: " + d.target.__data__.value + "% <br/> Female Lines: " + d.target.__data__.value_f+"% <br/> Genres: " +d.target.__data__.genre)
.style("left", gX + "px")
.style("top", gY + "px");
}//tooltipOn
function tooltipOff(d) {
d3.select(this)
.classed("selected", false)
// .attr("r", d=>{
// return d.radius
// })
tooltip.transition()
.duration(500)
.style("opacity", 0);
}//tooltipOff
svg.append("g")
.style("font-size", "15")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).tickFormat(d => d + "%"));
svg.append("text")
.attr("font-size", 15)
.attr("font-weight", 300)
.attr("transform", "translate(" + (width/2) + " ," +
(height+margin.bottom-5) + ")")
.style("text-anchor", "middle")
.text("Female Line Percentages by Movie");
update();
doneLoading();
});
});
});
</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>