Public
Edited
Dec 4
Insert cell
Insert cell
Insert cell
Insert cell
movingAverageData = computeMovingAverage(solarData, mawindow);
Insert cell
// Fetch the data from the Fraunhofer API using a Glitch proxy
solarData = {
const proxyUrl = "https://proxyme.glitch.me/proxy?url=";
const targetUrl = "https://api.energy-charts.info/solar_share_daily_avg?country=de";
const url = proxyUrl + encodeURIComponent(targetUrl);

return fetch(url, { headers: { accept: "application/json" } })
.then(response => {
if (!response.ok) throw new Error(`network error: ${response.statusText}`);
return response.json();
})
.then(data => {
// Converting to d3 format
return data.days.map((d, i) => ({
date: new Date(d.split('.').reverse().join('-')),
value: data.data[i]
}));
})
.catch(error => {
console.error("Error fetching data:", error);
return [];
});
}
Insert cell
function computeMovingAverage(data, windowSize) {
return data.map((d, i, arr) => {
const start = Math.max(0, i - windowSize + 1);
const subset = arr.slice(start, i + 1);
return {
date: d.date,
value: d3.mean(subset, x => x.value)
};
});
}
Insert cell
import {d3} from "https://d3js.org/d3.v7.min.js"
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more