-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
115 lines (99 loc) · 2.88 KB
/
script.js
File metadata and controls
115 lines (99 loc) · 2.88 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
function generateDateRange(startDate) {
const dates = []
const currentDate = new Date()
let current = new Date(startDate)
while (
current < currentDate ||
(current.getFullYear() === currentDate.getFullYear() &&
current.getMonth() === currentDate.getMonth() &&
current.getDate() === currentDate.getDate())
) {
const year = current.getFullYear()
const month = String(current.getMonth() + 1).padStart(2, '0')
const day = String(current.getDate()).padStart(2, '0')
dates.push(`${year}-${month}-${day}`)
current.setDate(current.getDate() + 1)
}
return dates
}
const startDate = '2023-10-05'
const dateRange = generateDateRange(startDate)
let chart
function initChart() {
chart = Highcharts.chart('container', {
title: { text: 'Nation Cards Data' },
xAxis: { categories: dateRange, tickInterval: 30 },
series: [],
plotOptions: {
line: {
marker: {
enabled: false,
},
connectNulls: true,
lineWidth: 2,
},
},
})
}
async function loadData() {
const data = []
for (const date of dateRange) {
try {
const response = await fetch(`/data/${date}.json`)
const jsonData = await response.json()
data.push(jsonData)
} catch (error) {
data.push([])
// console.error(`Failed to load data for ${date}:`, error)
}
}
return data
}
async function updateChart() {
const selectedMetric = document.getElementById('metricDropdown').value
const nationInput = document.getElementById('nationInput').value.trim()
const selectedNations = nationInput
.split(',')
.map(n => n.trim())
.filter(n => n.length > 0)
if (selectedNations.length === 0) {
alert('Please enter at least one nation.')
return
}
while (chart.series.length) {
chart.series[0].remove(true)
}
const data = await loadData()
selectedNations.forEach(nation => {
let firstValidDateIndex = -1
const seriesData = data.map((entryArray, index) => {
const nationData = entryArray.find(n => n.Nation === nation)
if (nationData && nationData[selectedMetric] !== null) {
if (firstValidDateIndex === -1) {
firstValidDateIndex = index
}
return nationData[selectedMetric]
}
return null
})
if (firstValidDateIndex !== -1) {
if (selectedNations.length === 0) {
const filteredSeriesData = seriesData.slice(firstValidDateIndex)
const filteredDateRange = dateRange.slice(firstValidDateIndex)
chart.addSeries({
name: nation,
data: filteredSeriesData,
})
chart.xAxis[0].setCategories(filteredDateRange)
} else {
chart.addSeries({
name: nation,
data: seriesData,
})
}
} else {
console.warn(`Nation "${nation}" not found in the data or has no valid data for the selected metric.`)
}
})
}
initChart()