@samizdata/mozaic 0.12.1 → 0.14.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/dist/core/context.d.ts +8 -0
- package/dist/core/context.js +13 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/scales.d.ts +8 -0
- package/dist/core/scales.js +10 -0
- package/dist/frame/Chart.svelte +8 -3
- package/dist/frame/Chart.svelte.d.ts +4 -1
- package/dist/frame/Source.svelte +2 -2
- package/dist/frame/Source.svelte.d.ts +1 -1
- package/dist/frame/index.d.ts +1 -1
- package/dist/frame/index.js +1 -1
- package/dist/marks/Area.svelte +3 -2
- package/dist/marks/Line.svelte +3 -2
- package/dist/marks/Points.svelte +3 -3
- package/dist/table/Sparkline.svelte +3 -1
- package/package.json +1 -1
package/dist/core/context.d.ts
CHANGED
|
@@ -23,3 +23,11 @@ export declare function useEmbedded(): boolean;
|
|
|
23
23
|
*/
|
|
24
24
|
export declare function setDownloads(on?: boolean): void;
|
|
25
25
|
export declare function useDownloads(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the charts inside this component (a site's layout, say) add their style's web-font stylesheet to the
|
|
28
|
+
* page head, unless a chart sets `<Chart loadFonts>`. A site that loads the fonts itself, or inlines their
|
|
29
|
+
* `@font-face` rules, calls `setLoadFonts(false)`: one render-blocking stylesheet fewer, and no copy of the
|
|
30
|
+
* `<link>` per chart.
|
|
31
|
+
*/
|
|
32
|
+
export declare function setLoadFonts(on?: boolean): void;
|
|
33
|
+
export declare function useLoadFonts(): boolean;
|
package/dist/core/context.js
CHANGED
|
@@ -6,6 +6,7 @@ const PLOT = Symbol('mozaic:plot');
|
|
|
6
6
|
const INITIAL_WIDTH = Symbol('mozaic:initial-width');
|
|
7
7
|
const EMBEDDED = Symbol('mozaic:embedded');
|
|
8
8
|
const DOWNLOADS = Symbol('mozaic:downloads');
|
|
9
|
+
const LOAD_FONTS = Symbol('mozaic:load-fonts');
|
|
9
10
|
/** Used when a component renders outside `<Chart>` (tests, quick sketches). */
|
|
10
11
|
const fallbackChart = {
|
|
11
12
|
theme: base,
|
|
@@ -67,3 +68,15 @@ export function setDownloads(on = true) {
|
|
|
67
68
|
export function useDownloads() {
|
|
68
69
|
return getContext(DOWNLOADS) ?? false;
|
|
69
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Whether the charts inside this component (a site's layout, say) add their style's web-font stylesheet to the
|
|
73
|
+
* page head, unless a chart sets `<Chart loadFonts>`. A site that loads the fonts itself, or inlines their
|
|
74
|
+
* `@font-face` rules, calls `setLoadFonts(false)`: one render-blocking stylesheet fewer, and no copy of the
|
|
75
|
+
* `<link>` per chart.
|
|
76
|
+
*/
|
|
77
|
+
export function setLoadFonts(on = true) {
|
|
78
|
+
setContext(LOAD_FONTS, on);
|
|
79
|
+
}
|
|
80
|
+
export function useLoadFonts() {
|
|
81
|
+
return getContext(LOAD_FONTS) ?? true;
|
|
82
|
+
}
|
package/dist/core/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export { default as Svg } from './Svg.svelte';
|
|
|
3
3
|
export { default as Html } from './Html.svelte';
|
|
4
4
|
export { default as Canvas } from './Canvas.svelte';
|
|
5
5
|
export { default as Facets } from './Facets.svelte';
|
|
6
|
-
export { setChartContext, setDownloads, setInitialWidth, setPlotContext, useChart, useDownloads, useInitialWidth, useLocale, usePlot, useTheme } from './context.js';
|
|
6
|
+
export { setChartContext, setDownloads, setLoadFonts, setInitialWidth, setPlotContext, useChart, useDownloads, useLoadFonts, useInitialWidth, useLocale, usePlot, useTheme } from './context.js';
|
|
7
7
|
export { estimateTextWidth, isOrdinal, isScale, position, scaled } from './scales.js';
|
|
8
8
|
export { measureTextWidth, useTextWidth } from './text.svelte.js';
|
|
9
9
|
export { mm, pt, PX_PER_MM } from './units.js';
|
package/dist/core/index.js
CHANGED
|
@@ -3,7 +3,7 @@ export { default as Svg } from './Svg.svelte';
|
|
|
3
3
|
export { default as Html } from './Html.svelte';
|
|
4
4
|
export { default as Canvas } from './Canvas.svelte';
|
|
5
5
|
export { default as Facets } from './Facets.svelte';
|
|
6
|
-
export { setChartContext, setDownloads, setInitialWidth, setPlotContext, useChart, useDownloads, useInitialWidth, useLocale, usePlot, useTheme } from './context.js';
|
|
6
|
+
export { setChartContext, setDownloads, setLoadFonts, setInitialWidth, setPlotContext, useChart, useDownloads, useLoadFonts, useInitialWidth, useLocale, usePlot, useTheme } from './context.js';
|
|
7
7
|
export { estimateTextWidth, isOrdinal, isScale, position, scaled } from './scales.js';
|
|
8
8
|
export { measureTextWidth, useTextWidth } from './text.svelte.js';
|
|
9
9
|
export { mm, pt, PX_PER_MM } from './units.js';
|
package/dist/core/scales.d.ts
CHANGED
|
@@ -14,3 +14,11 @@ export declare function estimateTextWidth(text: string, fontSize: number, averag
|
|
|
14
14
|
* longitude/latitude; otherwise they go through the x and y scales.
|
|
15
15
|
*/
|
|
16
16
|
export declare function position(plot: PlotContext, x: unknown, y: unknown): [number, number];
|
|
17
|
+
/**
|
|
18
|
+
* Decimals in the path data of lines and areas (d3's own default is 3). A tenth of a pixel cannot be seen, on
|
|
19
|
+
* screen or in print, and the coordinates are most of a chart's markup: on a page of long-running poll lines,
|
|
20
|
+
* this takes a fifth off the HTML.
|
|
21
|
+
*/
|
|
22
|
+
export declare const PATH_DIGITS = 1;
|
|
23
|
+
/** A coordinate for an SVG attribute, to a hundredth of a pixel: `5.737261287934863` becomes `5.74`. */
|
|
24
|
+
export declare function px(value: number): number;
|
package/dist/core/scales.js
CHANGED
|
@@ -30,3 +30,13 @@ export function position(plot, x, y) {
|
|
|
30
30
|
}
|
|
31
31
|
return [scaled(plot.x, x), scaled(plot.y, y)];
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Decimals in the path data of lines and areas (d3's own default is 3). A tenth of a pixel cannot be seen, on
|
|
35
|
+
* screen or in print, and the coordinates are most of a chart's markup: on a page of long-running poll lines,
|
|
36
|
+
* this takes a fifth off the HTML.
|
|
37
|
+
*/
|
|
38
|
+
export const PATH_DIGITS = 1;
|
|
39
|
+
/** A coordinate for an SVG attribute, to a hundredth of a pixel: `5.737261287934863` becomes `5.74`. */
|
|
40
|
+
export function px(value) {
|
|
41
|
+
return Math.round(value * 100) / 100;
|
|
42
|
+
}
|
package/dist/frame/Chart.svelte
CHANGED
|
@@ -9,7 +9,7 @@ a map or a table.
|
|
|
9
9
|
-->
|
|
10
10
|
<script lang="ts">
|
|
11
11
|
import type { Snippet } from 'svelte';
|
|
12
|
-
import { setChartContext, useDownloads } from '../core/context.js';
|
|
12
|
+
import { setChartContext, useDownloads, useLoadFonts } from '../core/context.js';
|
|
13
13
|
import { createLocale, type LocaleCode } from '../locale/index.js';
|
|
14
14
|
import { base, breakpointFor, tokensToStyle } from '../styles/index.js';
|
|
15
15
|
import type { Medium, Theme } from '../styles/types.js';
|
|
@@ -26,7 +26,10 @@ a map or a table.
|
|
|
26
26
|
alt?: string;
|
|
27
27
|
/** Fixed width in px (print: use `mm()`). Otherwise the chart fills its container. */
|
|
28
28
|
width?: number;
|
|
29
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Add the style's web-font stylesheet to the page head. Turn off when the host page already loads the fonts
|
|
31
|
+
* (`setLoadFonts(false)` above the chart turns it off for every chart inside).
|
|
32
|
+
*/
|
|
30
33
|
loadFonts?: boolean;
|
|
31
34
|
/** The chart's embed page: shows an "Embed" button under the chart, with the code to put it on another site. */
|
|
32
35
|
embed?: ChartEmbed;
|
|
@@ -45,7 +48,7 @@ a map or a table.
|
|
|
45
48
|
medium = 'web',
|
|
46
49
|
alt,
|
|
47
50
|
width: fixedWidth,
|
|
48
|
-
loadFonts
|
|
51
|
+
loadFonts: loadFontsProp,
|
|
49
52
|
embed,
|
|
50
53
|
download,
|
|
51
54
|
class: className = '',
|
|
@@ -54,6 +57,8 @@ a map or a table.
|
|
|
54
57
|
|
|
55
58
|
const uid = $props.id();
|
|
56
59
|
const downloads = useDownloads();
|
|
60
|
+
const loadFontsDefault = useLoadFonts();
|
|
61
|
+
const loadFonts = $derived(loadFontsProp ?? loadFontsDefault);
|
|
57
62
|
const tools = $state<{ table: Snippet | null }>({ table: null });
|
|
58
63
|
const theme = $derived(style);
|
|
59
64
|
const loc = $derived(createLocale(locale ?? theme.defaultLocale));
|
|
@@ -12,7 +12,10 @@ interface Props {
|
|
|
12
12
|
alt?: string;
|
|
13
13
|
/** Fixed width in px (print: use `mm()`). Otherwise the chart fills its container. */
|
|
14
14
|
width?: number;
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Add the style's web-font stylesheet to the page head. Turn off when the host page already loads the fonts
|
|
17
|
+
* (`setLoadFonts(false)` above the chart turns it off for every chart inside).
|
|
18
|
+
*/
|
|
16
19
|
loadFonts?: boolean;
|
|
17
20
|
/** The chart's embed page: shows an "Embed" button under the chart, with the code to put it on another site. */
|
|
18
21
|
embed?: ChartEmbed;
|
package/dist/frame/Source.svelte
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
@component
|
|
3
3
|
"Source: …", with the label in the chart's language. Pass `sources` as text or as a list of
|
|
4
4
|
sources with optional links (`[{ label: 'ANRE', href: 'https://anre.md/…' }, …]`, joined with
|
|
5
|
-
"
|
|
5
|
+
", "), or write the text as children. The label turns plural for more than one listed source,
|
|
6
6
|
or set `plural`. Links open in a new tab; in print they are plain text.
|
|
7
7
|
-->
|
|
8
8
|
<script lang="ts">
|
|
@@ -28,7 +28,7 @@ or set `plural`. Links open in a new tab; in print they are plain text.
|
|
|
28
28
|
|
|
29
29
|
<p class="mz-source">
|
|
30
30
|
<span class="mz-label">{many ? chart.locale.strings.sources : chart.locale.strings.source}:</span>
|
|
31
|
-
{#each items as item, i (i)}{i > 0 ? '
|
|
31
|
+
{#each items as item, i (i)}{i > 0 ? ', ' : ''}{#if item.href && linked}<a
|
|
32
32
|
href={item.href}
|
|
33
33
|
target="_blank"
|
|
34
34
|
rel="noopener">{item.label}</a
|
|
@@ -10,7 +10,7 @@ interface Props {
|
|
|
10
10
|
/**
|
|
11
11
|
* "Source: …", with the label in the chart's language. Pass `sources` as text or as a list of
|
|
12
12
|
* sources with optional links (`[{ label: 'ANRE', href: 'https://anre.md/…' }, …]`, joined with
|
|
13
|
-
* "
|
|
13
|
+
* ", "), or write the text as children. The label turns plural for more than one listed source,
|
|
14
14
|
* or set `plural`. Links open in a new tab; in print they are plain text.
|
|
15
15
|
*/
|
|
16
16
|
declare const Source: import("svelte").Component<Props, {}, "">;
|
package/dist/frame/index.d.ts
CHANGED
|
@@ -9,5 +9,5 @@ export { default as Logo } from './Logo.svelte';
|
|
|
9
9
|
export { default as Byline } from './Byline.svelte';
|
|
10
10
|
export { default as DataTable } from './DataTable.svelte';
|
|
11
11
|
export { default as DataDownload } from './DataDownload.svelte';
|
|
12
|
-
export { setDownloads, useChart, useLocale, useTheme } from '../core/context.js';
|
|
12
|
+
export { setDownloads, setLoadFonts, useChart, useLocale, useTheme } from '../core/context.js';
|
|
13
13
|
export type { SourceItem } from './types.js';
|
package/dist/frame/index.js
CHANGED
|
@@ -9,4 +9,4 @@ export { default as Logo } from './Logo.svelte';
|
|
|
9
9
|
export { default as Byline } from './Byline.svelte';
|
|
10
10
|
export { default as DataTable } from './DataTable.svelte';
|
|
11
11
|
export { default as DataDownload } from './DataDownload.svelte';
|
|
12
|
-
export { setDownloads, useChart, useLocale, useTheme } from '../core/context.js';
|
|
12
|
+
export { setDownloads, setLoadFonts, useChart, useLocale, useTheme } from '../core/context.js';
|
package/dist/marks/Area.svelte
CHANGED
|
@@ -7,7 +7,7 @@ Pair a light area (opacity ~0.1–0.2) with a `<Line>` on top for emphasis.
|
|
|
7
7
|
<script lang="ts" generics="T">
|
|
8
8
|
import { area, curveLinear, type CurveFactory } from 'd3-shape';
|
|
9
9
|
import { useChart, usePlot } from '../core/context.js';
|
|
10
|
-
import { scaled } from '../core/scales.js';
|
|
10
|
+
import { PATH_DIGITS, scaled } from '../core/scales.js';
|
|
11
11
|
import { access, type Accessor } from '../core/accessor.js';
|
|
12
12
|
|
|
13
13
|
interface Props {
|
|
@@ -37,7 +37,8 @@ Pair a light area (opacity ~0.1–0.2) with a `<Line>` on top for emphasis.
|
|
|
37
37
|
.defined((p, i) =>
|
|
38
38
|
defined ? defined(p, i) : Number.isFinite(scaled(plot.x, x(p, i))) && Number.isFinite(scaled(plot.y, y(p, i)))
|
|
39
39
|
)
|
|
40
|
-
.curve(curve)
|
|
40
|
+
.curve(curve)
|
|
41
|
+
.digits(PATH_DIGITS)(data) ?? ''
|
|
41
42
|
);
|
|
42
43
|
</script>
|
|
43
44
|
|
package/dist/marks/Line.svelte
CHANGED
|
@@ -6,7 +6,7 @@ One line through `data` (one series). For several series, render one
|
|
|
6
6
|
<script lang="ts" generics="T">
|
|
7
7
|
import { curveLinear, line, type CurveFactory } from 'd3-shape';
|
|
8
8
|
import { useChart, usePlot } from '../core/context.js';
|
|
9
|
-
import { scaled } from '../core/scales.js';
|
|
9
|
+
import { PATH_DIGITS, scaled } from '../core/scales.js';
|
|
10
10
|
|
|
11
11
|
interface Props {
|
|
12
12
|
data: T[];
|
|
@@ -47,7 +47,8 @@ One line through `data` (one series). For several series, render one
|
|
|
47
47
|
.defined((p, i) =>
|
|
48
48
|
defined ? defined(p, i) : Number.isFinite(scaled(plot.x, x(p, i))) && Number.isFinite(scaled(plot.y, y(p, i)))
|
|
49
49
|
)
|
|
50
|
-
.curve(curve)
|
|
50
|
+
.curve(curve)
|
|
51
|
+
.digits(PATH_DIGITS)(data) ?? ''
|
|
51
52
|
);
|
|
52
53
|
</script>
|
|
53
54
|
|
package/dist/marks/Points.svelte
CHANGED
|
@@ -6,7 +6,7 @@ Each dot has a ring in the background colour so overlaps stay legible.
|
|
|
6
6
|
-->
|
|
7
7
|
<script lang="ts" generics="T">
|
|
8
8
|
import { useChart, usePlot } from '../core/context.js';
|
|
9
|
-
import { position } from '../core/scales.js';
|
|
9
|
+
import { position, px } from '../core/scales.js';
|
|
10
10
|
import { access, type Accessor, type Value } from '../core/accessor.js';
|
|
11
11
|
|
|
12
12
|
interface Props {
|
|
@@ -54,8 +54,8 @@ Each dot has a ring in the background colour so overlaps stay legible.
|
|
|
54
54
|
{#if Number.isFinite(cx) && Number.isFinite(cy)}
|
|
55
55
|
<!-- svelte-ignore a11y_no_static_element_interactions, a11y_click_events_have_key_events -->
|
|
56
56
|
<circle
|
|
57
|
-
{cx}
|
|
58
|
-
{cy}
|
|
57
|
+
cx={px(cx)}
|
|
58
|
+
cy={px(cy)}
|
|
59
59
|
r={access(r, d, i)}
|
|
60
60
|
fill={fill ? access(fill, d, i) : defaultFill}
|
|
61
61
|
fill-opacity={access(opacity, d, i)}
|
|
@@ -7,6 +7,7 @@ The last point gets a dot.
|
|
|
7
7
|
import { extent } from 'd3-array';
|
|
8
8
|
import { scaleLinear } from 'd3-scale';
|
|
9
9
|
import { line } from 'd3-shape';
|
|
10
|
+
import { PATH_DIGITS } from '../core/scales.js';
|
|
10
11
|
|
|
11
12
|
interface Props {
|
|
12
13
|
values: number[];
|
|
@@ -33,7 +34,8 @@ The last point gets a dot.
|
|
|
33
34
|
const d = $derived(
|
|
34
35
|
line<number>()
|
|
35
36
|
.x((_, i) => x(i))
|
|
36
|
-
.y((v) => y(v))
|
|
37
|
+
.y((v) => y(v))
|
|
38
|
+
.digits(PATH_DIGITS)(values) ?? ''
|
|
37
39
|
);
|
|
38
40
|
const last = $derived(values.length - 1);
|
|
39
41
|
</script>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samizdata/mozaic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.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",
|