-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
101 lines (94 loc) · 2.56 KB
/
index.html
File metadata and controls
101 lines (94 loc) · 2.56 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
<!DOCTYPE html>
<html lang="fi">
<head>
<meta charset="UTF-8">
<title>Pörssisähkön tuntihinnat</title>
<style>
body {
font-family: sans-serif;
background: #f5f5f5;
margin: 0;
padding: 20px;
text-align: center;
}
h1 {
margin-bottom: 10px;
}
.chart {
display: flex;
align-items: flex-end;
height: 300px;
margin-top: 20px;
border-left: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
.bar {
flex: 1;
margin: 0 1px;
transition: height 0.3s;
position: relative;
}
.bar span {
position: absolute;
bottom: 100%;
font-size: 10px;
transform: rotate(-45deg);
transform-origin: bottom left;
}
.bar:hover::after {
content: attr(title);
position: absolute;
top: -20px;
left: 0;
right: 0;
background: #333;
color: #fff;
font-size: 10px;
padding: 2px;
border-radius: 3px;
}
</style>
</head>
<body>
<h1>Pörssisähkön tuntihinnat tänään</h1>
<div class="chart" id="chart"></div>
<script>
async function fetchPrices() {
try {
const res = await fetch('https://api.porssisahko.net/v1/latest-prices');
const data = await res.json();
const today = new Date().toISOString().slice(0, 10);
const hourlyPrices = data.prices
.filter(p => p.startDate.includes(today))
.map(p => p.price);
const chart = document.getElementById('chart');
const maxPrice = Math.max(...hourlyPrices);
hourlyPrices.forEach((price, hour) => {
const bar = document.createElement('div');
bar.className = 'bar';
bar.style.height = `${(price / maxPrice) * 100}%`;
bar.title = `${hour}:00 – ${price.toFixed(2)} c/kWh`;
// Color logic
if (price < 3) {
bar.style.background = '#4caf50'; // Green
} else if (price < 5) {
bar.style.background = '#ffeb3b'; // Yellow
} else if (price < 8) {
bar.style.background = '#ff9800'; // Orange
} else {
bar.style.background = '#f44336'; // Red
}
const label = document.createElement('span');
label.textContent = `${hour}:00`;
bar.appendChild(label);
chart.appendChild(bar);
});
} catch (err) {
console.error('Fetch error:', err);
document.getElementById('chart').textContent = 'Hinnan haku epäonnistui.';
}
}
fetchPrices();
</script>
</body>
</html>