-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
215 lines (196 loc) · 6.71 KB
/
index.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Movies critics liked, audience didn't!</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.js"></script>
<link rel="stylesheet" href="style.css">
<script src="label.js"></script>
</head>
<body>
<div id="root">
<h3>Movies Critics Loved,But Audience Really Didn't</h3>
<h5>% gap between audience & critics 'rotten tomatoes' score</h5>
</div>
<p>
<span>Comedy </span>
<span>Sci-fi / Fantasy </span>
<span>Action/Adventure </span>
<span>Drama </span>
<span>Thriller </span>
<span>Horror</span>
</p>
<script>
let groupYear = {}, sign = 1,flag = 1.5,
labels, links, labelArray = [], anchorArray = [],
margin = {top:20,right:20,bottom:20,left:40},
width = 700 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom,
xExtent,yExtent,xScale,yScale,rScale;
//Redraw label and lines with transition
function redrawLabels(){
labels
.transition()
.duration(1500)
.attr("x",(d) => d.x)
.attr("y",(d) => d.y);
links
.transition()
.duration(1500)
.attr("x2",(d) => d.x)
.attr("y2",(d) => d.y);
}
//Initialize and append svg to body
let svg = d3.select('#root')
.append('svg')
.attr('width',width)
.attr('height',height);
// Draw boundaries of figure
var plot_boundary = svg.append("rect")
.attr("x", 0.0)
.attr("y", 0.0)
.attr("width", width)
.attr("height", height)
.style("fill-opacity", '0.0')
.style("stroke", "black")
.style("stroke-opacity", "0.4");
d3.csv('test.csv',data => {
//Sort the csv rows by years and then by %gap
data.sort(function(a,b){
if (a['Year'] === b['Year']){
return a['% gap'] - b['% gap'];
}else{
return a['Year'] - b['Year'];
}
});
//Define scale for x axis
xExtent = d3.extent(data,d => +d['% gap']);
xScale = d3.scaleLinear()
.domain([xExtent[0] - 3,xExtent[1] + 3]) //input
.range([margin.left,width - margin.right]); //output
//Define scale for y axis
yExtent = d3.extent(data,d => +d['Year']);
yScale = d3.scaleLinear()
.domain([yExtent[0] - 1,yExtent[1] + 1])
.range([height - margin.bottom, margin.top]);
//Define radius scale
rScale = d3.scaleLinear()
.domain([0,d3.max(data,d => +d['Budget ($million)'])])
.range([5,25]);
//Define color scale
let colorScale = d3.scaleOrdinal()
.domain(['comedy','sci-fi','fantasy','action','adventure','drama','thriller','horror'])
.range(['#F9D248','#A7F47B','#A7F47B','#EA963E','#EA963E','#62D9F4','#C992E0','#999999']);
//Initialize xAxis
let formatPercent = d3.format('.0%');
let xAxis = d3.axisBottom()
.scale(xScale)
.tickFormat(d => formatPercent(d/100))
.tickValues([0,15,30,45]);
//Initialize yAxis
let yAxis = () => d3.axisLeft()
.scale(yScale);
//Append xAxis to svg
svg.append('g')
.attr('transform','translate(' + [0,height - margin.bottom] + ')')
.attr('class','xaxis')
.call(xAxis);
//Append yAxis to svg
svg.append('g')
.attr('transform','translate(' + [margin.left,0] + ')')
.attr('class','yaxis')
.call(yAxis()
.tickSize(-width) // for grid lines
.tickFormat(d => d));
//Append circle to group
let circle = svg
.selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr('cx',(d,i) => {
if (!groupYear.hasOwnProperty(d['Year'])){
groupYear[d['Year']] = [];
}
return xScale(+d['% gap']);
})
.attr('cy',(d) => {
groupYear[d['Year']] = [...groupYear[d['Year']],{
cx: xScale(+d['% gap']),
cy: yScale(d['Year']),
r: rScale(+d['Budget ($million)'])
}];
return yScale(d['Year']);
})
.attr('r',(d) => {
labelArray.push({x: xScale(+d['% gap']), y: yScale(d['Year']), name: d['Film'], width: 0.0, height: 0.0});
anchorArray.push({x: xScale(+d['% gap']), y: yScale(d['Year']), r: rScale(+d['Budget ($million)'])});
return rScale(+d['Budget ($million)']);
})
.attr('stroke','white')
.attr('fill',(d) => {
let genre = d['Genre'].toLowerCase();
genre = genre.includes(',') ? genre.slice(0,genre.indexOf(',')) : genre;
return colorScale(genre);
})
.attr('fill-opacity',(d) => {
let count = 0,opacity = 1;
for (let i=0;i<groupYear[d['Year']].length;i++){
if (groupYear[d['Year']][i].cx === xScale(+d['% gap'])){
count += 1;
}
}
if (count > 2){
opacity = 0.3;
}else if (count === 2){
opacity = 0.5;
}
return opacity;
});
//Add labels
labels = svg
.selectAll('.label')
.data(labelArray)
.enter()
.append('text')
.text((d) => d.name)
.attr('font-family','sans-serif')
.attr('x',(d,i) => {
return d.x;
})
.attr('y',(d,i) => {
return d.y;
})
.attr('text-anchor','start')
.attr('font-size',6);
//Add height and width of label to array
var index = 0;
labels.each(function() {
labelArray[index].width = this.getBBox().width;
labelArray[index].height = this.getBBox().height;
index += 1;
});
//Add links connecting label and circle
links = svg.selectAll(".link")
.data(labelArray)
.enter()
.append("line")
.attr("class", "link")
.attr("x1", (d) => d.x)
.attr("y1", (d) => d.y)
.attr("x2", (d) => d.x)
.attr("y2", (d) => d.y)
.attr("stroke-width", 0.2)
.attr("stroke", "#6f6f6f");
//Remove overlaps
d3.labeler()
.label(labelArray)
.anchor(anchorArray)
.width(width)
.height(height)
.start(2000);
redrawLabels();
});
</script>
</body>
</html>