-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
141 lines (128 loc) · 4 KB
/
script.js
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
document.addEventListener('DOMContentLoaded', function() {
const movieTitleElement = document.getElementById('movieTitle');
const ratingSlider = document.getElementById('ratingSlider');
const submitBtn = document.getElementById('submitBtn');
const finalSubmitBtn = document.getElementById('finalSubmitBtn');
const notesInput = document.getElementById('notesInput');
const ratingsTableBody = document.getElementById('ratingsTableBody');
const ratingGraph = document.getElementById('ratingGraph');
const chartCanvas = document.getElementById('chart');
const movieTitle = "Movie Title";
let ratings = [];
// Set movie title
movieTitleElement.textContent = movieTitle;
// Generate rating segments
for (let i = 1; i <= 5; i++) {
const starDiv = document.createElement('div');
starDiv.classList.add('star');
starDiv.textContent = i;
starDiv.addEventListener('click', function() {
setRating(i);
});
ratingSlider.querySelector('.stars').appendChild(starDiv);
if (i < 5) {
const notchDiv = document.createElement('div');
notchDiv.classList.add('notch');
ratingSlider.querySelector('.rating-bar').appendChild(notchDiv);
}
}
// Set the active rating based on user selection
function setRating(rating) {
const stars = ratingSlider.querySelectorAll('.star');
stars.forEach((star, index) => {
if (index < rating) {
star.classList.add('active');
} else {
star.classList.remove('active');
}
});
submitBtn.disabled = false;
}
// Handle submit button click
submitBtn.addEventListener('click', function() {
const rating = getActiveRating();
const timestamp = new Date().toLocaleTimeString();
const notes = notesInput.value;
// Display rating in the table
const row = ratingsTableBody.insertRow();
const reviewNumberCell = row.insertCell();
const timestampCell = row.insertCell();
const ratingCell = row.insertCell();
const notesCell = row.insertCell();
reviewNumberCell.textContent = ratingsTableBody.rows.length;
timestampCell.textContent = timestamp;
ratingCell.textContent = rating;
notesCell.textContent = notes;
// Clear notes and disable submit button
notesInput.value = '';
submitBtn.disabled = true;
// Add rating to the ratings array
ratings.push({
rating: rating,
timestamp: timestamp,
notes: notes
});
});
// Handle final submit button click
finalSubmitBtn.addEventListener('click', function() {
if (ratings.length > 0) {
ratingGraph.style.display = 'block';
displayRatingGraph();
}
});
// Get the active rating from the slider
function getActiveRating() {
const stars = ratingSlider.querySelectorAll('.star');
let activeRating = 0;
stars.forEach((star, index) => {
if (star.classList.contains('active')) {
activeRating = index + 1;
}
});
return activeRating;
}
// Display the rating graph
function displayRatingGraph() {
const ratingData = ratings.map((rating, index) => ({
x: index + 1,
y: rating.rating
}));
// Create the chart
new Chart(chartCanvas, {
type: 'line',
data: {
datasets: [{
label: 'Rating',
data: ratingData,
borderColor: '#007bff',
fill: false
}]
},
options: {
scales: {
x: {
type: 'linear',
position: 'bottom',
title: {
display: true,
text: 'Review Number'
}
},
y: {
type: 'linear',
position: 'left',
title: {
display: true,
text: 'Rating'
},
suggestedMin: 1,
suggestedMax: 5,
ticks: {
stepSize: 1
}
}
}
}
});
}
});