components/tanstack-charts/area/geojson-map.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { geoShape } from "@tanstack/charts/geo";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { geoIdentity } from "d3-geo";
import { Map } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface GeoFeature {
type: "Feature";
properties: {
id: string;
name: string;
type: string;
};
geometry: {
type: "Polygon";
coordinates: number[][][];
};
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): { type: "FeatureCollection"; features: GeoFeature[] } {
const features: GeoFeature[] = [
{
type: "Feature",
properties: { id: "room-1", name: "Main Hall", type: "reception" },
geometry: {
type: "Polygon",
coordinates: [
[
[10, 10],
[50, 10],
[50, 40],
[10, 40],
[10, 10],
],
],
},
},
{
type: "Feature",
properties: { id: "room-2", name: "Conference Room A", type: "meeting" },
geometry: {
type: "Polygon",
coordinates: [
[
[55, 10],
[90, 10],
[90, 40],
[55, 40],
[55, 10],
],
],
},
},
{
type: "Feature",
properties: { id: "room-3", name: "Executive Suite", type: "office" },
geometry: {
type: "Polygon",
coordinates: [
[
[10, 45],
[45, 45],
[45, 80],
[10, 80],
[10, 45],
],
],
},
},
{
type: "Feature",
properties: { id: "room-4", name: "Engineering Hub", type: "workspace" },
geometry: {
type: "Polygon",
coordinates: [
[
[50, 45],
[90, 45],
[90, 80],
[50, 80],
[50, 45],
],
],
},
},
];
return { type: "FeatureCollection", features };
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const feature = points[0].datum as GeoFeature;
if (!feature || !feature.properties) return { title: "", rows: [] };
return {
title: feature.properties.name,
rows: [
{ label: "Zone Type", value: feature.properties.type, color: "var(--chart-1)" },
{ label: "ID", value: feature.properties.id, color: "var(--foreground)" },
],
};
}
export function GeojsonMap() {
const geoData = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
geoShape(geoData.features, {
key: (feature: GeoFeature) => feature.properties.id,
projection: {
type: geoIdentity,
fit: geoData,
},
fill: "var(--chart-1)",
fillOpacity: 0.25,
stroke: "var(--chart-1)",
strokeWidth: 2,
}),
],
scales: {
x: null,
y: null,
},
margin: 16,
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
keyboard: true,
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "pointer",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[geoData],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>GeoJSON Architectural Plan</CardTitle>
<CardDescription>Spatial polygon projection mapping for venue floor layouts</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="GeoJSON Architectural Plan Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
4 spatial zones rendered with d3 identity projection <Map className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Custom GeoJSON feature collection mapped to SVG paths
</div>
</CardFooter>
</Card>
);
}
export default GeojsonMap;