feat: add force layout transform#529
Draft
RobertFrenken wants to merge 2 commits intosvelteplot:mainfrom
Draft
Conversation
✅ Deploy Preview for svelteplot ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Add d3-force-based layout transform for network/graph visualizations. Two-tier API: forceLayout() for direct use, and forceNode()/forceLink() factory pair following the *Node/*Link convention with shared simulation cache. Configurable forces (charge, center, link, collide, x, y, radial) with number shorthand and false-to-disable support. Includes 8 examples (graph variants, bubble chart), doc page with live examples and full options reference, 16 VR baselines, and 20 unit tests covering both API tiers, edge cases, and cache behavior. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
9635302 to
db6e263
Compare
Contributor
|
I think I'm going to hold off on this transform for the 1.0 milestone for two reasons:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a d3-force-based layout transform for network/graph visualizations.
forceLayout(nodes, links, options)— runs a force simulation synchronously, returns positioned{ nodes, links }for use with Dot and Arrow marksforceNode(links, options)/forceLink(nodes, options)— curried factory pair following the*Node/*Linkconvention (liketreeNode/treeLinkin feat: tree transform #526). Paired factories share aWeakMap-based cache so the simulation runs only once per data array + options referencecharge,center,link,collide,x,y,radial— each accepts an options object, a number shorthand, orfalseto disable.collide,x,y,radialare opt-in (not applied by default)setupcallback for advanced simulation customization, typed asSimulation<SimulationNodeDatum>What's included
src/lib/transforms/force.ts) — 432 lines, full TypeScript types and JSDocsrc/lib/transforms/force.test.ts) — both API tiers, customnodeId, field preservation, invalid link filtering, option shorthands, mutation safety, empty inputs, single-node graph, disconnected graph, cache verificationsrc/routes/examples/force/) — basic graph, hover highlighting, link styling, performance-tuned, degree sizing, tooltip, edge weights, bubble chartsrc/routes/transforms/force/+page.md) — live example,forceNode/forceLinkfactory pattern example, full options and forces reference tablessrc/snapshots/force/) — light + dark for all 8 examplesd3-force ^3.0.0+@types/d3-force ^3.0.10src/lib/transforms/index.tsconfig/sidebar.tsDesign decisions
useForceSimulationhook with per-frame animation is planned as a follow-up (would also benefit from canvas Arrow Canvas rendering for Arrow mark #26 and canvas Link Canvas rendering for Link marks #28)forceNode/forceLink— same convention astreeNode/treeLinkin feat: tree transform #526. Hierarchy transforms produce new record sets rather than modifying existing channel datacollidedefaults to off (likex/y/radial), not on. Graph layouts don't typically need collision detection, and radius=1 has negligible visual effect at typical node sizessrc/shims.d.ts— 47 lines of unused virtual module type declarations;pnpm checkpasses with 0 errors without itRelated issues
*Node/*Linkconvention, will have minor merge conflicts inindex.tsandsidebar.ts(both append at different positions)<Plot canvas>toggle (Option to toggle canvas rendering in entire Plot #32)Test plan
pnpm test— 778 tests pass (92 files)pnpm lint— 0 errorspnpm check— 0 type errorspnpm build— successful static build🤖 Generated with Claude Code