@samizdata/mozaic 0.12.0 → 0.13.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 CHANGED
@@ -40,7 +40,7 @@ pnpm add @samizdata/mozaic d3-scale d3-array d3-shape
40
40
  | `@samizdata/mozaic/locale` | `createLocale` (`en`, `ro`, `ru`) |
41
41
  | `@samizdata/mozaic/embed` | `Embed`, `useEmbedParams`, `embedCode`, `hostScript`, `observeHeight` |
42
42
  | `@samizdata/mozaic/export` | `figureToSvg`, `downloadSvg`, `downloadPng`, `figureToPng` |
43
- | `@samizdata/mozaic/recipes` | `BarChart`, `ColumnChart`, `LineChart`, `ScatterChart`, `ArrowChart`, `BarDotChart`, `StackedBarChart`, `MosaicChart`, `PercentileChart` |
43
+ | `@samizdata/mozaic/recipes` | `BarChart`, `ColumnChart`, `LineChart`, `ScatterChart`, `ArrowChart`, `BarDotChart`, `StackedBarChart`, `MosaicChart`, `PercentileChart`, `StripChart` |
44
44
 
45
45
  ## CLI
46
46
 
@@ -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;
@@ -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
+ }
@@ -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';
@@ -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';
@@ -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;
@@ -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
+ }
@@ -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
- /** Add the style's web-font stylesheet to the page head. Turn off when the host page already loads the fonts. */
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 = true,
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
- /** Add the style's web-font stylesheet to the page head. Turn off when the host page already loads the fonts. */
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;
@@ -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';
@@ -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';
@@ -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)(data) ?? ''
40
+ .curve(curve)
41
+ .digits(PATH_DIGITS)(data) ?? ''
41
42
  );
42
43
  </script>
43
44
 
@@ -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)(data) ?? ''
50
+ .curve(curve)
51
+ .digits(PATH_DIGITS)(data) ?? ''
51
52
  );
52
53
  </script>
53
54
 
@@ -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)}
@@ -132,7 +132,8 @@ file into your project and change anything.
132
132
  const barColor = (d: T) => color?.(d) ?? theme.palettes.categorical[0];
133
133
  /** White on dark bars, the text colour on light ones. */
134
134
  const inkOn = (d: T) => (contrastRatio(barColor(d), '#ffffff') >= 4.5 ? '#ffffff' : 'var(--mz-color-text)');
135
- let hovered = $state<T | null>(null);
135
+ // Raw, not a proxy: marks compare the hovered row with each datum by identity.
136
+ let hovered = $state.raw<T | null>(null);
136
137
  </script>
137
138
 
138
139
  <Chart {style} {locale} {medium} {width} {alt} {embed}>
@@ -74,7 +74,8 @@ copies this file into your project.
74
74
  .domain([Math.min(0, min(data, value) ?? 0), Math.max(0, max(data, value) ?? 1)])
75
75
  .nice()
76
76
  );
77
- let hovered = $state<T | null>(null);
77
+ // Raw, not a proxy: marks compare the hovered row with each datum by identity.
78
+ let hovered = $state.raw<T | null>(null);
78
79
  </script>
79
80
 
80
81
  <Chart {style} {locale} {medium} {width} {alt} {embed}>
@@ -187,7 +187,8 @@ copies this file into your project.
187
187
  : basePadding;
188
188
  };
189
189
  const withUnit = (s: string, unit?: string) => (unit ? `${s} ${unit}` : s);
190
- let hovered = $state<T | null>(null);
190
+ // Raw, not a proxy: marks compare the hovered row with each datum by identity.
191
+ let hovered = $state.raw<T | null>(null);
191
192
  </script>
192
193
 
193
194
  <Chart {style} {locale} {medium} {width} {alt} {embed}>
@@ -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))(values) ?? ''
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.12.0",
3
+ "version": "0.13.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",
@@ -132,7 +132,8 @@ file into your project and change anything.
132
132
  const barColor = (d: T) => color?.(d) ?? theme.palettes.categorical[0];
133
133
  /** White on dark bars, the text colour on light ones. */
134
134
  const inkOn = (d: T) => (contrastRatio(barColor(d), '#ffffff') >= 4.5 ? '#ffffff' : 'var(--mz-color-text)');
135
- let hovered = $state<T | null>(null);
135
+ // Raw, not a proxy: marks compare the hovered row with each datum by identity.
136
+ let hovered = $state.raw<T | null>(null);
136
137
  </script>
137
138
 
138
139
  <Chart {style} {locale} {medium} {width} {alt} {embed}>
@@ -74,7 +74,8 @@ copies this file into your project.
74
74
  .domain([Math.min(0, min(data, value) ?? 0), Math.max(0, max(data, value) ?? 1)])
75
75
  .nice()
76
76
  );
77
- let hovered = $state<T | null>(null);
77
+ // Raw, not a proxy: marks compare the hovered row with each datum by identity.
78
+ let hovered = $state.raw<T | null>(null);
78
79
  </script>
79
80
 
80
81
  <Chart {style} {locale} {medium} {width} {alt} {embed}>
@@ -187,7 +187,8 @@ copies this file into your project.
187
187
  : basePadding;
188
188
  };
189
189
  const withUnit = (s: string, unit?: string) => (unit ? `${s} ${unit}` : s);
190
- let hovered = $state<T | null>(null);
190
+ // Raw, not a proxy: marks compare the hovered row with each datum by identity.
191
+ let hovered = $state.raw<T | null>(null);
191
192
  </script>
192
193
 
193
194
  <Chart {style} {locale} {medium} {width} {alt} {embed}>