Source
74 chart lines · 125 data-selection lines · 3 files · 5.5 kBBenchmark harness excluded · shared/mount.ts
HMS Beagle voyage303 records · CSV · 4.1 kB
@charts-poc/demo-data/beagleSelection: Complete published snapshot
- 0
- number
- 1
- number
Charles Darwin's voyage on the HMS Beagleobservablehq/plot@356f579b1d94 · revision 356f579b1d94 · test/data/beagle.csv · Observable Plot repository MIT; upstream source credited · SHA-256 338919940e13Pinned snapshot
cases/105-route-map/tanstack.ts53 lines · entry
cases/105-route-map/tanstack.ts
import { defineChart } from '@tanstack/charts'
import { geoShape } from '@tanstack/charts/geo'
import { geoEqualEarth } from 'd3-geo'
import {
detailedWorldLand,
worldGraticule,
worldSphere,
} from '../../shared/fixtures/country-atlas'
import { beagleRoute } from './transform'
import { tanstackMount } from '../../shared/mount'
import type { ConformanceInput } from '../../types'
const routeColors = ['#dc2626', '#2563eb']
const projection = {
type: () => geoEqualEarth().rotate([-10, 0]),
fit: 'sphere' as const,
}
const definition = (input: ConformanceInput) =>
defineChart({
marks: [
geoShape([detailedWorldLand], {
projection,
fill: '#e2e8f0',
stroke: '#ffffff',
strokeWidth: 0.5,
}),
geoShape([worldGraticule], {
projection,
fill: 'none',
stroke: 'currentColor',
strokeOpacity: 0.2,
strokeWidth: 0.5,
}),
geoShape([beagleRoute], {
projection,
fill: 'none',
stroke: routeColors[input.revision % 2] ?? routeColors[0],
strokeWidth: 2,
strokeOpacity: 0.9,
}),
geoShape([worldSphere], {
projection,
fill: 'none',
stroke: 'currentColor',
strokeOpacity: 0.4,
strokeWidth: 0.75,
}),
],
margin: 10,
})
export const mount = tanstackMount(definition, 'HMS Beagle voyage')cases/105-route-map/transform.ts21 lines · support
cases/105-route-map/transform.ts
import { beagle } from '@charts-poc/demo-data/beagle'
import type { ExtendedFeature, GeoGeometryObjects } from 'd3-geo'
type RouteLineString = Extract<GeoGeometryObjects, { type: 'LineString' }>
export type BeagleRoute = ExtendedFeature<
RouteLineString,
{ name: 'HMS Beagle voyage' }
>
export const beagleRoute: BeagleRoute = {
type: 'Feature',
id: 'hms-beagle',
properties: {
name: 'HMS Beagle voyage',
},
geometry: {
type: 'LineString',
coordinates: beagle.map(([longitude, latitude]) => [longitude, latitude]),
},
}shared/fixtures/country-atlas.ts125 lines · fixture
shared/fixtures/country-atlas.ts
import countriesAtlasJson from 'world-atlas/countries-110m.json'
import landAtlasJson from 'world-atlas/land-110m.json'
import detailedLandAtlasJson from 'world-atlas/land-50m.json'
import { geoGraticule10 } from 'd3-geo'
import { feature } from 'topojson-client'
import type {
ExtendedFeature,
ExtendedFeatureCollection,
GeoGeometryObjects,
GeoSphere,
} from 'd3-geo'
type AtlasTopology = Parameters<typeof feature>[0]
export type CountryGeometry = Extract<
GeoGeometryObjects,
{ type: 'Polygon' | 'MultiPolygon' }
>
export interface CountryProperties {
name: string
}
export type CountryFeature = ExtendedFeature<CountryGeometry, CountryProperties>
export type LandFeature = ExtendedFeature<CountryGeometry, Record<never, never>>
export const worldSphere: GeoSphere = { type: 'Sphere' }
export const worldGraticule = geoGraticule10()
const countriesTopology = atlasTopology(
countriesAtlasJson,
'world-atlas countries-110m',
)
const countriesObject = countriesTopology.objects.countries
if (!countriesObject) {
throw new TypeError('world-atlas countries-110m is missing countries')
}
const convertedCountries = feature(countriesTopology, countriesObject)
if (convertedCountries.type !== 'FeatureCollection') {
throw new TypeError('world-atlas countries did not produce a collection')
}
export const worldCountries: readonly CountryFeature[] =
convertedCountries.features.flatMap<CountryFeature>((entry) => {
if (
!isCountryGeometry(entry.geometry) ||
!isRecord(entry.properties) ||
typeof entry.properties.name !== 'string'
) {
return []
}
return [
{
type: 'Feature',
id: entry.id === undefined ? entry.properties.name : String(entry.id),
geometry: entry.geometry,
properties: {
name: entry.properties.name,
},
},
]
})
if (worldCountries.length !== 177) {
throw new TypeError(
`Expected 177 world-atlas countries, got ${worldCountries.length}`,
)
}
export const worldCountryCollection: ExtendedFeatureCollection<CountryFeature> =
{
type: 'FeatureCollection',
features: [...worldCountries],
}
export const worldLand = convertLand(landAtlasJson, 'world-atlas land-110m')
export const detailedWorldLand = convertLand(
detailedLandAtlasJson,
'world-atlas land-50m',
)
function atlasTopology(value: unknown, label: string): AtlasTopology {
if (
!isRecord(value) ||
value.type !== 'Topology' ||
!Array.isArray(value.arcs) ||
!isRecord(value.objects)
) {
throw new TypeError(`${label} is not valid TopoJSON`)
}
return value as unknown as AtlasTopology
}
function convertLand(value: unknown, label: string): LandFeature {
const topology = atlasTopology(value, label)
const landObject = topology.objects.land
if (!landObject) {
throw new TypeError(`${label} is missing land`)
}
const converted = feature(topology, landObject)
const land =
converted.type === 'FeatureCollection' ? converted.features[0] : converted
if (!land || land.type !== 'Feature' || !isCountryGeometry(land.geometry)) {
throw new TypeError(`${label} did not produce polygon geometry`)
}
return {
type: 'Feature',
geometry: land.geometry,
properties: {},
}
}
function isCountryGeometry(
geometry: GeoGeometryObjects,
): geometry is CountryGeometry {
return geometry.type === 'Polygon' || geometry.type === 'MultiPolygon'
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}