-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
98 lines (82 loc) · 2.48 KB
/
script.js
File metadata and controls
98 lines (82 loc) · 2.48 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
document.getElementById("input-form").addEventListener("submit", async function (event) {
event.preventDefault();
const latitude = document.getElementById("latitude").value;
const longitude = document.getElementById("longitude").value;
const apiKey = "0WZOCxfi4Tbx3";
await fetchPowerBreakdown(latitude, longitude, apiKey);
});
async function fetchPowerBreakdown(latitude, longitude, apiKey) {
try {
const response = await fetch("http://127.0.0.1:5000/fetch_power", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ latitude, longitude, api_key: apiKey }),
});
if (!response.ok) throw new Error("Failed to fetch data");
const data = await response.json();
if (data.error) {
alert(data.error);
return;
}
const historicalData = data.history;
if (!historicalData || historicalData.length === 0) {
alert("No historical data available.");
return;
}
// Prepare data for the line chart
const labels = historicalData.map((entry) => new Date(entry.datetime).toLocaleString());
const sources = {};
// Collect production values for each source
historicalData.forEach((entry) => {
const production = entry.powerProductionBreakdown;
if (production) {
Object.entries(production).forEach(([source, value]) => {
if (!sources[source]) {
sources[source] = Array(historicalData.length).fill(null);
}
sources[source][historicalData.indexOf(entry)] = value ?? 0;
});
}
});
renderLineChart(labels, sources);
} catch (error) {
alert("Error: " + error.message);
}
}
function renderLineChart(labels, sources) {
const ctx = document.getElementById("renewableChart").getContext("2d");
const datasets = Object.entries(sources).map(([source, data]) => ({
label: source,
data,
borderColor: `rgba(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, 1)`,
fill: false,
tension: 0.1,
}));
new Chart(ctx, {
type: "line",
data: {
labels,
datasets,
},
options: {
responsive: true,
scales: {
x: {
title: {
display: true,
text: "Time",
},
},
y: {
beginAtZero: true,
title: {
display: true,
text: "Energy Production (MW)",
},
},
},
},
});
}