-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
286 lines (237 loc) · 11.9 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TraffIQ Paris</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<title>Fact Sheet</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 20px;
}
h1 {
font-size: 24px;
}
h2 {
font-size: 20px;
margin-top: 20px;
}
p {
margin-bottom: 10px;
}
ul {
margin-top: 0;
padding-left: 20px;
}
.container {
width: 30%;
/* Set width to 49% for two plots in one row */
display: inline-block;
/* Allow wrapping of elements */
margin: 1%;
/* Add margin for spacing */
border: 100% solid #ccc;
/* Add border */
}
.legend-label {
margin-left: 10%;
font-size: 85%;
/* Add margin between line marker and labels */
}
.axis-label {
font-size: 85%;
font-family: Arial, sans-serif;
/* Use Arial font */
}
.tick text {
font-size: 95%;
/* Adjust tick label font size */
font-family: Arial, sans-serif;
/* Use Arial font */
}
</style>
</head>
<body>
<div class="fact-sheet">
<h1>TraffIQ: Traffic Intelligence</h1>
</div>
<div id="plots"></div>
<script>
function createPlots() {
// Get the width and height of the viewport
const viewportWidth = window.innerWidth || document.documentElement.clientWidth;
const viewportHeight = window.innerHeight || document.documentElement.clientHeight;
// fetch("/data")
fetch("frontend/data.json")
.then(response => response.json())
.then(data => {
data.forEach(plotData => {
const container = document.createElement('div');
container.classList.add('container'); // Add container class
const svgWidth = "100%"; // Set SVG width as a percentage of the container
const svgHeight = "40"; // Set SVG height as a percentage of the viewport height
const svg = d3.create("svg")
.attr("width", svgWidth)
.attr("height", percentageToPixels(svgHeight, viewportHeight));
function percentageToPixels(percentageDimentions, viewportDimension) {
// Convert the percentage to pixels
const DimensionInPixels = (percentageDimentions / 100) * viewportDimension;
return DimensionInPixels;
}
percentageWidth = 30
percentageHeight = 30
// Convert the percentage to pixels
const widthInPixels = percentageToPixels(percentageWidth, viewportWidth);
const heightInPixels = percentageToPixels(percentageHeight, viewportHeight)
const margin = {
top: percentageToPixels(5, viewportHeight),
right: percentageToPixels(0.5, viewportWidth),
bottom: percentageToPixels(2, viewportHeight),
left: percentageToPixels(5, viewportWidth)
};
const width = widthInPixels - margin.left - margin.right;
const height = heightInPixels - margin.top - margin.bottom;
// Convert date-time strings to Date objects
const parseDate = d3.timeParse("%d-%m-%Y:%H"); // Define your date format
const xData = plotData.real_time_idx.concat(plotData.predictions_time_idx)
.map(dateStr => parseDate(dateStr));
const xScale = d3.scaleTime()
.domain([d3.min(xData), d3.max(xData)])
.range([margin.left, width + margin.left]);
const yScale = d3.scaleLinear()
// .domain([0, d3.max(plotData.real_q.concat(plotData.predictions_preds))])
.domain([0, 8000])
.range([height + margin.top, margin.top]);
const xAxis = d3.axisBottom(xScale).ticks(6).tickFormat(d3.timeFormat("%d-%m %H:%M")); // Set number of ticks and format
const yAxis = d3.axisLeft(yScale).ticks(5); // Set number of ticks
// Draw x axis
svg.append("g")
.attr("transform", `translate(0, ${height + margin.top})`)
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-45)");
// Draw y axis
svg.append("g")
.attr("transform", `translate(${margin.left}, 0)`)
.call(yAxis)
// Add grid lines for x-axis
svg.append("g")
.attr("class", "grid")
.attr("transform", `translate(0, ${height + margin.top})`)
.attr("stroke-width", 1)
.attr("stroke-opacity", 0.3)
.call(d3.axisBottom(xScale).tickSize(-height).tickFormat(""));
// Add grid lines for y-axis
svg.append("g")
.attr("class", "grid")
.attr("transform", `translate(${margin.left}, 0)`)
.attr("stroke-opacity", 0.3)
.call(d3.axisLeft(yScale).tickSize(-width).tickFormat(""));
const realLine = d3.line()
.defined(d => d !== null) // Skip null values
.x((d, i) => xScale(parseDate(plotData.real_time_idx[i])))
.y(d => yScale(d));
const predictionsLine = d3.line()
.x((d, i) => xScale(parseDate(plotData.predictions_time_idx[i])))
.y(d => yScale(d));
// Define the area function for the confidence interval
const predictionVariance = d3.area()
.x((d, i) => xScale(parseDate(plotData.predictions_time_idx[i])))
.y0((d, i) => yScale(plotData.lower_bound[i])) // Define the lower bound of the confidence interval
.y1((d, i) => yScale(plotData.upper_bound[i])); // Define the upper bound of the confidence interval
// Draw observed trend line
svg.append("path")
.datum(plotData.real_q)
.attr("fill", "none")
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("d", realLine);
// Draw predicted trend line
svg.append("path")
.datum(plotData.predictions_preds)
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr("d", predictionsLine);
// Show confidence interval
svg.append("path")
.datum(plotData.predictions_time_idx)
.attr("fill", "red")
.attr("fill-opacity", 0.2) // Set opacity to 50%
.attr("stroke", "none")
.attr("d", predictionVariance);
// Add x-axis label
svg.append("text")
.attr("class", "axis-label")
.attr("x", width / 2 + 0.05 * width)
.attr("y", height + 0.6 * height)
.attr("text-anchor", "middle")
.text("Time-of-day");
// Add y-axis label
svg.append("text")
.attr("class", "axis-label")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2 - 0.1 * height)
.attr("y", width - 0.95 * width)
.attr("text-anchor", "middle")
.text("Traffic Volume");
// Add legend labels and lines
// Add legend labels
svg.append("text")
.attr("x", 0.2 * margin.left)
.attr("y", 0.5 * margin.top)
.attr("fill", "blue")
.text("Observed")
.classed("legend-label", true); // Add class for styling
/* svg.append("line") // Add horizontal line marker for observed trend line
.attr("x1", 1 * margin.left)
.attr("y1", 0.4 * margin.top)
.attr("x2", 1.5 * margin.left)
.attr("y2", 0.4 * margin.top)
.attr("stroke", "blue")
.attr("stroke-width", 2); */
svg.append("text")
.attr("x", 1.8 * margin.left)
.attr("y", 0.5 * margin.top)
.attr("fill", "red")
.text("Predicted")
.classed("legend-label", true); // Add class for styling
/* svg.append("line") // Add horizontal line marker for predicted trend line
.attr("x1", 2.6 * margin.left)
.attr("y1", 0.4 * margin.top)
.attr("x2", 3.1 * margin.left)
.attr("y2", 0.4 * margin.top)
.attr("stroke", "red")
.attr("stroke-width", 2); */
// Add Paris_id label
svg.append("text")
.attr("x", width / 2 + margin.left)
.attr("y", margin.top / 2)
.attr("fill", "black")
.text(`Detector: ${plotData.paris_id}`)
.classed("legend-label", true);
container.appendChild(svg.node());
document.getElementById("plots").appendChild(container);
});
})
.catch(error => console.error('Error fetching data:', error));
}
// Function to update plot dimensions on window resize
function updatePlotDimensions() {
const plotsContainer = document.getElementById("plots");
plotsContainer.innerHTML = ''; // Clear existing plots
createPlots(); // Re-create plots with updated dimensions
}
// Initial creation of plots
createPlots();
// Add event listener for window resize event
window.addEventListener('resize', updatePlotDimensions);
</script>
</body>
</html>