Public
Edited
Sep 23, 2023
Insert cell
Insert cell
Insert cell
data = FileAttachment("players_20.csv").csv()
Insert cell
dataMetrics = d3.rollup(
data,
(v) => ({
player_count: v.length,
avg_age: d3.mean(v, (d) => d.age)
}),
(d) => d.nationality
)
Insert cell
{
const target = html`<svg id="target" viewBox="0 0 ${width} ${height}"></svg>`;

const margin = { left: 80, 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(data, (d) => d.potential)])
.range([0, iwidth])
.nice();

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

const color = d3.scaleOrdinal(d3.schemeAccent);
// .domain(Array.from(new Set(data.map(d => d.profession)).values()));

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

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

const circles = gDrawing
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", (d) => x(d.potential))
.attr("cy", (d) => y(d.wage_eur))
.attr("r", 5)
.attr("fill", (d) => color(d.nationality));

//Add a Title
svg
.append("text")
.attr("x", width / 2)
.attr("y", 55)
.text("Player's Wage vs Potential, Colored By Nationality")
.style("font-size", "22px")
.attr("text-anchor", "middle");

//Add an X-Axis Label
svg
.append("text")
.attr("class", "x label")
.attr("x", width / 2)
.attr("y", height - 6)
.text("Player's 'Potential' Rating")
.style("font-size", "14px")
.attr("text-anchor", "middle");

//Add a Y-Axis Label
gDrawing
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x", 0 - height / 2)
.attr("dy", "1em")
.style("font-size", "14px")
.style("text-anchor", "middle")
.text("Player's Wage (€)");

return target;
}
Insert cell
height = width * 0.6
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