Public
Edited
Sep 14, 2024
Insert cell
Insert cell
viewof sel = navio(data, { attribWidth: 20 })
Insert cell
{
const target = html`<svg id="target" viewBox="0 0 ${width} ${height}">
</svg>`;

const svg = d3.select(target);

for (let d of data) {
svg
.append("circle")
.attr("cx", d.skill_fk_accuracy * 10)
.attr("cy", d.skill_ball_control * 5)
.attr("r", 5)
.attr("fill", "steelblue");
}

return target;
}
Insert cell
md`## Axis`
Insert cell
{
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(data, (d) => d.skill_fk_accuracy * 10)])
.range([0, iwidth])
.nice();

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

const color = d3.scaleOrdinal(d3.schemeAccent);

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.skill_fk_accuracy * 10))
.attr("cy", (d) => y(d.skill_ball_control * 5))
.attr("r", 5)
.attr("fill", (d) => color(d.body_type));

// Add X-axis label
gDrawing
.append("text")
.attr("x", iwidth / 2)
.attr("y", iheight + 40)
.attr("text-anchor", "middle")
.text("FK Accuracy");

// Add Y-axis label
gDrawing
.append("text")
.attr("x", -(iheight / 2))
.attr("y", -35)
.attr("text-anchor", "middle")
.attr("transform", "rotate(-90)")
.text("Ball Control");

// Create a legend and move it halfway up on the right side of the graph
const legend = svg
.append("g")
.attr("class", "legend")
.attr(
"transform",
`translate(${iwidth + margin.left - 100}, ${iheight / 2})`
);

const legendData = Array.from(new Set(data.map((d) => d.body_type))); // Extract unique body types

// Add legend title
legend.append("text").attr("x", 0).attr("y", 0).text("Body Type");

// Add legend items
legend
.selectAll("legend-item")
.data(legendData)
.join("g")
.attr("class", "legend-item")
.attr("transform", (d, i) => `translate(0, ${20 * (i + 1)})`)
.each(function (d) {
const g = d3.select(this);
g.append("circle")
.attr("r", 5)
.attr("cx", 0)
.attr("cy", -5)
.attr("fill", color(d));
g.append("text").attr("x", 10).attr("y", 0).attr("dy", "0.32em").text(d);
});

return target;
}
Insert cell
height = width * 0.5
Insert cell
dataGrouped = d3.group(data, (d) => d.nationality)
Insert cell
import { navio } from "@john-guerra/navio"
Insert cell
Insert cell
data = FileAttachment("players_20.csv").csv()
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