Public
Edited
Dec 3, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Inputs.table(df.filter(d => +d.rating_match1 + +d.rating_match2 + +d.rating_match3 + +d.rating_match4 + +d.rating_match5 + +d.rating_match6 + +d.rating_match7 > 0).filter(d => position.includes(d.position)), {columns: ["team", "name", "position", "club", "league", "avgRating", "gamesPlayed", "rating_match1", "rating_match2", "rating_match3", "rating_match4", "rating_match5", "rating_match6", "rating_match7"], sort: "avgRating", reverse: true})
Insert cell
positions = new Set(df.map(d => d.position))
Insert cell
width
Insert cell
startingPosition = function(numPlayers) {
if(numPlayers == 5){return 1}
if(numPlayers == 4){return 1.5}
if(numPlayers == 3){return 2}
if(numPlayers == 2){return 2.5}
if(numPlayers == 1){return 3}

}
Insert cell
height = 700
Insert cell
x = d3.scaleLinear()
.domain([1, 5])
.range([150, width - 150])
Insert cell
y = d3.scaleBand()
.domain(["Goalkeeper", "Defender", "Midfielder", "Forward"])
.range([0, height])
Insert cell
y("Defender")
Insert cell
dreamTeam = [
topPlayerPerPosition('Goalkeeper'),
topPlayerPerPosition('Defender'),
topPlayerPerPosition('Midfielder'),
topPlayerPerPosition('Forward')
]
Insert cell
topPlayers = {
let combined = [].concat(topGoalkeeper, topDefenders, topMidfielders, topForwards)
combined.forEach((o, i) => o.overallId = i + 1);
return(combined)
}
Insert cell
maxGames = d3.max(df, d => d.gamesPlayed)
Insert cell
topGoalkeeper = {
update;
let filteredData = df.filter(d => d.avgRating > 0 & d.position == "Goalkeeper" & d.gamesPlayed >= (maxGames / 2)).sort(function(a, b) {
let random1 = Math.random();
let random2 = Math.random();
return d3.descending(+a.avgRating, +b.avgRating) || d3.descending(+a.gamesPlayed, +b.gamesPlayed) || d3.descending(random1, random2);
}).slice(0, 1)
filteredData.forEach(d => d.id = 3);
return filteredData;
}
Insert cell
topDefenders = {
update;
let numDefenders = formation.split("-")[0];
let filteredData = df.filter(d => d.avgRating > 0 & d.position == "Defender" & d.gamesPlayed >= (maxGames / 2)).sort(function(a, b) {
let random1 = Math.random();
let random2 = Math.random();
return d3.descending(+a.avgRating, +b.avgRating) || d3.descending(+a.gamesPlayed, +b.gamesPlayed) || d3.descending(random1, random2);
}).slice(0, numDefenders)
filteredData.forEach((o, i) => o.id = i + startingPosition(numDefenders));
return filteredData;
}
Insert cell
topMidfielders = {
update;
let numMidfielders = formation.split("-")[1];
let filteredData = df.filter(d => d.avgRating > 0 & d.position == "Midfielder" & d.gamesPlayed >= (maxGames / 2)).sort(function(a, b) {
let random1 = Math.random();
let random2 = Math.random();
return d3.descending(+a.avgRating, +b.avgRating) || d3.descending(+a.gamesPlayed, +b.gamesPlayed) || d3.descending(random1, random2);
}).slice(0, numMidfielders)
filteredData.forEach((o, i) => o.id = i + startingPosition(numMidfielders));
return filteredData;
}
Insert cell
topForwards = {
update;
let numForwards = formation.split("-")[2];
let filteredData = df.filter(d => d.avgRating > 0 & d.position == "Forward" & d.gamesPlayed >= (maxGames / 2)).sort(function(a, b) {
let random1 = Math.random();
let random2 = Math.random();
return d3.descending(+a.avgRating, +b.avgRating)|| d3.descending(+a.gamesPlayed, +b.gamesPlayed) || d3.descending(random1, random2);
}).slice(0, numForwards)
filteredData.forEach((o, i) => o.id = i + startingPosition(numForwards));
return filteredData;
}
Insert cell
topPlayerPerPosition = function(position) {
let data = df.filter(d => d.position == position);

let maxRating = d3.max(data.map(d => d.avgRating));
let topPlayer = data.filter(d => d.avgRating == maxRating).map(d => d.name);
return({
name: topPlayer,
position: position,
})


}
Insert cell
df.forEach(function(d) {
let gamesPlayed = +(d.rating_match1 != "") + +(d.rating_match2 != "") + +(d.rating_match3 != "") + +(d.rating_match4 != "") + +(d.rating_match5 != "") + +(d.rating_match6 != "") + +(d.rating_match7 != "");

let ratingSum = +d.rating_match1 + +d.rating_match2 + +d.rating_match3 + +d.rating_match4 + +d.rating_match5 + +d.rating_match6 + +d.rating_match7;
d.gamesPlayed = gamesPlayed;
d.avgRating = ratingSum / gamesPlayed;
if(d.position == "Forward (winger)") {d.position = "Forward"};
});
Insert cell
import {data as df} from "@joshua-feldman/euro-2020-squads-by-age"
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