Public
Edited
Sep 24, 2023
1 star
Insert cell
Insert cell
Insert cell
{
const target = html`<svg id="target" viewBox="0 0 ${width} ${height}"></svg>`;

const processedData = process(data);

const margin = { left: 70, right: 20, top: 70, bottom: 100 };
const iwidth = width - margin.left - margin.right;
const iheight = height - margin.top - margin.bottom;

const svg = d3.select(target);
const gDrawing = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

gDrawing
.append("rect")
.attr("width", iwidth)
.attr("height", iheight)
.attr("fill", "#eee");

const x = d3
.scaleBand()
.domain(processedData.map((d) => d.nationality))
.range([0, iwidth])
.padding(0.1);

const y = d3
.scaleLinear()
.domain([15, d3.max(processedData, (d) => d.avgAge)])
.range([iheight, 0])
.nice();

const maxWage = d3.max(processedData, (d) => d.avgWage);

const rScale = d3.scaleLinear().domain([0, maxWage]).range([3, 15]);

gDrawing
.append("g")
.attr("class", "x--axis")
.attr("transform", `translate(0, ${iheight})`)
.call(d3.axisBottom(x))
.selectAll("text")
.attr("transform", "rotate(-90)")
.attr("text-anchor", "end");

gDrawing.append("g").attr("class", "y--axis").call(d3.axisLeft(y));

gDrawing
.append("text")
.attr("x", iwidth / 2)
.attr("y", -30)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text("Nationality vs. Average Age w/ Average Wage (size)");

gDrawing
.append("text")
.attr("x", iwidth / 2)
.attr("y", iheight + 95)
.attr("text-anchor", "middle")
.text("Nationality");

gDrawing
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -iheight / 2)
.attr("y", -60)
.attr("dy", "1em")
.attr("text-anchor", "middle")
.text("Average Age");

gDrawing
.selectAll("circle")
.data(processedData)
.join("circle")
.attr("cx", (d) => x(d.nationality) + x.bandwidth() / 2)
.attr("cy", (d) => y(d.avgAge))
.attr("r", (d) => rScale(d.avgWage))
.attr("fill-opacity", 0.7)
.attr("stroke", "black")
.attr("stroke-width", 0.5)
.attr("fill", "steelblue");

const legendValues = [
{
value: maxWage / 3,
text: `${Math.round(maxWage / 3).toLocaleString()} Euros`
},
{
value: (maxWage * 2) / 3,
text: `${Math.round((maxWage * 2) / 3).toLocaleString()} Euros`
},
{ value: maxWage, text: `${maxWage.toLocaleString()} Euros` }
];

const legendX = iwidth - 150;
const legendY = 0;

const legendG = gDrawing
.append("g")
.attr("transform", `translate(${legendX}, ${legendY})`);

const legendHeight = legendValues.length * 32;

legendG
.append("rect")
.attr("x", -5)
.attr("y", 0)
.attr("width", 155)
.attr("height", legendHeight)
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 0.5);

legendValues.forEach((item, idx) => {
const yPosition = 20 + idx * 30;

legendG
.append("circle")
.attr("cx", 15)
.attr("cy", yPosition)
.attr("r", rScale(item.value))
.attr("fill-opacity", 0.7)
.attr("stroke", "black")
.attr("stroke-width", 0.5)
.attr("fill", "steelblue");

legendG
.append("text")
.attr("x", 35)
.attr("y", yPosition)
.attr("dy", "0.32em")
.text(item.text);
});

return target;
}
Insert cell
function process(data) {
const grouped = d3.group(data, (d) => d.nationality);

const output = [];

grouped.forEach((players, nationality) => {
const totalAge = players.reduce((acc, player) => acc + +player.age, 0);
const totalWage = players.reduce(
(acc, player) => acc + +player.wage_eur,
0
);
const avgAge = totalAge / players.length;
const avgWage = totalWage / players.length;

output.push({
nationality,
playerCount: players.length,
avgAge,
avgWage
});
});

return output;
}
Insert cell
data = FileAttachment("players_20@1.csv").csv()
Insert cell
height = width * 0.6
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
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