All blocks
Density heatmap
DataA day of ride pickups as a heatmap or clusters, filtered to the hour you pick.
Use the arrow keys to pan, plus and minus to zoom, and Shift with the arrow keys to rotate.
Ride pickups
291 pickups · 17% of the day · 17:00 to 20:00
18:00
24 px
Install
Adds the block to src/lib/components/density-heatmap and the map if you do not have
it yet.
Code
<script lang="ts">
import type { FeatureCollection, Point } from 'geojson';
import { cn } from '$lib/utils.js';
import { Button } from '$lib/components/ui/button/index.js';
import { ButtonGroup } from '$lib/components/ui/button-group/index.js';
import * as Card from '$lib/components/ui/card/index.js';
import { Label } from '$lib/components/ui/label/index.js';
import { Switch } from '$lib/components/ui/switch/index.js';
import {
Map,
MapClusterLayer,
MapControlContainer,
MapHeatmap,
MapZoomControl,
type MapStyle
} from '$lib/components/ui/map/index.js';
import { HOURS_PER_DAY, PICKUPS, VIEWPORT, type Pickup } from './data.js';
interface Props {
/** Basemap. Defaults to OpenFreeMap. */
mapStyle?: MapStyle;
class?: string;
}
let { mapStyle, class: className }: Props = $props();
type View = 'heatmap' | 'clusters';
const VIEWS: { value: View; label: string }[] = [
{ value: 'heatmap', label: 'Heatmap' },
{ value: 'clusters', label: 'Clusters' }
];
/** Hours either side of the picked one that count as "around" it. */
const WINDOW = 1;
const START_HOUR = 18;
const PERCENT = 100;
/** Hour chart drawing size, in SVG units. */
const CHART = { width: 240, height: 56, gap: 2 };
const BAR_WIDTH = CHART.width / HOURS_PER_DAY - CHART.gap;
const hourly = Array.from({ length: HOURS_PER_DAY }, (_, hour) => ({
hour,
count: PICKUPS.filter((pickup) => pickup.properties.hour === hour).length
}));
// At least 1, so a day with no pickups draws flat bars instead of dividing by 0.
const busiest = Math.max(1, ...hourly.map((bar) => bar.count));
const busiestHour = hourly.findIndex((bar) => bar.count === busiest);
let hour = $state(START_HOUR);
let allDay = $state(false);
let view = $state<View>('heatmap');
let radius = $state(24);
function hourDistance(a: number, b: number): number {
const gap = Math.abs(a - b) % HOURS_PER_DAY;
return Math.min(gap, HOURS_PER_DAY - gap);
}
function isShown(pickupHour: number): boolean {
return allDay || hourDistance(pickupHour, hour) <= WINDOW;
}
const visible: FeatureCollection<Point, Pickup> = $derived({
type: 'FeatureCollection',
features: PICKUPS.filter((pickup) => isShown(pickup.properties.hour))
});
const share = $derived(Math.round((visible.features.length / PICKUPS.length) * PERCENT));
function clock(value: number): string {
const wrapped = ((value % HOURS_PER_DAY) + HOURS_PER_DAY) % HOURS_PER_DAY;
return `${String(wrapped).padStart(2, '0')}:00`;
}
const windowLabel = $derived(
allDay ? 'All day' : `${clock(hour - WINDOW)} to ${clock(hour + WINDOW + 1)}`
);
</script>
<div class={cn('grid min-h-[560px] gap-4 p-4 lg:grid-cols-[minmax(0,1fr)_20rem]', className)}>
<Map
class="min-h-[360px] rounded-lg border"
aria-label="Pickup density map"
{mapStyle}
viewport={VIEWPORT}
>
{#if view === 'heatmap'}
<MapHeatmap id="pickups-heat" data={visible} {radius} />
{:else}
<MapClusterLayer id="pickups-clusters" data={visible} />
{/if}
<MapControlContainer position="top-left">
<ButtonGroup aria-label="Show pickups as">
{#each VIEWS as option (option.value)}
<Button
variant={view === option.value ? 'default' : 'outline'}
size="sm"
aria-pressed={view === option.value}
onclick={() => (view = option.value)}
>
{option.label}
</Button>
{/each}
</ButtonGroup>
</MapControlContainer>
<MapControlContainer position="top-right">
<MapZoomControl />
</MapControlContainer>
</Map>
<Card.Root class="min-h-0 gap-4 overflow-y-auto">
<Card.Header>
<Card.Title>Ride pickups</Card.Title>
<Card.Description aria-live="polite" data-testid="pickup-summary">
{visible.features.length} pickups · {share}% of the day · {windowLabel}
</Card.Description>
</Card.Header>
<Card.Content class="flex flex-col gap-5">
<svg
class="w-full"
viewBox="0 0 {CHART.width} {CHART.height}"
role="img"
aria-label="Pickups per hour, busiest at {clock(busiestHour)}"
>
{#each hourly as bar (bar.hour)}
{@const height = Math.max(1, (bar.count / busiest) * CHART.height)}
<rect
x={bar.hour * (BAR_WIDTH + CHART.gap)}
y={CHART.height - height}
width={BAR_WIDTH}
{height}
rx="1"
class={isShown(bar.hour) ? 'fill-primary' : 'fill-muted-foreground/25'}
/>
{/each}
</svg>
<div class="flex flex-col gap-2">
<div class="flex items-center justify-between text-sm">
<Label for="pickup-hour">Hour</Label>
<span class="text-muted-foreground tabular-nums">{clock(hour)}</span>
</div>
<input
id="pickup-hour"
class="w-full accent-primary disabled:opacity-50"
type="range"
min="0"
max={HOURS_PER_DAY - 1}
bind:value={hour}
disabled={allDay}
/>
</div>
<div class="flex items-center gap-2">
<Switch id="pickup-all-day" bind:checked={allDay} />
<Label for="pickup-all-day">Whole day</Label>
</div>
{#if view === 'heatmap'}
<div class="flex flex-col gap-2">
<div class="flex items-center justify-between text-sm">
<Label for="pickup-radius">Blur radius</Label>
<span class="text-muted-foreground tabular-nums">{radius} px</span>
</div>
<input
id="pickup-radius"
class="w-full accent-primary"
type="range"
min="8"
max="48"
bind:value={radius}
/>
</div>
{/if}
</Card.Content>
</Card.Root>
</div>
import type { Feature, Point } from 'geojson';
import type { LngLatTuple, MapViewport } from '$lib/components/ui/map/index.js';
/** New York City. */
export const VIEWPORT: MapViewport = {
center: [-73.955, 40.735],
zoom: 11,
bearing: 0,
pitch: 0
};
export const HOURS_PER_DAY = 24;
export interface Pickup {
/** Hour of the day the ride started, 0 to 23. */
hour: number;
}
interface Hotspot {
center: LngLatTuple;
/** Hour this spot is busiest. */
peakHour: number;
/** Pickups generated around this spot. */
count: number;
/** Spread in degrees. */
spread: number;
}
const HOTSPOTS: Hotspot[] = [
{ center: [-73.9855, 40.758], peakHour: 18, count: 520, spread: 0.012 },
{ center: [-74.0094, 40.7093], peakHour: 8, count: 360, spread: 0.009 },
{ center: [-73.9573, 40.7141], peakHour: 23, count: 300, spread: 0.01 },
{ center: [-73.874, 40.7769], peakHour: 6, count: 220, spread: 0.006 },
{ center: [-73.9442, 40.6782], peakHour: 20, count: 180, spread: 0.014 },
{ center: [-73.9712, 40.7831], peakHour: 12, count: 160, spread: 0.011 }
];
/** Spread of each spot's pickups around its peak hour. */
const HOUR_SPREAD = 3;
const SEED = 20_261_107;
/** A small seeded random generator, so every load shows the same pickups. */
function seeded(seed: number): () => number {
let state = seed;
return () => {
state = (state + 0x6d2b79f5) | 0;
let value = Math.imul(state ^ (state >>> 15), 1 | state);
value = (value + Math.imul(value ^ (value >>> 7), 61 | value)) ^ value;
return ((value ^ (value >>> 14)) >>> 0) / 4_294_967_296;
};
}
/** Roughly normal noise from the sum of three uniform draws, centred on 0. */
function noise(random: () => number): number {
return random() + random() + random() - 1.5;
}
function generate(): Feature<Point, Pickup>[] {
const random = seeded(SEED);
return HOTSPOTS.flatMap((spot) =>
Array.from({ length: spot.count }, (): Feature<Point, Pickup> => {
const hour = Math.round(spot.peakHour + noise(random) * HOUR_SPREAD * 2);
return {
type: 'Feature',
properties: { hour: ((hour % HOURS_PER_DAY) + HOURS_PER_DAY) % HOURS_PER_DAY },
geometry: {
type: 'Point',
coordinates: [
spot.center[0] + noise(random) * spot.spread * 2,
spot.center[1] + noise(random) * spot.spread * 1.5
]
}
};
})
);
}
/** One weekday of ride pickups. */
export const PICKUPS: Feature<Point, Pickup>[] = generate();