-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
354 lines (317 loc) · 13.2 KB
/
Copy pathindex.html
File metadata and controls
354 lines (317 loc) · 13.2 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
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
<!DOCTYPE html>
<html lang="English">
<head>
<title>Interactive Dashboard</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #f0f0f0;
}
#header {
background-color: #2A323D;
color: white;
text-align: left;
padding: 25px;
display: flex;
align-items: center;
font-family: "Articulat", sans-serif;
}
.comment-section {
margin-left: 20px;
margin-top: 10px;
}
.chart-container {
border: 1px solid #ddd;
margin: 10px;
padding: 20px;
display: flex;
flex-direction: column;
background-color: white;
transition: width 0.5s;
width: 100%;
box-sizing: border-box;
}
.comment-container {
border: 1px solid #ddd;
margin-top: 20px;
margin-left: 20px;
padding: 20px;
background-color: white;
width: 100%;
box-sizing: border-box;
transition: width 0.5s;
}
#title {
transform-origin: left top;
white-space: nowrap;
font-size: 32px;
margin: 0;
padding: 0;
}
#addChartButton {
text-align: center;
padding: 20px;
cursor: pointer;
}
#resetChartButton {
text-align: center;
padding: 20px;
cursor: pointer;
display: none;
}
.axis-selection {
display: flex;
justify-content: center;
margin-bottom: 10px;
}
.chart {
height: calc(100vh - 350px); /* Adjust based on your header and button sizes */
}
#chartParentContainer {
width: 100%;
height: calc(100vh - 350px); /* Adjust based on your header and button sizes */
display: flex;
flex-direction: row;
}
</style>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="header">
<h1 id="title">COVID-19 DATA DASHBOARD</h1>
</div>
<div id="resetChartButton">
<button>Reset Chart</button>
</div>
<div id="addChartButton">
<button onclick="addChart()">Add Chart</button>
</div>
<div id="chartParentContainer">
<div class="axis-controls" id="axisControls1"></div>
<div class="axis-controls" id="axisControls2"></div>
</div>
<script>
let chartCounter = 0;
let firstChartAdded = false;
let secondChartAdded = false;
function populatePredefinedOptions(select) {
const predefinedOptions = [
{id: '1', name: "Global Cases Weekly (ECDC)"},
{id: '2', name: "Global Deaths Weekly (ECDC)"},
{id: '3', name: "Global Deaths vs. Cases (ECDC)"},
{id: '4', name: "Positive Test Results (CDC)"},
{id: '5', name: "Negative Test Results (CDC)"},
{id: '6', name: "COVID Deaths by GDP per capita"},
];
select.innerHTML = '';
predefinedOptions.forEach(option => {
const optionElement = document.createElement('option');
optionElement.value = option.id;
optionElement.textContent = option.name;
select.appendChild(optionElement);
});
}
function addChart() {
chartCounter++;
const { newChartContainer, commentContainer } = createChartContainer(chartCounter);
const chartParentContainer = document.getElementById('chartParentContainer');
if (!firstChartAdded) {
firstChartAdded = true;
chartParentContainer.appendChild(newChartContainer);
newChartContainer.appendChild(commentContainer);
} else if (!secondChartAdded) {
secondChartAdded = true;
const firstChartContainer = document.querySelector('.chart-container:nth-child(3)');
if (firstChartContainer) {
firstChartContainer.style.width = '50%';
const oldChart = firstChartContainer.querySelector('.chart');
if (oldChart) {
oldChart.innerHTML = '';
}
const oldCommentSection = firstChartContainer.querySelector('.comment-section');
if (oldCommentSection) {
oldCommentSection.remove();
}
const firstCommentContainer = firstChartContainer.querySelector('.comment-container');
if (firstCommentContainer) {
firstCommentContainer.style.display = 'none';
firstCommentContainer.innerHTML = '';
}
}
chartParentContainer.appendChild(newChartContainer);
newChartContainer.appendChild(commentContainer);
}
if (firstChartAdded && secondChartAdded) {
const addButton = document.getElementById('addChartButton');
addButton.style.display = 'none';
const resetButton = document.getElementById('resetChartButton');
resetButton.style.display = 'block';
resetButton.onclick = removeChart;
}
}
function removeChart() {
const chartParentContainer = document.getElementById('chartParentContainer');
const secondChartContainer = document.querySelector('.chart-container:nth-child(4)');
const firstChartContainer = document.querySelector('.chart-container:nth-child(3)');
chartParentContainer.removeChild(secondChartContainer);
chartParentContainer.removeChild(firstChartContainer);
secondChartAdded = false;
firstChartAdded = false;
const resetButton = document.getElementById('resetChartButton');
resetButton.style.display = 'none';
const addButton = document.getElementById('addChartButton');
addButton.style.display = 'block';
const commentSections = document.querySelectorAll('.comment-section');
commentSections.forEach(section => {
section.parentNode.removeChild(section);
});
}
function createChartContainer(chartIndex) {
const newChartContainer = document.createElement('div');
newChartContainer.classList.add('chart-container');
const commentContainer = document.createElement('div');
commentContainer.classList.add('comment-container');
commentContainer.id = `comments_${chartIndex}`;
commentContainer.style.display = 'none';
if (!firstChartAdded) {
newChartContainer.style.width = '100%';
commentContainer.style.width = '100%';
} else {
newChartContainer.style.width = '50%';
}
newChartContainer.style.height = 'calc(100vh - 250px)';
newChartContainer.innerHTML = `
<div class="axis-selection">
<select class="options-select"></select>
<button onclick="generateChart(event, ${chartIndex})">Generate Chart</button>
</div>
<div class="chart" id="chart_${chartIndex}"></div>
`;
populatePredefinedOptions(newChartContainer.querySelector('.options-select'));
return { newChartContainer, commentContainer };
}
async function postComment(event, chartIndex) {
const username = document.getElementById(`username_${chartIndex}`).value;
const commentText = document.getElementById(`comment_${chartIndex}`).value;
const selectedOptionId = document.querySelector(`.chart-container:nth-child(${chartIndex + 2}) .options-select`).value;
if (!username.trim() && !commentText.trim()) {
return;
}
try {
let response = await fetch('http://127.0.0.1:5000/api/save_username', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username })
});
await response.json();
response = await fetch('http://127.0.0.1:5000/api/add_comment', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chart_id: selectedOptionId,
comment_text: commentText,
user_id: username
})
});
await response.json();
document.getElementById(`username_${chartIndex}`).value = '';
document.getElementById(`comment_${chartIndex}`).value = '';
await fetchComments(chartIndex, selectedOptionId);
} catch (error) {
console.error('Error posting comment:', error);
}
}
async function fetchComments(chartIndex, chartId) {
const commentsDiv = document.getElementById(`comments_${chartIndex}`);
commentsDiv.innerHTML = '';
try {
const response = await fetch(`http://127.0.0.1:5000/api/get_comments?chart_id=${chartId}`);
const comments = await response.json();
comments.forEach(comment => {
const commentElement = document.createElement('div');
commentElement.innerHTML = `<strong>${comment.user_id}:</strong> ${comment.comment_text}`;
commentsDiv.appendChild(commentElement);
});
} catch (error) {
console.error('Error fetching comments:', error);
}
}
async function generateChart(event, chartIndex) {
const selectedOption = event.target.parentElement.querySelector('.options-select').value;
try {
const response = await fetch('http://127.0.0.1:5000/api/generate_chart', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ selected_option: selectedOption })
});
const chartData = await response.json();
await plotChart(chartIndex, chartData.title, chartData.x_axis_title, chartData.y_axis_title, chartData.x, chartData.y, selectedOption);
const commentContainer = document.getElementById(`comments_${chartIndex}`);
if (commentContainer) {
commentContainer.style.display = 'block';
}
const chartContainer = document.getElementById(`chart_${chartIndex}`);
const existingCommentSection = chartContainer.nextElementSibling;
if (existingCommentSection && existingCommentSection.classList.contains('comment-section')) {
existingCommentSection.remove();
}
const commentSectionHTML = `
<div class="comment-section">
<input type="text" id="username_${chartIndex}" class="username" placeholder="Username">
<input type="text" id="comment_${chartIndex}" class="comment" placeholder="Your comment">
<button onclick="postComment(event, ${chartIndex})">Post Comment</button>
</div>
`;
chartContainer.insertAdjacentHTML('afterend', commentSectionHTML);
await fetchComments(chartIndex, selectedOption);
} catch (error) {
console.error('Error generating chart data:', error);
}
}
async function plotChart(chartIndex, title, xTitle, yTitle, xData, yData, selectedOption) {
let trace;
if (selectedOption === '3') {
trace = {
x: xData,
y: yData,
mode: 'markers',
type: 'scatter',
marker: {
size: 10
}
};
} else if (selectedOption === '6') {
trace = {
x: xData,
y: yData,
type: 'line',
mode: 'lines+markers'
};
} else {
trace = {
x: xData,
y: yData,
type: 'bar'
};
}
const layout = {
title: title,
xaxis: {
title: xTitle
},
yaxis: {
title: yTitle
}
};
Plotly.newPlot(`chart_${chartIndex}`, [trace], layout);
}
</script>
</body>
</html>