Source

51 chart lines · 125 data-selection lines · 2 files · 4.9 kBBenchmark harness excluded · shared/mount.ts

cases/104-orthographic-globe/tanstack.ts51 lines · entry
cases/104-orthographic-globe/tanstack.ts
import { defineChart } from '@tanstack/charts'
import { geoShape } from '@tanstack/charts/geo'
import { geoOrthographic } from 'd3-geo'
import {
  worldGraticule,
  worldLand,
  worldSphere,
} from '../../shared/fixtures/country-atlas'
import { tanstackMount } from '../../shared/mount'
import type { ConformanceInput } from '../../types'

const definition = (input: ConformanceInput) =>
  defineChart({
    marks: [
      geoShape([worldSphere], {
        projection: {
          type: () => geoOrthographic().rotate([0, -30, 20]),
          fit: 'sphere',
        },
        fill: '#dbeafe',
        stroke: '#64748b',
        strokeWidth: 1.25,
      }),
      geoShape([worldGraticule], {
        projection: {
          type: () => geoOrthographic().rotate([0, -30, 20]),
          fit: 'sphere',
        },
        fill: 'none',
        stroke: '#94a3b8',
        strokeOpacity: 0.5,
        strokeWidth: 0.75,
      }),
      geoShape([worldLand], {
        projection: {
          type: () => geoOrthographic().rotate([0, -30, 20]),
          fit: 'sphere',
        },
        fill: input.revision % 2 === 0 ? '#22c55e' : '#0d9488',
        fillOpacity: 0.82,
        stroke: '#f8fafc',
        strokeWidth: 0.75,
      }),
    ],
    margin: 10,
  })

export const mount = tanstackMount(
  definition,
  'Orthographic globe with graticule',
)
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
}