-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
117 lines (115 loc) · 3.48 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
<!DOCTYPE html>
<html>
<head>
<title>Electricity spot price</title>
<meta charset="utf-8" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<style media="screen">
/* Add space between Vega-Embed links */
.vega-actions a {
margin-right: 5px;
}
</style>
</head>
<body>
<p>Spot prices for <span id="priceArea"></span></p>
<div id="vis" class="vega-embed" style="width: 80%;"></div>
<script>
const size = 20;
var vlSpec = {
$schema: "https://vega.github.io/schema/vega-lite/v5.json",
width: "container",
height: 2000,
mark: "bar",
encoding: {
y: {
field: "HourDK",
type: "ordinal",
},
x: {
field: "SpotPriceDKK",
type: "quantitative",
axis: {
title: "Spot price [DKK/kWh]",
},
},
},
layer: [
{
mark: "bar",
encoding: {
color: {
field: "SpotPriceDKK",
scale: {
domain: [0, 3, 6],
range: ["green", "red", "black"],
type: "linear",
},
legend: null,
},
},
},
{
mark: "rule",
encoding: {
y: {
field: "HourDK",
},
size: {
condition: [
{
value: "5",
test: {
field: "IsCurrent",
equal: true,
},
},
],
value: 0,
},
},
},
{
mark: {
type: "text",
align: "left",
baseline: "middle",
dx: 3,
fontSize: size
},
encoding: {
text: {
field: "SpotPriceDKK",
type: "quantitative",
},
},
},
],
};
vlSpec.config = {axisY: {titleFontSize: size, labelFontSize: size}, axisX: {titleFontSize: size, labelFontSize: size}};
const priceArea = (new URLSearchParams(window.location.search)).get("priceArea") ?? "DK2";
document.getElementById("priceArea").innerHTML = priceArea;
fetch("https://api.energidataservice.dk/dataset/Elspotprices?limit=48&filter={%22PriceArea%22:%22" + priceArea + "%22}")
.then((x) => x.json())
.then((data) => {
data = data["records"];
const now = new Date();
data.forEach((record) => {
const hour = record["HourDK"];
record["HourDK"] = hour.substring(5, 10) + " " + hour.substring(11, 16);
record["SpotPriceDKK"] = (record["SpotPriceDKK"] / 1000).toFixed(2);
const dataDate = parseInt(hour.substring(8, 10));
const dataHour = parseInt(hour.substring(11, 13));
record["IsCurrent"] = now.getDate() == dataDate && now.getHours() == dataHour;
});
vlSpec.data = {
values: data,
};
// Embed the visualization in the container with id `vis`
vegaEmbed("#vis", vlSpec);
});
</script>
</body>
</html>