-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratings-breakdown.html
146 lines (130 loc) · 4.23 KB
/
ratings-breakdown.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
<html>
<head>
<!-- Load d3.js -->
<link rel="stylesheet" href="web.css">
<script src="https://d3js.org/d3.v6.js"></script>
</head>
<body>
<div class="ratings-container">
<h2>Rating Breakdown</h2>
<p style="width: 500px">Percentage breakdown of users' ratings values. Hover over bars to see the number of votes.</p>
<div id="chart" style="width:500px;height:400px; float:left;"></div>
</div>
<style>
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
<script>
d3.csv("data_cleaned_merged/imdb_with_metadata.csv").then((movie_data) => {
let wanted_movie = sessionStorage["movie_rating"];
let single_movie = ""
for(let i = 0; i < movie_data.length; i++){
if(wanted_movie === movie_data[i]["title"]){
single_movie = movie_data[i]
break
}
}
if ( single_movie !== ""){
let total_votes = parseInt(single_movie["total_votes"])
let data = []
for(let i = 10; i > 0; i--){
let votes = parseInt(single_movie[`votes_${i}`]);
let p = (votes/total_votes)*100;
data.push({value: i, percent: p, votes: votes})
}
const tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// GRAPH SET UP
// GRAPH: OVERALL RATINGS
const height = 400;
const width = 500;
const margin = ({ top: 20, right: 50, bottom: 50, left: 60 });
var color = '#dddbd0';
var svg = d3.create('svg')
.attr('width', width)
.attr('height', height);
var y = d3.scaleBand()
.domain(d3.range(data.length))
.rangeRound([margin.top, height - margin.bottom])
.padding(0.1)
var x = d3.scaleLinear()
.domain([0, d3.max(data, d => d.percent)])
.range([margin.left, width - margin.right])
//x-axis
svg.append("text")
.attr("y", 15)
.attr("x", width/2 )
.style("text-anchor", "middle")
.text("Percentage breakdown of users ratings");
// //7. Drawing our y-axis
var yAxis = svg.append('g')
.attr("transform", `translate(${margin.left},0)`)
.attr("font-family", "Avenir Next")
.attr("font-size", 20)
.call(d3.axisLeft(y).tickFormat(i => data[i].value).tickSizeOuter(0))
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 5)
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.attr("font-size", 15)
.style("text-anchor", "middle")
.text("Rating");
svg.append("g")
.attr("fill", color)
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", x(0))
.attr("y", (d, i) => y(i))
.attr("width", 0)
.attr("height", y.bandwidth())
.on("mouseover", function (event, d) {
tooltip.transition()
.duration(200)
.style("opacity", .9)
tooltip.html(`Number of votes: ${d.votes}`)
.style("left", (event.pageX) + "px")
.style("top", (event.pageY - 28) + "px");
})
.on("mouseout", function (event, d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
})
.transition().duration(1000)
.attr("x", x(0))
.attr("y", (d, i) => y(i))
.attr("width", d => x(d.percent) - x(0))
.attr("height", y.bandwidth())
svg.append("g")
.attr("fill", "black")
.attr("text-anchor", "start")
.attr("font-family", "Avenir Next")
.attr("font-size", 12)
.selectAll("text")
.data(data)
.join("text")
.attr("x", d => x(0))//x(d.percent))
.attr("y", (d, i) => y(i) + y.bandwidth() / 2)
.attr("dy", "0.35em")
.attr("dx", +4)
.text(d => `${d.percent.toFixed(1)}%`)
.transition().duration(1000)
.attr("x", d => x(d.percent))
.attr("y", (d, i) => y(i) + y.bandwidth() / 2)
.attr("dy", "0.35em")
.attr("dx", +4)
document.getElementById("chart").appendChild(svg.node());}
});
</script>
</body>
</html>