-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovies.jsx
More file actions
301 lines (257 loc) · 8.79 KB
/
movies.jsx
File metadata and controls
301 lines (257 loc) · 8.79 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
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
import * as d3 from "d3";
import _ from 'lodash';
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
const margin = {top: 80, right: 70, bottom: 80, left: 90};
const width = 1000 - margin.left - margin.right,
height = 550 - margin.top - margin.bottom;
const container = d3.select("body").append("div").attr("id", "container");
const svg = d3.select("#container")
.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 + ")");
const tooltipAll = d3.select("body")
.append("div")
.attr("id", "tooltipAll");
const tooltip = d3.select("#tooltipAll")
.append("div")
.attr("id", "tooltip");
const tooltip2 = d3.select("#tooltipAll")
.append("div")
.attr("id", "tooltip2");
const tooltip3 = d3.select("#tooltipAll")
.append("div")
.attr("id", "tooltip3");
const tooltip4 = d3.select("#tooltipAll")
.append("div")
.attr("id", "tooltip4");
// set the ranges
var x = d3.scaleLinear().rangeRound([0, width]);
var y = d3.scaleLinear().rangeRound([0, height]);
// get data from csv
let AllMovieInfo = [];
d3.queue()
.defer(d3.csv, "data/imdb250.csv")
.defer(d3.csv, "data/boxoffice.csv")
.defer(d3.csv, "data/oscars.csv")
.await(ready);
// once data is fetched, then do stuff
function ready(error, imdb, giants, oscars){
let movies = imdb;
if (error) throw error;
let oldestYear = 1960;
movies.forEach(function(d){
d.Year = Number(d.Year);
d.Budget = Number(d.Budget);
d.WWBO = Number(d.WWBO);
d.DomBO = Number(d.DomBO);
d.ForBO = Number(d.ForBO);
// Profit and RoI
d.WWProfit = (d.WWBO - d.Budget);
d.ForProfit = (d.ForBO - d.Budget);
d.PerFRevenue = (d.ForBO / d.WWBO) * 100;
d.RoI = (d.WWProfit) / (d.Budget);
d.FRoI = (d.ForBO - d.Budget) / (d.Budget);
d.PerFRoI = d.FRoI / (d.RoI) * 100;
d.AYear = (d.Year - oldestYear) / (60/width);
});
// Scale the data's range
let maxY = 80;
// let maxY = d3.max(movies, function(d) { return d.PerFRevenue; });
x.domain([1962, 2017]);
y.domain([80, 0]);
// positioning tooltips
let logscale = 10000000;
let tooltipLeft = (d) => 330 + margin.left + d.AYear;
let tooltipTop = (d) => 50 + margin.top + height - (height * d.PerFRevenue/maxY);
let spaceTop = 28;
let tooltipTop2 = (d) => spaceTop + tooltipTop(d);
let tooltipTop3 = (d) => spaceTop + tooltipTop2(d);
let tooltipTop4 = (d) => -5 + spaceTop + tooltipTop3(d);
// color function
let color1 = d3.scaleLinear().domain([1960, 2017]).range(["red", "blue"]);
// Making the scatterplot
// Makes circles per movie and adds the hover over effect
svg.selectAll("circle")
.data(movies)
.enter()
.append("circle")
.attr("r", function(d) { return(Math.log(d.WWBO/logscale) * 4); })
.attr("cx", function(d) { return (d.AYear); })
.attr("cy", function(d) { return height - (d.PerFRevenue/maxY * height); })
.style('fill', (d) => color1(d.Year))
.style("fill-opacity", 0.5)
.on('mouseover', (d) => {
tooltip.transition()
.duration(100)
.style("z-index", 5)
.style('opacity', .9);
tooltip2.transition()
.duration(100)
.style("z-index", 5)
.style('opacity', .9);
tooltip3.transition()
.duration(100)
.style("z-index", 5)
.style('opacity', .9);
tooltip4.transition()
.duration(100)
.style("z-index", 5)
.style('opacity', .9);
// Hover over tool tip positioning and styling
// Rephrase numbers into words
let tooltip2Data = function(e) {
if (e > 1000000000 ) return `${round(e/1000000000, 2)} B`;
else if (e > 1000000) return `${round(e/1000000, 2)} mil`;
else return `${round(e/1000, 2)} Thousand`;
};
tooltip.text(`${d.Movie} (${d.Year})`)
.style("width", "100")
.style("position", "absolute")
.style('left', `${tooltipLeft(d)}px`)
.style('top', `${tooltipTop(d)}px`)
.style("padding", "5px")
.style("font-weight", "700")
.style("font-size", "20px")
.style("text-decoration", "underline")
.style("background", "white");
tooltip2.text(`Foreign BO: $${tooltip2Data(d.ForBO)}`)
.style("min-weight", "200px")
.style("padding", "5px")
.style("position", "absolute")
.style('left', `${tooltipLeft(d)}px`)
.style('top', `${tooltipTop2(d) + 3}px`)
.style("background", "white");
tooltip3.text(`Total BO: $${tooltip2Data(d.WWBO)}`)
.style("padding", "5px")
.style("position", "absolute")
.style('left', `${tooltipLeft(d)}px`)
.style('top', `${tooltipTop3(d)}px`)
.style("background", "white");
tooltip4.text(`Percent from Foreign: `)
.style("padding", "5px")
.style("position", "absolute")
.style('left', `${tooltipLeft(d)}px`)
.style('top', `${tooltipTop4(d)}px`)
.style("background", "white")
.append("text")
.text(`${round(d.PerFRevenue,2)}%`)
.style("font-weight", "700")
.style("font-size", "20px");
})
.on('mouseout', () => {
tooltip.transition()
.duration(400)
.style("z-index", -2)
.style('opacity', 0);
tooltip2.transition()
.style("z-index", -2)
.duration(400)
.style('opacity', 0);
tooltip3.transition()
.style("z-index", -2)
.duration(400)
.style('opacity', 0);
tooltip4.transition()
.style("z-index", -2)
.duration(400)
.style('opacity', 0);
});
// Add the X Axis
svg.append("g")
.attr("class", "xAxis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.append("text")
.attr("fill", "black")
.attr("text-anchor", "start")
.attr("x", "1em")
.attr("y", "2.5em")
.style("font-size", "18px")
.style("font-family", "Roboto")
.text("Year Released");
// Add the Y Axis
svg.append("g")
.attr("class", "axisLeft")
.call(d3.axisLeft(y).ticks(10, "s"));
// Spacing out the Y Axis Label
let xAxisTopText = -55;
let yAxisTopText1 = -55;
let yAxisTopText2 = yAxisTopText1 + 15;
let yAxisTopText3 = yAxisTopText2 + 15;
svg.append("g")
.attr("class", "text1")
.append("text")
.text("%")
.attr("class", "yAxisText1")
.attr("x", `${xAxisTopText}`)
.attr("y", `${yAxisTopText1}px`)
.attr("stroke");
svg.append("g")
.attr("class", "text2")
.append("text")
.text("Gross Box Office")
.attr("class", "yAxisText2")
.attr("x", `${xAxisTopText}`)
.attr("y", `${yAxisTopText2}px`)
.attr("stroke");
svg.append("g")
.attr("class", "text3")
.append("text")
.text("from Foreign Markets")
.attr("class", "yAxisText3")
.attr("x", `${xAxisTopText}`)
.attr("y", `${yAxisTopText3}px`);
// If a different movie subgroup is selected, change the movies to reflect the change.
d3.selectAll("input").on("change", function(){
movies = {};
let result = d3.select("input[type=radio]:checked").node().value;
if (result === "oscars") {movies = oscars;}
else if (result === "giants") {movies = giants;}
else {movies = imdb;}
movies.forEach(function(d){
d.Year = Number(d.Year);
d.Budget = Number(d.Budget);
d.WWBO = Number(d.WWBO);
d.DomBO = Number(d.DomBO);
d.ForBO = Number(d.ForBO);
// Profit and RoI
d.WWProfit = (d.WWBO - d.Budget);
d.ForProfit = (d.ForBO - d.Budget);
d.PerFRevenue = (d.ForBO / d.WWBO) * 100;
d.RoI = (d.WWProfit) / (d.Budget);
d.FRoI = (d.ForBO - d.Budget) / (d.Budget);
d.PerFRoI = d.FRoI / (d.RoI) * 100;
d.AYear = (d.Year - oldestYear) / ((2017 - oldestYear)/width);
// Done to deal with difference in list length and extra circles.
if (d.Movie === ""){
d.Year = 2030;
d.WWBO = logscale;
d.DomBO = 0;
d.ForBO = 0;
d.PerFRevenue = 0;
d.AYear = -5;
}
});
// Adjust color to make data pop more depending on the selection
let minX = d3.min(movies, function(d) { return d.Year; });
if (minX < 1960){ minX = 1960;}
color1 = d3.scaleLinear().domain([minX, 2017]).range(["red", "blue"]);
svg.selectAll("circle")
.data(movies)
.transition()
.duration(1000)
.delay(function(d, i){return i / movies.length * 500;})
.attr("r", function(d) {
return(Math.log(d.WWBO/logscale) * 4); })
.attr("cx", function(d) {
return d.AYear; })
.attr("cy", function(d) {
return height - (d.PerFRevenue/maxY * height); })
.style('fill', (d) => color1(d.Year));
});
}