@samizdata/mozaic 0.14.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/embed/ElementChart.svelte +25 -0
- package/dist/embed/ElementChart.svelte.d.ts +18 -0
- package/dist/embed/code.d.ts +20 -0
- package/dist/embed/code.js +18 -0
- package/dist/embed/element.d.ts +44 -0
- package/dist/embed/element.js +153 -0
- package/dist/embed/index.d.ts +2 -1
- package/dist/embed/index.js +2 -1
- package/dist/frame/ChartTools.svelte +5 -1
- package/dist/interactive/touch-hold.js +2 -1
- package/dist/recipes/FootprintMap.svelte +368 -0
- package/dist/recipes/FootprintMap.svelte.d.ts +116 -0
- package/dist/recipes/index.d.ts +3 -1
- package/dist/recipes/index.js +3 -1
- package/dist/transforms/contour.d.ts +42 -0
- package/dist/transforms/contour.js +112 -0
- package/dist/transforms/index.d.ts +1 -0
- package/dist/transforms/index.js +1 -0
- package/package.json +20 -3
- package/src/lib/recipes/FootprintMap.svelte +368 -0
- package/src/lib/recipes/index.ts +4 -1
- package/vite/index.d.ts +18 -0
- package/vite/index.js +52 -0
- package/vite/jsconfig.json +5 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samizdata/mozaic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Chart components for the web and print, built on Svelte 5 and d3, and the mozaic CLI (export embeds to SVG, PDF and PNG; eject recipes).",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"author": "Nicu Calcea",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"src/lib/recipes",
|
|
21
21
|
"cli/bin",
|
|
22
22
|
"cli/src",
|
|
23
|
+
"vite",
|
|
23
24
|
"!dist/**/*.test.*",
|
|
24
25
|
"!dist/**/*.spec.*"
|
|
25
26
|
],
|
|
@@ -108,13 +109,28 @@
|
|
|
108
109
|
"types": "./cli/src/index.d.ts",
|
|
109
110
|
"default": "./cli/src/index.js"
|
|
110
111
|
},
|
|
112
|
+
"./vite": {
|
|
113
|
+
"types": "./vite/index.d.ts",
|
|
114
|
+
"default": "./vite/index.js"
|
|
115
|
+
},
|
|
111
116
|
"./package.json": "./package.json"
|
|
112
117
|
},
|
|
113
118
|
"peerDependencies": {
|
|
114
|
-
"svelte": "^5.57.0"
|
|
119
|
+
"svelte": "^5.57.0",
|
|
120
|
+
"@sveltejs/vite-plugin-svelte": ">=6",
|
|
121
|
+
"vite": ">=7"
|
|
122
|
+
},
|
|
123
|
+
"peerDependenciesMeta": {
|
|
124
|
+
"@sveltejs/vite-plugin-svelte": {
|
|
125
|
+
"optional": true
|
|
126
|
+
},
|
|
127
|
+
"vite": {
|
|
128
|
+
"optional": true
|
|
129
|
+
}
|
|
115
130
|
},
|
|
116
131
|
"dependencies": {
|
|
117
132
|
"d3-array": "^3.2.4",
|
|
133
|
+
"d3-contour": "^4.0.3",
|
|
118
134
|
"d3-delaunay": "^6.0.4",
|
|
119
135
|
"d3-format": "^3.1.0",
|
|
120
136
|
"d3-geo": "^3.1.1",
|
|
@@ -130,6 +146,7 @@
|
|
|
130
146
|
"@sveltejs/package": "^3.0.1",
|
|
131
147
|
"@sveltejs/vite-plugin-svelte": "^7.3.1",
|
|
132
148
|
"@types/d3-array": "^3.2.1",
|
|
149
|
+
"@types/d3-contour": "^3.0.6",
|
|
133
150
|
"@types/d3-delaunay": "^6.0.4",
|
|
134
151
|
"@types/d3-format": "^3.0.4",
|
|
135
152
|
"@types/d3-geo": "^3.1.0",
|
|
@@ -167,7 +184,7 @@
|
|
|
167
184
|
"scripts": {
|
|
168
185
|
"dev": "vite dev",
|
|
169
186
|
"package": "svelte-kit sync && svelte-package && publint",
|
|
170
|
-
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json && tsc -p cli/jsconfig.json",
|
|
187
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json && tsc -p cli/jsconfig.json && tsc -p vite/jsconfig.json",
|
|
171
188
|
"test:unit": "vitest",
|
|
172
189
|
"test": "vitest --run",
|
|
173
190
|
"test:coverage": "vitest --run --coverage"
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Recipe: the footprints of several events on one map (the area each earthquake shook hard, each
|
|
4
|
+
flood covered or each fire burnt), stacked from the oldest, faintest, to the newest, strongest,
|
|
5
|
+
in one colour. Each footprint is outlined and labelled where you place its label; at narrow
|
|
6
|
+
widths, or for footprints without a label position, a key lists them instead. Pointing at the
|
|
7
|
+
map lists the footprints that cover that spot. Build footprints from point measurements with
|
|
8
|
+
`contourPoints` from `@samizdata/mozaic/transforms`. Use it as is, or run
|
|
9
|
+
`mozaic eject FootprintMap` to copy this file into your project and change anything.
|
|
10
|
+
-->
|
|
11
|
+
<script lang="ts" module>
|
|
12
|
+
/** A labelled point on the map: a town, an epicentre, a country name. */
|
|
13
|
+
export interface FootprintPlace {
|
|
14
|
+
name: string;
|
|
15
|
+
lon: number;
|
|
16
|
+
lat: number;
|
|
17
|
+
/** Draw a dot at the point (towns); without it the name is centred on the point (regions). */
|
|
18
|
+
dot?: boolean;
|
|
19
|
+
/** Grey text, for context such as country names. */
|
|
20
|
+
muted?: boolean;
|
|
21
|
+
/** Text anchor for names beside a dot; defaults to `start` (to the right). */
|
|
22
|
+
anchor?: 'start' | 'middle' | 'end';
|
|
23
|
+
/** Hide at narrow widths. */
|
|
24
|
+
hideSmall?: boolean;
|
|
25
|
+
}
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<script lang="ts" generics="T">
|
|
29
|
+
import type { Snippet } from 'svelte';
|
|
30
|
+
import { geoContains, type GeoPermissibleObjects, type GeoProjection } from 'd3-geo';
|
|
31
|
+
import { Chart, Header, Title, Subtitle, Footer, Source, Notes, type SourceItem } from '../frame/index.js';
|
|
32
|
+
import type { ChartEmbed } from '../embed/index.js';
|
|
33
|
+
import { Plot, Svg, Html, type PlotContext } from '../core/index.js';
|
|
34
|
+
import { Text } from '../marks/index.js';
|
|
35
|
+
import { Legend } from '../guides/index.js';
|
|
36
|
+
import { PointerArea, Tooltip } from '../interactive/index.js';
|
|
37
|
+
import type { LocaleCode } from '../locale/index.js';
|
|
38
|
+
import { base, readableColor, type Breakpoint, type Medium, type Theme } from '../styles/index.js';
|
|
39
|
+
|
|
40
|
+
interface Props {
|
|
41
|
+
/** One row per footprint. */
|
|
42
|
+
data: T[];
|
|
43
|
+
/** The footprint's area, e.g. a GeoJSON feature. */
|
|
44
|
+
geometry: (d: T) => GeoPermissibleObjects;
|
|
45
|
+
/** Name of the footprint, e.g. the year. */
|
|
46
|
+
label: (d: T) => string;
|
|
47
|
+
/** What decides the fading: lowest is drawn first and faintest. Defaults to data order (oldest first). */
|
|
48
|
+
order?: (d: T) => number | Date;
|
|
49
|
+
/**
|
|
50
|
+
* Where to write the footprint's name, as [longitude, latitude]: inside it, near its edge, or
|
|
51
|
+
* outside it, in empty space, where a leader line joins it to the nearest point of the edge.
|
|
52
|
+
* Footprints without one go in the key.
|
|
53
|
+
*/
|
|
54
|
+
labelAt?: (d: T) => [number, number] | null | undefined;
|
|
55
|
+
/** Text anchor of each label; defaults to `middle`. */
|
|
56
|
+
labelAnchor?: (d: T) => 'start' | 'middle' | 'end';
|
|
57
|
+
/**
|
|
58
|
+
* Keep the labels on the map at narrow widths (and in narrow print sizes) as well as listing
|
|
59
|
+
* the footprints in the key. Without it, narrow maps rely on the key and the tooltip, which
|
|
60
|
+
* print doesn't have. Defaults to true.
|
|
61
|
+
*/
|
|
62
|
+
labelSmall?: boolean;
|
|
63
|
+
/** Land for context, drawn in grey under the footprints (countries, regions). */
|
|
64
|
+
basemap?: GeoPermissibleObjects[];
|
|
65
|
+
/** Areas the chart is about, outlined on top of the footprints (e.g. the country). */
|
|
66
|
+
focus?: GeoPermissibleObjects[];
|
|
67
|
+
/** Towns, epicentres and region names. */
|
|
68
|
+
places?: FootprintPlace[];
|
|
69
|
+
/** A d3-geo projection; it is fitted to `fit`. */
|
|
70
|
+
projection: GeoProjection;
|
|
71
|
+
/** The area to show; footprints beyond it are cut off. */
|
|
72
|
+
fit: GeoPermissibleObjects;
|
|
73
|
+
/** Width ÷ height of the map. Defaults to 1.1. */
|
|
74
|
+
aspectRatio?: number;
|
|
75
|
+
/** Map height in px, or a function of the width and breakpoint; overrides `aspectRatio`. */
|
|
76
|
+
height?: number | ((size: { width: number; breakpoint: Breakpoint }) => number);
|
|
77
|
+
/** Colour of every footprint; defaults to the first categorical colour. */
|
|
78
|
+
color?: string;
|
|
79
|
+
/** Fill opacity of the oldest and the newest footprint. Defaults to [0.12, 0.4]. */
|
|
80
|
+
opacity?: [number, number];
|
|
81
|
+
/** Tooltip heading over the footprints under the pointer, e.g. "Shaken at intensity VII or more in". */
|
|
82
|
+
tooltipTitle?: string;
|
|
83
|
+
/** Tooltip text where no footprint covers the pointer; with none, no tooltip shows there. */
|
|
84
|
+
tooltipNone?: string;
|
|
85
|
+
style?: Theme;
|
|
86
|
+
locale?: LocaleCode;
|
|
87
|
+
medium?: Medium;
|
|
88
|
+
width?: number;
|
|
89
|
+
title?: string;
|
|
90
|
+
subtitle?: string;
|
|
91
|
+
notes?: string;
|
|
92
|
+
/** Plain text, or sources with optional links (see `<Source>`). */
|
|
93
|
+
source?: string | SourceItem[];
|
|
94
|
+
alt?: string;
|
|
95
|
+
/** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
|
|
96
|
+
embed?: ChartEmbed;
|
|
97
|
+
/** Extra SVG drawn on top (annotations); receives the plot context. */
|
|
98
|
+
annotations?: Snippet<[PlotContext]>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let {
|
|
102
|
+
data,
|
|
103
|
+
geometry,
|
|
104
|
+
label,
|
|
105
|
+
order,
|
|
106
|
+
labelAt,
|
|
107
|
+
labelAnchor,
|
|
108
|
+
labelSmall = true,
|
|
109
|
+
basemap = [],
|
|
110
|
+
focus = [],
|
|
111
|
+
places = [],
|
|
112
|
+
projection,
|
|
113
|
+
fit,
|
|
114
|
+
aspectRatio = 1.1,
|
|
115
|
+
height,
|
|
116
|
+
color,
|
|
117
|
+
opacity = [0.12, 0.4],
|
|
118
|
+
tooltipTitle,
|
|
119
|
+
tooltipNone,
|
|
120
|
+
style = base,
|
|
121
|
+
locale,
|
|
122
|
+
medium,
|
|
123
|
+
width,
|
|
124
|
+
title,
|
|
125
|
+
subtitle,
|
|
126
|
+
notes,
|
|
127
|
+
source,
|
|
128
|
+
alt,
|
|
129
|
+
embed,
|
|
130
|
+
annotations
|
|
131
|
+
}: Props = $props();
|
|
132
|
+
|
|
133
|
+
const uid = $props.id();
|
|
134
|
+
const theme = $derived(style);
|
|
135
|
+
const bg = $derived(theme.tokens['--mz-color-bg'] ?? '#ffffff');
|
|
136
|
+
const hue = $derived(color ?? theme.palettes.categorical[0]);
|
|
137
|
+
const ink = $derived(readableColor(hue, bg));
|
|
138
|
+
|
|
139
|
+
/** Oldest first, so the newest is drawn on top. */
|
|
140
|
+
const layers = $derived.by(() => {
|
|
141
|
+
const rows = order ? data.toSorted((a, b) => +order(a) - +order(b)) : [...data];
|
|
142
|
+
const n = rows.length;
|
|
143
|
+
return rows.map((d, i) => {
|
|
144
|
+
const t = n > 1 ? i / (n - 1) : 1;
|
|
145
|
+
const fill = opacity[0] + t * (opacity[1] - opacity[0]);
|
|
146
|
+
return { d, geo: geometry(d), name: label(d), fill, stroke: 0.5 + 0.5 * t, at: labelAt?.(d) ?? null };
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
/** Every vertex of a GeoJSON object. */
|
|
151
|
+
function vertices(g: unknown, out: [number, number][] = []): [number, number][] {
|
|
152
|
+
if (!g || typeof g !== 'object') return out;
|
|
153
|
+
const o = g as {
|
|
154
|
+
type?: string;
|
|
155
|
+
coordinates?: unknown;
|
|
156
|
+
geometry?: unknown;
|
|
157
|
+
features?: unknown[];
|
|
158
|
+
geometries?: unknown[];
|
|
159
|
+
};
|
|
160
|
+
if (o.type === 'Feature') return vertices(o.geometry, out);
|
|
161
|
+
if (o.type === 'FeatureCollection') o.features?.forEach((f) => vertices(f, out));
|
|
162
|
+
else if (o.type === 'GeometryCollection') o.geometries?.forEach((f) => vertices(f, out));
|
|
163
|
+
else if (o.coordinates) {
|
|
164
|
+
const walk = (c: unknown) => {
|
|
165
|
+
if (Array.isArray(c) && typeof c[0] === 'number') out.push([c[0], c[1] as number]);
|
|
166
|
+
else if (Array.isArray(c)) c.forEach(walk);
|
|
167
|
+
};
|
|
168
|
+
walk(o.coordinates);
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* A label placed outside its footprint gets a leader line to the nearest visible point of the
|
|
175
|
+
* footprint's edge, so labels can sit in empty space when footprints overlap.
|
|
176
|
+
*/
|
|
177
|
+
function leader(l: (typeof layers)[number], plot: PlotContext): [number, number, number, number] | null {
|
|
178
|
+
if (!l.at || !plot.projection || geoContains(l.geo, l.at)) return null;
|
|
179
|
+
const from = plot.projection(l.at);
|
|
180
|
+
if (!from) return null;
|
|
181
|
+
let best: [number, number] | null = null;
|
|
182
|
+
let dist = Infinity;
|
|
183
|
+
for (const v of vertices(l.geo)) {
|
|
184
|
+
const p = plot.projection(v);
|
|
185
|
+
if (!p || p[0] < 0 || p[1] < 0 || p[0] > plot.width || p[1] > plot.height) continue;
|
|
186
|
+
const dd = (p[0] - from[0]) ** 2 + (p[1] - from[1]) ** 2;
|
|
187
|
+
if (dd < dist) {
|
|
188
|
+
dist = dd;
|
|
189
|
+
best = [p[0], p[1]];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return best ? [from[0], from[1], best[0], best[1]] : null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The colour a footprint's fill shows over the background, for the key and tooltip swatches. */
|
|
196
|
+
function over(alpha: number): string {
|
|
197
|
+
const rgb = (h: string) => {
|
|
198
|
+
const s = h.replace('#', '');
|
|
199
|
+
const f = s.length === 3 ? s.replace(/./g, '$&$&') : s.slice(0, 6);
|
|
200
|
+
return [0, 2, 4].map((i) => parseInt(f.slice(i, i + 2), 16));
|
|
201
|
+
};
|
|
202
|
+
const [a, b] = [rgb(hue), rgb(bg)];
|
|
203
|
+
// Swatches are small, so show them a little stronger than the fill.
|
|
204
|
+
const k = Math.min(1, alpha * 1.6);
|
|
205
|
+
return `#${a
|
|
206
|
+
.map((v, i) =>
|
|
207
|
+
Math.round(k * v + (1 - k) * b[i])
|
|
208
|
+
.toString(16)
|
|
209
|
+
.padStart(2, '0')
|
|
210
|
+
)
|
|
211
|
+
.join('')}`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const newestFirst = $derived(layers.toReversed());
|
|
215
|
+
const unlabelled = $derived(layers.some((l) => !l.at));
|
|
216
|
+
|
|
217
|
+
let hover = $state<{ x: number; y: number; covered: typeof layers } | null>(null);
|
|
218
|
+
function onmove(px: number, py: number, plot: PlotContext) {
|
|
219
|
+
const lonlat = plot.projection?.invert?.([px, py]);
|
|
220
|
+
if (!lonlat) return (hover = null);
|
|
221
|
+
hover = { x: px, y: py, covered: newestFirst.filter((l) => geoContains(l.geo, lonlat)) };
|
|
222
|
+
}
|
|
223
|
+
const isHovered = (l: (typeof layers)[number]) => !!hover?.covered.includes(l);
|
|
224
|
+
</script>
|
|
225
|
+
|
|
226
|
+
<Chart {style} {locale} {medium} {width} {alt} {embed}>
|
|
227
|
+
<Header>
|
|
228
|
+
{#if title}<Title>{title}</Title>{/if}
|
|
229
|
+
{#if subtitle}<Subtitle>{subtitle}</Subtitle>{/if}
|
|
230
|
+
<!-- The key shows when a footprint has no label on the map, and at narrow widths, where it explains the fading. -->
|
|
231
|
+
<div class="mz-footprint-key" class:mz-footprint-key-always={unlabelled}>
|
|
232
|
+
<Legend items={newestFirst.map((l) => ({ label: l.name, color: over(l.fill) }))} />
|
|
233
|
+
</div>
|
|
234
|
+
</Header>
|
|
235
|
+
|
|
236
|
+
<Plot {projection} {fit} {aspectRatio} {height} padding={{ top: 0, right: 0, bottom: 0, left: 0 }}>
|
|
237
|
+
{#snippet children(plot)}
|
|
238
|
+
<Svg>
|
|
239
|
+
{#snippet defs()}
|
|
240
|
+
<clipPath id="mz-footprint-clip-{uid}">
|
|
241
|
+
<rect x={0} y={0} width={plot.width} height={plot.height} />
|
|
242
|
+
</clipPath>
|
|
243
|
+
{/snippet}
|
|
244
|
+
<g class="mz-footprint-map" clip-path="url(#mz-footprint-clip-{uid})">
|
|
245
|
+
{#each basemap as g, i (i)}
|
|
246
|
+
<path
|
|
247
|
+
class="mz-footprint-base"
|
|
248
|
+
d={plot.path?.(g) ?? ''}
|
|
249
|
+
fill="var(--mz-color-grid)"
|
|
250
|
+
fill-opacity="0.45"
|
|
251
|
+
stroke="var(--mz-color-axis)"
|
|
252
|
+
stroke-width="0.5"
|
|
253
|
+
stroke-linejoin="round"
|
|
254
|
+
/>
|
|
255
|
+
{/each}
|
|
256
|
+
{#each layers as l, i (i)}
|
|
257
|
+
<path
|
|
258
|
+
class="mz-footprint"
|
|
259
|
+
d={plot.path?.(l.geo) ?? ''}
|
|
260
|
+
fill={hue}
|
|
261
|
+
fill-opacity={l.fill}
|
|
262
|
+
stroke={hue}
|
|
263
|
+
stroke-opacity={isHovered(l) ? 1 : l.stroke}
|
|
264
|
+
stroke-width={isHovered(l) ? 2.5 : 1.25}
|
|
265
|
+
stroke-linejoin="round"
|
|
266
|
+
/>
|
|
267
|
+
{/each}
|
|
268
|
+
{#each focus as g, i (i)}
|
|
269
|
+
<path
|
|
270
|
+
class="mz-footprint-focus"
|
|
271
|
+
d={plot.path?.(g) ?? ''}
|
|
272
|
+
fill="none"
|
|
273
|
+
stroke="var(--mz-color-text)"
|
|
274
|
+
stroke-width="1.25"
|
|
275
|
+
stroke-linejoin="round"
|
|
276
|
+
/>
|
|
277
|
+
{/each}
|
|
278
|
+
</g>
|
|
279
|
+
{#each places.filter((p) => p.dot && !(p.hideSmall && plot.breakpoint === 's')) as p, i (i)}
|
|
280
|
+
{@const xy = plot.projection?.([p.lon, p.lat])}
|
|
281
|
+
{#if xy}
|
|
282
|
+
<circle
|
|
283
|
+
class="mz-footprint-dot"
|
|
284
|
+
cx={xy[0]}
|
|
285
|
+
cy={xy[1]}
|
|
286
|
+
r={2.5}
|
|
287
|
+
fill={p.muted ? 'var(--mz-color-muted)' : 'var(--mz-color-text)'}
|
|
288
|
+
stroke="var(--mz-color-bg)"
|
|
289
|
+
stroke-width="1"
|
|
290
|
+
/>
|
|
291
|
+
{/if}
|
|
292
|
+
{/each}
|
|
293
|
+
{#each places.filter((p) => !(p.hideSmall && plot.breakpoint === 's')) as p, i (i)}
|
|
294
|
+
{@const anchor = p.dot ? (p.anchor ?? 'start') : 'middle'}
|
|
295
|
+
<Text
|
|
296
|
+
data={[p]}
|
|
297
|
+
x={(d) => d.lon}
|
|
298
|
+
y={(d) => d.lat}
|
|
299
|
+
text={(d) => d.name}
|
|
300
|
+
dx={p.dot ? (anchor === 'start' ? 5 : anchor === 'end' ? -5 : 0) : 0}
|
|
301
|
+
dy={p.dot && anchor === 'middle' ? -9 : 0}
|
|
302
|
+
{anchor}
|
|
303
|
+
fill={p.muted ? 'var(--mz-color-muted)' : 'var(--mz-color-text)'}
|
|
304
|
+
fontWeight={p.dot ? 400 : 600}
|
|
305
|
+
halo
|
|
306
|
+
/>
|
|
307
|
+
{/each}
|
|
308
|
+
{#if plot.breakpoint !== 's' || labelSmall}
|
|
309
|
+
{#each layers.filter((l) => l.at) as l, i (i)}
|
|
310
|
+
{@const line = leader(l, plot)}
|
|
311
|
+
{#if line}
|
|
312
|
+
<g class="mz-footprint-leader">
|
|
313
|
+
<line x1={line[0]} y1={line[1]} x2={line[2]} y2={line[3]} stroke={ink} stroke-width="1" />
|
|
314
|
+
<circle cx={line[2]} cy={line[3]} r="2" fill={ink} />
|
|
315
|
+
</g>
|
|
316
|
+
{/if}
|
|
317
|
+
{/each}
|
|
318
|
+
{#each layers.filter((l) => l.at) as l, i (i)}
|
|
319
|
+
<Text
|
|
320
|
+
data={[l]}
|
|
321
|
+
x={(d) => d.at![0]}
|
|
322
|
+
y={(d) => d.at![1]}
|
|
323
|
+
text={(d) => d.name}
|
|
324
|
+
anchor={labelAnchor?.(l.d) ?? 'middle'}
|
|
325
|
+
fill={ink}
|
|
326
|
+
fontWeight={700}
|
|
327
|
+
halo
|
|
328
|
+
/>
|
|
329
|
+
{/each}
|
|
330
|
+
{/if}
|
|
331
|
+
{@render annotations?.(plot)}
|
|
332
|
+
{#if tooltipTitle}
|
|
333
|
+
<PointerArea onmove={({ x, y }) => onmove(x, y, plot)} onleave={() => (hover = null)} />
|
|
334
|
+
{/if}
|
|
335
|
+
</Svg>
|
|
336
|
+
<Html>
|
|
337
|
+
{#if hover && tooltipTitle && (hover.covered.length || tooltipNone)}
|
|
338
|
+
<Tooltip
|
|
339
|
+
x={hover.x}
|
|
340
|
+
y={hover.y}
|
|
341
|
+
title={tooltipTitle}
|
|
342
|
+
items={hover.covered.length
|
|
343
|
+
? hover.covered.map((l) => ({ label: l.name, value: '', color: over(l.fill), shape: 'square' as const }))
|
|
344
|
+
: [{ label: tooltipNone ?? '', value: '', color: 'transparent', shape: 'square' as const }]}
|
|
345
|
+
/>
|
|
346
|
+
{/if}
|
|
347
|
+
</Html>
|
|
348
|
+
{/snippet}
|
|
349
|
+
</Plot>
|
|
350
|
+
|
|
351
|
+
{#if notes || source}
|
|
352
|
+
<Footer>
|
|
353
|
+
{#if notes}<Notes>{notes}</Notes>{/if}
|
|
354
|
+
{#if source}<Source sources={source} />{/if}
|
|
355
|
+
</Footer>
|
|
356
|
+
{/if}
|
|
357
|
+
</Chart>
|
|
358
|
+
|
|
359
|
+
<style>
|
|
360
|
+
.mz-footprint-key {
|
|
361
|
+
display: none;
|
|
362
|
+
margin-top: 0.25rem;
|
|
363
|
+
}
|
|
364
|
+
.mz-footprint-key-always,
|
|
365
|
+
:global([data-mz-breakpoint='s']) .mz-footprint-key {
|
|
366
|
+
display: block;
|
|
367
|
+
}
|
|
368
|
+
</style>
|
package/src/lib/recipes/index.ts
CHANGED
|
@@ -8,6 +8,8 @@ export { default as StackedBarChart } from './StackedBarChart.svelte';
|
|
|
8
8
|
export { default as MosaicChart } from './MosaicChart.svelte';
|
|
9
9
|
export { default as PercentileChart } from './PercentileChart.svelte';
|
|
10
10
|
export { default as StripChart } from './StripChart.svelte';
|
|
11
|
+
export { default as FootprintMap } from './FootprintMap.svelte';
|
|
12
|
+
export type { FootprintPlace } from './FootprintMap.svelte';
|
|
11
13
|
|
|
12
14
|
/** Recipe names accepted by `mozaic eject`. */
|
|
13
15
|
export const recipes = [
|
|
@@ -20,5 +22,6 @@ export const recipes = [
|
|
|
20
22
|
'StackedBarChart',
|
|
21
23
|
'MosaicChart',
|
|
22
24
|
'PercentileChart',
|
|
23
|
-
'StripChart'
|
|
25
|
+
'StripChart',
|
|
26
|
+
'FootprintMap'
|
|
24
27
|
] as const;
|
package/vite/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { UserConfig } from 'vite';
|
|
2
|
+
|
|
3
|
+
export interface ElementConfigOptions {
|
|
4
|
+
/** The entry that calls `defineChartElements()`. Default `src/element.ts`. */
|
|
5
|
+
entry?: string;
|
|
6
|
+
/** Where to write the script: the SvelteKit build folder. Default `build`. */
|
|
7
|
+
outDir?: string;
|
|
8
|
+
/** Default `element.js`. */
|
|
9
|
+
fileName?: string;
|
|
10
|
+
/** Extra resolve conditions, before the defaults (`module`, `browser`, `production`). */
|
|
11
|
+
conditions?: string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Vite config that builds a project's web component embed (`element.js`). */
|
|
15
|
+
export function elementConfig(options?: ElementConfigOptions): UserConfig;
|
|
16
|
+
|
|
17
|
+
/** `1.5rem` → `calc(1.5 * var(--mz-rem, 16px))`, as the element build does to component CSS. */
|
|
18
|
+
export function remToVar(css: string): string;
|
package/vite/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Build helpers for projects (Node only): `import { elementConfig } from '@samizdata/mozaic/vite'`.
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `1.5rem` → `calc(1.5 * var(--mz-rem, 16px))`. Keep in step with `remToVar` in src/lib/embed/element.ts.
|
|
7
|
+
* @param {string} css
|
|
8
|
+
*/
|
|
9
|
+
export function remToVar(css) {
|
|
10
|
+
return css.replace(/(-?\d*\.?\d+)rem\b/g, 'calc($1 * var(--mz-rem, 16px))');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Vite config that builds a project's web component embed: one classic script (`element.js`, an
|
|
15
|
+
* IIFE, so host pages need no CORS) with the project's charts, Svelte and mozaic in it. Component
|
|
16
|
+
* CSS is injected into each chart's shadow root instead of emitted as a file. Run it after the
|
|
17
|
+
* SvelteKit build (which empties `outDir`): `vite build && vite build -c vite.element.config.ts`.
|
|
18
|
+
*
|
|
19
|
+
* @param {import('./index.d.ts').ElementConfigOptions} [options]
|
|
20
|
+
* @returns {import('vite').UserConfig}
|
|
21
|
+
*/
|
|
22
|
+
export function elementConfig(options = {}) {
|
|
23
|
+
const { entry = 'src/element.ts', outDir = 'build', fileName = 'element.js', conditions = [] } = options;
|
|
24
|
+
return {
|
|
25
|
+
plugins: [
|
|
26
|
+
svelte({
|
|
27
|
+
// Inject each component's CSS where it mounts (the shadow root), not into a .css file.
|
|
28
|
+
emitCss: false,
|
|
29
|
+
// In a shadow root `rem` is the host page's root size: use the element's `--mz-rem` instead (see element.ts).
|
|
30
|
+
preprocess: { style: ({ content }) => ({ code: remToVar(content) }) },
|
|
31
|
+
compilerOptions: {
|
|
32
|
+
css: 'injected',
|
|
33
|
+
// Runes for the project, the library's own setting for dependencies (as in the SvelteKit config).
|
|
34
|
+
runes: ({ filename }) => (filename.split(/[/\\]/).includes('node_modules') ? undefined : true)
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
],
|
|
38
|
+
resolve: {
|
|
39
|
+
alias: { $lib: resolve('src/lib') },
|
|
40
|
+
conditions: [...conditions, 'module', 'browser', 'production']
|
|
41
|
+
},
|
|
42
|
+
define: { 'process.env.NODE_ENV': '"production"' },
|
|
43
|
+
publicDir: false,
|
|
44
|
+
build: {
|
|
45
|
+
outDir,
|
|
46
|
+
// The SvelteKit build is already there.
|
|
47
|
+
emptyOutDir: false,
|
|
48
|
+
copyPublicDir: false,
|
|
49
|
+
lib: { entry, formats: ['iife'], name: 'mozaicElement', fileName: () => fileName }
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|