Public
Edited
Jan 26
Insert cell
Insert cell
Insert cell
data = FileAttachment("players_20.csv").csv()
Insert cell
d3 = require("d3@6")

Insert cell
groupNationality = d3.group(data, (d) => d.nationality)

Insert cell
{
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]);


// Axes
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");

// Data binding and scatter plot
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;
}
Insert cell
height = width * 0.5
Insert cell
Insert cell
https://colab.research.google.com/drive/1XgtJbbVPRLJ90qqLcnfs4HGhsCPPH6vc?usp=sharing
Insert cell
Insert cell
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