From c4c57daaf92339e76432414e18db42e421818f86 Mon Sep 17 00:00:00 2001 From: Sean Lynch Date: Tue, 17 Nov 2026 22:56:54 -0500 Subject: [PATCH 1/3] chore: Remove d3-dsv dependency with inline CSV parsing in sankeyGraphFromCsv Co-Authored-By: Claude Opus 5.5 --- .changeset/drop-d3-dsv.md | 5 +++ packages/layerchart/package.json | 2 -- .../src/lib/utils/graph/sankey.test.ts | 32 +++++++++++++++++++ .../layerchart/src/lib/utils/graph/sankey.ts | 23 ++++++------- pnpm-lock.yaml | 6 ---- 5 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 .changeset/drop-d3-dsv.md create mode 100644 packages/layerchart/src/lib/utils/graph/sankey.test.ts diff --git a/.changeset/drop-d3-dsv.md b/.changeset/drop-d3-dsv.md new file mode 100644 index 0000000000..1f3d50ab80 --- /dev/null +++ b/.changeset/drop-d3-dsv.md @@ -0,0 +1,5 @@ +--- +'layerchart': patch +--- + +chore: Remove `d3-dsv` dependency (`sankeyGraphFromCsv()` now splits on newlines and commas, without quoted field support) diff --git a/packages/layerchart/package.json b/packages/layerchart/package.json index e6048321bb..0b94c7886f 100644 --- a/packages/layerchart/package.json +++ b/packages/layerchart/package.json @@ -37,7 +37,6 @@ "@types/d3-color": "^3.1.3", "@types/d3-contour": "^3.0.6", "@types/d3-delaunay": "^6.0.4", - "@types/d3-dsv": "^3.0.7", "@types/d3-force": "^3.0.10", "@types/d3-geo": "^3.1.0", "@types/d3-hierarchy": "^3.1.7", @@ -80,7 +79,6 @@ "d3-color": "^3.1.0", "d3-contour": "^4.0.2", "d3-delaunay": "^6.0.4", - "d3-dsv": "^3.0.1", "d3-force": "^3.0.0", "d3-geo": "^3.1.1", "d3-geo-polygon": "^2.0.1", diff --git a/packages/layerchart/src/lib/utils/graph/sankey.test.ts b/packages/layerchart/src/lib/utils/graph/sankey.test.ts new file mode 100644 index 0000000000..30ceea5763 --- /dev/null +++ b/packages/layerchart/src/lib/utils/graph/sankey.test.ts @@ -0,0 +1,32 @@ +import { describe, it, expect } from 'vitest'; +import { sankeyGraphFromCsv } from './sankey.js'; + +describe('sankeyGraphFromCsv', () => { + it('parses source,target,value rows into links and distinct nodes', () => { + const graph = sankeyGraphFromCsv('A,B,5\nA,C,3\nB,C,2'); + expect(graph.links).toEqual([ + { source: 'A', target: 'B', value: 5 }, + { source: 'A', target: 'C', value: 3 }, + { source: 'B', target: 'C', value: 2 }, + ]); + expect(graph.nodes).toEqual([{ name: 'A' }, { name: 'B' }, { name: 'C' }]); + }); + + it('defaults a missing or non-numeric value to 1', () => { + const graph = sankeyGraphFromCsv('A,B\nA,C,\nB,C,abc'); + expect(graph.links.map((l) => l.value)).toEqual([1, 1, 1]); + }); + + it('skips blank lines and rows without a source or target', () => { + const graph = sankeyGraphFromCsv('\nA,B,1\n\nA\n,B,2\nB,C,3\n'); + expect(graph.links).toEqual([ + { source: 'A', target: 'B', value: 1 }, + { source: 'B', target: 'C', value: 3 }, + ]); + }); + + it('handles CRLF and CR line endings', () => { + expect(sankeyGraphFromCsv('A,B,1\r\nB,C,2').links).toHaveLength(2); + expect(sankeyGraphFromCsv('A,B,1\rB,C,2').links).toHaveLength(2); + }); +}); diff --git a/packages/layerchart/src/lib/utils/graph/sankey.ts b/packages/layerchart/src/lib/utils/graph/sankey.ts index 44f89a5b28..13233d5276 100644 --- a/packages/layerchart/src/lib/utils/graph/sankey.ts +++ b/packages/layerchart/src/lib/utils/graph/sankey.ts @@ -1,4 +1,3 @@ -import { csvParseRows } from 'd3-dsv'; import type { SankeyExtraProperties, SankeyGraph, @@ -9,19 +8,21 @@ import type { import type { hierarchy as d3Hierarchy } from 'd3-hierarchy'; /** - * Convert CSV rows in format: 'source,target,value' to SankeyGraph + * Convert CSV rows in format: 'source,target,value' to SankeyGraph. + * + * Rows missing a source or target are skipped, and a missing or non-numeric value defaults to `1`. + * Splits on newlines and commas only (no quoted fields). Parse other formats yourself and pass + * the links to `sankeyNodesFromLinks()`. */ export function sankeyGraphFromCsv(csv: string): SankeyGraph { - const links = csvParseRows(csv, (row) => { - const [source, target, rawValue] = row; - if (!source || !target) return null; + const links: Array<{ source: string; target: string; value: number }> = []; + + for (const line of csv.split(/\r\n|\r|\n/)) { + const [source, target, rawValue] = line.split(','); + if (!source || !target) continue; const num = rawValue ? +rawValue : NaN; - return { - source, - target, - value: isNaN(num) ? 1 : num, - }; - }); + links.push({ source, target, value: isNaN(num) ? 1 : num }); + } return { nodes: sankeyNodesFromLinks(links), links }; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3cba957205..a560352eee 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -818,9 +818,6 @@ importers: d3-delaunay: specifier: ^6.0.4 version: 6.0.4 - d3-dsv: - specifier: ^3.0.1 - version: 3.0.1 d3-force: specifier: ^3.0.0 version: 3.0.0 @@ -903,9 +900,6 @@ importers: '@types/d3-delaunay': specifier: ^6.0.4 version: 6.0.4 - '@types/d3-dsv': - specifier: ^3.0.7 - version: 3.0.7 '@types/d3-force': specifier: ^3.0.10 version: 3.0.10 From 01a18c3105857f677aa44dc3875ee35dea62f1b3 Mon Sep 17 00:00:00 2001 From: Sean Lynch Date: Tue, 17 Nov 2026 23:02:31 -0500 Subject: [PATCH 2/3] chore: Remove d3-geo-polygon dependency with local Gray-Fuller projection Co-Authored-By: Claude Opus 5.5 --- .changeset/drop-d3-geo-polygon.md | 5 ++ packages/layerchart/package.json | 1 - .../components/geo/GeoBin/GeoBin.sphere.ts | 3 +- .../lib/components/geo/GeoBin/grayFuller.ts | 64 +++++++++++++++++++ pnpm-lock.yaml | 13 ---- 5 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 .changeset/drop-d3-geo-polygon.md create mode 100644 packages/layerchart/src/lib/components/geo/GeoBin/grayFuller.ts diff --git a/.changeset/drop-d3-geo-polygon.md b/.changeset/drop-d3-geo-polygon.md new file mode 100644 index 0000000000..7776f4b82d --- /dev/null +++ b/.changeset/drop-d3-geo-polygon.md @@ -0,0 +1,5 @@ +--- +'layerchart': patch +--- + +chore: Remove `d3-geo-polygon` dependency (and its `d3-geo-projection` and `commander` dependencies) diff --git a/packages/layerchart/package.json b/packages/layerchart/package.json index 0b94c7886f..77f9148b5d 100644 --- a/packages/layerchart/package.json +++ b/packages/layerchart/package.json @@ -81,7 +81,6 @@ "d3-delaunay": "^6.0.4", "d3-force": "^3.0.0", "d3-geo": "^3.1.1", - "d3-geo-polygon": "^2.0.1", "d3-geo-voronoi": "^2.1.0", "d3-hierarchy": "^3.1.2", "d3-interpolate": "^3.0.1", diff --git a/packages/layerchart/src/lib/components/geo/GeoBin/GeoBin.sphere.ts b/packages/layerchart/src/lib/components/geo/GeoBin/GeoBin.sphere.ts index a3b82ca5f1..e40e16f130 100644 --- a/packages/layerchart/src/lib/components/geo/GeoBin/GeoBin.sphere.ts +++ b/packages/layerchart/src/lib/components/geo/GeoBin/GeoBin.sphere.ts @@ -10,12 +10,11 @@ import { geoRotation, } from 'd3-geo'; // @ts-expect-error - no types available -import { geoGrayFullerRaw } from 'd3-geo-polygon'; -// @ts-expect-error - no types available import { geoDelaunay } from 'd3-geo-voronoi'; import { radiansToDegrees } from '$lib/utils/math.js'; import { sideShade } from '$lib/utils/isometric.js'; import { weightOf } from '../../Bin/Bin.shared.svelte.js'; +import { geoGrayFullerRaw } from './grayFuller.js'; import type { BinCell, BinShape, diff --git a/packages/layerchart/src/lib/components/geo/GeoBin/grayFuller.ts b/packages/layerchart/src/lib/components/geo/GeoBin/grayFuller.ts new file mode 100644 index 0000000000..a9df44cba3 --- /dev/null +++ b/packages/layerchart/src/lib/components/geo/GeoBin/grayFuller.ts @@ -0,0 +1,64 @@ +/* + * Buckminster Fuller’s spherical triangle transformation procedure + * + * Based on Robert W. Gray’s formulae published in “Exact Transformation Equations + * For Fuller's World Map,” _Cartographica_, 32(3): 17-25 (1995). + * + * Implemented for D3.js by Philippe Rivière, 2018 (https://visionscarto.net/), as + * `geoGrayFullerRaw` in d3-geo-polygon. + * + * To the extent possible under law, Philippe Rivière has waived all copyright + * and related or neighboring rights to this implementation. (Public Domain.) + */ +import { geoGnomonicRaw, type GeoRawProjection } from 'd3-geo'; + +const epsilon = 1e-6; + +// `geoGnomonicRaw` is the raw projection itself, though `@types/d3-geo` types it as a factory +const gnomonicRaw = geoGnomonicRaw as unknown as GeoRawProjection; + +/** Maps one spherical icosahedron face onto a flat triangle with equal areas */ +export function geoGrayFullerRaw(): GeoRawProjection { + const SQRT_3 = Math.sqrt(3); + + // Gray’s constants + const Z = Math.sqrt(5 + 2 * Math.sqrt(5)) / Math.sqrt(15); + const el = Math.sqrt(8) / Math.sqrt(5 + Math.sqrt(5)); + const dve = Math.sqrt(3 + Math.sqrt(5)) / Math.sqrt(5 + Math.sqrt(5)); + + const grayFuller: GeoRawProjection = (lambda, phi) => { + const cosPhi = Math.cos(phi); + const s = Z / (cosPhi * Math.cos(lambda)); + const x = cosPhi * Math.sin(lambda) * s; + const y = Math.sin(phi) * s; + const a1p = Math.atan2((2 * y) / SQRT_3 + el / 3 - el / 2, dve); + const a2p = Math.atan2(x - y / SQRT_3 + el / 3 - el / 2, dve); + const a3p = Math.atan2(el / 3 - x - y / SQRT_3 - el / 2, dve); + + return [SQRT_3 * (a2p - a3p), 2 * a1p - a2p - a3p]; + }; + + // Inverse approximation + grayFuller.invert = (x, y) => { + // A point outside the triangle returns something meaningless, but far enough away + if (x * x + y * y > 5) return [0, 3]; + + const R = 2.9309936378128416; + const p = gnomonicRaw.invert!(x / R, y / R); + + let j = 0; + let dx: number; + let dy: number; + do { + const f = grayFuller(p[0], p[1]); + dx = x - f[0]; + dy = y - f[1]; + p[0] += 0.2 * dx; + p[1] += 0.2 * dy; + } while (j++ < 30 && Math.abs(dx) + Math.abs(dy) > epsilon); + + return p; + }; + + return grayFuller; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a560352eee..e7fd74d67f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -824,9 +824,6 @@ importers: d3-geo: specifier: ^3.1.1 version: 3.1.1 - d3-geo-polygon: - specifier: ^2.0.1 - version: 2.0.1 d3-geo-voronoi: specifier: ^2.1.0 version: 2.1.0 @@ -4124,10 +4121,6 @@ packages: resolution: {integrity: sha512-YyUI6AEuY/Wpt8KWLgZHsIU86atmikuoOmCfommt0LYHiQSPjvX2AcFc38PX0CBpr2RCyZhjex+NS/LPOv6YqA==} engines: {node: '>=12'} - d3-geo-polygon@2.0.1: - resolution: {integrity: sha512-e+CxAgyQLWaitzftCisJ7DUIYhUzSN8VSbDozouKp3bMb6QbRrRLShplpPLYisuyyZLtrdC1+5yAjpiTOne33g==} - engines: {node: '>=18'} - d3-geo-projection@4.0.0: resolution: {integrity: sha512-p0bK60CEzph1iqmnxut7d/1kyTmm3UWtPlwdkM31AU+LW+BXazd5zJdoCn7VFxNCHXRngPHRnsNn5uGjLRGndg==} engines: {node: '>=12'} @@ -9848,12 +9841,6 @@ snapshots: d3-format@3.1.0: {} - d3-geo-polygon@2.0.1: - dependencies: - d3-array: 3.2.4 - d3-geo: 3.1.1 - d3-geo-projection: 4.0.0 - d3-geo-projection@4.0.0: dependencies: commander: 7.2.0 From febb7c4f87ca7a5863b36e35af9558cf27b94dd5 Mon Sep 17 00:00:00 2001 From: Sean Lynch Date: Tue, 17 Nov 2026 23:16:50 -0500 Subject: [PATCH 3/3] chore: Replace d3-sankey with a local Sankey layout Co-Authored-By: Claude Opus 5.5 --- .changeset/drop-d3-dsv.md | 5 - .changeset/drop-d3-geo-polygon.md | 5 - .changeset/trim-dependencies.md | 5 + docs/package.json | 2 - docs/src/content/guides/bundle-size.md | 12 +- .../examples/components/Sankey/complex.svelte | 3 +- .../components/Sankey/hierarchy.svelte | 3 +- .../components/Sankey/node-select.svelte | 2 +- packages/layerchart/package.json | 2 - .../components/Chart/Chart.shared.svelte.ts | 2 +- .../src/lib/components/graph/Sankey.svelte | 48 +- .../layerchart/src/lib/utils/graph/sankey.ts | 4 +- .../src/lib/utils/graph/sankeyLayout.test.ts | 159 ++++++ .../src/lib/utils/graph/sankeyLayout.ts | 458 ++++++++++++++++++ packages/layerchart/src/lib/utils/types.ts | 2 +- pnpm-lock.yaml | 63 --- 16 files changed, 659 insertions(+), 116 deletions(-) delete mode 100644 .changeset/drop-d3-dsv.md delete mode 100644 .changeset/drop-d3-geo-polygon.md create mode 100644 .changeset/trim-dependencies.md create mode 100644 packages/layerchart/src/lib/utils/graph/sankeyLayout.test.ts create mode 100644 packages/layerchart/src/lib/utils/graph/sankeyLayout.ts diff --git a/.changeset/drop-d3-dsv.md b/.changeset/drop-d3-dsv.md deleted file mode 100644 index 1f3d50ab80..0000000000 --- a/.changeset/drop-d3-dsv.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'layerchart': patch ---- - -chore: Remove `d3-dsv` dependency (`sankeyGraphFromCsv()` now splits on newlines and commas, without quoted field support) diff --git a/.changeset/drop-d3-geo-polygon.md b/.changeset/drop-d3-geo-polygon.md deleted file mode 100644 index 7776f4b82d..0000000000 --- a/.changeset/drop-d3-geo-polygon.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'layerchart': patch ---- - -chore: Remove `d3-geo-polygon` dependency (and its `d3-geo-projection` and `commander` dependencies) diff --git a/.changeset/trim-dependencies.md b/.changeset/trim-dependencies.md new file mode 100644 index 0000000000..1a2c1c3cbc --- /dev/null +++ b/.changeset/trim-dependencies.md @@ -0,0 +1,5 @@ +--- +'layerchart': patch +--- + +chore: Remove `d3-dsv`, `d3-geo-polygon` and `d3-sankey` dependencies, dropping 12 packages (~2.9 MB) from installs, including the command-line-only `commander`, `iconv-lite` and `rw`, and older duplicate versions of `d3-array`, `d3-shape` and `d3-path` (`sankeyGraphFromCsv()` no longer handles quoted fields) diff --git a/docs/package.json b/docs/package.json index 2947d92c4f..c75b370dd0 100644 --- a/docs/package.json +++ b/docs/package.json @@ -55,7 +55,6 @@ "@types/d3-hierarchy": "^3.1.7", "@types/d3-interpolate": "^3.0.4", "@types/d3-random": "^3.0.3", - "@types/d3-sankey": "^0.12.5", "@types/d3-scale": "^4.0.9", "@types/d3-scale-chromatic": "^3.1.0", "@types/d3-shape": "^3.1.8", @@ -78,7 +77,6 @@ "d3-hierarchy": "^3.1.2", "d3-interpolate": "^3.0.1", "d3-random": "^3.0.1", - "d3-sankey": "^0.12.3", "d3-scale": "^4.0.2", "d3-scale-chromatic": "^3.1.0", "d3-shape": "^3.2.0", diff --git a/docs/src/content/guides/bundle-size.md b/docs/src/content/guides/bundle-size.md index f1db405581..d06875f602 100644 --- a/docs/src/content/guides/bundle-size.md +++ b/docs/src/content/guides/bundle-size.md @@ -36,12 +36,12 @@ These additions to your chart cause an extra HTTP fetch the first time the corre Components that bring in large d3 modules or framework-specific libraries are not re-exported from the root `'layerchart'` entry. They live behind opt-in sub-paths so the default barrel doesn't drag those deps into bundlers that don't tree-shake aggressively. -| Sub-path | Components | Heavy dep saved | -| ---------------------- | -------------------------------------- | -------------------------------------------------- | -| `layerchart/geo` | `Geo*` (12), `Graticule`, `TileImage` | `d3-geo` (~15 KB), `d3-tile` | -| `layerchart/hierarchy` | `Tree`, `Treemap`, `Pack`, `Partition` | `d3-hierarchy` (~6 KB) | -| `layerchart/force` | `ForceSimulation` | `d3-force` (~7 KB) | -| `layerchart/graph` | `Dagre`, `Sankey`, `Chord`, `Ribbon` | `@dagrejs/dagre` (~22 KB), `d3-sankey`, `d3-chord` | +| Sub-path | Components | Heavy dep saved | +| ---------------------- | -------------------------------------- | ---------------------------------------------------- | +| `layerchart/geo` | `Geo*` (12), `Graticule`, `TileImage` | `d3-geo` (~15 KB), `d3-tile` | +| `layerchart/hierarchy` | `Tree`, `Treemap`, `Pack`, `Partition` | `d3-hierarchy` (~6 KB) | +| `layerchart/force` | `ForceSimulation` | `d3-force` (~7 KB) | +| `layerchart/graph` | `Dagre`, `Sankey`, `Chord`, `Ribbon` | `@dagrejs/dagre` (~22 KB), Sankey layout, `d3-chord` | If you use these components, just import from the sub-path: diff --git a/docs/src/examples/components/Sankey/complex.svelte b/docs/src/examples/components/Sankey/complex.svelte index f911389719..53f475f221 100644 --- a/docs/src/examples/components/Sankey/complex.svelte +++ b/docs/src/examples/components/Sankey/complex.svelte @@ -53,7 +53,6 @@ onUpdate={(e) => { // Calculate domain extents from Sankey data // TODO: Update as 'nodeColorBy' changes - // @ts-expect-error const extents = extent(e.nodes, (d) => d[config.nodeColorBy]); // @ts-expect-error colorScale.domain(extents); @@ -93,7 +92,7 @@ { highlightLinkIndexes = [ diff --git a/docs/src/examples/components/Sankey/hierarchy.svelte b/docs/src/examples/components/Sankey/hierarchy.svelte index c7a9ea043e..411e879e28 100644 --- a/docs/src/examples/components/Sankey/hierarchy.svelte +++ b/docs/src/examples/components/Sankey/hierarchy.svelte @@ -59,7 +59,6 @@ onUpdate={(e) => { // Calculate domain extents from Sankey data // TODO: Update as 'nodeColorBy' changes - // @ts-expect-error const extents = extent(e.nodes, (d) => d[config.nodeColorBy]); // @ts-expect-error colorScale.domain(extents); @@ -94,7 +93,7 @@ { highlightLinkIndexes = [ diff --git a/docs/src/examples/components/Sankey/node-select.svelte b/docs/src/examples/components/Sankey/node-select.svelte index f0bbca7fd0..ec17ecd3a8 100644 --- a/docs/src/examples/components/Sankey/node-select.svelte +++ b/docs/src/examples/components/Sankey/node-select.svelte @@ -4,7 +4,7 @@