Public
Edited
May 3, 2024
17 forks
164 stars
Insert cell
Insert cell
athletes = [
{name: "Floyd Mayweather", sport: "Boxing", nation: "United States", earnings: 285},
{name: "Lionel Messi", sport: "Soccer", nation: "Argentina", earnings: 111},
{name: "Cristiano Ronaldo", sport: "Soccer", nation: "Portugal", earnings: 108},
{name: "Conor McGregor", sport: "MMA", nation: "Ireland", earnings: 99},
{name: "Neymar", sport: "Soccer", nation: "Brazil", earnings: 90},
{name: "LeBron James", sport: "Basketball", nation: "United States", earnings: 85.5},
{name: "Roger Federer", sport: "Tennis", nation: "Switzerland", earnings: 77.2},
{name: "Stephen Curry", sport: "Basketball", nation: "United States", earnings: 76.9},
{name: "Matt Ryan", sport: "Football", nation: "United States", earnings: 67.3},
{name: "Matthew Stafford", sport: "Football", nation: "United States", earnings: 59.5}
]
Insert cell
Insert cell
athletesBySport = d3.group(athletes, d => d.sport)
Insert cell
Insert cell
athletesBySport.get("Basketball")
Insert cell
Insert cell
<table>
<thead>
<tr><th>Sport</th><th>Athletes</th></tr>
</thead>
<tbody>${Array.from(athletesBySport, ([key, values]) => html`
<tr>
<td>${key}</td>
<td>${values.map(d => d.name).join(", ")}</td>
</tr>`)}</tbody>
</table>
Insert cell
Insert cell
athletesBySport.get("Soccer")
Insert cell
Insert cell
athletesByNationSport = d3.group(athletes, d => d.nation, d => d.sport)
Insert cell
Insert cell
athletesByNationSport.get("United States").get("Boxing")
Insert cell
Insert cell
d3.group(athletes, d => d.name[0])
Insert cell
Insert cell
d3.groups(athletes, d => d.name)
Insert cell
d3.groups(athletes, d => d.name, d => d.sport)
Insert cell
Insert cell
Insert cell
transactions = [
{name: "Jim", amount: 3400, date: new Date("2015-11-12")},
{name: "Carl", amount: 12011, date: new Date("2015-11-12")},
{name: "Carl", amount: 2391, date: new Date("2015-11-12")},
{name: "Stacy", amount: 3405, date: new Date("2015-10-09")},
{name: "Stacy", amount: 1201, date: new Date("2016-01-04")}
]
Insert cell
Insert cell
d3.group(transactions, d => d.date) // 👍 Dates can be equal!
Insert cell
Insert cell
d3.rollup(athletes, v => v.length, d => d.sport)
Insert cell
Insert cell
d3.rollup(athletes, v => d3.sum(v, d => d.earnings), d => d.sport)
Insert cell
Insert cell
d3.least(d3.rollup(athletes, v => d3.sum(v, d => d.earnings), d => d.sport), ([, sum]) => -sum)
Insert cell
Insert cell
d3.least(d3.group(athletes, d => d.sport), ([, v]) => -d3.sum(v, d => d.earnings))
Insert cell
Insert cell
d3.rollup(athletes, v => v.length, d => d.nation, d => d.sport)
Insert cell
Insert cell
d3.rollups(athletes, v => d3.sum(v, d => d.earnings), d => d.sport)
Insert cell
Insert cell
Insert cell
index = d3.index(athletes, d => d.name)
Insert cell
Insert cell
facts = [
{about: "Neymar", fact: "Neymar is Neymar da Silva Santos Júnior"},
{about: "Roger Federer", fact: "Federer has won 20 Grand Slam men's singles titles"},
{about: "Megan Rapinoe", fact: "Rapinoe was named The Best FIFA Women's Player in 2019"}
]
Insert cell
Insert cell
facts.map(({about: name, fact}) => ({fact, name, ...index.get(name)}))
Insert cell
Insert cell
{
const index = d3.index(facts, d => d.about);
return athletes.map(({name, ...values}) => ({name, fact: index.get(name)?.fact}))
}
Insert cell
{
const index = d3.index(facts, d => d.about);
return athletes.map(({name, ...values}) => ({name, fact: index.get(name)?.fact, ...values}))
}
Insert cell
Insert cell
d3.index(athletes, d => d.nation)
Insert cell
Insert cell
nn = d3.index(athletes, d => d.nation, d => d.sport, d => d.name)
Insert cell
nn.get("United States")?.get("Basketball")
Insert cell
nn.get("United States")?.get("Basketball")?.get("LeBron James")
Insert cell
nn.get("United States")?.get("Soccer")?.get("Rapinoe")
Insert cell
Insert cell
d3.indexes(athletes, d => d.nation, d => d.name)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more