-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
579 lines (539 loc) · 24.1 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
<!DOCTYPE html>
<html>
<head>
<title>Fed Data Collection</title>
<meta charset="UTF-8" />
<!-- Load Chart.js first -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>
<!-- Then load Luxon -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"></script>
<!-- Then load Chart.js adapter for Luxon -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chartjs-adapter-luxon.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet" />
<style>
body.dark-mode {
background-color: #121212;
color: #ffffff;
transition: background-color 0.3s ease, color 0.3s ease;
}
body {
font-family: "Roboto", sans-serif;
margin: 0;
padding: 20px;
background-color: #ffffff; /* Light mode preferred */
color: #333333;
transition: background-color 0.3s ease, color 0.3s ease;
padding-top: 60px; /* Adjust based on the header height */
}
h1 {
margin-top: 0;
text-align: center;
}
.header-container {
text-align: center;
margin-bottom: 20px;
position: sticky;
top: 0;
background-color: inherit; /* Ensures it adapts to light/dark mode */
z-index: 1000; /* Keeps the header above other elements */
padding: 10px 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
display: flex;
justify-content: space-between;
align-items: center;
}
.header-container h1 {
margin: 0;
font-size: 24px;
}
.chart-grid {
display: flex;
flex-wrap: wrap;
gap: 30px; /* Increased spacing */
justify-content: center;
}
.chart-container {
width: 45%;
background: #f5f5f5;
border-radius: 8px;
padding: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
transition: background 0.3s ease, box-shadow 0.3s ease;
height: 300px; /* Fixed height for charts */
position: relative;
}
body.dark-mode .chart-container {
background-color: #1e1e1e;
box-shadow: 0 2px 8px rgba(255,255,255,0.1);
}
/* Improved toggle switch */
.toggle-switch {
position: fixed;
right: 20px;
top: 10px;
width: 60px;
height: 34px;
background: #cccccc; /* Light gray toggle switch */
border-radius: 34px;
cursor: pointer;
transition: background 0.3s ease;
box-shadow: 0 0 5px rgba(0,0,0,0.3);
}
body.dark-mode .toggle-switch {
background: #888888; /* Darker toggle switch in dark mode */
}
.toggle-switch input {
display: none;
}
.toggle-switch label {
display: block;
width: 26px;
height: 26px;
background: #ffffff; /* White knob for contrast */
border-radius: 50%;
position: absolute;
top: 4px;
left: 4px;
transition: transform 0.2s ease, background 0.3s ease;
cursor: pointer; /* Ensure pointer cursor on hover */
}
.toggle-switch input:checked + label {
transform: translateX(26px);
background: #ffffff; /* White knob remains white when toggled */
}
/* Increase clickable area for toggle */
.toggle-switch label {
width: 26px;
height: 26px;
}
/* Remove default legends */
.chart-container canvas {
max-height: 100%;
}
/* Loading Spinner */
.loading-spinner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 4px solid rgba(0,0,0,0.1);
border-top: 4px solid #555555;
border-radius: 50%;
width: 30px;
height: 30px;
animation: spin 1s linear infinite;
}
body.dark-mode .loading-spinner {
border-top: 4px solid #ffffff;
}
@keyframes spin {
0% { transform: translate(-50%, -50%) rotate(0deg); }
100% { transform: translate(-50%, -50%) rotate(360deg); }
}
/* Error Message Styling */
.error-message {
color: #ff4d4d;
text-align: center;
font-weight: bold;
}
/* Tooltip Styling */
#tooltip {
position: absolute;
background: rgba(0, 0, 0, 0.8);
color: #fff;
padding: 6px 10px;
border-radius: 4px;
pointer-events: none;
font-size: 12px;
z-index: 1000;
display: none;
white-space: nowrap;
}
/* Enhanced Tooltip Styling */
#tooltip {
background: rgba(0, 0, 0, 0.8);
color: #fff;
padding: 8px 12px;
border-radius: 6px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
transition: opacity 0.2s ease, transform 0.2s ease;
transform: translateY(-10px);
}
#tooltip.display {
opacity: 1;
transform: translateY(0);
}
.chart-container.large-chart {
width: 90%; /* Increase width for the selected chart */
height: 500px; /* Optional: Increase height for better visibility */
padding: 20px; /* Increased padding if necessary */
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
@media (max-width: 600px) {
.header-container {
flex-direction: column;
align-items: flex-start;
}
.header-container h1 {
font-size: 20px;
}
.toggle-switch {
margin-top: 10px;
}
}
</style>
</head>
<body>
<div class="header-container">
<h1>Fed Data Collection</h1>
<!-- Toggle Switch -->
<div class="toggle-switch">
<input type="checkbox" id="darkToggle" aria-label="Toggle Dark Mode" />
<label for="darkToggle"></label>
</div>
</div>
<!-- Charts Grid -->
<div id="charts" class="chart-grid">
<!-- Chart containers will be dynamically inserted here -->
</div>
<!-- Tooltip Div -->
<div id="tooltip" role="tooltip"></div>
<script>
// Store chart instances
window.charts = [];
// Custom Plugin to add label inside the chart at top right
const chartLabelPlugin = {
id: 'chartLabelPlugin',
afterDraw: (chart) => {
const ctx = chart.ctx;
const label = chart.config.data.datasets[0].label;
const fontSize = 16;
const fontStyle = 'Arial';
const fontColor = chart.config.options.plugins.chartLabelPlugin.color || '#000';
ctx.save();
ctx.font = `${fontSize}px ${fontStyle}`;
ctx.fillStyle = fontColor;
ctx.textAlign = 'right';
ctx.fillText(label, chart.width - 10, 15); // Adjusted Y position to 15
ctx.restore();
// Measure label dimensions for tooltip detection
const textMetrics = ctx.measureText(label);
const labelWidth = textMetrics.width;
const labelHeight = fontSize; // Approximate height
// Store label area in chart options for hover detection
chart.config.options.plugins.chartLabelPlugin.labelArea = {
x: chart.width - 10 - labelWidth,
y: 15 - labelHeight,
width: labelWidth,
height: labelHeight
};
}
};
// Function to apply theme from localStorage
const applySavedTheme = () => {
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark') {
document.body.classList.add('dark-mode');
document.getElementById('darkToggle').checked = true;
} else if (savedTheme === 'light') {
document.body.classList.remove('dark-mode');
document.getElementById('darkToggle').checked = false;
}
};
// Apply saved theme on load
applySavedTheme();
// Toggle the dark mode and save preference
document.getElementById('darkToggle').addEventListener('change', () => {
document.body.classList.toggle('dark-mode');
const isDark = document.body.classList.contains('dark-mode');
localStorage.setItem('theme', isDark ? 'dark' : 'light');
updateChartsTheme();
});
// Function to update charts' colors based on theme
const updateChartsTheme = () => {
const isDark = document.body.classList.contains('dark-mode');
Chart.defaults.color = isDark ? '#ffffff' : '#333333';
Chart.defaults.borderColor = isDark ? '#ffffff' : '#333333';
window.charts.forEach(chart => {
chart.data.datasets.forEach(dataset => {
dataset.backgroundColor = isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(137,170,255, 0.2)';
dataset.borderColor = isDark ? '#ffffff' : '#1355FF';
dataset.pointBackgroundColor = isDark ? '#ffffff' : '#1355FF';
dataset.pointBorderColor = isDark ? '#ffffff' : '#1355FF';
dataset.pointHoverBackgroundColor = isDark ? '#ffffff' : '#1355FF';
dataset.pointHoverBorderColor = isDark ? '#ffffff' : '#1355FF';
});
// Update scales
chart.options.scales.x.ticks.color = isDark ? '#ffffff' : '#333333';
chart.options.scales.y.ticks.color = isDark ? '#ffffff' : '#333333';
chart.options.scales.x.grid.color = isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0,0,0,0.05)';
chart.options.scales.y.grid.color = isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0,0,0,0.05)';
// Update legend
if (chart.options.plugins.legend && chart.options.plugins.legend.labels) {
chart.options.plugins.legend.labels.color = isDark ? '#ffffff' : '#333333';
}
// Update display formats to include year
if (chart.options.scales.x.time) {
chart.options.scales.x.time.displayFormats = {
day: 'dd MMM yyyy' // Display day, month, and year
};
}
// Update label color in the plugin
if (chart.options.plugins.chartLabelPlugin) {
chart.options.plugins.chartLabelPlugin.color = isDark ? '#ffffff' : '#000000';
}
chart.update(); // Ensure the chart updates with new colors
});
};
// Function to create a chart for a single JSON file
const createChart = (data, chartId, label, description, startDate) => {
// Check if dark mode is active
const isDark = document.body.classList.contains('dark-mode');
const backgroundColor = isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(137,170,255, 0.2)';
const borderColor = isDark ? '#ffffff' : '#1355FF';
// Show loading spinner
const chartContainer = document.getElementById(chartId).parentNode;
const spinner = document.createElement('div');
spinner.className = 'loading-spinner';
chartContainer.appendChild(spinner);
// Extract data from observations within the specified time range
let observations = data.observations;
if (startDate) {
observations = observations.filter(obs => {
const obsDate = new Date(obs.date);
const afterStart = obsDate >= new Date(startDate);
return afterStart;
});
}
if (observations.length === 0) {
chartContainer.removeChild(spinner);
chartContainer.innerHTML = `<p class="error-message">No data available for ${label}.</p>`;
return;
}
const labels = observations.map(obs => obs.date);
const values = observations.map(obs => Number(obs.value));
const dataPointCount = values.length;
let dynamicBorderWidth = 0.5;
if (dataPointCount > 1000) {
dynamicBorderWidth = 0.3;
} else if (dataPointCount > 500) {
dynamicBorderWidth = 0.4;
}
// Create chart using Chart.js
const ctx = document.getElementById(chartId).getContext('2d');
const chart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: label,
data: values,
backgroundColor: backgroundColor,
borderColor: borderColor,
borderWidth: dynamicBorderWidth, // Use dynamic border width
fill: true,
tension: 0.4, // Makes the lines more curved and smooth
pointRadius: 3,
pointBackgroundColor: borderColor,
pointBorderColor: borderColor,
pointHoverRadius: 5,
pointHoverBackgroundColor: borderColor,
pointHoverBorderColor: borderColor,
}]
},
options: {
scales: {
x: {
type: 'time',
time: {
unit: 'day',
displayFormats: {
day: 'dd MMM yyyy' // Display day, month, and year
}
},
min: startDate || undefined, // Set minimum date if specified
ticks: {
color: isDark ? '#ffffff' : '#333333'
},
grid: {
color: isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0,0,0,0.05)'
}
},
y: {
beginAtZero: true,
min: 0, // Ensure y-axis starts at 0
ticks: {
color: isDark ? '#ffffff' : '#333333'
},
grid: {
color: isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0,0,0,0.05)'
}
}
},
plugins: {
legend: {
display: false // Hide the default legend
},
tooltip: {
mode: 'index',
intersect: false,
callbacks: {
title: function(context) {
const date = context[0].label;
const dt = luxon.DateTime.fromISO(date);
if (dt.isValid) {
return dt.toFormat('dd MMM yyyy');
} else {
return date; // Fallback to original label if invalid
}
}
}
},
chartLabelPlugin: {
color: isDark ? '#ffffff' : '#000000' // Set label color based on theme
},
decimation: {
enabled: values.length > 500, // Enable decimation only if more than 500 data points
algorithm: 'min-max',
samples: 500, // Adjust based on performance needs
},
},
responsive: true,
maintainAspectRatio: false
},
plugins: [chartLabelPlugin] // Register the custom plugin
});
// Remove loading spinner after chart is ready
// Chart.js renders charts synchronously, so remove spinner immediately
chartContainer.removeChild(spinner);
window.charts.push(chart);
// Add mousemove event listener for tooltip
chart.canvas.addEventListener('mousemove', (event) => {
const tooltip = document.getElementById('tooltip');
const canvasRect = chart.canvas.getBoundingClientRect();
const mouseX = event.clientX - canvasRect.left;
const mouseY = event.clientY - canvasRect.top;
const labelArea = chart.options.plugins.chartLabelPlugin.labelArea;
if (labelArea) {
if (
mouseX >= labelArea.x &&
mouseX <= labelArea.x + labelArea.width &&
mouseY >= labelArea.y - labelArea.height &&
mouseY <= labelArea.y + labelArea.height
) {
// Show tooltip
tooltip.innerHTML = description || "No description available.";
tooltip.style.display = 'block';
const tooltipWidth = tooltip.offsetWidth;
const tooltipHeight = tooltip.offsetHeight;
let left = event.pageX + 10;
let top = event.pageY + 10;
// Adjust tooltip position to stay within viewport
if (left + tooltipWidth > window.innerWidth) {
left = event.pageX - tooltipWidth - 10;
}
if (top + tooltipHeight > window.innerHeight) {
top = event.pageY - tooltipHeight - 10;
}
tooltip.style.left = `${left}px`;
tooltip.style.top = `${top}px`;
} else {
// Hide tooltip
tooltip.style.display = 'none';
}
}
});
// Hide tooltip when mouse leaves the canvas
chart.canvas.addEventListener('mouseleave', () => {
const tooltip = document.getElementById('tooltip');
tooltip.style.display = 'none';
});
};
const dataDir = 'data/';
// Fetch the files list from files.json
fetch(`${dataDir}files.json`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
const files = data.files;
// Create placeholder chart containers
files.forEach(file => {
let chartDiv = document.createElement('div');
chartDiv.className = 'chart-container';
// Add 'large-chart' class to the specific chart you want to enlarge
if (file.name === 'specificChart.json') { // Replace 'specificChart.json' with your chart's filename
chartDiv.classList.add('large-chart');
}
chartDiv.setAttribute('data-file', file.name);
chartDiv.setAttribute('data-label', file.label);
chartDiv.setAttribute('data-description', file.description);
chartDiv.setAttribute('data-start-date', file.startDate); // Only startDate is set
chartDiv.innerHTML = `<canvas id="${file.name.replace('.json', 'Chart')}"></canvas>`;
document.getElementById('charts').appendChild(chartDiv);
chartDiv.addEventListener('click', () => {
chartDiv.classList.toggle('large-chart');
});
});
// IntersectionObserver callback
const onIntersection = (entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const chartDiv = entry.target;
const fileName = chartDiv.getAttribute('data-file');
const label = chartDiv.getAttribute('data-label');
const description = chartDiv.getAttribute('data-description');
const chartId = fileName.replace('.json', 'Chart');
const startDate = chartDiv.getAttribute('data-start-date');
// Check if chart is already initialized
if (!chartDiv.getAttribute('data-loaded')) {
fetch(dataDir + fileName)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(chartData => {
createChart(chartData, chartId, label, description, startDate);
chartDiv.setAttribute('data-loaded', 'true'); // Mark as loaded
})
.catch(error => {
console.error(`Failed to load ${fileName}: `, error);
// Remove spinner if present
const spinner = chartDiv.querySelector('.loading-spinner');
if (spinner) {
chartDiv.removeChild(spinner);
}
// Display an error message in the UI
chartDiv.innerHTML = `<p class="error-message">Error loading ${label}</p>`;
});
// Stop observing this chartDiv
observer.unobserve(chartDiv);
}
}
});
};
// Create an IntersectionObserver
const observerOptions = {
root: null, // viewport
rootMargin: '0px',
threshold: 0.1 // 10% of the element is visible
};
const observer = new IntersectionObserver(onIntersection, observerOptions);
// Observe each chart container
document.querySelectorAll('.chart-container').forEach(chartDiv => {
observer.observe(chartDiv);
});
})
.catch(error => {
console.error('Failed to load files.json:', error);
// Display an error message in the UI
document.getElementById('charts').innerHTML = `<p class="error-message">Failed to load chart configurations.</p>`;
});
</script>
</body>
</html>