Public
Edited
Jul 4, 2023
Insert cell
Insert cell
function assignDates(year, values) {
return values.map((value, index) => {
const date = new Date(year, 0, 1); // Starts with January 1st
date.setDate(date.getDate() + index); // Adds the number of days (index)

return {
date: date,
value: value
};
});
}

Insert cell
new Date(2023, 0, 1)
Insert cell
Insert cell
dataTest = FileAttachment(
"cams_gfas_daily_carbon_data_2003_2023_canada.csv"
).csv({
typed: true
})
Insert cell
data2 = {
const data = await FileAttachment(
"cams_gfas_daily_carbon_data_2003_2023_canada.csv"
).csv({
typed: true
});

const dataByYear = data.flatMap((d) => {
const year = d["Year"];

const { Year, Country, ...dateValues } = d;

let dateArray = [];

for (let index = 1; index < 365; index++) {
if (dateValues[index]) {
dateArray.push(dateValues[index]);
} else {
dateArray.push(0);
}
}

let sum = 0;
let cumulativeDateArray = dateArray.map((num) => (sum += num));

const datedData = assignDates(2023, cumulativeDateArray);

return datedData.map((d) => ({ ...d, year: Year.toString() }));
});

return dataByYear;
}
Insert cell
[...new Set(data2.map((d) => d.year))]
Insert cell
Plot.plot({
y: { domain: [0, 180], grid: true },
marks: [
Plot.ruleY([0]),
Plot.lineY(data2, { x: "date", y: "value", stroke: "year", z: "year" })
]
})
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