-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheurusd.html
394 lines (367 loc) · 17.7 KB
/
eurusd.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
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>Andamento Euro/Dollaro</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- Chart.js -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<!-- Chart.js Annotation Plugin -->
<!-- Chart.js Zoom Plugin -->
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns"></script>
<style>
body {
padding-top: 20px;
}
.container {
max-width: 1400px;
margin: auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #333;
}
.home-button {
text-decoration: none;
color: #ffffff;
background-color: #0073e6;
padding: 10px 15px;
border-radius: 8px;
font-size: 16px;
border: none;
cursor: pointer;
transition: background-color 0.3s ease;
}
.home-button:hover {
background-color: #ebf3fc;
}
#resetZoomButton {
vertical-align: top;
}
.percentage-change {
margin-top: 1rem !important;
display: inline-block;
text-align: left;
vertical-align: middle;
font-size: 18px;
margin-top: 20px;
color: #0073e6;
padding: .375rem .75rem;
background-color: #f0f8ff;
font-weight: 400;
line-height: 1.5;
border-radius: .25rem;
}
</style>
</head>
<body>
<div class="container">
<div>
<a href="/" class="home-button">🏠 Home</a>
</div>
<br>
<h1 class="mt-4 mb-4">Andamento Euro/Dollaro</h1>
<h6 class="text-center">Il cambio Euro/Dollaro rappresenta il valore di un Euro in Dollari.
I dati storici precedenti il 1999, anno di introduzione dell'Euro, sono stati estrapolati dall'ECU (European
Currency Unit),
che era un'unità di conto utilizzata dalle istituzioni europee prima dell'Euro.
Prima dell'ECU, i dati sono stati calcolati come media dei cambi delle valute nazionali europee con il
Dollaro.
</h6>
<div class="row">
<div class="col-md-6">
<label for="startDate">Data Inizio:</label>
<input type="date" id="startDate" class="form-control">
</div>
<div class="col-md-6">
<label for="endDate">Data Fine:</label>
<input type="date" id="endDate" class="form-control">
</div>
</div>
<canvas id="chartCanvas" height="120"></canvas>
<button id="resetZoomButton" class="btn btn-primary mt-3">Reset Zoom</button>
<div class="percentage-change" id="percentageChange">Clicca su un punto e poi su un altro per calcolare la
variazione %</div>
</div>
<script>
$(document).ready(function () {
let chartInstance = null;
let mergedData = [];
let updateTimeout = null;
let minDate = null;
let maxDate = null;
let firstPoint = null;
let secondPoint = null;
function mergeData(historicalData, updatedData) {
const mergedData = [...historicalData];
updatedData.forEach(updatedItem => {
const existingItemIndex = mergedData.findIndex(item => item.Date === updatedItem.Date);
if (existingItemIndex !== -1) {
mergedData[existingItemIndex] = updatedItem;
} else {
mergedData.push(updatedItem);
}
});
return mergedData;
}
function filterDataByDate(data, startDate, endDate) {
return data.filter(item => {
const date = new Date(item.Date);
return date >= new Date(startDate) && date <= new Date(endDate);
});
}
function calculateAverage(data) {
const sum = data.reduce((acc, item) => acc + item, 0);
return sum / data.length;
}
function calculatePercentageChange(start, end) {
return ((end - start) / start) * 100;
}
function updateChart(data) {
const dates = [];
const exchangeRates = [];
const sampleRate = 3;
data.forEach((item, index) => {
if (index % sampleRate === 0) {
dates.push(item.Date);
exchangeRates.push(parseFloat(item.Close));
}
});
const averageRate = calculateAverage(exchangeRates);
const ctx = document.getElementById('chartCanvas').getContext('2d');
if (chartInstance) {
chartInstance.destroy();
}
chartInstance = new Chart(ctx, {
type: 'line',
data: {
labels: dates,
datasets: [
{
label: 'Cambio Euro/Dollaro',
data: exchangeRates,
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
fill: false,
tension: 0.1,
pointRadius: 0
},
]
},
options: {
responsive: true,
interaction: {
mode: 'index',
intersect: false,
},
plugins: {
tooltip: {
mode: 'index',
intersect: false,
},
annotation: {
annotations: {
line0: {
type: 'line',
yMin: 1,
yMax: 1,
borderColor: 'red',
borderWidth: 2,
label: {
content: 'Parità di cambio',
enabled: true,
position: 'start'
}
},
line1: {
type: 'line',
yMin: averageRate,
yMax: averageRate,
borderColor: 'blue',
borderWidth: 1,
label: {
content: 'Media ' + averageRate.toFixed(4),
enabled: true,
position: 'start'
}
}
}
},
zoom: {
pan: {
enabled: true,
mode: 'x'
},
zoom: {
wheel: {
enabled: false,
},
pinch: {
enabled: true
},
mode: 'x',
drag: {
enabled: true,
backgroundColor: 'rgba(0,0,0,0.3)',
}
}
}
},
scales: {
x: {
type: 'time',
time: {
unit: 'month',
tooltipFormat: 'yyyy-MM-dd'
},
title: {
display: true,
text: 'Data'
}
},
y: {
beginAtZero: false,
suggestedMax: Math.max(...exchangeRates) * 1.1,
title: {
display: true,
text: 'Cambio Euro/Dollaro'
}
}
},
onClick: function (event, elements) {
if (elements.length > 0) {
const index = elements[0].index;
const value = exchangeRates[index];
const date = dates[index];
if (!firstPoint) {
firstPoint = { value, date };
$('#percentageChange').text('Primo punto selezionato: ' + value);
chartInstance.options.plugins.annotation.annotations.firstPointLine = {
type: 'line',
scaleID: 'x',
value: date,
borderColor: 'green',
borderWidth: 2,
label: {
content: 'Primo punto' + firstPoint.value.toFixed(4),
enabled: true,
position: 'start'
}
};
chartInstance.update();
} else {
secondPoint = { value, date };
const percentageChange = calculatePercentageChange(firstPoint.value, secondPoint.value);
const initialAmount = 1000;
const dollarsAtFirstPoint = initialAmount * firstPoint.value;
const eurosAtSecondPoint = dollarsAtFirstPoint / secondPoint.value;
const difference = eurosAtSecondPoint - initialAmount;
$('#percentageChange').html(
'Cambio al primo punto: ' + firstPoint.value.toFixed(4) + ' dollari per un euro <br>' +
'Cambio al secondo punto: ' + secondPoint.value.toFixed(4) + ' dollari per un euro<br>' +
'Variazione percentuale: ' + percentageChange.toFixed(2) + '%<br>' +
'Comprando 1000€ avresti ottenuto ' + dollarsAtFirstPoint.toFixed(2) + '$ al primo cambio.<br>' +
'Vendendo quei dollari al secondo cambio avresti ottenuto ' + eurosAtSecondPoint.toFixed(2) + '€.<br>' +
(difference > 0 ? ('Guadagno:') : ('Perdita:')) + difference.toFixed(2) + '€' + ' (' + (difference > 0 ? '+' : '') + (difference / initialAmount * 100).toFixed(2) + '% dell\'investimento iniziale)'
);
chartInstance.options.plugins.annotation.annotations.secondPointLine = {
type: 'line',
scaleID: 'x',
value: date,
borderColor: 'red',
borderWidth: 2,
label: {
content: 'Secondo punto' + secondPoint.value.toFixed(4),
enabled: true,
position: 'start'
}
};
chartInstance.options.plugins.annotation.annotations.areaBetweenPoints = {
type: 'box',
xMin: firstPoint.date,
xMax: secondPoint.date,
backgroundColor: 'rgba(0, 255, 0, 0.1)',
borderColor: 'rgba(0, 255, 0, 0.25)',
borderWidth: 1
};
chartInstance.update();
firstPoint = null;
secondPoint = null;
}
}
}
}
});
}
Promise.all([
fetch('json/cambio_eur_usd_storico.json').then(response => {
if (!response.ok) {
throw new Error("Errore nel caricamento del dataset storico: " + response.statusText);
}
return response.json();
}),
fetch('json/cambio_eur_usd_updated.json').then(response => {
if (!response.ok) {
throw new Error("Errore nel caricamento del dataset aggiornato: " + response.statusText);
}
return response.json();
})
])
.then(([historicalData, updatedData]) => {
mergedData = mergeData(historicalData, updatedData);
minDate = new Date(Math.min(...mergedData.map(item => new Date(item.Date))));
maxDate = new Date(Math.max(...mergedData.map(item => new Date(item.Date))));
$('#startDate').val(minDate.toISOString().split('T')[0]);
$('#endDate').val(maxDate.toISOString().split('T')[0]);
$('#startDate').attr('min', minDate.toISOString().split('T')[0]);
$('#startDate').attr('max', maxDate.toISOString().split('T')[0]);
$('#endDate').attr('min', minDate.toISOString().split('T')[0]);
$('#endDate').attr('max', maxDate.toISOString().split('T')[0]);
updateChart(mergedData);
})
.catch(error => {
console.error("Errore durante il caricamento dei dataset:", error);
});
$('#startDate, #endDate').change(function () {
clearTimeout(updateTimeout);
updateTimeout = setTimeout(() => {
const startDate = $('#startDate').val();
const endDate = $('#endDate').val();
if (new Date(startDate) < minDate || new Date(endDate) > maxDate || startDate == '' || endDate == '') {
alert('Le date selezionate devono essere comprese tra ' + minDate.toISOString().split('T')[0] + ' e ' + maxDate.toISOString().split('T')[0]);
$('#startDate').val(minDate.toISOString().split('T')[0]);
$('#endDate').val(maxDate.toISOString().split('T')[0]);
return;
}
const filteredData = filterDataByDate(mergedData, startDate, endDate);
updateChart(filteredData);
}, 500);
});
$('#resetZoomButton').click(function () {
if (chartInstance) {
chartInstance.resetZoom();
// Reset delle annotazioni e delle variabili dei punti selezionati
chartInstance.options.plugins.annotation.annotations.firstPointLine = null;
chartInstance.options.plugins.annotation.annotations.secondPointLine = null;
chartInstance.options.plugins.annotation.annotations.areaBetweenPoints = null;
chartInstance.update();
firstPoint = null;
secondPoint = null;
$('#percentageChange').text('Clicca su un punto e poi su un altro per calcolare la variazione %');
}
});
});
</script>
<!-- Bootstrap JS (con Popper) -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
</body>
</html>