|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +Guide for AI assistants working on the function-plot codebase. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +**function-plot** is a TypeScript/JavaScript library for rendering interactive 2D mathematical function plots using D3.js. It supports multiple function types (linear, parametric, implicit, polar, points, vectors) and uses interval arithmetic for pixel-perfect rendering. |
| 8 | + |
| 9 | +- **Version:** 1.22.7 |
| 10 | +- **License:** MIT |
| 11 | +- **Author:** Mauricio Poppe |
| 12 | + |
| 13 | +## Quick Reference Commands |
| 14 | + |
| 15 | +```bash |
| 16 | +# Install dependencies |
| 17 | +npm install |
| 18 | + |
| 19 | +# Run tests |
| 20 | +npm test |
| 21 | + |
| 22 | +# Development server |
| 23 | +npm start |
| 24 | + |
| 25 | +# Full build (webpack bundle + type declarations + site copy) |
| 26 | +npm run build |
| 27 | + |
| 28 | +# Individual build steps |
| 29 | +npm run build:webpack # Webpack UMD bundle → dist/function-plot.js |
| 30 | +npm run build:types # TypeScript declarations → dist/ |
| 31 | + |
| 32 | +# Generate API docs |
| 33 | +npm run docs |
| 34 | + |
| 35 | +# Storybook |
| 36 | +npm run storybook |
| 37 | +npm run build-storybook |
| 38 | +``` |
| 39 | + |
| 40 | +## Project Structure |
| 41 | + |
| 42 | +``` |
| 43 | +src/ |
| 44 | +├── index.ts # Main entry point — Chart class (extends EventEmitter) |
| 45 | +├── types.ts # All TypeScript type definitions |
| 46 | +├── evaluate.ts # Orchestrates sampler selection for function evaluation |
| 47 | +├── datum-defaults.ts # Default configuration for data items |
| 48 | +├── globals.ts # Color palette and default dimensions |
| 49 | +├── utils.ts # linspace, logspace, sgn, color utilities |
| 50 | +├── tip.ts # Mouse hover tooltip system |
| 51 | +├── polyfills.ts # :scope CSS selector polyfill |
| 52 | +├── graph-types/ # Rendering implementations |
| 53 | +│ ├── polyline.ts # Line-based graphs |
| 54 | +│ ├── interval.ts # Interval arithmetic rectangle rendering |
| 55 | +│ └── scatter.ts # Point/dot-based graphs |
| 56 | +├── samplers/ # Function evaluation strategies |
| 57 | +│ ├── builtIn.ts # Standard math evaluation with asymptote detection |
| 58 | +│ └── interval.ts # Interval arithmetic sampling |
| 59 | +├── helpers/ # Feature helpers |
| 60 | +│ ├── derivative.ts # Tangent line computation |
| 61 | +│ ├── secant.ts # Secant line calculation |
| 62 | +│ ├── annotations.ts # Vertical/horizontal reference lines |
| 63 | +│ └── eval.ts # Expression compilation and evaluation |
| 64 | +├── plugins/ # Plugin system |
| 65 | +│ └── zoom-box.ts # Zoom box interaction |
| 66 | +├── stories/ # Storybook documentation and examples |
| 67 | +└── evaluate.test.js # Jest test suite |
| 68 | +``` |
| 69 | + |
| 70 | +## Architecture |
| 71 | + |
| 72 | +- **Chart class** (`src/index.ts`): Central orchestrator. Extends EventEmitter. Manages D3 selections, scales, axes, zoom/pan, and renders data items. |
| 73 | +- **Samplers** (`src/samplers/`): Evaluate mathematical expressions. `builtIn` uses standard JS math; `interval` uses interval arithmetic for guaranteed correctness. |
| 74 | +- **Graph types** (`src/graph-types/`): Render sampled data as SVG elements (polylines, rectangles, scatter dots). |
| 75 | +- **Helpers** (`src/helpers/`): Compute derivative tangent lines, secant lines, and annotations. |
| 76 | +- **Plugins** (`src/plugins/`): Extensible behaviors (zoom-box). |
| 77 | + |
| 78 | +Data flow: `Chart.bindGraphType()` → `evaluate()` (selects sampler) → sampler produces points → graph-type renders SVG. |
| 79 | + |
| 80 | +## Code Conventions |
| 81 | + |
| 82 | +- **Module system:** ES6 import/export in source; compiled to CommonJS (`"module": "commonjs"` in tsconfig). UMD output for browsers. |
| 83 | +- **Formatting (Prettier):** No semicolons, single quotes, 120 char line width, no trailing commas. |
| 84 | +- **Indentation:** 2 spaces. |
| 85 | +- **Linting:** ESLint with StandardX + @typescript-eslint parser. `no-unused-vars` handled by TypeScript plugin. |
| 86 | +- **TypeScript:** Strict (`noImplicitAny: true`), target ES6, declarations emitted to `dist/`. |
| 87 | +- **Private methods:** Prefixed with underscore (e.g., `_bindNewGraphType`). |
| 88 | +- **D3 patterns:** Selection-based API, functional composition, method chaining. |
| 89 | + |
| 90 | +## Build System |
| 91 | + |
| 92 | +- **Webpack 4** bundles `src/index.ts` → `dist/function-plot.js` (UMD, library name `functionPlot`). |
| 93 | +- **ts-loader** handles TypeScript compilation in webpack. |
| 94 | +- **Babel 7** with preset-env and preset-typescript for transpilation. |
| 95 | +- **TypeDoc** generates API documentation to `site/docs/`. |
| 96 | + |
| 97 | +## Testing |
| 98 | + |
| 99 | +- **Framework:** Jest 26 |
| 100 | +- **Config:** `jest.config.js` (rootDir: `src`) |
| 101 | +- **Test files:** `src/evaluate.test.js` |
| 102 | +- **Run:** `npm test` |
| 103 | +- Tests cover the evaluate function with both built-in and interval samplers. |
| 104 | + |
| 105 | +## Key Dependencies |
| 106 | + |
| 107 | +| Dependency | Purpose | |
| 108 | +|---|---| |
| 109 | +| d3-* (axis, scale, selection, shape, zoom, etc.) | Core rendering and interaction | |
| 110 | +| built-in-math-eval | Standard math expression evaluation | |
| 111 | +| interval-arithmetic-eval | Interval arithmetic for pixel-perfect bounds | |
| 112 | +| clamp | Value clamping utility | |
| 113 | +| events | EventEmitter base class | |
| 114 | + |
| 115 | +## CI |
| 116 | + |
| 117 | +- **Travis CI** runs on Node.js 12: `npm install` → `npm test` → `npm run build` → chromatic (Storybook visual testing). |
| 118 | +- GitHub stale bot closes issues after 90 days of inactivity (30-day warning). |
| 119 | + |
| 120 | +## Tips for Contributors |
| 121 | + |
| 122 | +- The main Chart class in `src/index.ts` is the largest file (~760 lines). Most rendering logic lives there. |
| 123 | +- All public types are defined in `src/types.ts` — update these when changing the API surface. |
| 124 | +- The `evaluate()` function in `src/evaluate.ts` selects between samplers based on `datum.sampler` field. |
| 125 | +- Graph types in `src/graph-types/` follow a consistent pattern: receive D3 selection + sampled data, return rendered SVG groups. |
0 commit comments