{
const avg = d3
.rollups(
data,
(v) => ({
count: v.length,
age: d3.mean(v, (d) => +d.age),
avg_overall: d3.mean(v, (d) => +d.overall)
}),
(d) => d.nationality
)
.map(([nationality, values]) => ({
nationality,
...values
}));
const target = html`<svg id="target" viewBox="0 0 ${width} ${height}"></svg>`;
const margin = { left: 50, right: 20, top: 20, bottom: 50 },
iwidth = width - margin.left - margin.right,
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
.scaleLinear()
.domain([0, d3.max(avg, (d) => d.age)])
.range([0, iwidth])
.nice();
const y = d3
.scaleLinear()
.domain([0, d3.max(avg, (d) => d.count)])
.range([iheight, 0])
.nice();
const size = d3
.scaleSqrt()
.domain([85, d3.max(avg, (d) => d.avg_overall)])
.range([1, 10]);
const xAxis = d3.axisBottom(x).ticks(10);
const yAxis = d3.axisLeft(y).ticks(10);
gDrawing
.append("g")
.attr("class", "x--axis")
.attr("transform", `translate(0, ${iheight})`)
.call(xAxis)
.append("text")
.attr("x", iwidth / 2)
.attr("y", 40)
.attr("fill", "black")
.style("text-anchor", "middle")
.text("Age");
gDrawing
.append("g")
.attr("class", "y--axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -iheight / 2)
.attr("y", -40)
.attr("fill", "black")
.style("text-anchor", "middle")
.text("Player Count");
gDrawing
.selectAll("circle")
.data(avg)
.join("circle")
.attr("cx", (d) => x(d.age))
.attr("cy", (d) => y(d.count))
.attr("r", (d) => size(d.avg_overall))
.attr("fill", "green")
.attr("opacity", 0.8);
return target;
}