@danielsimonjr/mathts-plot 0.4.4 → 0.4.6

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
@@ -1,142 +1,142 @@
1
- # @danielsimonjr/mathts-plot
2
-
3
- Headless SVG 2D/3D plotting for [MathTS](https://github.com/danielsimonjr/mathts).
4
-
5
- Pure functions returning self-contained SVG strings — no browser, no canvas, no runtime
6
- dependencies beyond the MathTS bedrock (`core`/`functions`/`expression`). Every function
7
- coerces its input and never throws on bad data: empty, `NaN`, or `Infinity` inputs render a
8
- "no data" placeholder SVG instead of crashing. See
9
- `docs/superpowers/specs/2026-07-07-plotting-design.md` for the design.
10
-
11
- ## Install
12
-
13
- ```sh
14
- npm install @danielsimonjr/mathts-plot
15
- ```
16
-
17
- ## Usage
18
-
19
- Every function below returns a self-contained SVG string (`<svg ...>...</svg>`) — render it
20
- directly in an `<img src="data:image/svg+xml,...">`, write it to a `.svg` file, or drop it
21
- into HTML.
22
-
23
- ### `line` — one series, or overlaid with others
24
-
25
- ```ts
26
- import { line } from '@danielsimonjr/mathts-plot';
27
-
28
- const svg = line([0, 1, 2, 3], [0, 1, 4, 9], { title: 'squares', xLabel: 'x', yLabel: 'y' });
29
- // svg === '<svg ...>...</svg>'
30
- ```
31
-
32
- ### `overlay` — combine several layers on shared, auto-scaled axes
33
-
34
- ```ts
35
- import { overlay } from '@danielsimonjr/mathts-plot';
36
-
37
- const svg = overlay(
38
- [
39
- { type: 'line', x: [0, 1, 2], y: [0, 1, 2], label: 'model' },
40
- { type: 'scatter', x: [0, 1, 2], y: [0.1, 1.2, 1.8], label: 'data' },
41
- ],
42
- { legend: true }
43
- );
44
- ```
45
-
46
- ### `surface` — 3D surface via orthographic projection + painter's algorithm
47
-
48
- ```ts
49
- import { surface } from '@danielsimonjr/mathts-plot';
50
-
51
- const z = [
52
- [0, 1, 2],
53
- [1, 2, 3],
54
- [2, 3, 4],
55
- ]; // z[row][col]
56
- const svg = surface(z, { kind: 'filled', azim: 45, elev: 25 });
57
- ```
58
-
59
- ### `plot` — generic, polymorphic entry point (including expression strings)
60
-
61
- ```ts
62
- import { plot } from '@danielsimonjr/mathts-plot';
63
-
64
- // numeric series → line
65
- plot([0, 1, 4, 9]);
66
- plot([0, 1, 2, 3], [0, 1, 4, 9]);
67
-
68
- // expression string, sampled via functions/expression → line
69
- plot('sin(x)', { from: 0, to: Math.PI, samples: 200 });
70
-
71
- // two free variables → contour (default) or 3D surface
72
- plot('x^2 + y^2', { from: -2, to: 2 });
73
- plot('x^2 + y^2', { from: -2, to: 2, kind: 'surface' });
74
-
75
- // array of Layer2D → overlay
76
- plot([
77
- { type: 'line', x: [0, 1, 2], y: [0, 1, 2] },
78
- { type: 'scatter', x: [0, 1, 2], y: [0.1, 1.2, 1.8] },
79
- ]);
80
- ```
81
-
82
- ### TikZ / LaTeX output
83
-
84
- Every function also accepts `{ format: 'tikz' }` (default is `'svg'`), and there's a
85
- generic `toTikZ()` mirroring `plot()`. The geometry matches the SVG output exactly (a
86
- deterministic y-flip maps SVG's top-left origin to TikZ's bottom-left); only
87
- `\usepackage{tikz}` is required to compile the result.
88
-
89
- ```ts
90
- import { toTikZ, line, surface } from '@danielsimonjr/mathts-plot';
91
-
92
- // generic entry point, expression source → line
93
- const tex = toTikZ('sin(x)', { from: 0, to: Math.PI });
94
-
95
- // per-type function via the format option
96
- const tex2 = line([0, 1, 2, 3], [0, 1, 4, 9], { format: 'tikz' });
97
-
98
- // standalone: false → a bare tikzpicture for \input, not a compilable document
99
- const z = [
100
- [0, 1, 2],
101
- [1, 2, 3],
102
- [2, 3, 4],
103
- ];
104
- const fragment = surface(z, { format: 'tikz', tikz: { standalone: false } });
105
- ```
106
-
107
- `toTikZ`/`{ format: 'tikz' }` return a LaTeX string, not SVG. With `standalone`
108
- (default `true`) the string is a complete `\documentclass{...}` document you can
109
- compile as-is; `standalone: false` returns just the `tikzpicture` environment, meant
110
- to be pulled in via `\input{...}` from a larger document.
111
-
112
- ## What it provides
113
-
114
- Per-type mark functions, each `(x, y, opts?) => string` unless noted:
115
-
116
- - **2D**: `line`, `scatter`, `bar`, `area`, `step`, `errorbar(x, y, yerr, opts?)`,
117
- `quiver(x, y, u, v, opts?)`.
118
- - **`histogram(data, opts?)`** — bins via `functions.histogram`, then renders bar heights at
119
- bin centers.
120
- - **`heatmap(z, opts?)`** — viridis-colored grid, `z` as `z[row][col]`.
121
- - **`contour(z, opts?)`** — marching squares over a `z[row][col]` grid.
122
- - **`overlay(layers, opts?)`** — several `Layer2D`s on shared, auto-scaled axes.
123
- - **3D**: `surface(z, opts?)` (wireframe or painter's-algorithm filled),
124
- `scatter3d(x, y, z, opts?)`, `curve3d(x, y, z, opts?)` — all projected with an orthographic
125
- camera (`azim`/`elev`).
126
- - **`plot(...)`** — generic polymorphic dispatch over numeric series, layer arrays, and
127
- expression-source strings (evaluated via `@danielsimonjr/mathts-expression` /
128
- `@danielsimonjr/mathts-functions`).
129
- - **`viridis(t)`** — the perceptually-uniform colormap used by `heatmap`/`contour`/`surface`,
130
- exposed for reuse.
131
-
132
- Notes:
133
-
134
- - Pure ESM, `strict` TypeScript, no runtime dependencies beyond the MathTS bedrock
135
- (`core`/`functions`/`expression`).
136
- - SVG-only in v1 — no PNG/canvas rendering.
137
- - `workbook` renders its charts through this package (its former private `svg.ts` plotter was
138
- removed).
139
-
140
- ## License
141
-
142
- MIT (c) Daniel Simon Jr.
1
+ # @danielsimonjr/mathts-plot
2
+
3
+ Headless SVG 2D/3D plotting for [MathTS](https://github.com/danielsimonjr/mathts).
4
+
5
+ Pure functions returning self-contained SVG strings — no browser, no canvas, no runtime
6
+ dependencies beyond the MathTS bedrock (`core`/`functions`/`expression`). Every function
7
+ coerces its input and never throws on bad data: empty, `NaN`, or `Infinity` inputs render a
8
+ "no data" placeholder SVG instead of crashing. See
9
+ `docs/superpowers/specs/2026-07-07-plotting-design.md` for the design.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ npm install @danielsimonjr/mathts-plot
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Every function below returns a self-contained SVG string (`<svg ...>...</svg>`) — render it
20
+ directly in an `<img src="data:image/svg+xml,...">`, write it to a `.svg` file, or drop it
21
+ into HTML.
22
+
23
+ ### `line` — one series, or overlaid with others
24
+
25
+ ```ts
26
+ import { line } from '@danielsimonjr/mathts-plot';
27
+
28
+ const svg = line([0, 1, 2, 3], [0, 1, 4, 9], { title: 'squares', xLabel: 'x', yLabel: 'y' });
29
+ // svg === '<svg ...>...</svg>'
30
+ ```
31
+
32
+ ### `overlay` — combine several layers on shared, auto-scaled axes
33
+
34
+ ```ts
35
+ import { overlay } from '@danielsimonjr/mathts-plot';
36
+
37
+ const svg = overlay(
38
+ [
39
+ { type: 'line', x: [0, 1, 2], y: [0, 1, 2], label: 'model' },
40
+ { type: 'scatter', x: [0, 1, 2], y: [0.1, 1.2, 1.8], label: 'data' },
41
+ ],
42
+ { legend: true }
43
+ );
44
+ ```
45
+
46
+ ### `surface` — 3D surface via orthographic projection + painter's algorithm
47
+
48
+ ```ts
49
+ import { surface } from '@danielsimonjr/mathts-plot';
50
+
51
+ const z = [
52
+ [0, 1, 2],
53
+ [1, 2, 3],
54
+ [2, 3, 4],
55
+ ]; // z[row][col]
56
+ const svg = surface(z, { kind: 'filled', azim: 45, elev: 25 });
57
+ ```
58
+
59
+ ### `plot` — generic, polymorphic entry point (including expression strings)
60
+
61
+ ```ts
62
+ import { plot } from '@danielsimonjr/mathts-plot';
63
+
64
+ // numeric series → line
65
+ plot([0, 1, 4, 9]);
66
+ plot([0, 1, 2, 3], [0, 1, 4, 9]);
67
+
68
+ // expression string, sampled via functions/expression → line
69
+ plot('sin(x)', { from: 0, to: Math.PI, samples: 200 });
70
+
71
+ // two free variables → contour (default) or 3D surface
72
+ plot('x^2 + y^2', { from: -2, to: 2 });
73
+ plot('x^2 + y^2', { from: -2, to: 2, kind: 'surface' });
74
+
75
+ // array of Layer2D → overlay
76
+ plot([
77
+ { type: 'line', x: [0, 1, 2], y: [0, 1, 2] },
78
+ { type: 'scatter', x: [0, 1, 2], y: [0.1, 1.2, 1.8] },
79
+ ]);
80
+ ```
81
+
82
+ ### TikZ / LaTeX output
83
+
84
+ Every function also accepts `{ format: 'tikz' }` (default is `'svg'`), and there's a
85
+ generic `toTikZ()` mirroring `plot()`. The geometry matches the SVG output exactly (a
86
+ deterministic y-flip maps SVG's top-left origin to TikZ's bottom-left); only
87
+ `\usepackage{tikz}` is required to compile the result.
88
+
89
+ ```ts
90
+ import { toTikZ, line, surface } from '@danielsimonjr/mathts-plot';
91
+
92
+ // generic entry point, expression source → line
93
+ const tex = toTikZ('sin(x)', { from: 0, to: Math.PI });
94
+
95
+ // per-type function via the format option
96
+ const tex2 = line([0, 1, 2, 3], [0, 1, 4, 9], { format: 'tikz' });
97
+
98
+ // standalone: false → a bare tikzpicture for \input, not a compilable document
99
+ const z = [
100
+ [0, 1, 2],
101
+ [1, 2, 3],
102
+ [2, 3, 4],
103
+ ];
104
+ const fragment = surface(z, { format: 'tikz', tikz: { standalone: false } });
105
+ ```
106
+
107
+ `toTikZ`/`{ format: 'tikz' }` return a LaTeX string, not SVG. With `standalone`
108
+ (default `true`) the string is a complete `\documentclass{...}` document you can
109
+ compile as-is; `standalone: false` returns just the `tikzpicture` environment, meant
110
+ to be pulled in via `\input{...}` from a larger document.
111
+
112
+ ## What it provides
113
+
114
+ Per-type mark functions, each `(x, y, opts?) => string` unless noted:
115
+
116
+ - **2D**: `line`, `scatter`, `bar`, `area`, `step`, `errorbar(x, y, yerr, opts?)`,
117
+ `quiver(x, y, u, v, opts?)`.
118
+ - **`histogram(data, opts?)`** — bins via `functions.histogram`, then renders bar heights at
119
+ bin centers.
120
+ - **`heatmap(z, opts?)`** — viridis-colored grid, `z` as `z[row][col]`.
121
+ - **`contour(z, opts?)`** — marching squares over a `z[row][col]` grid.
122
+ - **`overlay(layers, opts?)`** — several `Layer2D`s on shared, auto-scaled axes.
123
+ - **3D**: `surface(z, opts?)` (wireframe or painter's-algorithm filled),
124
+ `scatter3d(x, y, z, opts?)`, `curve3d(x, y, z, opts?)` — all projected with an orthographic
125
+ camera (`azim`/`elev`).
126
+ - **`plot(...)`** — generic polymorphic dispatch over numeric series, layer arrays, and
127
+ expression-source strings (evaluated via `@danielsimonjr/mathts-expression` /
128
+ `@danielsimonjr/mathts-functions`).
129
+ - **`viridis(t)`** — the perceptually-uniform colormap used by `heatmap`/`contour`/`surface`,
130
+ exposed for reuse.
131
+
132
+ Notes:
133
+
134
+ - Pure ESM, `strict` TypeScript, no runtime dependencies beyond the MathTS bedrock
135
+ (`core`/`functions`/`expression`).
136
+ - SVG-only in v1 — no PNG/canvas rendering.
137
+ - `workbook` renders its charts through this package (its former private `svg.ts` plotter was
138
+ removed).
139
+
140
+ ## License
141
+
142
+ MIT (c) Daniel Simon Jr.
package/dist/index.js CHANGED
@@ -281,9 +281,13 @@ function renderLine(layer, f, i) {
281
281
  }
282
282
  function renderScatter(layer, f, i) {
283
283
  const color = layer.color ?? f.color(i);
284
- return xy(layer).map(
285
- ([x, y]) => ({ k: "circle", cx: f.px(x), cy: f.py(y), r: 3, fill: color })
286
- );
284
+ return xy(layer).map(([x, y]) => ({
285
+ k: "circle",
286
+ cx: f.px(x),
287
+ cy: f.py(y),
288
+ r: 3,
289
+ fill: color
290
+ }));
287
291
  }
288
292
  function renderBar(layer, f, i) {
289
293
  const color = layer.color ?? f.color(i);
@@ -1208,7 +1212,7 @@ function curve3d(x, y, z, opts = {}) {
1208
1212
  }
1209
1213
 
1210
1214
  // src/index.ts
1211
- var VERSION = "0.4.4";
1215
+ var VERSION = "0.4.6";
1212
1216
  export {
1213
1217
  VERSION,
1214
1218
  area,
@@ -1 +1 @@
1
- {"version":3,"file":"render-core.d.ts","sourceRoot":"","sources":["../src/render-core.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,KAAK,EAAE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,CAAC;IACf,KAAK,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;CAC9B;AA2JD,kEAAkE;AAClE,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,CAmBvE"}
package/package.json CHANGED
@@ -1,71 +1,72 @@
1
- {
2
- "name": "@danielsimonjr/mathts-plot",
3
- "version": "0.4.4",
4
- "description": "Headless SVG 2D/3D plotting for MathTS — dependency-light, expression-aware, built on core/functions/expression",
5
- "author": "Daniel Simon Jr.",
6
- "license": "MIT",
7
- "type": "module",
8
- "main": "./dist/index.js",
9
- "module": "./dist/index.js",
10
- "types": "./dist/index.d.ts",
11
- "exports": {
12
- ".": {
13
- "import": "./dist/index.js",
14
- "types": "./dist/index.d.ts"
15
- },
16
- "./render": {
17
- "import": "./dist/render-file.js",
18
- "types": "./dist/render-file.d.ts"
19
- }
20
- },
21
- "files": [
22
- "dist",
23
- "README.md"
24
- ],
25
- "scripts": {
26
- "build": "tsup && tsc -p tsconfig.dts.json",
27
- "dev": "tsup --watch",
28
- "test": "bun test --isolate --timeout 30000",
29
- "test:watch": "vitest",
30
- "test:coverage": "bun test --isolate --timeout 30000 --coverage",
31
- "typecheck": "tsc --noEmit",
32
- "lint": "oxlint src",
33
- "lint:fix": "oxlint src --fix",
34
- "clean": "rm -rf dist"
35
- },
36
- "dependencies": {
37
- "@danielsimonjr/mathts-core": "^0.15.0",
38
- "@danielsimonjr/mathts-functions": "^0.64.1",
39
- "@danielsimonjr/mathts-expression": "^0.8.0"
40
- },
41
- "devDependencies": {
42
- "@types/node": "^26.4.0",
43
- "@xmldom/xmldom": "^0.9.12",
44
- "tsup": "^8.0.0",
45
- "typescript": "^7.0.2",
46
- "vitest": "^5.0.0"
47
- },
48
- "publishConfig": {
49
- "access": "public"
50
- },
51
- "repository": {
52
- "type": "git",
53
- "url": "git+https://github.com/danielsimonjr/MathTS.git",
54
- "directory": "plot"
55
- },
56
- "keywords": [
57
- "math",
58
- "typescript",
59
- "plot",
60
- "plotting",
61
- "chart",
62
- "svg",
63
- "2d",
64
- "3d",
65
- "surface",
66
- "contour",
67
- "heatmap",
68
- "visualization",
69
- "headless"
70
- ]
71
- }
1
+ {
2
+ "name": "@danielsimonjr/mathts-plot",
3
+ "version": "0.4.6",
4
+ "description": "Headless SVG 2D/3D plotting for MathTS — dependency-light, expression-aware, built on core/functions/expression",
5
+ "author": "Daniel Simon Jr.",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "types": "./dist/index.d.ts"
15
+ },
16
+ "./render": {
17
+ "import": "./dist/render-file.js",
18
+ "types": "./dist/render-file.d.ts"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
25
+ "scripts": {
26
+ "build": "tsup && tsc -p tsconfig.dts.json",
27
+ "build:prod": "tsup --minify --treeshake && tsc -p tsconfig.dts.json",
28
+ "dev": "tsup --watch --onSuccess \"tsc -p tsconfig.dts.json\"",
29
+ "test": "bun test --isolate --timeout 30000",
30
+ "test:watch": "vitest",
31
+ "test:coverage": "bun test --isolate --timeout 30000 --coverage",
32
+ "typecheck": "tsc --noEmit && tsc -p tsconfig.test.json",
33
+ "lint": "oxlint src",
34
+ "lint:fix": "oxlint src --fix",
35
+ "clean": "rm -rf dist"
36
+ },
37
+ "dependencies": {
38
+ "@danielsimonjr/mathts-core": "^0.15.5",
39
+ "@danielsimonjr/mathts-functions": "^0.65.0",
40
+ "@danielsimonjr/mathts-expression": "^0.8.2"
41
+ },
42
+ "devDependencies": {
43
+ "@types/node": "^26.4.0",
44
+ "@xmldom/xmldom": "^0.9.12",
45
+ "tsup": "^8.0.0",
46
+ "typescript": "^7.0.2",
47
+ "vitest": "^5.0.0"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "repository": {
53
+ "type": "git",
54
+ "url": "git+https://github.com/danielsimonjr/MathTS.git",
55
+ "directory": "plot"
56
+ },
57
+ "keywords": [
58
+ "math",
59
+ "typescript",
60
+ "plot",
61
+ "plotting",
62
+ "chart",
63
+ "svg",
64
+ "2d",
65
+ "3d",
66
+ "surface",
67
+ "contour",
68
+ "heatmap",
69
+ "visualization",
70
+ "headless"
71
+ ]
72
+ }