Public
Edited
Sep 19, 2023
1 star
Insert cell
Insert cell
Insert cell
data = FileAttachment("players_20.csv").csv()
Insert cell
players_20.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
function getColumnHeaders(data) {
if (!data || data.length === 0) {
return [];
}
return Object.keys(data[0]);
}

Insert cell
columnHeaders = getColumnHeaders(data)
Insert cell
Insert cell
groupedData
Type Table, then Shift-Enter. Ctrl-space for more options.

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
{
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([0, d3.max(processedData, (d) => d.avgAge)])
.range([iheight, 0])
.nice();

const rScale = d3
.scaleLinear()
.domain([0, d3.max(processedData, (d) => d.avgWage)])
.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");

return target;
}
Insert cell
height = width * 0.6
Insert cell
Insert cell
Insert cell
{
const target = html`<div class='tableauPlaceholder' id='viz1695090707645' style='position: relative'><noscript><a href='#'><img alt='Average Age and Wage by Nationality ' src='https:&#47;&#47;public.tableau.com&#47;static&#47;images&#47;W2&#47;W209-Fall2023-Sec5-Charlton-HW4&#47;Sheet1&#47;1_rss.png' style='border: none' /></a></noscript><object class='tableauViz' style='display:none;'><param name='host_url' value='https%3A%2F%2Fpublic.tableau.com%2F' /> <param name='embed_code_version' value='3' /> <param name='site_root' value='' /><param name='name' value='W209-Fall2023-Sec5-Charlton-HW4&#47;Sheet1' /><param name='tabs' value='no' /><param name='toolbar' value='yes' /><param name='static_image' value='https:&#47;&#47;public.tableau.com&#47;static&#47;images&#47;W2&#47;W209-Fall2023-Sec5-Charlton-HW4&#47;Sheet1&#47;1.png' /> <param name='animate_transition' value='yes' /><param name='display_static_image' value='yes' /><param name='display_spinner' value='yes' /><param name='display_overlay' value='yes' /><param name='display_count' value='yes' /><param name='language' value='en-US' /><param name='filter' value='publish=yes' /></object></div>`;

// Return the div first so the following code can find it
yield target;

var divElement = document.getElementById("viz1695090707645");
var vizElement = divElement.getElementsByTagName("object")[0];
if (divElement.offsetWidth > 800) {
vizElement.style.width = "100%";
vizElement.style.height = divElement.offsetWidth * 0.75 + "px";
} else if (divElement.offsetWidth > 500) {
vizElement.style.width = "100%";
vizElement.style.height = divElement.offsetWidth * 0.75 + "px";
} else {
vizElement.style.width = "100%";
vizElement.style.height = "727px";
}
var scriptElement = document.createElement("script");
scriptElement.src = "https://public.tableau.com/javascripts/api/viz_v1.js";
vizElement.parentNode.insertBefore(scriptElement, vizElement);

yield target;
}
Insert cell
Insert cell
Insert cell
d3 = require("d3@6")
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