-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-arquero-line-timeselection2.html
247 lines (213 loc) · 6.85 KB
/
11-arquero-line-timeselection2.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
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
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.6.11/core.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.8.7/polyfill.js"></script>
<script src="https://vega.github.io/vega/assets/promise.min.js"></script>
<script src="https://vega.github.io/vega/assets/symbol.min.js"></script>
<script src="https://vega.github.io/vega/assets/fetch.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vega@5/build-es5/vega.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@4/build-es5/vega-lite.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6/build-es5/vega-embed.js"></script>
<!--D3-->
<script src="https://d3js.org/d3.v5.min.js"></script>
<!--CSS-->
<link rel="stylesheet" href="css/style.css" />
<!--Arquero-->
<script src="https://cdn.jsdelivr.net/npm/arquero@latest"></script>
</head>
<body>
<div id="container">
<h1>Date-select Trend Chart</h1>
<p>
This uses a number input.
</p>
<p>
<!-- Choose range:
<input type="radio" id="full" name="buttons" value="100" checked />
<label for="full">Full pandemic</label>
<input
type="radio"
id="recent"
name="buttons"
value="90"
style="margin-left: 15px"
/>
<label for="recent">Last 90 days</label><br />
</p>
<hr> -->
<form id="numberinput">
<label for="inputNum" style="display:inline-block;">Show data for the last</label>
<input type="number" style="display:inline-block" id="inputNum" name="inputNum" min="1" max="30">
months. <button style="display:inline-block" onclick="updateChart(0)">Reset</button>
</form>
<div id="vis2" class="chartdest" style="width: 100%"></div>
<hr><a href="index.html">Back to main</a> </div>
<script type="text/javascript">
// arquero code to retrieve a CSV:
var dt;
var print = document.getElementById("print");
var fullTable;
var num;
var tot;
var shortTable = [];
// Loads the csv as an arquero table
aq.loadCSV(
"https://raw.githubusercontent.com/nychealth/coronavirus-data/master/trends/data-by-day.csv"
).then(data => {
dt = data;
fullTable = dt.objects();
vegaEmbed("#vis2", lineChart).then((res) =>
res.view.insert("lineData", fullTable).run()
);
});
var floorNum;
var numDays
var inputNum;
document.getElementById('inputNum').addEventListener('change',function() {
console.log('fired')
event.preventDefault();
inputNum = document.getElementById('inputNum').value;
numDays = inputNum * 30
floorNum = dt.numRows() - numDays;
// this only works to REDUCE because we are adding to shortTable via push in the forloop, but we're not removing from it
if (floorNum > 1) {
updateChart(floorNum);
} else {
updateChart(1); // prevent negative
}
},
false);
function updateChart(x) {
shortTable = fullTable.slice(x, fullTable.length);
vegaEmbed("#vis2", lineChart).then((res) =>
res.view.insert("lineData", shortTable).run()
);
}
// THIS IS FOR RADIO BUTTON INPUT
// window.onload = listenRadios;
// function listenRadios() {
// radios = document.querySelectorAll('input[type=radio][name="buttons"]');
// radios.forEach(function (radio) {
// return radio.addEventListener("change", function () {
// console.log("button changed!");
// if (radio.value == 100) {
// lineChart.config.bar.continuousBandSize = 1;
// vegaEmbed("#vis2", lineChart).then((res) =>
// res.view.insert("lineData", fullTable).runAsync()
// );
// } else if (radio.value == 90) {
// lineChart.config.bar.continuousBandSize = 5;
// vegaEmbed("#vis2", lineChart).then((res) =>
// res.view.insert("lineData", ninetyTable).runAsync()
// );
// } else {
// }
// });
// });
// }
var lineChart = {
$schema: "https://vega.github.io/schema/vega-lite/v5.json",
description: "Test rate over time",
width: "container",
height: 250,
config: {
bar: {
continuousBandSize: 1,
},
background: "#FFFFFF",
axisX: {
grid: false,
ticks: true,
labels: true,
},
axisY: {
domain: false,
ticks: false,
gridDash: [2],
gridWidth: 1,
},
view: {
stroke: "transparent",
},
},
data: {
name: "lineData",
},
layer: [
{
mark: {
type: "bar",
point: null,
tooltip: true,
interpolate: "natural",
strokeWidth: 0.5,
},
encoding: {
x: {
field: "date_of_interest",
type: "temporal",
title: " ",
},
y: {
field: "CASE_COUNT",
type: "quantitative",
title: " ",
},
color: {
value: "lightblue",
},
tooltip: [
{
field: "CASE_COUNT",
title: "New York City",
},
{
field: "date_of_interest",
type: "temporal",
title: "Week ending",
},
],
},
},
{
mark: {
type: "line",
point: null,
tooltip: true,
interpolate: "natural",
strokeWidth: 1.5,
},
encoding: {
x: {
field: "date_of_interest",
type: "temporal",
title: " ",
},
y: {
field: "CASE_COUNT_7DAY_AVG",
type: "quantitative",
title: " ",
},
color: {
value: "darkblue",
},
opacity: { value: 0.7 },
tooltip: [
{
field: "CASE_COUNT_7DAY_AVG",
title: "7-day average",
},
{
field: "date_of_interest",
type: "temporal",
title: "Week ending",
},
],
},
},
],
};
</script>
</body>
</html>