Public
Edited
May 1, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
priceByYearByState = {
// Group the array elements by year and state name
const groups = priceByDateByState.reduce((acc, curr) => {
const year = curr.date.slice(0, 4);
const key = `${year}_${curr.stateName}`;
if (!acc[key]) {
acc[key] = {
stateName: curr.stateName,
year: year,
prices: []
};
}
acc[key].prices.push(curr.price);
return acc;
}, {});

// Calculate the average price for each state and year
const result = Object.values(groups).map((group) => {
const { stateName, year, prices } = group;
const sum = prices.reduce((sum, price) => sum + price, 0);
const avgPrice = Number((sum / prices.length).toFixed(2));
return { stateName, year, avgPrice };
});
return result;
}
Insert cell
Insert cell
Insert cell
import { LineChart } from "@d3/multi-line-chart"
Insert cell
import { BarChart } from "@d3/horizontal-bar-chart"
Insert cell
import { DivergingBarChart } from "@d3/diverging-bar-chart"
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