-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathertos_by_time.html
More file actions
220 lines (175 loc) · 5.92 KB
/
ertos_by_time.html
File metadata and controls
220 lines (175 loc) · 5.92 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
<!DOCTYPE html>
<html lang="en">
<!--
Template of the data visualization (line chart) in d3.js from CSV data file
Reference: Brush & Zoom line chart - bl.ocks.org: https://bl.ocks.org/EfratVil/92f894ac0ba265192411e73f633a3e2f
-->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>COVID-19 Data Visualization</title>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@500;700&display=swap" rel="stylesheet">
<style>
:root {
--main-bg-color: #F3F3F4;
--text-color: #323A56;
--yellow: #D4B158;
--red: #D25A5E;
--blue: #1C558C;
--green: #7FC0BA;
}
html,
body {
height: 100%;
margin: 0;
}
body,
text {
font-family: 'Montserrat', sans-serif;
color: var(--text-color);
text-transform: uppercase;
}
.centered-graph {
display: flex;
align-items: center;
height: 100%;
}
div {
flex: auto;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}
.zoom {
cursor: move;
fill: none;
pointer-events: all;
}
</style>
</head>
<body>
<div id='graph' class="centered-graph">
<svg width="800" height="480" viewBox="0 0 800 480" preserveAspectRatio="xMidYMid meet"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
const svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 110, left: 100},
margin2 = {top: 430, right: 20, bottom: 30, left: 100},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
height2 = +svg.attr("height") - margin2.top - margin2.bottom;
const parseTime = d3.timeParse("%Y-%m-%d");
const x = d3.scaleTime().range([0, width]),
x2 = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
y2 = d3.scaleLinear().range([height2, 0]);
const xAxis = d3.axisBottom(x),
xAxis2 = d3.axisBottom(x2),
yAxis = d3.axisLeft(y);
const brush = d3.brushX()
.extent([[0, 0], [width, height2]])
.on("brush end", brushed);
const zoom = d3.zoom()
.scaleExtent([1, Infinity])
.translateExtent([[0, 0], [width, height]])
.extent([[0, 0], [width, height]])
.on("zoom", zoomed);
const line = d3.line()
.x(function (d) {
return x(d.data);
})
.y(function (d) {
return y(d.n_mero_d_afectats);
});
const line2 = d3.line()
.x(function (d) {
return x2(d.data);
})
.y(function (d) {
return y2(d.n_mero_d_afectats);
});
const Line_chart = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("clip-path", "url(#clip)");
const focus = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
const context = svg.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
/* Upload a data CSV file "data.csv" to your public GitHub repository.
Then, make sure that your uploaded file exists by accessing the following URL in your browser.
https://github.com/USERNAME/REPOSITORY/blob/master/data.csv
The CSV file must have two columns: "data" and "n_mero_d_afectats".
data: Timestamp formatted such as "2020-03-21 21:33:54"
n_mero_d_afectats: numerical n_mero_d_afectats
Note: The direct public link of the CSV file is shown below, not the above (for browser).
*/
d3.csv("https://raw.githubusercontent.com/HPAI-BSC/hpai-bsc.github.io/master/csv_charts/ertos_by_time.csv",
dataType, function (error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.data; }));
y.domain([0, d3.max(data, function (d) { return d.n_mero_d_afectats; })]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "axis axis--y")
.call(yAxis);
Line_chart.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
context.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line2);;
context.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, x.range());
svg.append("rect")
.attr("class", "zoom")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(zoom);
console.log(data);
});
function brushed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
const s = d3.event.selection || x2.range();
x.domain(s.map(x2.invert, x2));
Line_chart.select(".line").attr("d", line);
focus.select(".axis--x").call(xAxis);
svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
.scale(width / (s[1] - s[0]))
.translate(-s[0], 0));
}
function zoomed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
const t = d3.event.transform;
x.domain(t.rescaleX(x2).domain());
Line_chart.select(".line").attr("d", line);
focus.select(".axis--x").call(xAxis);
context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
}
function dataType(d) {
d.data= parseTime(d.data);
d.n_mero_d_afectats = +d.n_mero_d_afectats;
return d;
}
</script>
</div>
</body>
</html>