-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNWSS_minmax.html
193 lines (176 loc) · 7.22 KB
/
NWSS_minmax.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.slim.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.5.0/echarts.min.js"></script>
<title>Estimated Infections from Wastewater</title>
</head>
<body>
<select id="countries"></select>
<select id="regions"></select>
<label>
<input type="checkbox" id="showWastewater"> Show Wastewater Graph
</label>
<style>
#main, html, body {
width: 100%;
}
#main {
height: 400px;
}
</style>
<div id="main"></div>
<script>
var countries = ['United_States'];
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
$("#countries").html(countries.map(country => `<option value="${country}">${country}</option>`).join(""));
// Function to dynamically update regions and measures based on the data
function updateRegionsAndMeasures(data) {
let regions = [...new Set(data.map(item => item.Region))];
$("#regions").html(regions.map(region => `<option value="${region}">${region}</option>`).join(""));
}
// Helper function to round numbers to two significant digits
function roundToTwoSignificantDigits(num) {
if (num === 0) return 0;
let scale = Math.floor(Math.log10(Math.abs(num))) + 1 - 2;
return Math.round(num * Math.pow(10, -scale)) * Math.pow(10, scale);
}
// Main function to load data based on selected country
function loadData() {
fetch(`./${$("#countries").val()}_wwb.json`)
.then(response => response.json())
.then(data => {
updateRegionsAndMeasures(data); // Populate the regions dropdown
updateData(); // Update chart with the selected region data
})
.catch(err => {
console.log(err);
});
}
function updateData() {
var selected_country = $('#countries').val();
var selected_region = $('#regions').val();
var showWastewater = $('#showWastewater').is(':checked');
Promise.all([
fetch(`./${selected_country}_wwb.json`).then(response => response.json()),
fetch(`./${selected_country}_min_wwb.json`).then(response => response.json())
]).then(([maxData, minData]) => {
var series = [];
var labels = [];
var legends = [];
// Filter and display Infection data by default
['inf'].forEach((measure) => {
let filteredMaxData = maxData.filter(item => item.Region === selected_region && item.Measure === measure);
let filteredMinData = minData.filter(item => item.Region === selected_region && item.Measure === measure);
let maxTimeData = filteredMaxData.map(item => new Date(item.Date).toISOString().split('T')[0]);
let minTimeData = filteredMinData.map(item => new Date(item.Date).toISOString().split('T')[0]);
let maxValueData = filteredMaxData.map(item => item.Value);
let minValueData = filteredMinData.map(item => item.Value);
labels = [...new Set([...labels, ...maxTimeData])].sort();
// Infection min and max series with specified shades of red
series.push(
{
name: 'Infections Min',
type: 'line',
data: minValueData,
yAxisIndex: 1,
showSymbol: false,
lineStyle: { color: '#FFB6C1', width: 2 }, // Light red for line
areaStyle: { opacity: 0.2, color: '#FFB6C1' },
itemStyle: { color: '#FFB6C1' } // Marker color for legend and hover
},
{
name: 'Infections Max',
type: 'line',
data: maxValueData,
yAxisIndex: 1,
showSymbol: false,
lineStyle: { color: '#FF4500', width: 3 }, // Darker red for stronger line
areaStyle: { opacity: 0.2, color: '#FF4500' },
itemStyle: { color: '#FF4500' }, // Marker color for legend and hover
markLine: selected_country === 'United_States' ? {
data: [{
xAxis: labels[labels.length - 14],
lineStyle: { type: 'dashed', color: 'orange' },
label: { formatter: ' \npreliminary estimate', position: 'start', color: 'orange' }
}]
} : null,
markArea: selected_country === 'United_States' ? {
itemStyle: { color: 'rgba(255, 165, 0, 0.1)' },
data: [[{ xAxis: labels[labels.length - 14] }, { xAxis: labels[labels.length - 1] }]]
} : null
}
);
legends.push('Infections Min', 'Infections Max');
});
// Optionally add Wastewater data if selected
if (showWastewater) {
let wastewaterMaxData = maxData.filter(item => item.Region === selected_region && item.Measure === 'wastewater');
let wastewaterMaxValueData = wastewaterMaxData.map(item => item.Value);
series.push({
name: 'Wastewater',
type: 'line',
data: wastewaterMaxValueData,
yAxisIndex: 0,
showSymbol: false,
lineStyle: { color: '#4682B4', width: 2 }, // Blue for Wastewater line
areaStyle: { opacity: 0.2, color: '#4682B4' },
itemStyle: { color: '#4682B4' } // Marker color for legend and hover
});
legends.push('Wastewater');
}
// Set the chart options
myChart.setOption({
tooltip: {
trigger: 'axis',
formatter: function(params) {
var result = params[0].axisValueLabel + '<br>';
params.forEach(function(item) {
result += item.marker + ' ' + item.seriesName + ': ' + Math.round(item.data) + '<br>';
});
return result;
}
},
legend: { data: legends, textStyle: { color: '#000' } },
toolbox: {
show: true,
feature: {
dataZoom: { yAxisIndex: 'none' },
dataView: { readOnly: false },
magicType: { type: ['line', 'bar'] },
restore: {},
saveAsImage: {}
}
},
xAxis: { type: 'category', data: labels },
yAxis: [
{
type: 'value',
name: showWastewater ? 'Wastewater (copies/capita)' : '', // Dynamically set title
show: showWastewater // Only show axis if wastewater is displayed
},
{
type: 'value',
name: 'New Infections'
}
],
series: series
});
}).catch(err => {
console.log(err);
});
}
// Initial load and event listeners
loadData();
$("#countries").on("change", function() {
loadData();
});
$("#regions, #showWastewater").on("change", function() {
updateData();
});
</script>
</body>
</html>