TanStack
TanStack source
54 chart lines · 1 file · 1.6 kBBenchmark harness excluded · shared/mount.ts
Palmer penguins344 records · CSV · 13.5 kB
@charts-poc/demo-data/penguinsSelection: Complete published snapshot
- species
- string
- island
- string
- culmen_length_mm
- number | null
- culmen_depth_mm
- number | null
- flipper_length_mm
- number | null
- body_mass_g
- number | null
- sex
- string | null
Dr. Kristen Gorman / Palmer Station LTER@observablehq/sample-datasets@1.0.1 · revision 732c0148de74 · penguins.csv · ISC distribution; upstream source credited · SHA-256 dfee817d1c14Pinned snapshot
cases/bar-grouped/tanstack.ts54 lines · entry
cases/bar-grouped/tanstack.ts
import { penguins } from '@charts-poc/demo-data/penguins'
import { barY, colorLegend, defineChart, group } from '@tanstack/charts'
import { rollups } from 'd3-array'
import { scaleBand, scaleLinear } from 'd3-scale'
import { tanstackMount } from '../../shared/mount'
import type { ConformanceInput } from '../../types'
const sexDomain = ['FEMALE', 'MALE']
const sexColors = ['#2563eb', '#f97316']
const definition = (input: ConformanceInput) =>
defineChart(({ width }) => {
const rows = rollups(
penguins
.slice(0, penguins.length - input.revision * 12)
.filter((row) => row.sex !== null),
(values) => values.length,
(row) => row.species,
(row) => row.sex,
).flatMap(([species, groups]) =>
groups.map(([sex, count]) => ({ species, sex, count })),
)
return {
marks: [
barY(rows, {
x: 'species',
y: 'count',
color: 'sex',
layout: group({
scale: scaleBand<string>().domain(sexDomain).paddingInner(0.08),
}),
inset: 1,
}),
],
x: {
scale: () => scaleBand<string>().paddingInner(0.14).paddingOuter(0.06),
axis: { tickLabels: { rotate: width < 640 ? -32 : 0 } },
},
y: {
scale: scaleLinear,
grid: true,
axis: { ticks: { count: 5 }, label: 'Penguins' },
},
color: {
range: sexColors,
legend: colorLegend({
label: 'Sex',
}),
},
}
})
export const mount = tanstackMount(definition, 'Penguins grouped by species')