This repository was archived by the owner on Mar 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
187 lines (157 loc) · 5.86 KB
/
script.js
File metadata and controls
187 lines (157 loc) · 5.86 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
// Available countries based on the publish directory structure
const countries = [
{ name: 'Albania', folder: 'albania' },
{ name: 'Armenia', folder: 'armenia' },
{ name: 'Azerbaijan', folder: 'azerbaijan' },
{ name: 'Belarus', folder: 'belarus' },
{ name: 'Bosnia & Herzegovina', folder: 'bosnia-&-herzegovina' },
{ name: 'Bulgaria', folder: 'bulgaria' },
{ name: 'Croatia', folder: 'croatia' },
{ name: 'Czechia', folder: 'czechia' },
{ name: 'Estonia', folder: 'estonia' },
{ name: 'Georgia', folder: 'georgia' },
{ name: 'Hungary', folder: 'hungary' },
{ name: 'Kazakhstan', folder: 'kazakhstan' },
{ name: 'Kosovo', folder: 'kosovo' },
{ name: 'Kyrgyzstan', folder: 'kyrgyzstan' },
{ name: 'Latvia', folder: 'latvia' },
{ name: 'Lithuania', folder: 'lithuania' },
{ name: 'Moldova', folder: 'moldova' },
{ name: 'Montenegro', folder: 'montenegro' },
{ name: 'North Macedonia', folder: 'north-macedonia' },
{ name: 'Poland', folder: 'poland' },
{ name: 'Romania', folder: 'romania' },
{ name: 'Russia', folder: 'russia' },
{ name: 'Serbia', folder: 'serbia' },
{ name: 'Slovakia', folder: 'slovakia' },
{ name: 'Slovenia', folder: 'slovenia' },
{ name: 'Tajikistan', folder: 'tajikistan' },
{ name: 'Turkey', folder: 'turkey' },
{ name: 'Turkmenistan', folder: 'turkmenistan' },
{ name: 'Ukraine', folder: 'ukraine' },
{ name: 'Uzbekistan', folder: 'uzbekistan' }
];
// DOM elements
const countrySelect = document.getElementById('country-select');
const chartsContainer = document.getElementById('charts-container');
const countryTitle = document.getElementById('country-title');
const cleanChart = document.getElementById('clean-chart');
const labelsChart = document.getElementById('labels-chart');
const downloadCleanBtn = document.getElementById('download-clean');
const downloadLabelsBtn = document.getElementById('download-labels');
const loadingElement = document.getElementById('loading');
const errorElement = document.getElementById('error');
// Initialize the application
function init() {
populateCountrySelect();
setupEventListeners();
}
// Populate the country select dropdown
function populateCountrySelect() {
countries.forEach(country => {
const option = document.createElement('option');
option.value = country.folder;
option.textContent = country.name;
countrySelect.appendChild(option);
});
}
// Setup event listeners
function setupEventListeners() {
countrySelect.addEventListener('change', handleCountryChange);
downloadCleanBtn.addEventListener('click', () => downloadChart('clean'));
downloadLabelsBtn.addEventListener('click', () => downloadChart('labels'));
}
// Handle country selection change
function handleCountryChange() {
const selectedCountry = countrySelect.value;
if (!selectedCountry) {
hideCharts();
return;
}
loadCharts(selectedCountry);
}
// Load charts for the selected country
function loadCharts(countryFolder) {
showLoading();
hideError();
const countryData = countries.find(c => c.folder === countryFolder);
const countryName = countryData ? countryData.name : countryFolder;
// Set country title
countryTitle.textContent = `${countryName} Climate Stripes`;
// Set image sources
const cleanImagePath = `publish/${countryFolder}/clean.png`;
const labelsImagePath = `publish/${countryFolder}/labels.png`;
// Load images
let imagesLoaded = 0;
let hasError = false;
const checkAllImagesLoaded = () => {
imagesLoaded++;
if (imagesLoaded === 2 && !hasError) {
hideLoading();
showCharts();
}
};
const handleImageError = () => {
hasError = true;
hideLoading();
showError();
hideCharts();
};
// Clean chart
cleanChart.onload = checkAllImagesLoaded;
cleanChart.onerror = handleImageError;
cleanChart.src = cleanImagePath;
// Labels chart
labelsChart.onload = checkAllImagesLoaded;
labelsChart.onerror = handleImageError;
labelsChart.src = labelsImagePath;
}
// Download chart function
async function downloadChart(chartType) {
const selectedCountry = countrySelect.value;
if (!selectedCountry) return;
const countryData = countries.find(c => c.folder === selectedCountry);
const countryName = countryData ? countryData.name : selectedCountry;
const imagePath = `publish/${selectedCountry}/${chartType}.png`;
const fileName = `${countryName.toLowerCase().replace(/\s+/g, '-')}-climate-stripes-${chartType}.png`;
try {
// Fetch the image
const response = await fetch(imagePath);
if (!response.ok) throw new Error('Failed to fetch image');
const blob = await response.blob();
// Create download link
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
// Cleanup
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} catch (error) {
console.error('Download failed:', error);
alert('Failed to download the chart. Please try again.');
}
}
// Show/hide functions
function showLoading() {
loadingElement.style.display = 'block';
}
function hideLoading() {
loadingElement.style.display = 'none';
}
function showCharts() {
chartsContainer.style.display = 'block';
}
function hideCharts() {
chartsContainer.style.display = 'none';
}
function showError() {
errorElement.style.display = 'block';
}
function hideError() {
errorElement.style.display = 'none';
}
// Initialize the application when the DOM is loaded
document.addEventListener('DOMContentLoaded', init);