-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (53 loc) · 1.23 KB
/
index.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
const submit = document.querySelector('#submit');
const input = document.querySelector('#input');
const responseField = document.querySelector('#responseField');
const plot = document.querySelector('#graph');
const axios = require('axios');
async function processData() {
try {
const response = await axios.get('api/countyData', {
headers: { "name": input.value }
});
if (response.status === 200) {
renderResponse(response.data, input.value);
} else {
renderError(response.status);
}
} catch (err) {
renderError(err);
}
}
function renderResponse(response, county) {
try {
if (response && county) {
const data = [{
x: response.weeks,
y: response.cases,
name: county
}]
Plotly.plot(plot, data, {
title: {
text:'New Weekly Covid Cases',
font: {
family: "Optima",
size: 24
},
xref: 'paper',
x: 0.05,
},
margin: { t: 40 }, showlegend: true}, {displayModeBar: false});
} else {
renderError("Invalid county");
}
} catch (err) {
renderError(err);
}
}
function renderError(error) {
alert(`Error: ${error}`);
}
const displayData = event => {
event.preventDefault();
processData();
}
submit.addEventListener('click', displayData);