{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "geo-routes",
  "type": "registry:component",
  "title": "Geo Routes",
  "description": "A geo-map layer of journeys and flows between places: great-circle routes (the shortest way over the globe) or arcs drawn over the map, thicker for larger amounts, with an arrow for the direction; each read by arrow keys and listed in a table.",
  "categories": [
    "viz"
  ],
  "dependencies": [
    "d3-geo",
    "d3-scale"
  ],
  "devDependencies": [
    "@types/d3-geo",
    "@types/d3-scale"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/chart",
    "@rivier-ui/cn",
    "@rivier-ui/geo-map"
  ],
  "files": [
    {
      "path": "registry/viz/geo-routes.tsx",
      "type": "registry:component",
      "target": "components/viz/geo-routes.tsx",
      "content": "\"use client\";\n/**\n * @name geo-routes\n * @category viz\n * @description A geo-map layer of journeys and flows between places: great-circle routes (the shortest way over the globe) or arcs drawn over the map, thicker for larger amounts, with an arrow for the direction; each read by arrow keys and listed in a table.\n * @when Showing movement between places: flights, deliveries, migration, trade. Keep to a few dozen routes, or aggregate them first. For flows without geography use sankey-chart or chord-diagram. Goes inside geo-map.\n * @example\n * <GeoMap label=\"Flights from the hub\" projection=\"natural-earth\">\n *   <GeoGraticule />\n *   <GeoRoutes\n *     label=\"Flights\"\n *     routes={[\n *       { id: \"1\", name: \"Hub to Port\", from: [10.7, 59.9], to: [-74, 40.7], value: 12 },\n *       { id: \"2\", name: \"Hub to Cape\", from: [10.7, 59.9], to: [18.4, -33.9], value: 4 },\n *     ]}\n *     valueLabel=\"Flights a week\"\n *   />\n * </GeoMap>\n */\nimport { geoDistance, geoInterpolate } from \"d3-geo\";\nimport { scaleSqrt } from \"d3-scale\";\nimport type { ComponentProps } from \"react\";\nimport { cn } from \"@/lib/cn\";\nimport { chartSeries, formatNumber } from \"@/components/viz/chart\";\nimport { GeoLayer, GeoTable, useGeoMap } from \"@/components/viz/geo-map\";\n\nexport type GeoRoute = {\n  id: string;\n  name: string;\n  /** [longitude, latitude]. */\n  from: [number, number];\n  to: [number, number];\n  /** Places it passes through on the way, in order. */\n  via?: readonly [number, number][];\n  /** The amount carried: the line's width, and a row in the tooltip and the table. */\n  value?: number;\n  /** A group: one colour per group, in order of first appearance. */\n  group?: string;\n};\n\nexport type GeoRoutesProps = Omit<ComponentProps<\"g\">, \"children\"> & {\n  label: string;\n  routes: readonly GeoRoute[];\n  /** \"great-circle\" follows the globe; \"arc\" bows each route over the map, which keeps crossing routes apart. */\n  variant?: \"great-circle\" | \"arc\";\n  /** An arrow at the middle of each route. Default true. */\n  showDirection?: boolean;\n  valueLabel?: string;\n  /** Words for the table: the route column and the distance column. */\n  labels?: { route: string; distance: string };\n  formatValue?: (value: number) => string;\n};\n\nconst LABELS = { route: \"Route\", distance: \"Distance\" };\n\nexport function GeoRoutes({ label, routes, variant = \"great-circle\", showDirection = true, valueLabel = \"Value\", labels = LABELS, formatValue, className, ...props }: GeoRoutesProps) {\n  const { projection, path, locale } = useGeoMap(\"GeoRoutes\");\n  const number = formatValue ?? ((amount: number) => formatNumber(amount, locale));\n  const groups = [...new Set(routes.map((r) => r.group ?? \"\"))];\n  const grouped = groups.length > 1;\n  const width = scaleSqrt()\n    .domain([0, Math.max(1e-9, ...routes.map((r) => Math.max(0, r.value ?? 0)))])\n    .range([1.25, 7]);\n  const distance = (route: GeoRoute): number => {\n    const stops = [route.from, ...(route.via ?? []), route.to];\n    return stops.slice(1).reduce((sum, stop, i) => sum + geoDistance(stops[i] ?? stop, stop) * 6371, 0);\n  };\n  const drawn = routes.map((route) => {\n    const stroke = route.value === undefined ? 2 : width(Math.max(0, route.value));\n    const colour = grouped ? Math.min(groups.indexOf(route.group ?? \"\"), 5) : 0;\n    if (variant === \"great-circle\") {\n      const d = path({ type: \"LineString\", coordinates: [route.from, ...(route.via ?? []), route.to] }) ?? \"\";\n      const middle = projection(geoInterpolate(route.from, route.to)(0.5));\n      const ahead = projection(geoInterpolate(route.from, route.to)(0.52));\n      return { route, d, stroke, colour: colour > 4 ? -1 : colour, middle, angle: middle && ahead ? Math.atan2(ahead[1] - middle[1], ahead[0] - middle[0]) : 0 };\n    }\n    const a = projection(route.from);\n    const b = projection(route.to);\n    if (!a || !b) return { route, d: \"\", stroke, colour, middle: null, angle: 0 };\n    const mx = (a[0] + b[0]) / 2;\n    const my = (a[1] + b[1]) / 2;\n    const dx = b[0] - a[0];\n    const dy = b[1] - a[1];\n    // The control point sits off the middle, square to the route, a quarter of its length away.\n    const cx = mx - dy * 0.25;\n    const cy = my + dx * 0.25;\n    const middle: [number, number] = [(a[0] + 2 * cx + b[0]) / 4, (a[1] + 2 * cy + b[1]) / 4];\n    return { route, d: `M${a[0]},${a[1]}Q${cx},${cy} ${b[0]},${b[1]}`, stroke, colour: colour > 4 ? -1 : colour, middle, angle: Math.atan2(b[1] - a[1], b[0] - a[0]) };\n  });\n  const kilometres = (value: number): string => formatNumber(value, locale, { style: \"unit\", unit: \"kilometer\", maximumFractionDigits: 0 });\n  const items = drawn.map((item) => ({\n    x: item.middle?.[0] ?? -1,\n    y: item.middle?.[1] ?? -1,\n    title: item.route.name,\n    rows: [...(item.route.value !== undefined ? [{ label: valueLabel, value: number(item.route.value) }] : []), { label: labels.distance, value: kilometres(distance(item.route)) }],\n  }));\n  const ends = routes\n    .flatMap((r) => [r.from, r.to])\n    .flatMap((p) => {\n      const at = projection(p);\n      return at ? [at] : [];\n    });\n  return (\n    <>\n      <GeoLayer label={label} items={items} className={className} {...props}>\n        {(active, point) => (\n          <g data-slot=\"geo-routes\" aria-hidden=\"true\" fill=\"none\">\n            {drawn.map((item, index) => (\n              <g\n                key={item.route.id}\n                onPointerEnter={() => point(index)}\n                className={cn(\"transition-opacity duration-instant motion-reduce:transition-none\", active !== null && active !== index && \"opacity-35\")}\n              >\n                <path d={item.d} stroke=\"transparent\" strokeWidth={Math.max(12, item.stroke + 8)} />\n                <path d={item.d} className={chartSeries(item.colour).stroke} strokeWidth={item.stroke} strokeLinecap=\"round\" strokeOpacity={0.85} />\n                {showDirection && item.middle ? (\n                  <path\n                    d=\"M-5,-4 L3,0 L-5,4\"\n                    transform={`translate(${item.middle[0]},${item.middle[1]}) rotate(${(item.angle * 180) / Math.PI})`}\n                    className=\"stroke-foreground\"\n                    strokeWidth={1.75}\n                    strokeLinejoin=\"round\"\n                    strokeLinecap=\"round\"\n                  />\n                ) : null}\n              </g>\n            ))}\n            {ends.map((p, i) => (\n              <circle key={i} cx={p[0]} cy={p[1]} r={2.5} className=\"fill-foreground stroke-background\" strokeWidth={1} />\n            ))}\n            {active !== null && drawn[active] ? (\n              <path d={drawn[active]?.d ?? \"\"} className=\"pointer-events-none stroke-foreground\" strokeWidth={(drawn[active]?.stroke ?? 2) + 2} strokeOpacity={0.5} />\n            ) : null}\n          </g>\n        )}\n      </GeoLayer>\n      <GeoTable\n        caption={label}\n        columns={[labels.route, ...(routes.some((r) => r.value !== undefined) ? [valueLabel] : []), labels.distance]}\n        rows={routes.map((r) => [r.name, ...(routes.some((q) => q.value !== undefined) ? [r.value === undefined ? \"\" : number(r.value)] : []), kilometres(distance(r))])}\n      />\n    </>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "viz",
    "when": "Showing movement between places: flights, deliveries, migration, trade. Keep to a few dozen routes, or aggregate them first. For flows without geography use sankey-chart or chord-diagram. Goes inside geo-map.",
    "example": "<GeoMap label=\"Flights from the hub\" projection=\"natural-earth\">\n  <GeoGraticule />\n  <GeoRoutes\n    label=\"Flights\"\n    routes={[\n      { id: \"1\", name: \"Hub to Port\", from: [10.7, 59.9], to: [-74, 40.7], value: 12 },\n      { id: \"2\", name: \"Hub to Cape\", from: [10.7, 59.9], to: [18.4, -33.9], value: 4 },\n    ]}\n    valueLabel=\"Flights a week\"\n  />\n</GeoMap>",
    "client": true
  }
}
