@danielsimonjr/mathts-plot

中文文档
0.4.31发布于 2026/11/11MITTypeScriptES Module近 30 天下载 3436

Headless SVG 2D/3D plotting for MathTS — dependency-light, expression-aware, built on core/functions/expression

原始使用文档
文档目录10 节
  1. Install
  2. Usage
  3. line — one series, or overlaid with others
  4. overlay — combine several layers on shared, auto-scaled axes
  5. surface — 3D surface via orthographic projection + painter'…
  6. plot — generic, polymorphic entry point (including expressi…
  7. TikZ / LaTeX output
  8. What it provides
  9. ./render — PNG/PDF from SVG (Node only)
  10. License

@danielsimonjr/mathts-plot

Headless SVG 2D/3D plotting for MathTS.

Pure functions returning self-contained SVG strings — no browser, no canvas, no runtime dependencies beyond the MathTS bedrock (core/functions/expression). Every function coerces its input and never throws on bad data: empty, NaN, or Infinity inputs render a "no data" placeholder SVG instead of crashing. See docs/superpowers/specs/2026-07-07-plotting-design.md for the design.

Install

npm install @danielsimonjr/mathts-plot

Usage

Every function below returns a self-contained SVG string (<svg ...>...</svg>) — render it directly in an <img src="data:image/svg+xml,...">, write it to a .svg file, or drop it into HTML.

line — one series, or overlaid with others

import { line } from '@danielsimonjr/mathts-plot';

const svg = line([0, 1, 2, 3], [0, 1, 4, 9], { title: 'squares', xLabel: 'x', yLabel: 'y' });
// svg === '<svg ...>...</svg>'

overlay — combine several layers on shared, auto-scaled axes

import { overlay } from '@danielsimonjr/mathts-plot';

const svg = overlay(
  [
    { type: 'line', x: [0, 1, 2], y: [0, 1, 2], label: 'model' },
    { type: 'scatter', x: [0, 1, 2], y: [0.1, 1.2, 1.8], label: 'data' },
  ],
  { legend: true }
);

surface — 3D surface via orthographic projection + painter's algorithm

import { surface } from '@danielsimonjr/mathts-plot';

const z = [
  [0, 1, 2],
  [1, 2, 3],
  [2, 3, 4],
]; // z[row][col]
const svg = surface(z, { kind: 'filled', azim: 45, elev: 25 });

plot — generic, polymorphic entry point (including expression strings)

import { plot } from '@danielsimonjr/mathts-plot';

// numeric series → line
plot([0, 1, 4, 9]);
plot([0, 1, 2, 3], [0, 1, 4, 9]);

// expression string, sampled via functions/expression → line
plot('sin(x)', { from: 0, to: Math.PI, samples: 200 });

// two free variables → contour (default) or 3D surface
plot('x^2 + y^2', { from: -2, to: 2 });
plot('x^2 + y^2', { from: -2, to: 2, kind: 'surface' });

// array of Layer2D → overlay
plot([
  { type: 'line', x: [0, 1, 2], y: [0, 1, 2] },
  { type: 'scatter', x: [0, 1, 2], y: [0.1, 1.2, 1.8] },
]);

TikZ / LaTeX output

Every function also accepts { format: 'tikz' } (default is 'svg'), and there's a generic toTikZ() mirroring plot(). The geometry matches the SVG output exactly (a deterministic y-flip maps SVG's top-left origin to TikZ's bottom-left); only \usepackage{tikz} is required to compile the result.

import { toTikZ, line, surface } from '@danielsimonjr/mathts-plot';

// generic entry point, expression source → line
const tex = toTikZ('sin(x)', { from: 0, to: Math.PI });

// per-type function via the format option
const tex2 = line([0, 1, 2, 3], [0, 1, 4, 9], { format: 'tikz' });

// standalone: false → a bare tikzpicture for \input, not a compilable document
const z = [
  [0, 1, 2],
  [1, 2, 3],
  [2, 3, 4],
];
const fragment = surface(z, { format: 'tikz', tikz: { standalone: false } });

toTikZ/{ format: 'tikz' } return a LaTeX string, not SVG. With standalone (default true) the string is a complete \documentclass{...} document you can compile as-is; standalone: false returns just the tikzpicture environment, meant to be pulled in via \input{...} from a larger document.

What it provides

Per-type mark functions, each (x, y, opts?) => string unless noted:

  • 2D: line, scatter, bar, area, step, errorbar(x, y, yerr, opts?), quiver(x, y, u, v, opts?).
  • histogram(data, opts?) — bins via functions.histogram, then renders bar heights at bin centers.
  • heatmap(z, opts?) — viridis-colored grid, z as z[row][col].
  • contour(z, opts?) — marching squares over a z[row][col] grid.
  • overlay(layers, opts?) — several Layer2Ds on shared, auto-scaled axes.
  • 3D: surface(z, opts?) (wireframe or painter's-algorithm filled), scatter3d(x, y, z, opts?), curve3d(x, y, z, opts?) — all projected with an orthographic camera (azim/elev).
  • plot(...) — generic polymorphic dispatch over numeric series, layer arrays, and expression-source strings (evaluated via @danielsimonjr/mathts-expression / @danielsimonjr/mathts-functions).
  • viridis(t) — the perceptually-uniform colormap used by heatmap/contour/surface, exposed for reuse.

./render — PNG/PDF from SVG (Node only)

The main . entry returns SVG/TikZ strings only. For file output, import the isolated Node subpath (shells out to rsvg-convert / resvg or pdflatex / tectonic on PATH):

import { renderToFile, latexToPdf } from '@danielsimonjr/mathts-plot/render';

await renderToFile(svgString, 'plot.png', { density: 144 });

See docs/api/plot.md for RenderOptions, PlotRenderError, and hasTool.

Notes:

  • Pure ESM, strict TypeScript, no runtime dependencies beyond the MathTS bedrock (core/functions/expression) on the browser-safe . entry.
  • workbook renders its charts through this package (its former private svg.ts plotter was removed).

License

MIT (c) Daniel Simon Jr.

版本与发布记录

@danielsimonjr/mathts-plot 共发布 82 个版本,首次发布于 2026/07/09;过去 12 个月发布了 82 个版本。

版本发布日期距今
0.4.31latest2026/11/11今天
0.4.302026/11/11今天
0.4.292026/11/11今天
0.4.282026/11/10今天
0.4.272026/11/101 天前
0.4.262026/11/101 天前

关于 @danielsimonjr/mathts-plot 的常见问题

@danielsimonjr/mathts-plot 是什么?

Headless SVG 2D/3D plotting for MathTS — dependency-light, expression-aware, built on core/functions/expression

如何安装 @danielsimonjr/mathts-plot?

在项目目录运行 npm install @danielsimonjr/mathts-plot,或使用 pnpm add / yarn add / bun add。

@danielsimonjr/mathts-plot 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@danielsimonjr/mathts-plot 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@danielsimonjr/mathts-plot 可以商用吗?

@danielsimonjr/mathts-plot 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@danielsimonjr/mathts-plot 的下载量有多少?

根据 npm 官方下载统计,@danielsimonjr/mathts-plot 在 2026/10/12 – 2026/11/10 这 30 天内被下载 3,436 次,日均约 115 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@danielsimonjr/mathts-plot 还在维护吗?

最新版本 0.4.31 发布于 2026/11/11(今天);至今共发布 82 个版本,其中过去 12 个月发布了 82 个;首次发布于 2026/07/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。