diff --git a/README.md b/README.md index cfede8cb6..9f959abcc 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,7 @@ English | [简体中文](./README.zh-CN.md)

npm version build - 0.1.0 npm entry, minified and gzipped: 91.5 kB - UMD gzip size + UMD gzip size MIT license

@@ -75,7 +74,7 @@ const graph = new Graph({ }, }, edge: { type: 'line', style: { lineWidth: 2 } }, - layout: { type: 'circular', radius: 140 }, + layout: { type: 'force', linkDistance: 120 }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node', 'hover-highlight'], }); @@ -86,7 +85,7 @@ Only package-root exports from `@antv/g6-lite` and `package.json#exports` are pu ## CDN / UMD -Use the UMD build directly in a browser without a bundler. It exposes `window.G6Lite` and includes the runtime dependencies and built-in layouts. See [Optional layouts](https://g6lite.antv.vision/en/docs/guides/layout-overview#optional-layouts) for additional layouts. +Use the UMD build in a browser to access `window.G6Lite`. This example uses the built-in `force` layout in `0.1.0`. See the [layout overview](https://g6lite.antv.vision/en/docs/guides/layout-overview) for layout options by version. ```html
@@ -101,17 +100,17 @@ Use the UMD build directly in a browser without a bundler. It exposes `window.G6 { source: 'gateway', target: 'inventory' }, ], }, - layout: { type: 'circular', radius: 140 }, + layout: { type: 'force', linkDistance: 120 }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node'], }); graph.render().catch(console.error); ``` -- [jsDelivr](https://cdn.jsdelivr.net/npm/@antv/g6-lite@0.1.0/dist/g6-lite.min.js) -- [UNPKG](https://unpkg.com/@antv/g6-lite@0.1.0/dist/g6-lite.min.js) +- [jsDelivr](https://cdn.jsdelivr.net/npm/@antv/g6-lite/dist/g6-lite.min.js) +- [UNPKG](https://unpkg.com/@antv/g6-lite/dist/g6-lite.min.js) -The minzip badge records Bundlephobia's measurement of the 0.1.0 npm entry (91,468 bytes gzip). The UMD badge measures the complete CDN download via UNPKG. +The UMD gzip badge measures the complete UMD file served by UNPKG. ## Learn More diff --git a/README.zh-CN.md b/README.zh-CN.md index c288b8349..4598e630d 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -13,8 +13,7 @@

npm 版本 构建 - 0.1.0 npm 入口压缩后体积:91.5 kB - UMD gzip 体积 + UMD gzip 体积 MIT 许可证

@@ -75,7 +74,7 @@ const graph = new Graph({ }, }, edge: { type: 'line', style: { lineWidth: 2 } }, - layout: { type: 'circular', radius: 140 }, + layout: { type: 'force', linkDistance: 120 }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node', 'hover-highlight'], }); @@ -86,7 +85,7 @@ await graph.render(); ## CDN / UMD -无需构建工具,在浏览器中直接加载 UMD 包即可使用 `window.G6Lite`。主脚本包含运行时依赖和内置布局。可选布局的接入方式见[布局总览](https://g6lite.antv.vision/docs/guides/layout-overview#optional-layouts)。 +无需构建工具,在浏览器中直接加载 UMD 包即可使用 `window.G6Lite`。以下示例使用 `0.1.0` 中内置的 `force`。其他算法的接入方式随 G6Lite 版本变化,见[布局总览](https://g6lite.antv.vision/docs/guides/layout-overview)。 ```html
@@ -101,17 +100,17 @@ await graph.render(); { source: 'gateway', target: 'inventory' }, ], }, - layout: { type: 'circular', radius: 140 }, + layout: { type: 'force', linkDistance: 120 }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node'], }); graph.render().catch(console.error); ``` -- [jsDelivr](https://cdn.jsdelivr.net/npm/@antv/g6-lite@0.1.0/dist/g6-lite.min.js) -- [UNPKG](https://unpkg.com/@antv/g6-lite@0.1.0/dist/g6-lite.min.js) +- [jsDelivr](https://cdn.jsdelivr.net/npm/@antv/g6-lite/dist/g6-lite.min.js) +- [UNPKG](https://unpkg.com/@antv/g6-lite/dist/g6-lite.min.js) -minzip 徽章记录 Bundlephobia 对 0.1.0 版本 npm 入口的测量值(gzip 后 91,468 字节);UMD 徽章通过 UNPKG 测量完整 CDN 文件的下载体积。 +UMD gzip 徽章测量 UNPKG 提供的完整 UMD 文件体积。 ## 继续阅读 diff --git a/package.json b/package.json index 3131e2389..4cd12d2b2 100644 --- a/package.json +++ b/package.json @@ -12,30 +12,6 @@ ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" - }, - "./layouts/mds": { - "types": "./dist/layouts/mds.d.ts", - "import": "./dist/layouts/mds.js" - }, - "./layouts/radial": { - "types": "./dist/layouts/radial.d.ts", - "import": "./dist/layouts/radial.js" - }, - "./layouts/indented": { - "types": "./dist/layouts/indented.d.ts", - "import": "./dist/layouts/indented.js" - }, - "./layouts/mindmap": { - "types": "./dist/layouts/mindmap.d.ts", - "import": "./dist/layouts/mindmap.js" - }, - "./layouts/fishbone": { - "types": "./dist/layouts/fishbone.d.ts", - "import": "./dist/layouts/fishbone.js" - }, - "./layouts/random": { - "types": "./dist/layouts/random.d.ts", - "import": "./dist/layouts/random.js" } }, "files": [ @@ -97,7 +73,7 @@ }, "dependencies": { "@antv/event-emitter": "^0.1.3", - "@antv/layout": "^2.0.0", + "@antv/layout": "^2.1.0", "regl": "^2.1.1" }, "engines": { diff --git a/rollup.config.js b/rollup.config.js index 9d6d11a7d..801b404fd 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -19,13 +19,6 @@ const ts = () => }, }); -const optionalLayouts = Object.fromEntries( - ['mds', 'radial', 'indented', 'mindmap', 'fishbone', 'random'].map((type) => [ - `layouts/${type}`, - `src/layout/optional/${type}.ts`, - ]), -); - export default [ // npm 产物:ESM bundle + 独立 layout worker chunk。 { @@ -34,7 +27,6 @@ export default [ // workerLayout.ts 以 `new URL('./layoutWorker.js', import.meta.url)` 引用 // worker,入口必须保持扁平文件名,落在 dist 根目录。 layoutWorker: 'src/layout/layoutWorker.ts', - ...optionalLayouts, }, external, output: { @@ -46,9 +38,8 @@ export default [ }, plugins: [ts()], }, - // Share declarations between the main entry and the optional layouts. { - input: { index: 'src/index.ts', ...optionalLayouts }, + input: 'src/index.ts', external, output: { dir: 'dist', @@ -71,15 +62,4 @@ export default [ }, plugins: [resolve({ browser: true, preferBuiltins: false }), commonjs(), ts()], }, - { - input: 'src/layout/optional/index.ts', - output: { - file: 'dist/g6-lite-layouts.min.js', - format: 'umd', - name: 'G6LiteLayouts', - sourcemap: true, - plugins: [terser()], - }, - plugins: [resolve({ browser: true, preferBuiltins: false }), commonjs(), ts()], - }, ]; diff --git a/site/content/docs/en/api/graph.mdx b/site/content/docs/en/api/graph.mdx index 3cdcf6f1a..72fb5adbc 100644 --- a/site/content/docs/en/api/graph.mdx +++ b/site/content/docs/en/api/graph.mdx @@ -762,9 +762,9 @@ console.log(client, point); ### setLayout() [#setlayout] -`graph.setLayout(config: LayoutConfig): void` +`graph.setLayout(config: LayoutConfig | LayoutConstructor): void` -Replaces the layout configuration. To rearrange an existing graph with the new configuration, call [layout()](#graph-layout-layout). +Replaces the layout configuration. Pass a layout class directly, as in `graph.setLayout(CircularLayout)`, or set it as `config.type` alongside algorithm options. See [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts) for imports and version requirements. Call [layout()](#graph-layout-layout) to rearrange the graph. ### layout() [#graph-layout-layout] @@ -775,7 +775,9 @@ After the first render, recalculates and updates node positions using the curren ```ts -graph.setLayout({ type: 'circular' }); +import { CircularLayout } from '@antv/layout'; + +graph.setLayout({ type: CircularLayout }); await graph.layout(); graph.fitView(); ``` @@ -824,14 +826,15 @@ declare function runLayout( ```ts import { runLayout } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; -const { nodes } = await runLayout(data, { type: 'dagre', rankdir: 'LR' }); +const { nodes } = await runLayout(data, { type: DagreLayout, rankdir: 'LR' }); console.log(nodes); ``` -Execution order follows the configuration reference: registered names, built-in functional trees, Worker, then library algorithm. With viewport, extension execute context width/height prefer viewport values; explicit config.width/height remain in context.options. Without viewport, context dimensions fall back to config or 800×600. +For a string name, G6Lite checks for a registered extension first. Built-in algorithms can use a G6Lite Worker and fall back to the main-thread algorithm. A layout class manages its own execution. With viewport, extension execute context width/height prefer viewport values; explicit config.width/height remain in context.options. Without viewport, context dimensions fall back to config or 800×600. Standalone runLayout does not perform Graph's final fx/fy override, execute Graph transforms, or emit Graph layout events. Use results in your own renderer; use instance layout for Graph's writeback, state, and event sequence. diff --git a/site/content/docs/en/api/options.mdx b/site/content/docs/en/api/options.mdx index 74b25ee27..08e21b969 100644 --- a/site/content/docs/en/api/options.mdx +++ b/site/content/docs/en/api/options.mdx @@ -20,7 +20,7 @@ import { NodeDocExample } from '@/components/node-doc-example'; | `node` | Sets node shapes, appearance, state styles, labels, and decorations. | [`NodeConfig`](#options-node) | `circle` with theme and built-in styles | | `edge` | Sets edge shapes, line styles, arrowheads, labels, and state styles. | [`EdgeConfig`](#options-edge) | `line` with theme and built-in styles | | `combo` | Sets the appearance, labels, and collapsed shapes of all Combos. Define membership through `data.combos` and each node's `comboId`. | [`ComboConfig`](#options-combo) | Default Combo styles | -| `layout` | Sets the algorithm and parameters used to position nodes. | [`LayoutConfig`](#options-layout) | Existing `x` and `y`, with missing axes set to `0` | +| `layout` | Sets the algorithm and its parameters. Pass a layout class directly to use its defaults. | [`LayoutConfig`](#options-layout) or `LayoutConstructor` | Existing `x` and `y`, with missing axes set to `0` | | `behaviors` | Configures interactions such as zoom, drag, and selection. An explicit array replaces the defaults. Pass `[]` to disable all behaviors. | [`BehaviorOption[]`](#options-behaviors) | `['zoom-canvas', 'drag-canvas']` | | `transforms` | Enables custom transforms or parallel edge separation. | [`TransformOption[]`](#transforms) | — | | `plugins` | Configures registered plugins to mount in the graph container. | [`PluginOption[]`](#plugins) | No plugins | @@ -101,6 +101,7 @@ Use distinct IDs for nodes, edges, and Combos, and keep them stable across updat ```ts import { Graph, type GraphNode } from '@antv/g6-lite'; +import { CircularLayout } from '@antv/layout'; // Without a layout, x and y position each node. const nodes: GraphNode[] = [ @@ -115,7 +116,7 @@ const graph = new Graph({ await graph.render(); // Enable circular layout and pass fx through setData() to fix A's x coordinate. -graph.setLayout({ type: 'circular' }); +graph.setLayout({ type: CircularLayout }); await graph.setData({ nodes: [{ id: 'A', fx: 100 }, { id: 'B' }], edges: [], @@ -550,8 +551,8 @@ See the [Labels and icons guide](/en/docs/guides/label#global-label-policies) fo | Property | Description | Type | Default | | --- | --- | --- | --- | -| `type` | Required. A built-in or registered layout name, such as `'dagre'` or `'grid'`. Unknown names use random placement. Register optional layouts before use. | `string` | — | -| `worker` | Set to `false` to disable. See the [layout guide](/en/docs/guides/layout#worker) for execution order and fallback behavior. | `boolean` | Graph attempts to use a Worker by default at 1,000 nodes or more | +| `type` | Required. A built-in layout name (`'force'`, `'force-atlas2'`, `'d3-force'`, `'fruchterman'`), a registered layout name, or a layout class imported from `@antv/layout` (such as `DagreLayout`). Unknown names use random placement. | `string \| LayoutConstructor` | — | +| `worker` | Controls whether a string-named built-in layout attempts to use a G6Lite Worker. See the [layout guide](/en/docs/guides/layout#worker) for execution order and fallback. | `boolean` | Graph attempts a Worker at 1,000 nodes or more for string-named built-in layouts | | `width` | Width used for layout computation. | `number` | Current canvas width | | `height` | Height used for layout computation. | `number` | Current canvas height | | `center` | Layout center. Each algorithm decides whether to use it. | `[number, number]` | `[width / 2, height / 2]` | @@ -563,6 +564,8 @@ See the [Labels and icons guide](/en/docs/guides/label#global-label-policies) fo The same tree can appear as a mind map, an indented list, or a fishbone diagram. Declare nodes in `nodes` and express each parent-to-child relationship as an edge from `source` to `target`. Use a connected tree with one root. +All three are external layouts. Import `MindmapLayout`, `IndentedLayout`, or `FishboneLayout` from `@antv/layout` and pass the class as `layout.type`; no registration is required. +
| Layout | Preview | When to use it | @@ -583,10 +586,11 @@ Branches extend horizontally from a central topic. This example uses ID prefixes ```ts import type { GraphNode, LayoutConfig } from '@antv/g6-lite'; +import { MindmapLayout } from '@antv/layout'; const leftBranches = new Set(['A', 'C']); const layout: LayoutConfig = { - type: 'mindmap', + type: MindmapLayout, direction: 'H', levelGap: 100, nodeGap: 24, @@ -604,14 +608,16 @@ const layout: LayoutConfig = { | `branchColors` | Writes the branch color to `data._branchColor` for style functions to read. | `string[]` | Built-in palette of 10 colors | | `getWidth` | Gets the node width. | `(node: GraphNode) => number` | Returns `100` | | `getHeight` | Gets the node height. | `(node: GraphNode) => number` | Returns `40` | -| `getSide` | Chooses the side for a branch. The callback's node contains only `id` and `data`. | `(node: GraphNode, index: number, parentSide?: 'left' \| 'right') => 'left' \| 'right'` | Returns `'right'` | +| `getSide` | Chooses the side for a branch. To access application data, look up the original node by `id`. | `(node: GraphNode, index: number, parentSide?: 'left' \| 'right') => 'left' \| 'right'` | Returns `'right'` | #### Indented tree (indented) [#config-layout-indented] Each node occupies a row. Children appear below their parents and indent farther at each level. Use `indent` for horizontal spacing and `rowHeight` for vertical spacing. ```ts -const layout: LayoutConfig = { type: 'indented', indent: 72, rowHeight: 30 }; +import { IndentedLayout } from '@antv/layout'; + +const layout: LayoutConfig = { type: IndentedLayout, indent: 72, rowHeight: 30 }; const edge: EdgeConfig = { type: 'polyline', style: { router: 'indented', endArrow: false } }; ``` @@ -629,7 +635,9 @@ const edge: EdgeConfig = { type: 'polyline', style: { router: 'indented', endArr Use three levels for a problem, categories of causes, and individual causes. `categoryGap` spaces the categories along the spine and `categoryDistance` moves them away from it. ```ts -const layout: LayoutConfig = { type: 'fishbone', categoryGap: 156, categoryDistance: 130, itemHStep: 54 }; +import { FishboneLayout } from '@antv/layout'; + +const layout: LayoutConfig = { type: FishboneLayout, categoryGap: 156, categoryDistance: 130, itemHStep: 54 }; const edge: EdgeConfig = { type: 'polyline', style: { router: 'fishbone', endArrow: false } }; ``` @@ -645,7 +653,7 @@ const edge: EdgeConfig = { type: 'polyline', style: { router: 'fishbone', endArr ### Layout options [#config-library-layout-fields] -Set the layout `type` and its options in `layout`. For example, `layout: { type: 'dagre', rankdir: 'LR' }` arranges a flow from left to right. +Set the layout `type` and its options in `layout`. For example, `layout: { type: DagreLayout, rankdir: 'LR' }` arranges a flow from left to right. | Layout type | Example options | Effect | | --- | --- | --- | diff --git a/site/content/docs/en/compatibility/g6-v5-migration.mdx b/site/content/docs/en/compatibility/g6-v5-migration.mdx index 481972c93..5ffd7a526 100644 --- a/site/content/docs/en/compatibility/g6-v5-migration.mdx +++ b/site/content/docs/en/compatibility/g6-v5-migration.mdx @@ -34,7 +34,7 @@ export async function mountGraph(container: HTMLElement) { edges: [{ id: 'a-b', source: 'a', target: 'b' }], }, node: { style: { label: (node) => node.id } }, - layout: { type: 'circular', radius: 100 }, + layout: { type: 'force', linkDistance: 100 }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node', 'click-select'], }); try { diff --git a/site/content/docs/en/getting-started/quick-start.mdx b/site/content/docs/en/getting-started/quick-start.mdx index 280035db1..644f7bfea 100644 --- a/site/content/docs/en/getting-started/quick-start.mdx +++ b/site/content/docs/en/getting-started/quick-start.mdx @@ -188,7 +188,7 @@ Save the HTML below as `index.html` and open it in your browser. ### Use the UMD script [#cdn-umd] -The UMD script includes runtime dependencies and built-in layouts. Once loaded, create a graph with `G6Lite.Graph`. This minimal example draws two nodes and one edge: +The UMD script includes runtime dependencies and built-in layouts. This `0.1.0` example uses the built-in `force` layout to draw two nodes and one edge with `G6Lite.Graph`: ```html
@@ -200,7 +200,7 @@ The UMD script includes runtime dependencies and built-in layouts. Once loaded, nodes: [{ id: 'Alice' }, { id: 'Bob' }], edges: [{ source: 'Alice', target: 'Bob' }], }, - layout: { type: 'circular', radius: 80 }, + layout: { type: 'force', linkDistance: 80 }, node: { style: { label: (node) => node.id } }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node'], }); @@ -212,7 +212,7 @@ The UMD script includes runtime dependencies and built-in layouts. Once loaded, ``` -ESM integrations support on-demand layout loading through a bundler; the UMD script bundles built-in layouts. Both integrations require [importing and registering optional layouts](/en/docs/guides/layout-overview#optional-layouts) before use. See the [Worker guide](/en/docs/guides/layout-worker) for Worker deployment requirements. +ESM integrations support on-demand layout loading through a bundler. The UMD script bundles built-in layouts. See the [layout overview](/en/docs/guides/layout-overview#builtin-and-external-layouts) for the layout options available in each version and the [Worker guide](/en/docs/guides/layout-worker) for Worker deployment requirements. The standalone HTML examples above destroy their instances on `pagehide`. Recreate them if the browser restores the page from its back/forward cache. diff --git a/site/content/docs/en/getting-started/react.mdx b/site/content/docs/en/getting-started/react.mdx index 7f7fd5bdd..9285e1667 100644 --- a/site/content/docs/en/getting-started/react.mdx +++ b/site/content/docs/en/getting-started/react.mdx @@ -57,7 +57,7 @@ export default function GraphView() { const graph = new Graph({ container: containerRef.current, data: basicData, - layout: { type: 'circular', radius: 120 }, + layout: { type: 'force', linkDistance: 120 }, node: { style: { size: 24, label: (node) => node.id, labelPlacement: 'bottom' } }, edge: { style: { endArrow: true } }, autoFit: fitView, diff --git a/site/content/docs/en/getting-started/vue.mdx b/site/content/docs/en/getting-started/vue.mdx index ee13a9886..d4022feb0 100644 --- a/site/content/docs/en/getting-started/vue.mdx +++ b/site/content/docs/en/getting-started/vue.mdx @@ -56,7 +56,7 @@ onMounted(async () => { const instance = new Graph({ container: containerRef.value, data: basicData, - layout: { type: 'circular', radius: 120 }, + layout: { type: 'force', linkDistance: 120 }, node: { style: { size: 24, label: (node) => node.id, labelPlacement: 'bottom' } }, edge: { style: { endArrow: true } }, autoFit: fitView, diff --git a/site/content/docs/en/graph/overview.mdx b/site/content/docs/en/graph/overview.mdx index 654be5882..2a798a6ff 100644 --- a/site/content/docs/en/graph/overview.mdx +++ b/site/content/docs/en/graph/overview.mdx @@ -60,7 +60,7 @@ After the first render, you can focus a node or update the data. For method timi | `edge` | `EdgeConfig` | Straight edges | Sets edge shapes, arrowheads, and states. | | `combo` | `ComboConfig` | Built-in styles | Sets group appearance, labels, and collapsed shapes. | | `label` | `LabelOptions` | Built-in policy | Controls label scaling and visibility. | -| `layout` | `LayoutConfig` | Input coordinates | Chooses the algorithm and options for positioning nodes. | +| `layout` | `LayoutConfig` or `LayoutConstructor` | Input coordinates | Configures an algorithm and its options, or accepts a layout class directly. | | `behaviors` | `BehaviorOption[]` | Canvas zoom and drag | Configures interactions such as zooming, dragging, and selection. | | `theme` | `ThemeOption` | `'light'` | Sets default styles for the canvas and elements. | | `transforms` | `TransformOption[]` | None | Processes input data in order before layout. | diff --git a/site/content/docs/en/guides/custom-layout.mdx b/site/content/docs/en/guides/custom-layout.mdx index 9a1bff011..a8d452360 100644 --- a/site/content/docs/en/guides/custom-layout.mdx +++ b/site/content/docs/en/guides/custom-layout.mdx @@ -7,6 +7,8 @@ import { NodeDocExample } from '@/components/node-doc-example'; Use a `LayoutExtension` to calculate node positions from graph data. This example arranges nodes in a row with equal spacing. See the [Layout overview](/en/docs/guides/layout-overview) for built-in layouts. +Existing layout classes imported from `@antv/layout` can be passed directly as `layout.type` without registration. Only a custom `LayoutExtension` needs to be registered with `register`. + ## Implement and execute Read current data and runtime.options, retain node IDs/business fields, and return finite x/y. Reading options from runtime also supports standalone execution without init. diff --git a/site/content/docs/en/guides/data.mdx b/site/content/docs/en/guides/data.mdx index 42cf2cacf..abcd334a0 100644 --- a/site/content/docs/en/guides/data.mdx +++ b/site/content/docs/en/guides/data.mdx @@ -191,11 +191,12 @@ If the endpoint returns a complete dataset, render an empty graph first, then lo ```ts import { Graph, type GraphData } from '@antv/g6-lite'; +import { GridLayout } from '@antv/layout'; const graph = new Graph({ container: '#container', data: { nodes: [], edges: [] }, - layout: { type: 'grid' }, + layout: { type: GridLayout }, }); await graph.render(); diff --git a/site/content/docs/en/guides/edge.mdx b/site/content/docs/en/guides/edge.mdx index 10ce72bbd..7b06d323b 100644 --- a/site/content/docs/en/guides/edge.mdx +++ b/site/content/docs/en/guides/edge.mdx @@ -282,7 +282,9 @@ For text-only child nodes, use `right` to extend the line to the end of the text Polylines follow the layout direction. For Dagre, `rankdir: 'LR'` goes left to right and `rankdir: 'TB'` goes top to bottom. Use **Direction** in the [orthogonal connections example](#orthogonal-connections-and-rounding) to compare them. ```ts -graph.setLayout({ type: 'dagre', rankdir: 'TB', nodeSize: [84, 32] }); +import { DagreLayout } from '@antv/layout'; + +graph.setLayout({ type: DagreLayout, rankdir: 'TB', nodeSize: [84, 32] }); await graph.render(); ``` diff --git a/site/content/docs/en/guides/layout-arrangements.mdx b/site/content/docs/en/guides/layout-arrangements.mdx index 630d77181..fe1371de5 100644 --- a/site/content/docs/en/guides/layout-arrangements.mdx +++ b/site/content/docs/en/guides/layout-arrangements.mdx @@ -9,9 +9,17 @@ import { NodeDocExample } from '@/components/node-doc-example'; | Type | Description | Example configuration | | --- | --- | --- | -| [`grid`](/en/docs/guides/layout-grid) | Arrange nodes in two rows and three columns. | `{ type: 'grid', rows: 2, cols: 3 }` | -| [`circular`](/en/docs/guides/layout-circular) | Arrange nodes in a ring with radius 100. | `{ type: 'circular', radius: 100 }` | -| [`dagre`](/en/docs/guides/layout-dagre) | Arrange dependency layers from left to right. | `{ type: 'dagre', rankdir: 'LR' }` | +| [`grid`](/en/docs/guides/layout-grid) | Arrange nodes in two rows and three columns. | `{ type: GridLayout, rows: 2, cols: 3 }` | +| [`circular`](/en/docs/guides/layout-circular) | Arrange nodes in a ring with radius 100. | `{ type: CircularLayout, radius: 100 }` | +| [`dagre`](/en/docs/guides/layout-dagre) | Arrange dependency layers from left to right. | `{ type: DagreLayout, rankdir: 'LR' }` | + +Import the layout classes used by the example configurations: + +```ts +import { GridLayout } from '@antv/layout'; +import { CircularLayout } from '@antv/layout'; +import { DagreLayout } from '@antv/layout'; +``` ## Compare layouts diff --git a/site/content/docs/en/guides/layout-circular.mdx b/site/content/docs/en/guides/layout-circular.mdx index 31cff9cdf..ffbfd7059 100644 --- a/site/content/docs/en/guides/layout-circular.mdx +++ b/site/content/docs/en/guides/layout-circular.mdx @@ -18,10 +18,12 @@ Use a circle for networks without a hierarchy, or a spiral for ordered nodes. Set the layout type with `layout.type`. ```ts +import { CircularLayout } from '@antv/layout'; + const graph = new Graph({ container, data, - layout: { type: 'circular' }, + layout: { type: CircularLayout }, }); ``` @@ -47,7 +49,9 @@ Eight nodes sit on a circle with a radius of 100. Set `startRadius` and `endRadius` to different values and omit `radius`. ```ts -const layout = { type: 'circular', startRadius: 30, endRadius: 140 }; +import { CircularLayout } from '@antv/layout'; + +const layout = { type: CircularLayout, startRadius: 30, endRadius: 140 }; ``` [Compare Grid, Circular, and Dagre](/en/docs/guides/layout-arrangements) diff --git a/site/content/docs/en/guides/layout-concentric.mdx b/site/content/docs/en/guides/layout-concentric.mdx index f2abf22a6..5e6d5b784 100644 --- a/site/content/docs/en/guides/layout-concentric.mdx +++ b/site/content/docs/en/guides/layout-concentric.mdx @@ -18,10 +18,12 @@ Use it to emphasize hubs by their number of connections. Set the layout type with `layout.type`. ```ts +import { ConcentricLayout } from '@antv/layout'; + const graph = new Graph({ container, data, - layout: { type: 'concentric' }, + layout: { type: ConcentricLayout }, }); ``` diff --git a/site/content/docs/en/guides/layout-dagre.mdx b/site/content/docs/en/guides/layout-dagre.mdx index 1d454bd6b..e6864a00d 100644 --- a/site/content/docs/en/guides/layout-dagre.mdx +++ b/site/content/docs/en/guides/layout-dagre.mdx @@ -18,10 +18,12 @@ Use it for workflows, call chains, and dependency graphs. Set the layout type with `layout.type`. ```ts +import { DagreLayout } from '@antv/layout'; + const graph = new Graph({ container, data, - layout: { type: 'dagre' }, + layout: { type: DagreLayout }, }); ``` diff --git a/site/content/docs/en/guides/layout-fishbone.mdx b/site/content/docs/en/guides/layout-fishbone.mdx index db4f52677..23d48cab1 100644 --- a/site/content/docs/en/guides/layout-fishbone.mdx +++ b/site/content/docs/en/guides/layout-fishbone.mdx @@ -15,18 +15,16 @@ Use it for three-level structures such as a problem, cause categories, and speci ## Basic usage -This layout is optional. Import and register it before setting `layout.type`. For CDN usage, see [Optional layouts](/en/docs/guides/layout-overview#optional-layouts). +This is an external layout. Import its class from `@antv/layout` and pass it as `layout.type`; no registration is required. See [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts). ```ts -import { Graph, register } from '@antv/g6-lite'; -import fishbone from '@antv/g6-lite/layouts/fishbone'; - -register('layout', 'fishbone', fishbone); +import { Graph } from '@antv/g6-lite'; +import { FishboneLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'fishbone' }, + layout: { type: FishboneLayout }, edge: { type: 'polyline', style: { router: 'fishbone', endArrow: false } }, }); ``` diff --git a/site/content/docs/en/guides/layout-grid.mdx b/site/content/docs/en/guides/layout-grid.mdx index d59802ed7..e62c6040a 100644 --- a/site/content/docs/en/guides/layout-grid.mdx +++ b/site/content/docs/en/guides/layout-grid.mdx @@ -18,10 +18,12 @@ Use it for device panels or resource lists that need aligned nodes. Set the layout type with `layout.type`. ```ts +import { GridLayout } from '@antv/layout'; + const graph = new Graph({ container, data, - layout: { type: 'grid' }, + layout: { type: GridLayout }, }); ``` diff --git a/site/content/docs/en/guides/layout-indented.mdx b/site/content/docs/en/guides/layout-indented.mdx index cab74c4b5..45ac01a09 100644 --- a/site/content/docs/en/guides/layout-indented.mdx +++ b/site/content/docs/en/guides/layout-indented.mdx @@ -15,18 +15,16 @@ Use it for file directories, navigation menus, and taxonomies. ## Basic usage -This layout is optional. Import and register it before setting `layout.type`. For CDN usage, see [Optional layouts](/en/docs/guides/layout-overview#optional-layouts). +This is an external layout. Import its class from `@antv/layout` and pass it as `layout.type`; no registration is required. See [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts). ```ts -import { Graph, register } from '@antv/g6-lite'; -import indented from '@antv/g6-lite/layouts/indented'; - -register('layout', 'indented', indented); +import { Graph } from '@antv/g6-lite'; +import { IndentedLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'indented' }, + layout: { type: IndentedLayout }, }); ``` diff --git a/site/content/docs/en/guides/layout-mds.mdx b/site/content/docs/en/guides/layout-mds.mdx index d0c15a434..1e9f81f58 100644 --- a/site/content/docs/en/guides/layout-mds.mdx +++ b/site/content/docs/en/guides/layout-mds.mdx @@ -15,18 +15,16 @@ Use it to examine how close or far apart nodes are in a network. ## Basic usage -This layout is optional. Import and register it before setting `layout.type`. For CDN usage, see [Optional layouts](/en/docs/guides/layout-overview#optional-layouts). +This is an external layout. Import its class from `@antv/layout` and pass it as `layout.type`; no registration is required. See [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts). ```ts -import { Graph, register } from '@antv/g6-lite'; -import mds from '@antv/g6-lite/layouts/mds'; - -register('layout', 'mds', mds); +import { Graph } from '@antv/g6-lite'; +import { MDSLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'mds' }, + layout: { type: MDSLayout }, }); ``` diff --git a/site/content/docs/en/guides/layout-mindmap.mdx b/site/content/docs/en/guides/layout-mindmap.mdx index 7d867260b..944e49ce3 100644 --- a/site/content/docs/en/guides/layout-mindmap.mdx +++ b/site/content/docs/en/guides/layout-mindmap.mdx @@ -15,18 +15,16 @@ Use it to organize ideas, knowledge, and categories. ## Basic usage -This layout is optional. Import and register it before setting `layout.type`. For CDN usage, see [Optional layouts](/en/docs/guides/layout-overview#optional-layouts). +This is an external layout. Import its class from `@antv/layout` and pass it as `layout.type`; no registration is required. See [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts). ```ts -import { Graph, register } from '@antv/g6-lite'; -import mindmap from '@antv/g6-lite/layouts/mindmap'; - -register('layout', 'mindmap', mindmap); +import { Graph } from '@antv/g6-lite'; +import { MindmapLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'mindmap' }, + layout: { type: MindmapLayout }, }); ``` diff --git a/site/content/docs/en/guides/layout-networks.mdx b/site/content/docs/en/guides/layout-networks.mdx index b9dc9a3ee..e6c129fb0 100644 --- a/site/content/docs/en/guides/layout-networks.mdx +++ b/site/content/docs/en/guides/layout-networks.mdx @@ -18,7 +18,7 @@ import { NodeDocExample } from '@/components/node-doc-example'; The example starts with a radial layout. Set `focusNode` to `0` to choose its center and `unitRadius` to 68 to space the rings. Use **Layout** to compare all seven layouts on the same network. -Register Radial and MDS before using them. [Optional layouts](/en/docs/guides/layout-overview#optional-layouts) +Import `RadialLayout` and `MDSLayout` from `@antv/layout` and pass the class as `layout.type`. [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts) diff --git a/site/content/docs/en/guides/layout-overview.mdx b/site/content/docs/en/guides/layout-overview.mdx index 4dbbd1df3..8f428a657 100644 --- a/site/content/docs/en/guides/layout-overview.mdx +++ b/site/content/docs/en/guides/layout-overview.mdx @@ -13,47 +13,59 @@ A layout determines where nodes go. A workflow can run left to right, while a fo | Type | Description | Example configuration | | --- | --- | --- | -| [`grid`](/en/docs/guides/layout-grid) | Arrange nodes in rows and columns. | `{ type: 'grid', cols: 3 }` | -| [`circular`](/en/docs/guides/layout-circular) | Arrange nodes along a circle. | `{ type: 'circular', radius: 100 }` | -| [`dagre`](/en/docs/guides/layout-dagre) | Arrange steps or dependencies in layers. | `{ type: 'dagre', rankdir: 'LR', ranksep: 80 }` | | [`force`](/en/docs/guides/layout-force) | Bring linked nodes closer while spacing nodes apart. | `{ type: 'force', linkDistance: 100 }` | | [`d3-force`](/en/docs/guides/layout-d3-force) | Arrange a network with D3 force simulation. | `{ type: 'd3-force', linkDistance: 100 }` | | [`force-atlas2`](/en/docs/guides/layout-force-atlas2) | Use force simulation to reveal network clusters. | `{ type: 'force-atlas2', kr: 12, kg: 0.5 }` | | [`fruchterman`](/en/docs/guides/layout-fruchterman) | Spread nodes using attraction and repulsion. | `{ type: 'fruchterman', gravity: 4 }` | -| [`radial`](/en/docs/guides/layout-radial) | **Optional.** Arrange nodes in rings by graph distance from a focal node. | `{ type: 'radial', focusNode: '0', unitRadius: 68 }` | -| [`concentric`](/en/docs/guides/layout-concentric) | Arrange nodes in concentric rings by degree. | `{ type: 'concentric', sortBy: 'degree' }` | -| [`mds`](/en/docs/guides/layout-mds) | **Optional.** Approximate graph distances in two dimensions. | `{ type: 'mds', linkDistance: 80 }` | -| [`indented`](/en/docs/guides/layout-indented) | **Optional.** Arrange a tree as an indented list. | `{ type: 'indented', indent: 80, rowHeight: 40 }` | -| [`mindmap`](/en/docs/guides/layout-mindmap) | **Optional.** Arrange a tree as mind map branches. | `{ type: 'mindmap', levelGap: 120, nodeGap: 30 }` | -| [`fishbone`](/en/docs/guides/layout-fishbone) | **Optional.** Arrange categories and items along a fishbone spine. | `{ type: 'fishbone', categoryGap: 180 }` | -| [`random`](/en/docs/guides/layout-random) | **Optional.** Scatter nodes randomly within an area. | `{ type: 'random', width: 800, height: 600, center: [400, 300] }` | +| [`grid`](/en/docs/guides/layout-grid) | **External.** Arrange nodes in rows and columns. | `{ type: GridLayout, cols: 3 }` | +| [`circular`](/en/docs/guides/layout-circular) | **External.** Arrange nodes along a circle. | `{ type: CircularLayout, radius: 100 }` | +| [`dagre`](/en/docs/guides/layout-dagre) | **External.** Arrange steps or dependencies in layers. | `{ type: DagreLayout, rankdir: 'LR', ranksep: 80 }` | +| [`concentric`](/en/docs/guides/layout-concentric) | **External.** Arrange nodes in concentric rings by degree. | `{ type: ConcentricLayout, sortBy: 'degree' }` | +| [`radial`](/en/docs/guides/layout-radial) | **External.** Arrange nodes in rings by graph distance from a focal node. | `{ type: RadialLayout, focusNode: '0', unitRadius: 68 }` | +| [`mds`](/en/docs/guides/layout-mds) | **External.** Approximate graph distances in two dimensions. | `{ type: MDSLayout, linkDistance: 80 }` | +| [`indented`](/en/docs/guides/layout-indented) | **External.** Arrange a tree as an indented list. | `{ type: IndentedLayout, indent: 80, rowHeight: 40 }` | +| [`mindmap`](/en/docs/guides/layout-mindmap) | **External.** Arrange a tree as mind map branches. | `{ type: MindmapLayout, levelGap: 120, nodeGap: 30 }` | +| [`fishbone`](/en/docs/guides/layout-fishbone) | **External.** Arrange categories and items along a fishbone spine. | `{ type: FishboneLayout, categoryGap: 180 }` | +| [`random`](/en/docs/guides/layout-random) | **External.** Scatter nodes randomly within an area. | `{ type: RandomLayout, width: 800, height: 600, center: [400, 300] }` | Pass these configuration objects to `layout`. See [Common layout options](/en/docs/guides/layout) for more parameters and previews. -## Optional layouts [#optional-layouts] +## Built-in and external layouts [#builtin-and-external-layouts] -Indented, Mindmap, Fishbone, Radial, MDS, and Random require registration. With npm, import the layout from its own subpath, register it, and set `layout.type`. +The external layout class examples on this page require a G6Lite build that includes the layout refactor. The published `0.1.0` uses built-in string names such as `type: 'dagre'`. Optional layouts such as mind maps are imported from `@antv/g6-lite/layouts/*` and registered before use. -```ts -import { register } from '@antv/g6-lite'; -import radial from '@antv/g6-lite/layouts/radial'; +G6Lite bundles only four force-directed layouts for large-scale graph analysis: `force`, `force-atlas2`, `d3-force`, and `fruchterman`. Use their string names directly. -register('layout', 'radial', radial); +With a G6Lite build that accepts layout classes, install `@antv/layout` directly in your app. Import the layout class and pass it as `layout.type`. -const layout = { type: 'radial', focusNode: '0', unitRadius: 68 }; +```bash +npm install @antv/layout@^2.1.0 ``` -When using UMD from a CDN, load the same version of `dist/g6-lite-layouts.min.js` after the main script. The layout extensions are available on `G6LiteLayouts`. +```ts +import { Graph } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; -```html - - - +const graph = new Graph({ + container: '#container', + data, + layout: { type: DagreLayout, rankdir: 'LR', ranksep: 80 }, +}); ``` -Registered layouts run on the main thread. +You can also pass the class directly without an options object: `layout: DagreLayout`. + +External layout classes manage their own execution. `layout.worker` controls the G6Lite Worker for string-named built-in layouts. + +## Other layouts [#optional-layouts] + +Indented, Mindmap, Fishbone, Radial, MDS, and Random are also external layouts. Import their classes directly from `@antv/layout`; no registration is required. This example uses Radial: + +```ts +import { RadialLayout } from '@antv/layout'; + +const layout = { type: RadialLayout, focusNode: '0', unitRadius: 68 }; +``` ## Configuration and example @@ -61,6 +73,7 @@ Set the algorithm with `layout.type`. This example uses Dagre to arrange three s ```ts import { Graph } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; const graph = new Graph({ container: '#container', @@ -71,7 +84,7 @@ const graph = new Graph({ { source: 'Review', target: 'Publish' }, ], }, - layout: { type: 'dagre', rankdir: 'LR', ranksep: 80 }, + layout: { type: DagreLayout, rankdir: 'LR', ranksep: 80 }, node: { style: { label: (node) => node.id } }, }); diff --git a/site/content/docs/en/guides/layout-radial.mdx b/site/content/docs/en/guides/layout-radial.mdx index f45f8c34e..fa4e22922 100644 --- a/site/content/docs/en/guides/layout-radial.mdx +++ b/site/content/docs/en/guides/layout-radial.mdx @@ -15,18 +15,16 @@ Use it to see the direct and indirect relationships of an account, service, or d ## Basic usage -This layout is optional. Import and register it before setting `layout.type`. For CDN usage, see [Optional layouts](/en/docs/guides/layout-overview#optional-layouts). +This is an external layout. Import its class from `@antv/layout` and pass it as `layout.type`; no registration is required. See [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts). ```ts -import { Graph, register } from '@antv/g6-lite'; -import radial from '@antv/g6-lite/layouts/radial'; - -register('layout', 'radial', radial); +import { Graph } from '@antv/g6-lite'; +import { RadialLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'radial' }, + layout: { type: RadialLayout }, }); ``` diff --git a/site/content/docs/en/guides/layout-random.mdx b/site/content/docs/en/guides/layout-random.mdx index 081cb1f8e..b085d2235 100644 --- a/site/content/docs/en/guides/layout-random.mdx +++ b/site/content/docs/en/guides/layout-random.mdx @@ -15,18 +15,16 @@ Use it for a quick preview of graph data. Each run samples new positions. ## Basic usage -This layout is optional. Import and register it before setting `layout.type`. For CDN usage, see [Optional layouts](/en/docs/guides/layout-overview#optional-layouts). +This is an external layout. Import its class from `@antv/layout` and pass it as `layout.type`; no registration is required. See [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts). ```ts -import { Graph, register } from '@antv/g6-lite'; -import random from '@antv/g6-lite/layouts/random'; - -register('layout', 'random', random); +import { Graph } from '@antv/g6-lite'; +import { RandomLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'random' }, + layout: { type: RandomLayout }, }); ``` diff --git a/site/content/docs/en/guides/layout-trees.mdx b/site/content/docs/en/guides/layout-trees.mdx index dc5039b1d..5d2cf5adf 100644 --- a/site/content/docs/en/guides/layout-trees.mdx +++ b/site/content/docs/en/guides/layout-trees.mdx @@ -19,7 +19,7 @@ Express parent-child relationships as edges with the parent as `source` and the Use **Tree layout** to compare the three arrangements. The example also changes the edge routing. Indented trees use elbow connectors, while the fishbone layout uses angled branches. -Register each tree layout before using it. [Optional layouts](/en/docs/guides/layout-overview#optional-layouts) +Import each tree layout class from `@antv/layout` and pass it as `layout.type`; no registration is required. See [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts). diff --git a/site/content/docs/en/guides/layout-worker.mdx b/site/content/docs/en/guides/layout-worker.mdx index 412b407e4..27afa7d32 100644 --- a/site/content/docs/en/guides/layout-worker.mdx +++ b/site/content/docs/en/guides/layout-worker.mdx @@ -15,11 +15,11 @@ layout: { type: 'force-atlas2', worker: true, maxIteration: 100 }, | layout.worker | Graph behavior | | --- | --- | -| Omitted | Attempts a Worker at ≥ 1000 nodes; main thread below that threshold | -| true | Attempts a Worker for built-in library layouts; registered extensions, including all six optional layouts, execute on the main thread | -| false | Explicitly disables Workers, including for large graphs | +| Omitted | Attempts a G6Lite Worker at 1,000 nodes or more for string-named built-in layouts | +| true | Attempts a G6Lite Worker for string-named built-in layouts | +| false | Disables the G6Lite Worker at any graph size | -Standalone runLayout has no node-count-based automatic switch. Worker failures fall back to the same main-thread algorithm. See the [layout reference](/en/docs/guides/layout#worker) for execution order and iteration defaults. +A registered extension with a matching name takes precedence. Registered extensions and imported layout classes manage their own execution. Standalone `runLayout` uses a G6Lite Worker when explicitly requested. Worker failures fall back to the same main-thread algorithm. See the [layout reference](/en/docs/guides/layout#worker) for execution order and iteration defaults. ## Code and rendered result @@ -39,7 +39,7 @@ Creation, asset loading, postMessage cloning, or computation failures can trigge ## Cancellation and cleanup -After rendering, graph.cancelLayout() can terminate that instance's active Worker and discard results. It neither rolls back completed layouts nor affects another Graph's Worker. To continue, setLayout again and await layout. +After rendering, `graph.cancelLayout()` can terminate the instance's G6Lite Worker and discard the pending layout result. Custom layouts manage their own background tasks and resources. To continue, call `setLayout()` and await `layout()`. The example's generation check also protects application-side completion messages and fitView from stale calls. Call graph.destroy() on unmount, and clean up your own controls, timers, and subscriptions. diff --git a/site/content/docs/en/guides/layout.mdx b/site/content/docs/en/guides/layout.mdx index 4fa1fb608..a33a821a3 100644 --- a/site/content/docs/en/guides/layout.mdx +++ b/site/content/docs/en/guides/layout.mdx @@ -11,7 +11,7 @@ Configure the layout in `new Graph()` through `layout`. See the [Layout overview | Field | Purpose | | --- | --- | -| `type` | Algorithm name, such as `dagre` | +| `type` | Algorithm name for built-in layouts (such as `force`) or a layout class imported from `@antv/layout` (such as `DagreLayout`) | | `width`, `height` | Computation bounds, defaulting to canvas dimensions | | `center` | Layout center for algorithms that use it | | `worker` | Whether to try computing in a Worker | @@ -24,23 +24,23 @@ Direction and spacing depend on the algorithm. See the [layout reference](/en/do The thumbnails show 11 layouts on the same data. Try them in the [layout gallery](/en/docs/examples/layout-gallery) or see the [AntV Layout documentation](https://layout.antv.vision/) for more options. -Layouts marked **Optional** must be imported and registered. See [Optional layouts](/en/docs/guides/layout-overview#optional-layouts). +Layouts marked **External** must be imported from `@antv/layout` and passed as classes through `layout.type`; no registration is required. See [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts).
| type | Preview | Purpose | Common algorithm options | | --- | --- | --- | --- | -| [`circular`](/en/docs/guides/layout-circular) | ![Nodes are evenly spaced on a circle](/layouts/reference/circular.svg) | Circle or spiral | radius, startRadius, endRadius, startAngle, endAngle, clockwise | -| [`concentric`](/en/docs/guides/layout-concentric) | ![Nodes are arranged in rings by degree](/layouts/reference/concentric.svg) | Rings by importance | sortBy, maxLevelDiff, nodeSize, nodeSpacing, preventOverlap | -| [`grid`](/en/docs/guides/layout-grid) | ![Nodes align in rows and columns](/layouts/reference/grid.svg) | Rows and columns | rows, cols, begin, sortBy, nodeSize, nodeSpacing, preventOverlap | -| [`random`](/en/docs/guides/layout-random) | ![Nodes are scattered across the canvas](/layouts/reference/random.svg) | **Optional.** Random positions | width, height, center | -| [`dagre`](/en/docs/guides/layout-dagre) | ![The directed graph is layered from left to right](/layouts/reference/dagre.svg) | Directed layers | rankdir: TB/BT/LR/RL, nodesep, ranksep, edgesep, nodeSize, ranker | +| [`circular`](/en/docs/guides/layout-circular) | ![Nodes are evenly spaced on a circle](/layouts/reference/circular.svg) | **External.** Circle or spiral | radius, startRadius, endRadius, startAngle, endAngle, clockwise | +| [`concentric`](/en/docs/guides/layout-concentric) | ![Nodes are arranged in rings by degree](/layouts/reference/concentric.svg) | **External.** Rings by importance | sortBy, maxLevelDiff, nodeSize, nodeSpacing, preventOverlap | +| [`grid`](/en/docs/guides/layout-grid) | ![Nodes align in rows and columns](/layouts/reference/grid.svg) | **External.** Rows and columns | rows, cols, begin, sortBy, nodeSize, nodeSpacing, preventOverlap | +| [`random`](/en/docs/guides/layout-random) | ![Nodes are scattered across the canvas](/layouts/reference/random.svg) | **External.** Random positions | width, height, center | +| [`dagre`](/en/docs/guides/layout-dagre) | ![The directed graph is layered from left to right](/layouts/reference/dagre.svg) | **External.** Directed layers | rankdir: TB/BT/LR/RL, nodesep, ranksep, edgesep, nodeSize, ranker | | [`force`](/en/docs/guides/layout-force) | ![Connected nodes are drawn together by the force simulation](/layouts/reference/force.svg) | Force-directed network | linkDistance, nodeStrength, edgeStrength, maxIteration, minMovement | | [`d3-force`](/en/docs/guides/layout-d3-force) | ![Node positions from the D3 force simulation](/layouts/reference/d3-force.svg) | D3 simulation | linkDistance, nodeStrength, alphaDecay, alphaMin; use nodeStrength, not charge | | [`force-atlas2`](/en/docs/guides/layout-force-atlas2) | ![Node positions from ForceAtlas2](/layouts/reference/force-atlas2.svg) | ForceAtlas2 network | kr, kg, barnesHut, prune, maxIteration | | [`fruchterman`](/en/docs/guides/layout-fruchterman) | ![Node positions from Fruchterman](/layouts/reference/fruchterman.svg) | Fruchterman network | gravity, speed, maxIteration, minMovement | -| [`radial`](/en/docs/guides/layout-radial) | ![Nodes form rings by graph distance from the focus](/layouts/reference/radial.svg) | **Optional.** Rings around a focus | focusNode, unitRadius, linkDistance, preventOverlap | -| [`mds`](/en/docs/guides/layout-mds) | ![Graph distances determine node positions in two dimensions](/layouts/reference/mds.svg) | **Optional.** Positions from graph distances | linkDistance, center | +| [`radial`](/en/docs/guides/layout-radial) | ![Nodes form rings by graph distance from the focus](/layouts/reference/radial.svg) | **External.** Rings around a focus | focusNode, unitRadius, linkDistance, preventOverlap | +| [`mds`](/en/docs/guides/layout-mds) | ![Graph distances determine node positions in two dimensions](/layouts/reference/mds.svg) | **External.** Positions from graph distances | linkDistance, center |
@@ -48,7 +48,7 @@ Rendered node sizes are not automatically passed to algorithms. For overlap avoi ### Computation defaults [#computation-defaults] -G6Lite injects or rewrites the following options; explicit values win. These rules apply to library layouts, not the three functional tree layouts. +G6Lite injects or rewrites the following options; explicit values win. These rules apply to the library layouts above, including tree layout classes imported from `@antv/layout`. | Algorithm / option | Default or rule | | --- | --- | @@ -74,7 +74,7 @@ Use **Layout** to compare Grid, Circular, and Dagre on the same data. The code s Set parent-child relationships in `edges`, with the parent as `source` and the child as `target`. Compare mind map, indented, and fishbone layouts in the [tree layout example](/en/docs/guides/layout-trees#configuration-and-example). -### Functional tree layouts [#functional-tree-layouts] +### Tree layouts [#functional-tree-layouts] Use a connected tree with one root and no cycles. @@ -82,9 +82,9 @@ Use a connected tree with one root and no cycles. | type | Preview | Purpose | | --- | --- | --- | -| [`mindmap`](/en/docs/guides/layout-mindmap) | ![Branches fan out on both sides of the root](/layouts/reference/mindmap.svg) | **Optional.** Organize a hierarchy into branches | -| [`indented`](/en/docs/guides/layout-indented) | ![Nodes are listed in rows and indented by depth](/layouts/reference/indented.svg) | **Optional.** Show a tree as a list | -| [`fishbone`](/en/docs/guides/layout-fishbone) | ![Categories alternate along a horizontal spine, with items branching off the ribs](/layouts/reference/fishbone.svg) | **Optional.** Group categories and items along a spine | +| [`mindmap`](/en/docs/guides/layout-mindmap) | ![Branches fan out on both sides of the root](/layouts/reference/mindmap.svg) | **External.** Organize a hierarchy into branches | +| [`indented`](/en/docs/guides/layout-indented) | ![Nodes are listed in rows and indented by depth](/layouts/reference/indented.svg) | **External.** Show a tree as a list | +| [`fishbone`](/en/docs/guides/layout-fishbone) | ![Categories alternate along a horizontal spine, with items branching off the ribs](/layouts/reference/fishbone.svg) | **External.** Group categories and items along a spine |
@@ -98,7 +98,7 @@ Use a connected tree with one root and no cycles. | `branchColors` | `string[]` | Built-in 10 colors; writes data._branchColor, which needs an explicit style function to affect colors | | `getWidth` | `(node: GraphNode) => number` | Returns 100 | | `getHeight` | `(node: GraphNode) => number` | Returns 40 | -| `getSide` | `(node: GraphNode, index: number, parentSide?: 'left' \| 'right') => 'left' \| 'right'` | Returns right; node contains only id and data | +| `getSide` | `(node: GraphNode, index: number, parentSide?: 'left' \| 'right') => 'left' \| 'right'` | Defaults to right. To access application data, look up the original node by ID. | The first parentless input node becomes root; otherwise the first node does. Root position is (0, 0). Use levelGap/nodeGap; legacy hGap/vGap names are not supported. @@ -136,7 +136,9 @@ For root → category → item structures. Deeper nodes do not receive a complet For a graph that has completed render: setLayout replaces the entire configuration. Fit the graph after layout only when needed: ```ts -graph.setLayout({ type: 'dagre', rankdir: 'LR', nodesep: 32, ranksep: 64 }); +import { DagreLayout } from '@antv/layout'; + +graph.setLayout({ type: DagreLayout, rankdir: 'LR', nodesep: 32, ranksep: 64 }); await graph.layout(); graph.fitView({ padding: 48, maxZoom: 1 }); ``` @@ -149,7 +151,7 @@ Layout may change existing x/y. Graph's fx/fy override the corresponding final a ## Worker -Graph supplies worker: true when node count ≥ 1000 and worker is omitted, without filtering by algorithm name. Standalone runLayout has no automatic switch. Execution order is registered extension → built-in tree → Worker → main-thread library. Worker creation, cloning, or execution failures fall back to the same main-thread algorithm, which may block again. +Graph attempts a G6Lite Worker for string-named built-in layouts when `worker` is omitted and the graph has 1,000 nodes or more. Registered extensions take precedence, and they manage their own execution, as do imported layout classes. Standalone `runLayout` requires an explicit Worker request. Worker creation, cloning, or execution failures fall back to the same main-thread algorithm. Prefer explicit worker: false for function-valued options. See [layout methods](/en/docs/api/graph#layout) for cancellation. Successful configuration does not prove a Worker actually ran. diff --git a/site/content/docs/en/metrics/bundle-size.mdx b/site/content/docs/en/metrics/bundle-size.mdx index 25b95bf35..b7f01b9a5 100644 --- a/site/content/docs/en/metrics/bundle-size.mdx +++ b/site/content/docs/en/metrics/bundle-size.mdx @@ -5,7 +5,7 @@ description: See the consolidated performance results This content has moved to [Performance comparison](/en/docs/metrics/performance#bundle-size). -- [build measurement](/en/docs/metrics/performance#bundle-size) -- [esm and layout chunks](/en/docs/metrics/performance#bundle-size) -- [umd cdn](/en/docs/metrics/performance#bundle-size) -- [reproduce](/en/docs/metrics/performance#benchmark-method) +- [UMD build measurements](/en/docs/metrics/performance#umd-size) +- [Loading layouts with ESM](/en/docs/guides/layout-overview#builtin-and-external-layouts) +- [UMD and CDN file sizes](/en/docs/metrics/performance#umd-size) +- [Reproduce UMD size measurements](/en/docs/metrics/performance#umd-measurement) diff --git a/site/content/docs/en/metrics/performance.mdx b/site/content/docs/en/metrics/performance.mdx index dc0ae95bb..dce63e33e 100644 --- a/site/content/docs/en/metrics/performance.mdx +++ b/site/content/docs/en/metrics/performance.mdx @@ -3,9 +3,9 @@ title: Performance comparison description: First-draw timing and browser bundle sizes for G6Lite, G6, Sigma.js, and ECharts on the same graph data --- -This comparison covers G6Lite, G6, Sigma.js, and the ECharts Graph series. All renderers receive the same precomputed positions, and all bundles use the same build settings. The 2026-11-13 comparison used a local G6Lite development build whose version field was `0.1.0`, rather than the package later published to npm. +This page records first-draw timings and UMD file sizes for G6Lite, G6, Sigma.js, and the ECharts Graph series. Each renderer receives the same precomputed positions in the first-draw test. The G6Lite timing from November 13, 2026 uses a local development build whose version field was `0.1.0`. -The original record did not retain a source commit or package integrity identifier for that development build, so these results apply only to that build. First-draw performance of the published package needs a separate measurement. The [CDN file sizes](#umd-size) below have been measured again using the published npm package. +The original record did not retain a source commit or package integrity identifier for that development build, so these results apply only to that build. First-draw performance of the published package needs a separate measurement. See [UMD file sizes](#umd-size) for measurements of the layout-refactor build and published CDN files. @@ -38,27 +38,31 @@ The table shows the median time in milliseconds from graph creation through two These results apply only to the environment above and do not certify minimum supported browser versions. See [Minimum browser versions](/en/docs/compatibility/browser#minimum-browser-versions) for capability version references and the remaining acceptance scope. -## Browser bundle size [#bundle-size] + -esbuild 0.21.5 bundles each npm entry and its dependencies into a single minified browser script. The G6Lite measurement uses the default entry, with optional layouts imported separately as needed. ECharts imports GraphChart and CanvasRenderer only. Sigma.js includes Graphology. Sizes are in KiB and exclude source maps. +## UMD file sizes [#umd-size] -| npm package | Minified (KiB) | gzip -9 (KiB) | -| --- | ---: | ---: | -| `@antv/g6-lite` development build (`0.1.0`) | 511.3 | 159.5 | -| `@antv/g6@5.1.1` | 1,435.3 | 413.4 | -| `sigma@4.0.4` + `graphology@0.26.0` | 438.4 | 110.7 | -| `echarts@6.1.0` | 386.3 | 130.2 | +The homepage shows the [source build for the layout refactor in PR #75](https://github.com/antvis/G6Lite/commit/34bf8b6d8bdac3ee97f339e2f06314c279d7c865). On November 30, 2026, `npm run size` measured `dist/g6-lite.min.js` at **118,503 bytes (118.5 kB)** with the default Node.js gzip settings. The minified file before compression was 408.1 kB. This build includes four built-in force-directed layouts and the runtime dependencies. Import other layouts as needed from `@antv/layout`. [Built-in and external layouts](/en/docs/guides/layout-overview#builtin-and-external-layouts) -### G6Lite CDN file sizes [#umd-size] +The table below measures published CDN files with `gzip -9`. The G6Lite measurement uses the `@antv/g6-lite@0.1.0` npm package. The other libraries use their UMD files from unpkg. One kB is 1,000 bytes. -These files come from the published npm package `@antv/g6-lite@0.1.0`, downloaded again and measured with `gzip -9` on 2026-11-24. One kB is 1,000 bytes. The homepage uses the default script's measurement from the same run. These are the distributed UMD files, measured separately from the esbuild bundles above. +| Library | UMD bundle | Minified (kB) | gzip -9 (kB) | +| --- | --- | ---: | ---: | +| `@antv/g6-lite@0.1.0` | `g6-lite.min.js` | 504.2 | 150.3 | +| `@antv/g6@5.1.1` | `g6.min.js` | 1,383.3 | 390.6 | +| `echarts@6.1.0` | `echarts.min.js` | 1,121.9 | 368.2 | +| `sigma@4.0.4` + `graphology@0.26.0` | `sigma.min.js` + `graphology.umd.min.js` | 466.7 | 117.6 | + +### Reproduce UMD size measurements [#umd-measurement] + +Build G6Lite from the source commit you want to measure, then run the size check. -| Published file | Contents | gzip -9 (kB) | -| --- | --- | ---: | -| `g6-lite.min.js` | Default script with rendering, interactions, and built-in layouts | 150.3 | -| `g6-lite-layouts.min.js` | Six optional layouts, loaded when needed | 29.1 | +```bash +npm run build +npm run size +``` -Load the optional script before registering the layout you want to use. [Optional layouts](/en/docs/guides/layout-overview#optional-layouts) +For published artifacts, download the UMD file for the version in the table and run `gzip -9 -c | wc -c` to count bytes. ## Measuring your own graph [#performance-tuning] diff --git a/site/content/docs/zh/api/graph.mdx b/site/content/docs/zh/api/graph.mdx index b65e524c6..df16c218d 100644 --- a/site/content/docs/zh/api/graph.mdx +++ b/site/content/docs/zh/api/graph.mdx @@ -762,9 +762,9 @@ console.log(client, point); ### setLayout() [#setlayout] -`graph.setLayout(config: LayoutConfig): void` +`graph.setLayout(config: LayoutConfig | LayoutConstructor): void` -替换布局配置。要让已有的图按新配置重排,再调用 [layout()](#graph-layout-layout)。 +替换布局配置。可直接传入布局类,例如 `graph.setLayout(CircularLayout)`,也可通过 `config.type` 指定布局类并设置参数。导入和版本要求见[内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts)。要按新配置重排,再调用 [layout()](#graph-layout-layout)。 ### layout() [#graph-layout-layout] @@ -775,7 +775,9 @@ console.log(client, point); ```ts -graph.setLayout({ type: 'circular' }); +import { CircularLayout } from '@antv/layout'; + +graph.setLayout({ type: CircularLayout }); await graph.layout(); graph.fitView(); ``` @@ -824,14 +826,15 @@ declare function runLayout( ```ts import { runLayout } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; -const { nodes } = await runLayout(data, { type: 'dagre', rankdir: 'LR' }); +const { nodes } = await runLayout(data, { type: DagreLayout, rankdir: 'LR' }); console.log(nodes); ``` -执行顺序同布局配置参考:已注册同名扩展、内置函数式树布局、Worker、库算法。传 viewport 时,扩展 execute 上下文的 width/height 优先使用 viewport;显式 config.width/height 仍在 context.options 中。无 viewport 时,上下文尺寸回退到 config 或 800×600。 +字符串布局先查找已注册的同名扩展,再对内置算法尝试 G6Lite Worker,最后执行主线程算法。直接传入的布局类由自身实现负责计算。传 viewport 时,扩展 execute 上下文的 width/height 优先使用 viewport;显式 config.width/height 仍在 context.options 中。无 viewport 时,上下文尺寸回退到 config 或 800×600。 独立 runLayout 不执行 Graph 的最终 fx/fy 覆盖,不运行 Graph transforms,也不发送 Graph 布局事件。返回后将坐标用于自己的渲染器;需要 Graph 的数据写回、状态和事件链时,使用实例 layout。 diff --git a/site/content/docs/zh/api/options.mdx b/site/content/docs/zh/api/options.mdx index 494857659..d0c01491e 100644 --- a/site/content/docs/zh/api/options.mdx +++ b/site/content/docs/zh/api/options.mdx @@ -20,7 +20,7 @@ import { NodeDocExample } from '@/components/node-doc-example'; | `node` | 设置节点形状、外观、状态样式、标签和装饰。 | [`NodeConfig`](#options-node) | `circle`,使用主题和内置样式 | | `edge` | 设置边的形状、线条、箭头、标签和状态样式。 | [`EdgeConfig`](#options-edge) | `line`,使用主题和内置样式 | | `combo` | 设置所有 Combo 的外观、标签和折叠形态。分组关系由 `data.combos` 和节点的 `comboId` 声明。 | [`ComboConfig`](#options-combo) | 使用默认分组样式 | -| `layout` | 指定布局算法及其参数,用于计算节点位置。 | [`LayoutConfig`](#options-layout) | 使用已有 `x`、`y`,缺省轴为 `0` | +| `layout` | 指定布局算法及其参数。可直接传入布局类以使用其默认参数。 | [`LayoutConfig`](#options-layout) 或 `LayoutConstructor` | 使用已有 `x`、`y`,缺省轴为 `0` | | `behaviors` | 配置缩放、拖拽、选择等交互。显式数组替换默认行为,`[]` 禁用全部行为。 | [`BehaviorOption[]`](#options-behaviors) | `['zoom-canvas', 'drag-canvas']` | | `transforms` | 启用自定义数据处理或平行边分离。 | [`TransformOption[]`](#transforms) | — | | `plugins` | 配置要在图容器中挂载的已注册插件。 | [`PluginOption[]`](#plugins) | 无插件 | @@ -101,6 +101,7 @@ import { NodeDocExample } from '@/components/node-doc-example'; ```ts import { Graph, type GraphNode } from '@antv/g6-lite'; +import { CircularLayout } from '@antv/layout'; // 省略 layout 时,节点按 x、y 定位。 const nodes: GraphNode[] = [ @@ -115,7 +116,7 @@ const graph = new Graph({ await graph.render(); // 启用圆形布局,通过 setData() 传入 fx 固定 A 的横坐标。 -graph.setLayout({ type: 'circular' }); +graph.setLayout({ type: CircularLayout }); await graph.setData({ nodes: [{ id: 'A', fx: 100 }, { id: 'B' }], edges: [], @@ -550,8 +551,8 @@ graph.updateNodeData('A', { score: 80 }); | 属性 | 描述 | 类型 | 默认值 | | --- | --- | --- | --- | -| `type` | 必填。内置布局名称或已注册的布局名,例如 `'dagre'`、`'grid'`。未知名称采用随机坐标。可选布局需先注册。 | `string` | — | -| `worker` | 设为 `false` 可关闭。执行顺序与回退见[布局指南](/docs/guides/layout#worker)。 | `boolean` | Graph 中节点数 ≥ 1000 时默认尝试启用 | +| `type` | 必填。内置布局名称(`'force'`、`'force-atlas2'`、`'d3-force'`、`'fruchterman'`)、已注册的布局名,或直接从 `@antv/layout` 导入的布局类(如 `DagreLayout`)。未知名称采用随机坐标。 | `string \| LayoutConstructor` | — | +| `worker` | 控制字符串形式的内置布局是否尝试使用 G6Lite Worker。执行顺序与回退见[布局指南](/docs/guides/layout#worker)。 | `boolean` | Graph 中节点数 ≥ 1000 且为字符串内置布局时默认尝试启用 | | `width` | 布局计算使用的宽度。 | `number` | 当前画布宽度 | | `height` | 布局计算使用的高度。 | `number` | 当前画布高度 | | `center` | 布局中心,是否使用由算法决定。 | `[number, number]` | `[width / 2, height / 2]` | @@ -563,6 +564,8 @@ graph.updateNodeData('A', { score: 80 }); 同一棵树可以排成思维导图、目录列表或鱼骨图。数据用 `nodes` 声明节点,用 `edges` 的 `source → target` 表示父子关系。输入一棵有且只有一个根节点的连通树。 +这三种布局均为外部布局,从 `@antv/layout` 导入 `MindmapLayout`、`IndentedLayout` 或 `FishboneLayout` 后作为 `layout.type` 传入,无需注册。 +
| 布局 | 排列效果 | 适用场景 | @@ -583,10 +586,11 @@ graph.updateNodeData('A', { score: 80 }); ```ts import type { GraphNode, LayoutConfig } from '@antv/g6-lite'; +import { MindmapLayout } from '@antv/layout'; const leftBranches = new Set(['A', 'C']); const layout: LayoutConfig = { - type: 'mindmap', + type: MindmapLayout, direction: 'H', levelGap: 100, nodeGap: 24, @@ -604,14 +608,16 @@ const layout: LayoutConfig = { | `branchColors` | 将分支颜色写入 `data._branchColor`,可在样式函数中读取。 | `string[]` | 内置 10 色 | | `getWidth` | 获取节点宽度。 | `(node: GraphNode) => number` | 返回 `100` | | `getHeight` | 获取节点高度。 | `(node: GraphNode) => number` | 返回 `40` | -| `getSide` | 决定分支放在哪一侧。回调中的节点只包含 `id` 和 `data`。 | `(node: GraphNode, index: number, parentSide?: 'left' \| 'right') => 'left' \| 'right'` | 返回 `'right'` | +| `getSide` | 决定分支所在侧。需要原始业务数据时,用节点 `id` 查找输入数据。 | `(node: GraphNode, index: number, parentSide?: 'left' \| 'right') => 'left' \| 'right'` | 返回 `'right'` | #### 缩进树 (indented) [#config-layout-indented] 每个节点占一行,子节点排在父节点下方,层级越深缩进越多。调整 `indent` 控制缩进,调整 `rowHeight` 控制行距。 ```ts -const layout: LayoutConfig = { type: 'indented', indent: 72, rowHeight: 30 }; +import { IndentedLayout } from '@antv/layout'; + +const layout: LayoutConfig = { type: IndentedLayout, indent: 72, rowHeight: 30 }; const edge: EdgeConfig = { type: 'polyline', style: { router: 'indented', endArrow: false } }; ``` @@ -629,7 +635,9 @@ const edge: EdgeConfig = { type: 'polyline', style: { router: 'indented', endArr 用三层节点表示「问题 → 原因分类 → 具体原因」。`categoryGap` 拉开主干上的分类,`categoryDistance` 调整分类离主干的距离。 ```ts -const layout: LayoutConfig = { type: 'fishbone', categoryGap: 156, categoryDistance: 130, itemHStep: 54 }; +import { FishboneLayout } from '@antv/layout'; + +const layout: LayoutConfig = { type: FishboneLayout, categoryGap: 156, categoryDistance: 130, itemHStep: 54 }; const edge: EdgeConfig = { type: 'polyline', style: { router: 'fishbone', endArrow: false } }; ``` @@ -645,7 +653,7 @@ const edge: EdgeConfig = { type: 'polyline', style: { router: 'fishbone', endArr ### 布局参数 [#config-library-layout-fields] -在 `layout` 中设置布局类型 `type`,再加上表里的参数。例如,`layout: { type: 'dagre', rankdir: 'LR' }` 会让流程从左向右排列。 +在 `layout` 中设置布局类型 `type`,再加上表里的参数。例如,`layout: { type: DagreLayout, rankdir: 'LR' }` 会让流程从左向右排列。 | 布局类型 | 参数示例 | 效果 | | --- | --- | --- | diff --git a/site/content/docs/zh/compatibility/g6-v5-migration.mdx b/site/content/docs/zh/compatibility/g6-v5-migration.mdx index d13d8d91c..701bb319b 100644 --- a/site/content/docs/zh/compatibility/g6-v5-migration.mdx +++ b/site/content/docs/zh/compatibility/g6-v5-migration.mdx @@ -32,7 +32,7 @@ export async function mountGraph(container: HTMLElement) { edges: [{ id: 'a-b', source: 'a', target: 'b' }], }, node: { style: { label: (node) => node.id } }, - layout: { type: 'circular', radius: 100 }, + layout: { type: 'force', linkDistance: 100 }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node', 'click-select'], }); try { diff --git a/site/content/docs/zh/getting-started/quick-start.mdx b/site/content/docs/zh/getting-started/quick-start.mdx index f5ede7c7b..ad41d122d 100644 --- a/site/content/docs/zh/getting-started/quick-start.mdx +++ b/site/content/docs/zh/getting-started/quick-start.mdx @@ -188,7 +188,7 @@ import { Graph } from 'https://esm.sh/@antv/g6-lite@0.1.0?bundle'; ### 使用 UMD 脚本 [#cdn-umd] -UMD 主脚本包含运行依赖和内置布局,加载后通过 `G6Lite.Graph` 创建图。下面的最小示例绘制两个节点和一条边: +UMD 主脚本包含运行依赖和内置布局,加载后通过 `G6Lite.Graph` 创建图。下面的 `0.1.0` 示例用内置的 `force` 绘制两个节点和一条边: ```html
@@ -200,7 +200,7 @@ UMD 主脚本包含运行依赖和内置布局,加载后通过 `G6Lite.Graph` nodes: [{ id: 'Alice' }, { id: 'Bob' }], edges: [{ source: 'Alice', target: 'Bob' }], }, - layout: { type: 'circular', radius: 80 }, + layout: { type: 'force', linkDistance: 80 }, node: { style: { label: (node) => node.id } }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node'], }); @@ -212,7 +212,7 @@ UMD 主脚本包含运行依赖和内置布局,加载后通过 `G6Lite.Graph` ``` -ESM 接入支持通过构建工具按需加载布局;UMD 主脚本已内联内置布局。两种接入方式使用可选布局时都需要[先导入并注册](/docs/guides/layout-overview#optional-layouts)。布局的 Worker 部署要求见 [Worker 指南](/docs/guides/layout-worker)。 +ESM 接入支持通过构建工具按需加载布局,UMD 主脚本已内联内置布局。其他算法的接入方式随 G6Lite 版本变化,请查阅[布局总览](/docs/guides/layout-overview#builtin-and-external-layouts)。Worker 的部署要求见 [Worker 指南](/docs/guides/layout-worker)。 上面的独立 HTML 示例在 `pagehide` 时销毁图实例。浏览器从往返缓存恢复页面时,需要重新创建实例。 diff --git a/site/content/docs/zh/getting-started/react.mdx b/site/content/docs/zh/getting-started/react.mdx index c272dfae9..034ec4ad4 100644 --- a/site/content/docs/zh/getting-started/react.mdx +++ b/site/content/docs/zh/getting-started/react.mdx @@ -57,7 +57,7 @@ export default function GraphView() { const graph = new Graph({ container: containerRef.current, data: basicData, - layout: { type: 'circular', radius: 120 }, + layout: { type: 'force', linkDistance: 120 }, node: { style: { size: 24, label: (node) => node.id, labelPlacement: 'bottom' } }, edge: { style: { endArrow: true } }, autoFit: fitView, diff --git a/site/content/docs/zh/getting-started/vue.mdx b/site/content/docs/zh/getting-started/vue.mdx index 6214cf4d7..0583a4e53 100644 --- a/site/content/docs/zh/getting-started/vue.mdx +++ b/site/content/docs/zh/getting-started/vue.mdx @@ -56,7 +56,7 @@ onMounted(async () => { const instance = new Graph({ container: containerRef.value, data: basicData, - layout: { type: 'circular', radius: 120 }, + layout: { type: 'force', linkDistance: 120 }, node: { style: { size: 24, label: (node) => node.id, labelPlacement: 'bottom' } }, edge: { style: { endArrow: true } }, autoFit: fitView, diff --git a/site/content/docs/zh/graph/overview.mdx b/site/content/docs/zh/graph/overview.mdx index b7e75fcc5..11ff03349 100644 --- a/site/content/docs/zh/graph/overview.mdx +++ b/site/content/docs/zh/graph/overview.mdx @@ -60,7 +60,7 @@ await graph.render(); | `edge` | `EdgeConfig` | 直线边 | 配置线型、箭头和状态。 | | `combo` | `ComboConfig` | 内置样式 | 配置分组外观、标签和折叠形态。 | | `label` | `LabelOptions` | 内置策略 | 控制标签的缩放和可见性。 | -| `layout` | `LayoutConfig` | 使用数据坐标 | 指定布局算法及参数,由布局计算节点位置。 | +| `layout` | `LayoutConfig` 或 `LayoutConstructor` | 使用数据坐标 | 配置布局算法及参数,也可直接传入布局类。 | | `behaviors` | `BehaviorOption[]` | 缩放、拖动画布 | 配置缩放、拖拽、选择等交互。 | | `theme` | `ThemeOption` | `'light'` | 设置画布和元素的默认样式。 | | `transforms` | `TransformOption[]` | 无 | 按顺序处理布局前的输入数据。 | diff --git a/site/content/docs/zh/guides/custom-layout.mdx b/site/content/docs/zh/guides/custom-layout.mdx index 58f3f4fa8..7c804ce97 100644 --- a/site/content/docs/zh/guides/custom-layout.mdx +++ b/site/content/docs/zh/guides/custom-layout.mdx @@ -7,6 +7,8 @@ import { NodeDocExample } from '@/components/node-doc-example'; 需要按业务规则安排节点时,可以注册 `LayoutExtension`。它接收图数据,返回节点坐标。本例把节点等距排成一行。内置算法见[布局总览](/docs/guides/layout-overview)。 +从 `@antv/layout` 导入的现有布局类可直接作为 `layout.type` 传入,无需注册;只有自定义 `LayoutExtension` 才需要调用 `register`。 + ## 实现与执行 `execute()` 接收本次图数据和运行参数。节点的 ID 和业务数据原样返回,位置用计算出的 `x`、`y`。示例从 `options.gap` 读取间距。 diff --git a/site/content/docs/zh/guides/data.mdx b/site/content/docs/zh/guides/data.mdx index 61d8d587d..e2a960468 100644 --- a/site/content/docs/zh/guides/data.mdx +++ b/site/content/docs/zh/guides/data.mdx @@ -191,11 +191,12 @@ graph.collapseCombo('storage'); ```ts import { Graph, type GraphData } from '@antv/g6-lite'; +import { GridLayout } from '@antv/layout'; const graph = new Graph({ container: '#container', data: { nodes: [], edges: [] }, - layout: { type: 'grid' }, + layout: { type: GridLayout }, }); await graph.render(); diff --git a/site/content/docs/zh/guides/edge.mdx b/site/content/docs/zh/guides/edge.mdx index f87c6e73a..a1187125e 100644 --- a/site/content/docs/zh/guides/edge.mdx +++ b/site/content/docs/zh/guides/edge.mdx @@ -284,7 +284,9 @@ edge: { 折线随布局方向走。Dagre 布局用 `rankdir`,`LR` 表示从左到右,`TB` 表示从上到下。切换[正交连接与圆角示例](#正交连接与圆角)里的 **Direction** 可以对比两种效果。 ```ts -graph.setLayout({ type: 'dagre', rankdir: 'TB', nodeSize: [84, 32] }); +import { DagreLayout } from '@antv/layout'; + +graph.setLayout({ type: DagreLayout, rankdir: 'TB', nodeSize: [84, 32] }); await graph.render(); ``` diff --git a/site/content/docs/zh/guides/layout-arrangements.mdx b/site/content/docs/zh/guides/layout-arrangements.mdx index fd06b296f..eab687f80 100644 --- a/site/content/docs/zh/guides/layout-arrangements.mdx +++ b/site/content/docs/zh/guides/layout-arrangements.mdx @@ -9,9 +9,17 @@ import { NodeDocExample } from '@/components/node-doc-example'; | 类型 | 描述 | 配置示例 | | --- | --- | --- | -| [`grid`](/docs/guides/layout-grid) | 将节点排成两行三列。 | `{ type: 'grid', rows: 2, cols: 3 }` | -| [`circular`](/docs/guides/layout-circular) | 沿半径为 100 的圆周排列。 | `{ type: 'circular', radius: 100 }` | -| [`dagre`](/docs/guides/layout-dagre) | 按依赖关系从左到右分层。 | `{ type: 'dagre', rankdir: 'LR' }` | +| [`grid`](/docs/guides/layout-grid) | 将节点排成两行三列。 | `{ type: GridLayout, rows: 2, cols: 3 }` | +| [`circular`](/docs/guides/layout-circular) | 沿半径为 100 的圆周排列。 | `{ type: CircularLayout, radius: 100 }` | +| [`dagre`](/docs/guides/layout-dagre) | 按依赖关系从左到右分层。 | `{ type: DagreLayout, rankdir: 'LR' }` | + +上述配置示例需要导入对应的布局类: + +```ts +import { GridLayout } from '@antv/layout'; +import { CircularLayout } from '@antv/layout'; +import { DagreLayout } from '@antv/layout'; +``` ## 布局对比 diff --git a/site/content/docs/zh/guides/layout-circular.mdx b/site/content/docs/zh/guides/layout-circular.mdx index bce3e2e78..618a93b1a 100644 --- a/site/content/docs/zh/guides/layout-circular.mdx +++ b/site/content/docs/zh/guides/layout-circular.mdx @@ -18,10 +18,12 @@ import { NodeDocExample } from '@/components/node-doc-example'; 布局类型通过 `layout.type` 指定。 ```ts +import { CircularLayout } from '@antv/layout'; + const graph = new Graph({ container, data, - layout: { type: 'circular' }, + layout: { type: CircularLayout }, }); ``` @@ -47,7 +49,9 @@ const graph = new Graph({ 起止半径分别通过 `startRadius` 和 `endRadius` 设置,`radius` 省略。 ```ts -const layout = { type: 'circular', startRadius: 30, endRadius: 140 }; +import { CircularLayout } from '@antv/layout'; + +const layout = { type: CircularLayout, startRadius: 30, endRadius: 140 }; ``` [对比网格、环形与层级布局](/docs/guides/layout-arrangements) diff --git a/site/content/docs/zh/guides/layout-concentric.mdx b/site/content/docs/zh/guides/layout-concentric.mdx index 9bef21caf..f49336869 100644 --- a/site/content/docs/zh/guides/layout-concentric.mdx +++ b/site/content/docs/zh/guides/layout-concentric.mdx @@ -18,10 +18,12 @@ import { NodeDocExample } from '@/components/node-doc-example'; 布局类型通过 `layout.type` 指定。 ```ts +import { ConcentricLayout } from '@antv/layout'; + const graph = new Graph({ container, data, - layout: { type: 'concentric' }, + layout: { type: ConcentricLayout }, }); ``` diff --git a/site/content/docs/zh/guides/layout-dagre.mdx b/site/content/docs/zh/guides/layout-dagre.mdx index 603a75646..834aba4b3 100644 --- a/site/content/docs/zh/guides/layout-dagre.mdx +++ b/site/content/docs/zh/guides/layout-dagre.mdx @@ -18,10 +18,12 @@ import { NodeDocExample } from '@/components/node-doc-example'; 布局类型通过 `layout.type` 指定。 ```ts +import { DagreLayout } from '@antv/layout'; + const graph = new Graph({ container, data, - layout: { type: 'dagre' }, + layout: { type: DagreLayout }, }); ``` diff --git a/site/content/docs/zh/guides/layout-fishbone.mdx b/site/content/docs/zh/guides/layout-fishbone.mdx index c9a04666e..938ee0bcf 100644 --- a/site/content/docs/zh/guides/layout-fishbone.mdx +++ b/site/content/docs/zh/guides/layout-fishbone.mdx @@ -15,18 +15,16 @@ import { NodeDocExample } from '@/components/node-doc-example'; ## 基本用法 -这是可选布局。先导入并注册,再设置 `layout.type`。CDN 接入见[可选布局](/docs/guides/layout-overview#optional-layouts)。 +这是外部布局。从 `@antv/layout` 导入布局类并作为 `layout.type` 传入,无需注册。详见[内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts)。 ```ts -import { Graph, register } from '@antv/g6-lite'; -import fishbone from '@antv/g6-lite/layouts/fishbone'; - -register('layout', 'fishbone', fishbone); +import { Graph } from '@antv/g6-lite'; +import { FishboneLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'fishbone' }, + layout: { type: FishboneLayout }, edge: { type: 'polyline', style: { router: 'fishbone', endArrow: false } }, }); ``` diff --git a/site/content/docs/zh/guides/layout-grid.mdx b/site/content/docs/zh/guides/layout-grid.mdx index a171ff11e..969e973d9 100644 --- a/site/content/docs/zh/guides/layout-grid.mdx +++ b/site/content/docs/zh/guides/layout-grid.mdx @@ -18,10 +18,12 @@ import { NodeDocExample } from '@/components/node-doc-example'; 布局类型通过 `layout.type` 指定。 ```ts +import { GridLayout } from '@antv/layout'; + const graph = new Graph({ container, data, - layout: { type: 'grid' }, + layout: { type: GridLayout }, }); ``` diff --git a/site/content/docs/zh/guides/layout-indented.mdx b/site/content/docs/zh/guides/layout-indented.mdx index c53822f4b..6c3912376 100644 --- a/site/content/docs/zh/guides/layout-indented.mdx +++ b/site/content/docs/zh/guides/layout-indented.mdx @@ -15,18 +15,16 @@ import { NodeDocExample } from '@/components/node-doc-example'; ## 基本用法 -这是可选布局。先导入并注册,再设置 `layout.type`。CDN 接入见[可选布局](/docs/guides/layout-overview#optional-layouts)。 +这是外部布局。从 `@antv/layout` 导入布局类并作为 `layout.type` 传入,无需注册。详见[内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts)。 ```ts -import { Graph, register } from '@antv/g6-lite'; -import indented from '@antv/g6-lite/layouts/indented'; - -register('layout', 'indented', indented); +import { Graph } from '@antv/g6-lite'; +import { IndentedLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'indented' }, + layout: { type: IndentedLayout }, }); ``` diff --git a/site/content/docs/zh/guides/layout-mds.mdx b/site/content/docs/zh/guides/layout-mds.mdx index 757457e36..5af1333be 100644 --- a/site/content/docs/zh/guides/layout-mds.mdx +++ b/site/content/docs/zh/guides/layout-mds.mdx @@ -15,18 +15,16 @@ import { NodeDocExample } from '@/components/node-doc-example'; ## 基本用法 -这是可选布局。先导入并注册,再设置 `layout.type`。CDN 接入见[可选布局](/docs/guides/layout-overview#optional-layouts)。 +这是外部布局。从 `@antv/layout` 导入布局类并作为 `layout.type` 传入,无需注册。详见[内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts)。 ```ts -import { Graph, register } from '@antv/g6-lite'; -import mds from '@antv/g6-lite/layouts/mds'; - -register('layout', 'mds', mds); +import { Graph } from '@antv/g6-lite'; +import { MDSLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'mds' }, + layout: { type: MDSLayout }, }); ``` diff --git a/site/content/docs/zh/guides/layout-mindmap.mdx b/site/content/docs/zh/guides/layout-mindmap.mdx index aab223dde..f78d42e3b 100644 --- a/site/content/docs/zh/guides/layout-mindmap.mdx +++ b/site/content/docs/zh/guides/layout-mindmap.mdx @@ -15,18 +15,16 @@ import { NodeDocExample } from '@/components/node-doc-example'; ## 基本用法 -这是可选布局。先导入并注册,再设置 `layout.type`。CDN 接入见[可选布局](/docs/guides/layout-overview#optional-layouts)。 +这是外部布局。从 `@antv/layout` 导入布局类并作为 `layout.type` 传入,无需注册。详见[内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts)。 ```ts -import { Graph, register } from '@antv/g6-lite'; -import mindmap from '@antv/g6-lite/layouts/mindmap'; - -register('layout', 'mindmap', mindmap); +import { Graph } from '@antv/g6-lite'; +import { MindmapLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'mindmap' }, + layout: { type: MindmapLayout }, }); ``` diff --git a/site/content/docs/zh/guides/layout-networks.mdx b/site/content/docs/zh/guides/layout-networks.mdx index 64a45f3ac..1a26ee6e8 100644 --- a/site/content/docs/zh/guides/layout-networks.mdx +++ b/site/content/docs/zh/guides/layout-networks.mdx @@ -18,7 +18,7 @@ import { NodeDocExample } from '@/components/node-doc-example'; 示例默认使用放射布局。中心节点通过 `focusNode` 设为 `0`,圈间距通过 `unitRadius` 设为 68。用 **Layout** 切换布局,对比同一张关系图的排布。 -放射布局和 MDS 需要先注册。 [可选布局](/docs/guides/layout-overview#optional-layouts) +从 `@antv/layout` 导入 `RadialLayout` 和 `MDSLayout` 后,可以直接作为 `layout.type` 传入。 [内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts) diff --git a/site/content/docs/zh/guides/layout-overview.mdx b/site/content/docs/zh/guides/layout-overview.mdx index 2237b3c64..d1625dccd 100644 --- a/site/content/docs/zh/guides/layout-overview.mdx +++ b/site/content/docs/zh/guides/layout-overview.mdx @@ -13,47 +13,59 @@ import { NodeDocExample } from '@/components/node-doc-example'; | 类型 | 描述 | 配置示例 | | --- | --- | --- | -| [`grid`](/docs/guides/layout-grid) | 排成整齐的行列。 | `{ type: 'grid', cols: 3 }` | -| [`circular`](/docs/guides/layout-circular) | 沿圆周排列。 | `{ type: 'circular', radius: 100 }` | -| [`dagre`](/docs/guides/layout-dagre) | 按流程或依赖关系分层。 | `{ type: 'dagre', rankdir: 'LR', ranksep: 80 }` | | [`force`](/docs/guides/layout-force) | 让相连节点靠近,同时拉开节点间距。 | `{ type: 'force', linkDistance: 100 }` | | [`d3-force`](/docs/guides/layout-d3-force) | 用 D3 力模拟排列关系网络。 | `{ type: 'd3-force', linkDistance: 100 }` | | [`force-atlas2`](/docs/guides/layout-force-atlas2) | 用力模拟呈现网络中的群组。 | `{ type: 'force-atlas2', kr: 12, kg: 0.5 }` | | [`fruchterman`](/docs/guides/layout-fruchterman) | 用引力和斥力分布节点。 | `{ type: 'fruchterman', gravity: 4 }` | -| [`radial`](/docs/guides/layout-radial) | **可选。** 围绕指定节点,按图上的距离分圈。 | `{ type: 'radial', focusNode: '0', unitRadius: 68 }` | -| [`concentric`](/docs/guides/layout-concentric) | 按节点度数排成同心圆。 | `{ type: 'concentric', sortBy: 'degree' }` | -| [`mds`](/docs/guides/layout-mds) | **可选。** 用平面上的距离表现节点在图中的远近。 | `{ type: 'mds', linkDistance: 80 }` | -| [`indented`](/docs/guides/layout-indented) | **可选。** 把树排成逐层缩进的目录。 | `{ type: 'indented', indent: 80, rowHeight: 40 }` | -| [`mindmap`](/docs/guides/layout-mindmap) | **可选。** 把树排成脑图分支。 | `{ type: 'mindmap', levelGap: 120, nodeGap: 30 }` | -| [`fishbone`](/docs/guides/layout-fishbone) | **可选。** 沿鱼骨主干展示分类和条目。 | `{ type: 'fishbone', categoryGap: 180 }` | -| [`random`](/docs/guides/layout-random) | **可选。** 在指定范围内随机放置节点。 | `{ type: 'random', width: 800, height: 600, center: [400, 300] }` | +| [`grid`](/docs/guides/layout-grid) | **外部。** 排成整齐的行列。 | `{ type: GridLayout, cols: 3 }` | +| [`circular`](/docs/guides/layout-circular) | **外部。** 沿圆周排列。 | `{ type: CircularLayout, radius: 100 }` | +| [`dagre`](/docs/guides/layout-dagre) | **外部。** 按流程或依赖关系分层。 | `{ type: DagreLayout, rankdir: 'LR', ranksep: 80 }` | +| [`concentric`](/docs/guides/layout-concentric) | **外部。** 按节点度数排成同心圆。 | `{ type: ConcentricLayout, sortBy: 'degree' }` | +| [`radial`](/docs/guides/layout-radial) | **外部。** 围绕指定节点,按图上的距离分圈。 | `{ type: RadialLayout, focusNode: '0', unitRadius: 68 }` | +| [`mds`](/docs/guides/layout-mds) | **外部。** 用平面上的距离表现节点在图中的远近。 | `{ type: MDSLayout, linkDistance: 80 }` | +| [`indented`](/docs/guides/layout-indented) | **外部。** 把树排成逐层缩进的目录。 | `{ type: IndentedLayout, indent: 80, rowHeight: 40 }` | +| [`mindmap`](/docs/guides/layout-mindmap) | **外部。** 把树排成脑图分支。 | `{ type: MindmapLayout, levelGap: 120, nodeGap: 30 }` | +| [`fishbone`](/docs/guides/layout-fishbone) | **外部。** 沿鱼骨主干展示分类和条目。 | `{ type: FishboneLayout, categoryGap: 180 }` | +| [`random`](/docs/guides/layout-random) | **外部。** 在指定范围内随机放置节点。 | `{ type: RandomLayout, width: 800, height: 600, center: [400, 300] }` | 表中的配置对象写在 `layout` 中。更多参数和效果见[布局通用配置项](/docs/guides/layout)。 -## 可选布局 [#optional-layouts] +## 内置与外部布局 [#builtin-and-external-layouts] -缩进树、思维导图、鱼骨图、放射布局、MDS 和随机布局需要先注册。npm 项目可以从各自的子路径导入,之后照常通过 `layout.type` 指定。 +本页的外部布局类示例需要包含布局重构的 G6Lite 构建。已发布的 `0.1.0` 仍使用 `type: 'dagre'` 等内置字符串名称,思维导图等可选布局从 `@antv/g6-lite/layouts/*` 导入后注册。 -```ts -import { register } from '@antv/g6-lite'; -import radial from '@antv/g6-lite/layouts/radial'; +G6Lite 只内置面向大规模图分析的四种力导向布局:`force`、`force-atlas2`、`d3-force`、`fruchterman`,直接用字符串指定即可。 -register('layout', 'radial', radial); +使用支持布局类的 G6Lite 构建时,在应用中安装 `@antv/layout`,再导入布局类并作为 `layout.type` 传入。 -const layout = { type: 'radial', focusNode: '0', unitRadius: 68 }; +```bash +npm install @antv/layout@^2.1.0 ``` -通过 CDN 使用 UMD 时,在主脚本之后加载同版本的 `dist/g6-lite-layouts.min.js`,再从 `G6LiteLayouts` 取出要注册的布局。 +```ts +import { Graph } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; -```html - - - +const graph = new Graph({ + container: '#container', + data, + layout: { type: DagreLayout, rankdir: 'LR', ranksep: 80 }, +}); ``` -注册的布局在主线程运行。 +也可以省略配置对象,直接传布局类:`layout: DagreLayout`。 + +外部布局类由各自的实现负责计算。`layout.worker` 调度的是 G6Lite 的字符串内置布局。 + +## 其他布局 [#optional-layouts] + +缩进树、思维导图、鱼骨图、放射布局、MDS 和随机布局同样属于外部布局,直接从 `@antv/layout` 导入对应布局类即可,无需注册。以放射布局为例: + +```ts +import { RadialLayout } from '@antv/layout'; + +const layout = { type: RadialLayout, focusNode: '0', unitRadius: 68 }; +``` ## 配置与示例 @@ -61,6 +73,7 @@ const layout = { type: 'radial', focusNode: '0', unitRadius: 68 }; ```ts import { Graph } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; const graph = new Graph({ container: '#container', @@ -71,7 +84,7 @@ const graph = new Graph({ { source: '审核', target: '发布' }, ], }, - layout: { type: 'dagre', rankdir: 'LR', ranksep: 80 }, + layout: { type: DagreLayout, rankdir: 'LR', ranksep: 80 }, node: { style: { label: (node) => node.id } }, }); diff --git a/site/content/docs/zh/guides/layout-radial.mdx b/site/content/docs/zh/guides/layout-radial.mdx index 9477dbd22..775c77fbc 100644 --- a/site/content/docs/zh/guides/layout-radial.mdx +++ b/site/content/docs/zh/guides/layout-radial.mdx @@ -15,18 +15,16 @@ import { NodeDocExample } from '@/components/node-doc-example'; ## 基本用法 -这是可选布局。先导入并注册,再设置 `layout.type`。CDN 接入见[可选布局](/docs/guides/layout-overview#optional-layouts)。 +这是外部布局。从 `@antv/layout` 导入布局类并作为 `layout.type` 传入,无需注册。详见[内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts)。 ```ts -import { Graph, register } from '@antv/g6-lite'; -import radial from '@antv/g6-lite/layouts/radial'; - -register('layout', 'radial', radial); +import { Graph } from '@antv/g6-lite'; +import { RadialLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'radial' }, + layout: { type: RadialLayout }, }); ``` diff --git a/site/content/docs/zh/guides/layout-random.mdx b/site/content/docs/zh/guides/layout-random.mdx index c44a69f6a..22a8ec2ab 100644 --- a/site/content/docs/zh/guides/layout-random.mdx +++ b/site/content/docs/zh/guides/layout-random.mdx @@ -15,18 +15,16 @@ import { NodeDocExample } from '@/components/node-doc-example'; ## 基本用法 -这是可选布局。先导入并注册,再设置 `layout.type`。CDN 接入见[可选布局](/docs/guides/layout-overview#optional-layouts)。 +这是外部布局。从 `@antv/layout` 导入布局类并作为 `layout.type` 传入,无需注册。详见[内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts)。 ```ts -import { Graph, register } from '@antv/g6-lite'; -import random from '@antv/g6-lite/layouts/random'; - -register('layout', 'random', random); +import { Graph } from '@antv/g6-lite'; +import { RandomLayout } from '@antv/layout'; const graph = new Graph({ container, data, - layout: { type: 'random' }, + layout: { type: RandomLayout }, }); ``` diff --git a/site/content/docs/zh/guides/layout-trees.mdx b/site/content/docs/zh/guides/layout-trees.mdx index 99a61d3f4..a43428592 100644 --- a/site/content/docs/zh/guides/layout-trees.mdx +++ b/site/content/docs/zh/guides/layout-trees.mdx @@ -19,7 +19,7 @@ import { NodeDocExample } from '@/components/node-doc-example'; 用 **Tree layout** 切换三种布局。示例同时调整边的画法,缩进树用折线连接,鱼骨图用斜线展示条目。 -这三种树布局都需要先注册。 [可选布局](/docs/guides/layout-overview#optional-layouts) +这三种树布局均从 `@antv/layout` 导入对应布局类并作为 `layout.type` 传入,无需注册。见[内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts)。 diff --git a/site/content/docs/zh/guides/layout-worker.mdx b/site/content/docs/zh/guides/layout-worker.mdx index 3e393b15b..7bab809b6 100644 --- a/site/content/docs/zh/guides/layout-worker.mdx +++ b/site/content/docs/zh/guides/layout-worker.mdx @@ -9,7 +9,7 @@ Worker 可以把布局计算放到后台线程,让画布在计算期间保持 ## 启用方式 -布局是否尝试在后台计算,由 `layout.worker` 设置。 +`layout.worker` 控制内置布局是否尝试使用 G6Lite Worker。 ```ts layout: { type: 'force-atlas2', worker: true, maxIteration: 100 }, @@ -17,11 +17,11 @@ layout: { type: 'force-atlas2', worker: true, maxIteration: 100 }, | layout.worker | Graph 中的行为 | | --- | --- | -| 省略 | 节点数 ≥ 1000 时尝试启用;低于 1000 时走主线程 | -| true | 对内置库布局尝试 Worker;注册扩展(包括六种可选布局)在主线程执行 | -| false | 显式关闭,包括大图 | +| 省略 | 字符串形式的内置布局在节点数 ≥ 1000 时尝试启用 | +| true | 对字符串形式的内置布局尝试使用 G6Lite Worker | +| false | 关闭 G6Lite Worker,包括大图 | -独立 runLayout 没有按节点数自动启用的行为。Worker 失败会回退到同一算法的主线程实现;具体执行顺序和默认迭代值见[布局参考](/docs/guides/layout#worker)。 +已注册的同名扩展优先。注册扩展和导入的布局类自行管理计算。独立调用 `runLayout` 时按显式的 `worker` 值启用 G6Lite Worker。Worker 失败会回退到同一算法的主线程实现,见[布局参考](/docs/guides/layout#worker)。 ## 代码与对应效果 @@ -41,7 +41,7 @@ Worker 仅收到节点 id/x/y/data 和边 source/target/data。Graph 在结果 ## 取消与清理 -在渲染完成的实例上调用 graph.cancelLayout(),可终止该实例正在使用的 Worker 并放弃结果;不会回滚已完成布局,也不会影响其他 Graph 的 Worker。继续切换时,重新 setLayout 后 await layout。 +在渲染完成的实例上调用 `graph.cancelLayout()`,可以终止该实例的 G6Lite Worker 并放弃本次布局结果。自定义布局自己管理后台任务与资源。继续切换时,调用 `setLayout()` 后等待 `layout()`。 示例的 generation 检查还保护应用自己的完成提示和 fitView,避免过期调用结束后继续执行这些副作用。卸载时调用 graph.destroy();自行创建的按钮、计时器和订阅也要清理。 diff --git a/site/content/docs/zh/guides/layout.mdx b/site/content/docs/zh/guides/layout.mdx index 14637c6b2..6e43cdfa5 100644 --- a/site/content/docs/zh/guides/layout.mdx +++ b/site/content/docs/zh/guides/layout.mdx @@ -13,7 +13,7 @@ import { NodeDocExample as LayoutDocExample } from '@/components/node-doc-exampl | 字段 | 用途 | | --- | --- | -| `type` | 指定布局算法,例如 `dagre` | +| `type` | 指定布局算法:内置布局名称(如 `force`)或从 `@antv/layout` 导入的布局类(如 `DagreLayout`) | | `width`、`height` | 设置计算范围,默认取画布宽高 | | `center` | 设置布局中心,默认取计算范围的中心,由算法决定是否使用 | | `worker` | 设置是否尝试在 Worker 中计算 | @@ -24,23 +24,23 @@ import { NodeDocExample as LayoutDocExample } from '@/components/node-doc-exampl 下表的 11 种布局用同一份数据生成缩略图。想试用其中一种,可以打开[布局示例合集](/docs/examples/layout-gallery)。完整算法参数见 [AntV Layout 文档](https://layout.antv.vision/)。 -标为「可选」的布局需先导入并注册,见[可选布局](/docs/guides/layout-overview#optional-layouts)。 +标为「外部」的布局都需从 `@antv/layout` 导入布局类并作为 `layout.type` 传入,无需注册,见[内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts)。
| type | 效果 | 用途 | 常用算法参数 | | --- | --- | --- | --- | -| [`circular`](/docs/guides/layout-circular) | ![节点沿圆周均匀排列](/layouts/reference/circular.svg) | 圆周或螺旋排列 | radius、startRadius、endRadius、startAngle、endAngle、clockwise | -| [`concentric`](/docs/guides/layout-concentric) | ![节点按度数从中心向外分圈](/layouts/reference/concentric.svg) | 按重要程度分圈 | sortBy、maxLevelDiff、nodeSize、nodeSpacing、preventOverlap | -| [`grid`](/docs/guides/layout-grid) | ![节点对齐到规则行列](/layouts/reference/grid.svg) | 规则行列 | rows、cols、begin、sortBy、nodeSize、nodeSpacing、preventOverlap | -| [`random`](/docs/guides/layout-random) | ![节点散布在画布上](/layouts/reference/random.svg) | **可选。** 随机位置 | width、height、center | -| [`dagre`](/docs/guides/layout-dagre) | ![有向图从左到右分层排列](/layouts/reference/dagre.svg) | 有向分层 | rankdir: TB/BT/LR/RL,nodesep、ranksep、edgesep、nodeSize、ranker | +| [`circular`](/docs/guides/layout-circular) | ![节点沿圆周均匀排列](/layouts/reference/circular.svg) | **外部。** 圆周或螺旋排列 | radius、startRadius、endRadius、startAngle、endAngle、clockwise | +| [`concentric`](/docs/guides/layout-concentric) | ![节点按度数从中心向外分圈](/layouts/reference/concentric.svg) | **外部。** 按重要程度分圈 | sortBy、maxLevelDiff、nodeSize、nodeSpacing、preventOverlap | +| [`grid`](/docs/guides/layout-grid) | ![节点对齐到规则行列](/layouts/reference/grid.svg) | **外部。** 规则行列 | rows、cols、begin、sortBy、nodeSize、nodeSpacing、preventOverlap | +| [`random`](/docs/guides/layout-random) | ![节点散布在画布上](/layouts/reference/random.svg) | **外部。** 随机位置 | width、height、center | +| [`dagre`](/docs/guides/layout-dagre) | ![有向图从左到右分层排列](/layouts/reference/dagre.svg) | **外部。** 有向分层 | rankdir: TB/BT/LR/RL,nodesep、ranksep、edgesep、nodeSize、ranker | | [`force`](/docs/guides/layout-force) | ![相连节点在力模拟中聚拢](/layouts/reference/force.svg) | 力导向网络 | linkDistance、nodeStrength、edgeStrength、maxIteration、minMovement | | [`d3-force`](/docs/guides/layout-d3-force) | ![D3 力模拟生成的节点排布](/layouts/reference/d3-force.svg) | D3 力模拟 | linkDistance、nodeStrength、alphaDecay、alphaMin;使用 nodeStrength,不是 charge | | [`force-atlas2`](/docs/guides/layout-force-atlas2) | ![ForceAtlas2 生成的节点排布](/layouts/reference/force-atlas2.svg) | ForceAtlas2 网络 | kr、kg、barnesHut、prune、maxIteration | | [`fruchterman`](/docs/guides/layout-fruchterman) | ![Fruchterman 生成的节点排布](/layouts/reference/fruchterman.svg) | Fruchterman 网络 | gravity、speed、maxIteration、minMovement | -| [`radial`](/docs/guides/layout-radial) | ![节点按到焦点的距离向外分层](/layouts/reference/radial.svg) | **可选。** 围绕焦点的同心层 | focusNode、unitRadius、linkDistance、preventOverlap | -| [`mds`](/docs/guides/layout-mds) | ![节点按图距离映射到二维平面](/layouts/reference/mds.svg) | **可选。** 按图距离求二维位置 | linkDistance、center | +| [`radial`](/docs/guides/layout-radial) | ![节点按到焦点的距离向外分层](/layouts/reference/radial.svg) | **外部。** 围绕焦点的同心层 | focusNode、unitRadius、linkDistance、preventOverlap | +| [`mds`](/docs/guides/layout-mds) | ![节点按图距离映射到二维平面](/layouts/reference/mds.svg) | **外部。** 按图距离求二维位置 | linkDistance、center |
@@ -48,7 +48,7 @@ import { NodeDocExample as LayoutDocExample } from '@/components/node-doc-exampl ### 计算默认值 [#计算默认值] -以下由 G6Lite 注入或改写,用户显式值优先;仅适用于上面的库布局,不用于三个函数式树布局。 +以下由 G6Lite 注入或改写,用户显式值优先;适用于上面的库布局,包括从 `@antv/layout` 导入的树布局类。 | 算法 / 选项 | 默认或规则 | | --- | --- | @@ -82,9 +82,9 @@ import { NodeDocExample as LayoutDocExample } from '@/components/node-doc-exampl | type | 效果 | 用途 | | --- | --- | --- | -| [`mindmap`](/docs/guides/layout-mindmap) | ![子树分布在根节点两侧](/layouts/reference/mindmap.svg) | **可选。** 按左右分支组织层级 | -| [`indented`](/docs/guides/layout-indented) | ![子节点按深度缩进并逐行排列](/layouts/reference/indented.svg) | **可选。** 逐行展示树形列表 | -| [`fishbone`](/docs/guides/layout-fishbone) | ![分类沿水平主干交替分布,条目挂在斜支线上](/layouts/reference/fishbone.svg) | **可选。** 沿主干展示分类和条目 | +| [`mindmap`](/docs/guides/layout-mindmap) | ![子树分布在根节点两侧](/layouts/reference/mindmap.svg) | **外部。** 按左右分支组织层级 | +| [`indented`](/docs/guides/layout-indented) | ![子节点按深度缩进并逐行排列](/layouts/reference/indented.svg) | **外部。** 逐行展示树形列表 | +| [`fishbone`](/docs/guides/layout-fishbone) | ![分类沿水平主干交替分布,条目挂在斜支线上](/layouts/reference/fishbone.svg) | **外部。** 沿主干展示分类和条目 |
@@ -98,7 +98,7 @@ import { NodeDocExample as LayoutDocExample } from '@/components/node-doc-exampl | `branchColors` | `string[]` | 内置 10 色;结果写入节点 data._branchColor,需样式函数读取才会着色 | | `getWidth` | `(node: GraphNode) => number` | 返回 100 | | `getHeight` | `(node: GraphNode) => number` | 返回 40 | -| `getSide` | `(node: GraphNode, index: number, parentSide?: 'left' \| 'right') => 'left' \| 'right'` | 返回 right;node 仅包含 id 和 data | +| `getSide` | `(node: GraphNode, index: number, parentSide?: 'left' \| 'right') => 'left' \| 'right'` | 默认返回 right。需要原始业务数据时,用节点 ID 查找输入数据。 | 根取输入中第一个无父节点;没有时取第一个节点,根坐标 (0, 0)。不支持旧写法 hGap/vGap,使用 levelGap/nodeGap。 @@ -136,7 +136,9 @@ import { NodeDocExample as LayoutDocExample } from '@/components/node-doc-exampl 首次渲染后,布局配置通过 `setLayout()` 替换,节点位置在 `layout()` 完成后更新。需要让整张图重新进入视野时,再调用 `fitView()`。 ```ts -graph.setLayout({ type: 'dagre', rankdir: 'LR', nodesep: 32, ranksep: 64 }); +import { DagreLayout } from '@antv/layout'; + +graph.setLayout({ type: DagreLayout, rankdir: 'LR', nodesep: 32, ranksep: 64 }); await graph.layout(); graph.fitView({ padding: 48, maxZoom: 1 }); ``` @@ -147,7 +149,7 @@ graph.fitView({ padding: 48, maxZoom: 1 }); ## Worker -库布局可通过 `worker: true` 尝试在后台线程计算。图达到 1000 个节点时默认尝试启用。算法参数包含函数时,可设置 `worker: false` 在主线程计算。启用、回退和取消示例见 [Worker 布局指南](/docs/guides/layout-worker)。 +字符串形式的内置布局可通过 `worker: true` 尝试使用 G6Lite Worker。省略该选项时,达到 1000 个节点的内置布局会默认尝试启用。注册扩展和导入的布局类自行管理计算。内置算法参数包含函数时,可设置 `worker: false` 在主线程计算。启用、回退和取消示例见 [Worker 布局指南](/docs/guides/layout-worker)。 ## 常见问题 diff --git a/site/content/docs/zh/metrics/bundle-size.mdx b/site/content/docs/zh/metrics/bundle-size.mdx index f5aa36cbe..c59eaff1d 100644 --- a/site/content/docs/zh/metrics/bundle-size.mdx +++ b/site/content/docs/zh/metrics/bundle-size.mdx @@ -5,7 +5,7 @@ description: 查看合并后的性能数据 内容已并入[性能对比](/docs/metrics/performance#bundle-size)。 -- [构建测量](/docs/metrics/performance#bundle-size) -- [esm 与布局分块](/docs/metrics/performance#bundle-size) -- [umd cdn](/docs/metrics/performance#bundle-size) -- [如何复测](/docs/metrics/performance#benchmark-method) +- [UMD 构建测量](/docs/metrics/performance#umd-size) +- [ESM 布局接入](/docs/guides/layout-overview#builtin-and-external-layouts) +- [UMD 与 CDN 体积](/docs/metrics/performance#umd-size) +- [复测 UMD 体积](/docs/metrics/performance#umd-measurement) diff --git a/site/content/docs/zh/metrics/performance.mdx b/site/content/docs/zh/metrics/performance.mdx index 7eafb3753..2f59750f4 100644 --- a/site/content/docs/zh/metrics/performance.mdx +++ b/site/content/docs/zh/metrics/performance.mdx @@ -3,9 +3,9 @@ title: 性能对比 description: 同一图数据下 G6Lite、G6、Sigma.js 与 ECharts 的首绘耗时和浏览器包体积 --- -本页比较 G6Lite、G6、Sigma.js 和 ECharts 的 Graph 系列。首绘使用同一份预置坐标数据,包体积使用同一种构建方式。2026-11-13 的对比测试使用 G6Lite 本地开发构建,当时的版本字段为 `0.1.0`;它不是后来发布到 npm 的正式包。 +本页记录 G6Lite、G6、Sigma.js 和 ECharts Graph 系列的首绘耗时与 UMD 文件体积。首绘使用同一份预置坐标数据。2026-11-13 的 G6Lite 首绘测试使用版本字段为 `0.1.0` 的本地开发构建。 -原始记录未保存该开发构建的源码提交号或包完整性标识,因此这些结果仅对应当时的构建。正式包的首绘性能需要单独复测;下方 [CDN 文件体积](#umd-size)已按 npm 正式发布包重新测量。 +原始记录未保存该开发构建的源码提交号或包完整性标识,因此这些结果仅对应当时的构建。正式包的首绘性能需要单独复测。下方 [UMD 文件体积](#umd-size)列出布局重构构建和已发布 CDN 产物的测量值。 @@ -34,27 +34,31 @@ description: 同一图数据下 G6Lite、G6、Sigma.js 与 ECharts 的首绘耗 本轮结果只适用于上述测试环境,不代表最低浏览器版本认证。各浏览器的能力版本参考与尚未覆盖的验收范围见[最低浏览器版本](/docs/compatibility/browser#最低浏览器版本)。 -## 浏览器包体积 [#bundle-size] + -使用 esbuild 0.21.5 将各库的 npm 入口和依赖打包为单个浏览器脚本,并进行代码压缩。G6Lite 测量默认入口,可选布局按需引入。ECharts 只引入 GraphChart 和 CanvasRenderer,Sigma.js 包含 Graphology。体积以 KiB 计,不含 source map。 +## UMD 文件体积 [#umd-size] -| npm 包 | 代码压缩后(KiB) | gzip -9(KiB) | -| --- | ---: | ---: | -| `@antv/g6-lite` 开发构建(`0.1.0`) | 511.3 | 159.5 | -| `@antv/g6@5.1.1` | 1,435.3 | 413.4 | -| `sigma@4.0.4` + `graphology@0.26.0` | 438.4 | 110.7 | -| `echarts@6.1.0` | 386.3 | 130.2 | +首页展示 [PR #75 的布局重构源码构建](https://github.com/antvis/G6Lite/commit/34bf8b6d8bdac3ee97f339e2f06314c279d7c865)。2026-11-30 构建后运行 `npm run size`,用 Node.js 的默认 gzip 设置测得 `dist/g6-lite.min.js` 为 **118,503 字节(118.5 kB)**,压缩前为 408.1 kB。该构建包含四种内置力导向布局和运行时依赖。其他布局可从 `@antv/layout` 按需导入。 [内置与外部布局](/docs/guides/layout-overview#builtin-and-external-layouts) -### G6Lite CDN 文件体积 [#umd-size] +下表使用 `gzip -9` 测量已发布的 CDN 产物。G6Lite 使用 npm 上的 `@antv/g6-lite@0.1.0`,其他库使用 unpkg 上的 UMD 文件。1 kB = 1,000 字节。 -以下文件来自 npm 正式发布的 `@antv/g6-lite@0.1.0`,于 2026-11-24 重新下载并使用 `gzip -9` 分别测量,1 kB = 1,000 字节。首页展示默认脚本的同次测量值。这些文件是现成的 UMD 产物,与上方 esbuild 重新打包的体积采用不同口径。 +| 库 | UMD 产物 | 压缩前(kB) | gzip 后(kB) | +| --- | --- | ---: | ---: | +| `@antv/g6-lite@0.1.0` | `g6-lite.min.js` | 504.2 | 150.3 | +| `@antv/g6@5.1.1` | `g6.min.js` | 1,383.3 | 390.6 | +| `echarts@6.1.0` | `echarts.min.js` | 1,121.9 | 368.2 | +| `sigma@4.0.4` + `graphology@0.26.0` | `sigma.min.js` + `graphology.umd.min.js` | 466.7 | 117.6 | + +### 复测 UMD 体积 [#umd-measurement] + +从要测量的源码提交构建 G6Lite,再运行体积检查。 -| 发布文件 | 内容 | gzip -9(kB) | -| --- | --- | ---: | -| `g6-lite.min.js` | 默认脚本,包含渲染、交互和内置布局 | 150.3 | -| `g6-lite-layouts.min.js` | 六种可选布局,使用时额外加载 | 29.1 | +```bash +npm run build +npm run size +``` -需要使用可选布局时,加载附加脚本后注册对应的布局。 [可选布局用法](/docs/guides/layout-overview#optional-layouts) +测量已发布产物时,下载表中对应版本的 UMD 文件,再用 `gzip -9 -c <文件路径> | wc -c` 获取字节数。 ## 业务图怎么测 [#performance-tuning] diff --git a/site/demos/basic.ts b/site/demos/basic.ts index 4b2546981..b6c5246b2 100644 --- a/site/demos/basic.ts +++ b/site/demos/basic.ts @@ -2,6 +2,7 @@ * Demo: 最简示例 — 少量节点边 + 环形布局 */ import { Graph } from '@antv/g6-lite'; +import { CircularLayout } from '@antv/layout'; export async function run(container: string | HTMLElement) { const graph = new Graph({ @@ -23,7 +24,7 @@ export async function run(container: string | HTMLElement) { { source: '3', target: '4' }, ], }, - layout: { type: 'circular', radius: 150 }, + layout: { type: CircularLayout, radius: 150 }, behaviors: ['zoom-canvas', 'drag-canvas'], theme: 'light', }); diff --git a/site/demos/behaviors.ts b/site/demos/behaviors.ts index 61f3dda97..134594177 100644 --- a/site/demos/behaviors.ts +++ b/site/demos/behaviors.ts @@ -4,6 +4,7 @@ * 操作:滚轮缩放、拖拽平移、悬停高亮;按住 Shift 拖拽框选节点(brush-select)。 */ import { Graph } from '@antv/g6-lite'; +import { CircularLayout } from '@antv/layout'; export async function run(container: string | HTMLElement) { const graph = new Graph({ @@ -33,7 +34,7 @@ export async function run(container: string | HTMLElement) { }, }, edge: { type: 'line', style: { lineWidth: 2 } }, - layout: { type: 'circular', radius: 150 }, + layout: { type: CircularLayout, radius: 150 }, behaviors: [ 'zoom-canvas', 'drag-canvas', diff --git a/site/demos/custom-behavior.ts b/site/demos/custom-behavior.ts index 0ec99edae..11699175f 100644 --- a/site/demos/custom-behavior.ts +++ b/site/demos/custom-behavior.ts @@ -8,6 +8,7 @@ import { type BehaviorOptions, type ExtensionContext, } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; const PIN_BEHAVIOR = 'pin-node' as const; @@ -73,7 +74,7 @@ export async function run(container: string | HTMLElement) { }, }, edge: { style: { endArrow: true } }, - layout: { type: 'dagre', rankdir: 'LR', nodesep: 24, ranksep: 70, nodeSize: [88, 36] }, + layout: { type: DagreLayout, rankdir: 'LR', nodesep: 24, ranksep: 70, nodeSize: [88, 36] }, behaviors: ['zoom-canvas', 'drag-canvas', { type: PIN_BEHAVIOR } satisfies PinBehaviorOptions], }); await graph.render(); diff --git a/site/demos/custom-label-renderer.ts b/site/demos/custom-label-renderer.ts index 0a1d04b60..f62613138 100644 --- a/site/demos/custom-label-renderer.ts +++ b/site/demos/custom-label-renderer.ts @@ -3,6 +3,7 @@ * The multiline behavior lives entirely in this demo, not in G6Lite core. */ import { Graph, register, type LabelRenderer } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; interface MultilineLayoutData { lines: string[]; @@ -62,7 +63,7 @@ export async function run(container: string | HTMLElement) { { source: 'gateway', target: 'target', data: { label: 'RPC\nresponse' } }, ], }, - layout: { type: 'dagre', rankdir: 'LR', nodeSize: 32, ranksep: 180 }, + layout: { type: DagreLayout, rankdir: 'LR', nodeSize: 32, ranksep: 180 }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node'], // #region node-label node: { diff --git a/site/demos/decoration-badges.ts b/site/demos/decoration-badges.ts index 21666b8e6..4e5813fa0 100644 --- a/site/demos/decoration-badges.ts +++ b/site/demos/decoration-badges.ts @@ -2,6 +2,7 @@ * Demo: stack built-in badges on a node boundary. */ import { Graph, type NodeBadgeItem } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; export async function run(container: string | HTMLElement) { const graph = new Graph({ @@ -59,7 +60,7 @@ export async function run(container: string | HTMLElement) { ], }, edge: { style: { endArrow: true, stroke: '#99ADD1' } }, - layout: { type: 'dagre', rankdir: 'LR', nodesep: 40, ranksep: 120, nodeSize: 56 }, + layout: { type: DagreLayout, rankdir: 'LR', nodesep: 40, ranksep: 120, nodeSize: 56 }, behaviors: ['zoom-canvas', 'drag-canvas'], }); await graph.render(); diff --git a/site/demos/dependency-graph.ts b/site/demos/dependency-graph.ts index dcce3496b..eb46aa092 100644 --- a/site/demos/dependency-graph.ts +++ b/site/demos/dependency-graph.ts @@ -2,6 +2,7 @@ * Demo: a left-to-right service call chain. */ import { Graph } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; const CALLS: Array<[string, string, string]> = [ ['gateway', '网关', 'auth'], @@ -41,7 +42,7 @@ export async function run(container: string | HTMLElement) { }, }, edge: { style: { endArrow: true, stroke: '#99ADD1', lineWidth: 1.5 } }, - layout: { type: 'dagre', rankdir: 'LR', nodesep: 24, ranksep: 64, nodeSize: [84, 34] }, + layout: { type: DagreLayout, rankdir: 'LR', nodesep: 24, ranksep: 64, nodeSize: [84, 34] }, behaviors: ['zoom-canvas', 'drag-canvas', 'highlight-node'], }); await graph.render(); diff --git a/site/demos/docs-built-in-layout.ts b/site/demos/docs-built-in-layout.ts index 25f5d8c38..0830480cf 100644 --- a/site/demos/docs-built-in-layout.ts +++ b/site/demos/docs-built-in-layout.ts @@ -1,22 +1,26 @@ -import { Graph, register, type GraphNode, type GraphOptions, type LayoutConfig } from '@antv/g6-lite'; -import mds from '@antv/g6-lite/layouts/mds'; -import radial from '@antv/g6-lite/layouts/radial'; -import indented from '@antv/g6-lite/layouts/indented'; -import mindmap from '@antv/g6-lite/layouts/mindmap'; -import fishboneLayout from '@antv/g6-lite/layouts/fishbone'; -import random from '@antv/g6-lite/layouts/random'; - -const optionalLayouts = new Map(Object.entries({ mds, radial, indented, mindmap, fishbone: fishboneLayout, random })); +import { Graph, type GraphNode, type GraphOptions, type LayoutConfig } from '@antv/g6-lite'; +import { + CircularLayout, + ConcentricLayout, + DagreLayout, + FishboneLayout, + GridLayout, + IndentedLayout, + MDSLayout, + MindmapLayout, + RadialLayout, + RandomLayout, +} from '@antv/layout'; const layouts = { // #region layout-grid-config - 'grid': { type: 'grid', rows: 2, cols: 3, width: 360, height: 160 }, + 'grid': { type: GridLayout, rows: 2, cols: 3, width: 360, height: 160 }, // #endregion layout-grid-config // #region layout-circular-config - 'circular': { type: 'circular', radius: 100 }, + 'circular': { type: CircularLayout, radius: 100 }, // #endregion layout-circular-config // #region layout-dagre-config - 'dagre': { type: 'dagre', rankdir: 'LR', nodesep: 32, ranksep: 80, nodeSize: [80, 32] }, + 'dagre': { type: DagreLayout, rankdir: 'LR', nodesep: 32, ranksep: 80, nodeSize: [80, 32] }, // #endregion layout-dagre-config // #region layout-force-config 'force': { type: 'force', linkDistance: 100 }, @@ -47,11 +51,11 @@ const layouts = { }, // #endregion layout-fruchterman-config // #region layout-radial-config - 'radial': { type: 'radial', focusNode: '0', unitRadius: 68 }, + 'radial': { type: RadialLayout, focusNode: '0', unitRadius: 68 }, // #endregion layout-radial-config // #region layout-concentric-config 'concentric': { - type: 'concentric', + type: ConcentricLayout, sortBy: 'degree', maxLevelDiff: 1, preventOverlap: true, @@ -60,14 +64,14 @@ const layouts = { }, // #endregion layout-concentric-config // #region layout-mds-config - 'mds': { type: 'mds', linkDistance: 80 }, + 'mds': { type: MDSLayout, linkDistance: 80 }, // #endregion layout-mds-config // #region layout-indented-config - 'indented': { type: 'indented', direction: 'LR', indent: 72, rowHeight: 30 }, + 'indented': { type: IndentedLayout, direction: 'LR', indent: 72, rowHeight: 30 }, // #endregion layout-indented-config // #region layout-mindmap-config 'mindmap': { - type: 'mindmap', + type: MindmapLayout, direction: 'H', levelGap: 100, nodeGap: 24, @@ -78,7 +82,7 @@ const layouts = { // #endregion layout-mindmap-config // #region layout-fishbone-config 'fishbone': { - type: 'fishbone', + type: FishboneLayout, direction: 'LR', categoryGap: 200, categoryDistance: 138, @@ -87,7 +91,7 @@ const layouts = { }, // #endregion layout-fishbone-config // #region layout-random-config - 'random': { type: 'random', width: 400, height: 240, center: [200, 120] }, + 'random': { type: RandomLayout, width: 400, height: 240, center: [200, 120] }, // #endregion layout-random-config } satisfies Record; @@ -266,8 +270,6 @@ function tree(type: string): ExampleOptions { // #endregion tree export function run(container: HTMLElement, type: BuiltInLayoutExample) { - const extension = optionalLayouts.get(type); - if (extension) register('layout', type, extension); const layout = layouts[type]; if (type === 'concentric') return render(container, layout, concentric()); if (type === 'fishbone') return render(container, layout, fishbone()); diff --git a/site/demos/docs-edge-indented.ts b/site/demos/docs-edge-indented.ts index cf37840f0..c0d2b7496 100644 --- a/site/demos/docs-edge-indented.ts +++ b/site/demos/docs-edge-indented.ts @@ -1,8 +1,7 @@ -import { Graph, register } from '@antv/g6-lite'; -import indented from '@antv/g6-lite/layouts/indented'; +import { Graph } from '@antv/g6-lite'; +import { IndentedLayout } from '@antv/layout'; export async function run(container: HTMLElement) { - register('layout', 'indented', indented); const graph = new Graph({ container, data: { @@ -18,7 +17,7 @@ export async function run(container: HTMLElement) { style: { width: 80, height: 28, borderRadius: 5, label: (node) => node.id, labelPlacement: 'center', labelColor: '#fff' }, }, // #region routing - layout: { type: 'indented', direction: 'LR', indent: 160, rowHeight: 70 }, + layout: { type: IndentedLayout, direction: 'LR', indent: 160, rowHeight: 70 }, edge: { type: 'polyline', style: { router: 'indented', indentTarget: 'left', radius: 10, lineWidth: 2, hitAreaWidth: 12 }, diff --git a/site/demos/docs-events-lifecycle.ts b/site/demos/docs-events-lifecycle.ts index 957514b54..97108c6c7 100644 --- a/site/demos/docs-events-lifecycle.ts +++ b/site/demos/docs-events-lifecycle.ts @@ -1,5 +1,6 @@ import { Graph, type FitViewOptions, type GraphData, type GraphEventMap } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { GridLayout } from '@antv/layout'; export async function run(container: HTMLElement) { const initialData = (): GraphData => ({ @@ -13,7 +14,7 @@ export async function run(container: HTMLElement) { const graph = new Graph({ container, data: initialData(), - layout: { type: 'grid', rows: 1, width: 320, height: 80 }, + layout: { type: GridLayout, rows: 1, width: 320, height: 80 }, node: { style: { size: 30, label: (node) => `${node.id}: ${node.data?.edits ?? 0}`, labelOffsetY: 8 } }, behaviors: [], autoFit: fit, diff --git a/site/demos/docs-extension-layout.ts b/site/demos/docs-extension-layout.ts index 12224b923..3a1e40a49 100644 --- a/site/demos/docs-extension-layout.ts +++ b/site/demos/docs-extension-layout.ts @@ -1,5 +1,6 @@ import { Graph, register, type ExtensionContext, type LayoutExtension } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { GridLayout } from '@antv/layout'; interface ColumnOptions extends Record { gap?: number; delay?: number } function pause(signal: AbortSignal | undefined, delay: number) { @@ -49,7 +50,7 @@ export async function run(container: HTMLElement) { const request = ++revision; status.textContent = mode === 'slow' ? 'Running slow layout…' : 'Running…'; if (mode === 'grid') graph.cancelLayout(); - graph.setLayout(mode === 'grid' ? { type: 'grid' } : { type: 'docs-column', gap: 100, delay: mode === 'slow' ? 180 : 0 }); + graph.setLayout(mode === 'grid' ? { type: GridLayout } : { type: 'docs-column', gap: 100, delay: mode === 'slow' ? 180 : 0 }); await graph.layout(); if (graph.destroyed || request !== revision) return; graph.fitView({ padding: 56 }); diff --git a/site/demos/docs-graph-options.ts b/site/demos/docs-graph-options.ts index 1429e193b..2dfa26d53 100644 --- a/site/demos/docs-graph-options.ts +++ b/site/demos/docs-graph-options.ts @@ -1,4 +1,5 @@ import { Graph } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; export async function run(container: HTMLElement) { const graph = new Graph({ @@ -55,7 +56,7 @@ export async function run(container: HTMLElement) { }, }, // #endregion element-options - layout: { type: 'dagre', rankdir: 'LR', ranksep: 84, nodesep: 16, nodeSize: 96 }, + layout: { type: DagreLayout, rankdir: 'LR', ranksep: 84, nodesep: 16, nodeSize: 96 }, label: { hideOnLowZoom: false, scaleWithZoom: false }, behaviors: ['click-select', 'drag-canvas', 'zoom-canvas'], autoFit: { padding: 36 }, diff --git a/site/demos/docs-layout-networks.ts b/site/demos/docs-layout-networks.ts index 513f91d6f..6f032bd68 100644 --- a/site/demos/docs-layout-networks.ts +++ b/site/demos/docs-layout-networks.ts @@ -1,15 +1,12 @@ -import { Graph, register, type LayoutConfig } from '@antv/g6-lite'; -import mds from '@antv/g6-lite/layouts/mds'; -import radial from '@antv/g6-lite/layouts/radial'; +import { Graph, type LayoutConfig } from '@antv/g6-lite'; +import { ConcentricLayout, MDSLayout, RadialLayout } from '@antv/layout'; import { createDemoPanel } from './demo-panel.ts'; export async function run(container: HTMLElement) { - register('layout', 'mds', mds); - register('layout', 'radial', radial); // #region network-options const layouts: Record = { - radial: { type: 'radial', focusNode: '0', unitRadius: 68 }, - concentric: { type: 'concentric', sortBy: 'degree', nodeSize: 24 }, + radial: { type: RadialLayout, focusNode: '0', unitRadius: 68 }, + concentric: { type: ConcentricLayout, sortBy: 'degree', nodeSize: 24 }, force: { type: 'force', linkDistance: 100 }, 'd3-force': { type: 'd3-force', linkDistance: 100 }, 'force-atlas2': { type: 'force-atlas2', kr: 12, kg: 0.5, preventOverlap: true, nodeSize: 24, maxIteration: 200 }, @@ -22,7 +19,7 @@ export async function run(container: HTMLElement) { maxIteration: 250, minMovement: 0.05, }, - mds: { type: 'mds', linkDistance: 80 }, + mds: { type: MDSLayout, linkDistance: 80 }, }; // #endregion network-options const connections = [ diff --git a/site/demos/docs-layout-result.ts b/site/demos/docs-layout-result.ts index c4f849f3b..cfac3c09e 100644 --- a/site/demos/docs-layout-result.ts +++ b/site/demos/docs-layout-result.ts @@ -1,4 +1,5 @@ import { Graph, runLayout } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; export async function run(container: HTMLElement) { const data = { @@ -9,7 +10,7 @@ export async function run(container: HTMLElement) { { source: 'Validate', target: 'Output' }, ], }; - const result = await runLayout(data, { type: 'dagre', rankdir: 'LR', ranksep: 72 }, { width: 600, height: 300 }); + const result = await runLayout(data, { type: DagreLayout, rankdir: 'LR', ranksep: 72 }, { width: 600, height: 300 }); // No layout option on Graph: display the coordinates already computed above. const graph = new Graph({ container, diff --git a/site/demos/docs-layout-switch.ts b/site/demos/docs-layout-switch.ts index 3a843fe9e..f6994735a 100644 --- a/site/demos/docs-layout-switch.ts +++ b/site/demos/docs-layout-switch.ts @@ -1,5 +1,8 @@ import { Graph, type FitViewOptions, type LayoutConfig } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { GridLayout } from '@antv/layout'; +import { CircularLayout } from '@antv/layout'; +import { DagreLayout } from '@antv/layout'; export function getFitOptions(): FitViewOptions { return { padding: [56, 48, 76, 48] }; @@ -8,9 +11,9 @@ export function getFitOptions(): FitViewOptions { export async function run(container: HTMLElement) { // #region layout-options const layouts: Record = { - grid: { type: 'grid', rows: 2, cols: 3, width: 360, height: 160 }, - circular: { type: 'circular', radius: 100 }, - dagre: { type: 'dagre', rankdir: 'LR', nodesep: 64, ranksep: 96, nodeSize: 28 }, + grid: { type: GridLayout, rows: 2, cols: 3, width: 360, height: 160 }, + circular: { type: CircularLayout, radius: 100 }, + dagre: { type: DagreLayout, rankdir: 'LR', nodesep: 64, ranksep: 96, nodeSize: 28 }, }; // #endregion layout-options const graph = new Graph({ diff --git a/site/demos/docs-layout-tree.ts b/site/demos/docs-layout-tree.ts index bfbd95072..c23cd7911 100644 --- a/site/demos/docs-layout-tree.ts +++ b/site/demos/docs-layout-tree.ts @@ -1,20 +1,15 @@ -import { Graph, register, type EdgeConfig, type GraphNode, type LayoutConfig } from '@antv/g6-lite'; -import indented from '@antv/g6-lite/layouts/indented'; -import mindmap from '@antv/g6-lite/layouts/mindmap'; -import fishbone from '@antv/g6-lite/layouts/fishbone'; +import { Graph, type EdgeConfig, type GraphNode, type LayoutConfig } from '@antv/g6-lite'; +import { FishboneLayout, IndentedLayout, MindmapLayout } from '@antv/layout'; import { createDemoPanel } from './demo-panel.ts'; export async function run(container: HTMLElement) { - register('layout', 'indented', indented); - register('layout', 'mindmap', mindmap); - register('layout', 'fishbone', fishbone); const nodeSize = 16; const leftBranches = new Set(['A', 'C']); // #region tree-layouts const layouts = { mindmap: { layout: { - type: 'mindmap', + type: MindmapLayout, direction: 'H', levelGap: 100, nodeGap: 24, @@ -25,12 +20,12 @@ export async function run(container: HTMLElement) { edge: { type: 'line', style: { endArrow: false } }, }, indented: { - layout: { type: 'indented', direction: 'LR', indent: 72, rowHeight: 30 }, + layout: { type: IndentedLayout, direction: 'LR', indent: 72, rowHeight: 30 }, edge: { type: 'polyline', style: { router: 'indented', endArrow: false } }, }, fishbone: { layout: { - type: 'fishbone', + type: FishboneLayout, direction: 'LR', categoryGap: 156, categoryDistance: 130, diff --git a/site/demos/docs-layout-worker.ts b/site/demos/docs-layout-worker.ts index 99deb473b..a63b1fe51 100644 --- a/site/demos/docs-layout-worker.ts +++ b/site/demos/docs-layout-worker.ts @@ -1,8 +1,9 @@ import { Graph, type LayoutConfig } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { GridLayout } from '@antv/layout'; export async function run(container: HTMLElement) { - const grid: LayoutConfig = { type: 'grid', rows: 10, cols: 16, width: 400, height: 200, worker: false }; + const grid: LayoutConfig = { type: GridLayout, rows: 10, cols: 16, width: 400, height: 200, worker: false }; const graph = new Graph({ container, data: { diff --git a/site/demos/docs-polyline-orth.ts b/site/demos/docs-polyline-orth.ts index f3fab120c..c267fbe7e 100644 --- a/site/demos/docs-polyline-orth.ts +++ b/site/demos/docs-polyline-orth.ts @@ -1,10 +1,11 @@ import { Graph, type EdgeConfig, type LayoutConfig } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { DagreLayout } from '@antv/layout'; export async function run(container: HTMLElement) { // #region routing const layout = { - type: 'dagre', rankdir: 'LR', + type: DagreLayout, rankdir: 'LR', nodesep: 40, ranksep: 96, nodeSize: [84, 32], } satisfies LayoutConfig; const edge: EdgeConfig = { diff --git a/site/demos/fishbone.ts b/site/demos/fishbone.ts index c3bae47c1..32b6f878f 100644 --- a/site/demos/fishbone.ts +++ b/site/demos/fishbone.ts @@ -1,8 +1,8 @@ /** * Demo: 鱼骨图 (Fishbone/Ishikawa Diagram) — 盈利未达预期分析 */ -import { Graph, register, type GraphEdge, type GraphNode } from '@antv/g6-lite'; -import fishbone from '@antv/g6-lite/layouts/fishbone'; +import { Graph, type GraphEdge, type GraphNode } from '@antv/g6-lite'; +import { FishboneLayout } from '@antv/layout'; import { attachLabelsCanvas, type GraphInstanceWithTools } from './tooling.ts'; const CATEGORY_COLORS = ['#E8684A', '#5B8FF9', '#5AD8A6', '#F6BD16', '#6F5EF9', '#5D7092']; @@ -106,7 +106,6 @@ function getEdgeColor(_source: GraphNode, _target: GraphNode, edge: GraphEdge) { } export async function run(container: string | HTMLElement) { - register('layout', 'fishbone', fishbone); const wrapper = typeof container === 'string' ? document.querySelector(container)! as HTMLElement : container; wrapper.style.position = 'relative'; @@ -116,7 +115,7 @@ export async function run(container: string | HTMLElement) { data, node: { type: 'rect', style: (n) => getNodeStyle(n) }, edge: { type: 'polyline', style: { stroke: getEdgeColor, lineWidth: 1.5, router: 'fishbone', endArrow: false, startArrow: false } }, - layout: { type: 'fishbone', direction: 'LR', categoryGap: 180, itemGap: 44, itemHStep: 50, categoryDistance: 200 }, + layout: { type: FishboneLayout, direction: 'LR', categoryGap: 180, itemHStep: 50, categoryDistance: 200 }, behaviors: ['zoom-canvas', 'drag-canvas'], theme: 'light', }); diff --git a/site/demos/flowchart.ts b/site/demos/flowchart.ts index 3f524b050..4d0e6e8bb 100644 --- a/site/demos/flowchart.ts +++ b/site/demos/flowchart.ts @@ -3,6 +3,7 @@ */ import { Graph } from '@antv/g6-lite'; import { attachLabelsCanvas, attachEdgeLabelsCanvas, type GraphInstanceWithTools } from './tooling.ts'; +import { DagreLayout } from '@antv/layout'; const COLORS: Record = { start: '#5B8FF9', @@ -81,7 +82,7 @@ export async function run(container: string | HTMLElement) { radius: 16, }, }, - layout: { type: 'dagre', rankdir: 'TB', nodesep: 40, ranksep: 60, nodeSize: [180, 32] }, + layout: { type: DagreLayout, rankdir: 'TB', nodesep: 40, ranksep: 60, nodeSize: [180, 32] }, behaviors: ['zoom-canvas', 'drag-canvas'], theme: 'light', }); diff --git a/site/demos/g6-v5-compat.ts b/site/demos/g6-v5-compat.ts index f0ff20e1a..3d206535d 100644 --- a/site/demos/g6-v5-compat.ts +++ b/site/demos/g6-v5-compat.ts @@ -5,6 +5,7 @@ import { Graph, register, type NodeExtension } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { DagreLayout } from '@antv/layout'; const statusBadgeNode: NodeExtension = { base: 'rect', @@ -89,7 +90,7 @@ export async function run(container: string | HTMLElement) { labelFontSize: 13, }, }, - layout: { type: 'dagre', rankdir: 'LR', nodesep: 45, ranksep: 90, nodeSize: [96, 34] }, + layout: { type: DagreLayout, rankdir: 'LR', nodesep: 45, ranksep: 90, nodeSize: [96, 34] }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node', 'click-select', 'hover-highlight'], }); diff --git a/site/demos/indented-tree.ts b/site/demos/indented-tree.ts index d3bba892c..5db90d782 100644 --- a/site/demos/indented-tree.ts +++ b/site/demos/indented-tree.ts @@ -7,8 +7,8 @@ * * 通过右上角菜单切换两种模式。 */ -import { Graph, register } from '@antv/g6-lite'; -import indented from '@antv/g6-lite/layouts/indented'; +import { Graph } from '@antv/g6-lite'; +import { IndentedLayout } from '@antv/layout'; import { createDemoPanel } from './demo-panel.ts'; import { attachLabelsCanvas, type GraphInstanceWithTools } from './tooling.ts'; @@ -64,7 +64,7 @@ function measureTextWidth(text: string): number { } const layoutBase = { - type: 'indented' as const, + type: IndentedLayout, indent: 80, rowHeight: 52, direction: 'LR' as const, @@ -146,7 +146,6 @@ function configMinimal() { } export async function run(container: string | HTMLElement) { - register('layout', 'indented', indented); const wrapper = typeof container === 'string' ? (document.querySelector(container)! as HTMLElement) diff --git a/site/demos/labels.ts b/site/demos/labels.ts index 5777c8947..f814cf76f 100644 --- a/site/demos/labels.ts +++ b/site/demos/labels.ts @@ -3,6 +3,7 @@ */ import { Graph, type GraphEdge, type GraphNode, type LabelPlacement } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { CircularLayout } from '@antv/layout'; const DATA = { nodes: [ @@ -40,7 +41,7 @@ export async function run(container: string | HTMLElement) { autoFit: true, container: el, data: DATA, - layout: { type: 'circular', radius: 180 }, + layout: { type: CircularLayout, radius: 180 }, behaviors: ['zoom-canvas', 'drag-canvas', 'drag-node'], node: { style: { @@ -93,7 +94,7 @@ export async function run(container: string | HTMLElement) { graph.updateOptions({ node: { style: { icon: iconsVisible ? iconForNode : false } } }); }); ui.addChoice('布局', ['circular', 'force'], 'circular', async (value) => { - graph.setLayout(value === 'circular' ? { type: 'circular', radius: 180 } : { type: 'd3-force' }); + graph.setLayout(value === 'circular' ? { type: CircularLayout, radius: 180 } : { type: 'd3-force' }); await graph.layout(); graph.fitView(); }); diff --git a/site/demos/layout-gallery.ts b/site/demos/layout-gallery.ts index eee635c83..a58dbba0f 100644 --- a/site/demos/layout-gallery.ts +++ b/site/demos/layout-gallery.ts @@ -1,25 +1,20 @@ -import { Graph, register, type LayoutConfig } from '@antv/g6-lite'; -import mds from '@antv/g6-lite/layouts/mds'; -import radial from '@antv/g6-lite/layouts/radial'; -import random from '@antv/g6-lite/layouts/random'; +import { Graph, type LayoutConfig } from '@antv/g6-lite'; +import { ConcentricLayout, MDSLayout, RadialLayout, RandomLayout } from '@antv/layout'; import { createDemoPanel } from './demo-panel.ts'; const NODES = Array.from({ length: 18 }, (_, index) => ({ id: String(index), data: { label: String(index) } })); const EDGES = Array.from({ length: 17 }, (_, index) => ({ id: `e-${index}`, source: String(Math.floor(index / 2)), target: String(index + 1) })); const LAYOUTS: Array<{ label: string; config: LayoutConfig }> = [ - { label: 'Radial', config: { type: 'radial', unitRadius: 68, linkDistance: 60 } }, - { label: 'Concentric', config: { type: 'concentric', nodeSize: 28, minNodeSpacing: 18 } }, + { label: 'Radial', config: { type: RadialLayout, unitRadius: 68, linkDistance: 60 } }, + { label: 'Concentric', config: { type: ConcentricLayout, nodeSize: 28, minNodeSpacing: 18 } }, { label: 'ForceAtlas2', config: { type: 'force-atlas2', maxIteration: 60, barnesHut: true, prune: false } }, { label: 'Fruchterman', config: { type: 'fruchterman', maxIteration: 80, gravity: 8, speed: 5 } }, - { label: 'MDS', config: { type: 'mds', linkDistance: 70 } }, - { label: 'Random', config: { type: 'random' } }, + { label: 'MDS', config: { type: MDSLayout, linkDistance: 70 } }, + { label: 'Random', config: { type: RandomLayout } }, ]; export async function run(container: string | HTMLElement) { - register('layout', 'mds', mds); - register('layout', 'radial', radial); - register('layout', 'random', random); const graph = new Graph({ autoFit: true, container, diff --git a/site/demos/layout-worker-cancel.ts b/site/demos/layout-worker-cancel.ts index c6f2fb781..ffb7a17b1 100644 --- a/site/demos/layout-worker-cancel.ts +++ b/site/demos/layout-worker-cancel.ts @@ -1,5 +1,6 @@ import { Graph, type GraphData } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { GridLayout } from '@antv/layout'; function createData(): GraphData { const nodes = Array.from({ length: 240 }, (_, index) => ({ id: String(index) })); @@ -16,7 +17,7 @@ export async function run(container: string | HTMLElement) { autoFit: true, container, data: createData(), - layout: { type: 'grid', rows: 15, cols: 16 }, + layout: { type: GridLayout, rows: 15, cols: 16 }, node: { style: { size: 7 } }, edge: { style: { stroke: '#cbd5e1', opacity: 0.45 } }, behaviors: ['zoom-canvas', 'drag-canvas'], @@ -47,13 +48,13 @@ export async function run(container: string | HTMLElement) { void runLayout({ type: 'force-atlas2', worker: true, maxIteration: 200, barnesHut: true, prune: false }, 'cancel test'); cancelTimer = window.setTimeout(() => { graph.cancelLayout(); - void runLayout({ type: 'grid', rows: 15, cols: 16 }, 'cancelled → grid'); + void runLayout({ type: GridLayout, rows: 15, cols: 16 }, 'cancelled → grid'); }, 20); }); ui.addAction('恢复 Grid', () => { clearTimeout(cancelTimer); graph.cancelLayout(); - void runLayout({ type: 'grid', rows: 15, cols: 16 }, 'grid'); + void runLayout({ type: GridLayout, rows: 15, cols: 16 }, 'grid'); }); ui.setStatus('grid ready'); diff --git a/site/demos/layouts.ts b/site/demos/layouts.ts index bb976eeb9..8b197bce6 100644 --- a/site/demos/layouts.ts +++ b/site/demos/layouts.ts @@ -3,6 +3,9 @@ */ import { Graph } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { CircularLayout } from '@antv/layout'; +import { GridLayout } from '@antv/layout'; +import { DagreLayout } from '@antv/layout'; const data = { nodes: [ @@ -18,10 +21,10 @@ const data = { }; const LAYOUTS = [ - { type: 'circular', radius: 180 }, + { type: CircularLayout, radius: 180 }, { type: 'force', linkDistance: 80 }, - { type: 'grid', rows: 2, cols: 5 }, - { type: 'dagre', rankdir: 'TB', nodesep: 30, ranksep: 40 }, + { type: GridLayout, rows: 2, cols: 5 }, + { type: DagreLayout, rankdir: 'TB', nodesep: 30, ranksep: 40 }, ] as const; export async function run(container: string | HTMLElement) { diff --git a/site/demos/mindmap.ts b/site/demos/mindmap.ts index 440a1203e..a852f1efb 100644 --- a/site/demos/mindmap.ts +++ b/site/demos/mindmap.ts @@ -1,8 +1,8 @@ /** * Demo: 思维导图 (Mind Map) — 台风形成因素 */ -import { Graph, register, type GraphEdge, type GraphNode } from '@antv/g6-lite'; -import mindmap from '@antv/g6-lite/layouts/mindmap'; +import { Graph, type GraphEdge, type GraphNode } from '@antv/g6-lite'; +import { MindmapLayout } from '@antv/layout'; import { attachLabelsCanvas, type GraphInstanceWithTools } from './tooling.ts'; const BRANCH_COLORS = ['#E8684A', '#5B8FF9', '#5AD8A6', '#F6BD16']; @@ -118,7 +118,6 @@ function getEdgeColor(_source: GraphNode, _target: GraphNode, edge: GraphEdge) { } export async function run(container: string | HTMLElement) { - register('layout', 'mindmap', mindmap); const wrapper = typeof container === 'string' ? document.querySelector(container)! as HTMLElement : container; wrapper.style.position = 'relative'; @@ -139,7 +138,7 @@ export async function run(container: string | HTMLElement) { }, }, layout: { - type: 'mindmap', + type: MindmapLayout, direction: 'H', levelGap: 120, // 缩短左右间距 nodeGap: 12, diff --git a/site/demos/neighbor-highlight.ts b/site/demos/neighbor-highlight.ts index 6bb40564d..43ceddf78 100644 --- a/site/demos/neighbor-highlight.ts +++ b/site/demos/neighbor-highlight.ts @@ -1,11 +1,10 @@ /** * Demo: hover a node to highlight its neighbors, or an edge to highlight its endpoints. */ -import { Graph, register } from '@antv/g6-lite'; -import radial from '@antv/g6-lite/layouts/radial'; +import { Graph } from '@antv/g6-lite'; +import { RadialLayout } from '@antv/layout'; export async function run(container: string | HTMLElement) { - register('layout', 'radial', radial); const graph = new Graph({ autoFit: true, container, @@ -30,7 +29,7 @@ export async function run(container: string | HTMLElement) { }, }, edge: { style: { stroke: '#C5CEDB', lineWidth: 2 } }, - layout: { type: 'radial', unitRadius: 120, linkDistance: 100 }, + layout: { type: RadialLayout, unitRadius: 120, linkDistance: 100 }, behaviors: ['zoom-canvas', 'drag-canvas', 'highlight-node', 'highlight-edge'], }); await graph.render(); diff --git a/site/demos/org-chart.ts b/site/demos/org-chart.ts index f14ffe268..f02cc2b5e 100644 --- a/site/demos/org-chart.ts +++ b/site/demos/org-chart.ts @@ -3,6 +3,7 @@ */ import { Graph } from '@antv/g6-lite'; import { attachLabelsCanvas, type GraphInstanceWithTools } from './tooling.ts'; +import { DagreLayout } from '@antv/layout'; const LEVEL_COLORS: Record = { ceo: '#4A7DC0', @@ -60,7 +61,7 @@ export async function run(container: string | HTMLElement) { startArrow: false, }, }, - layout: { type: 'dagre', rankdir: 'TB', nodesep: 50, ranksep: 80, nodeSize: [NODE_W, NODE_H] }, + layout: { type: DagreLayout, rankdir: 'TB', nodesep: 50, ranksep: 80, nodeSize: [NODE_W, NODE_H] }, behaviors: ['zoom-canvas', 'drag-canvas'], theme: 'light', }); diff --git a/site/demos/polyline-routing.ts b/site/demos/polyline-routing.ts index 083f0bcc5..df64d2cdd 100644 --- a/site/demos/polyline-routing.ts +++ b/site/demos/polyline-routing.ts @@ -2,6 +2,7 @@ * Demo: orthogonal polyline edges with rounded corners. */ import { Graph } from '@antv/g6-lite'; +import { DagreLayout } from '@antv/layout'; export async function run(container: string | HTMLElement) { const graph = new Graph({ @@ -45,7 +46,7 @@ export async function run(container: string | HTMLElement) { router: 'orth', }, }, - layout: { type: 'dagre', rankdir: 'LR', nodesep: 28, ranksep: 72, nodeSize: [88, 36] }, + layout: { type: DagreLayout, rankdir: 'LR', nodesep: 28, ranksep: 72, nodeSize: [88, 36] }, behaviors: ['zoom-canvas', 'drag-canvas'], }); await graph.render(); diff --git a/site/demos/radial-tree.ts b/site/demos/radial-tree.ts index ea17275a1..c0c362de6 100644 --- a/site/demos/radial-tree.ts +++ b/site/demos/radial-tree.ts @@ -1,8 +1,8 @@ /** * Demo: a topic tree laid out with the radial algorithm. */ -import { Graph, register } from '@antv/g6-lite'; -import radial from '@antv/g6-lite/layouts/radial'; +import { Graph } from '@antv/g6-lite'; +import { RadialLayout } from '@antv/layout'; const TREE: Array<[string, string, string?]> = [ ['root', '产品'], @@ -18,7 +18,6 @@ const TREE: Array<[string, string, string?]> = [ ]; export async function run(container: string | HTMLElement) { - register('layout', 'radial', radial); const graph = new Graph({ autoFit: { padding: 32 }, container, @@ -35,7 +34,7 @@ export async function run(container: string | HTMLElement) { }, }, edge: { style: { stroke: '#C5CEDB', lineWidth: 1.5 } }, - layout: { type: 'radial', unitRadius: 110, linkDistance: 90, nodeSize: 28 }, + layout: { type: RadialLayout, unitRadius: 110, linkDistance: 90, nodeSize: 28 }, behaviors: ['zoom-canvas', 'drag-canvas'], }); await graph.render(); diff --git a/site/demos/search-focus.ts b/site/demos/search-focus.ts index cb43ebd23..003670495 100644 --- a/site/demos/search-focus.ts +++ b/site/demos/search-focus.ts @@ -3,6 +3,7 @@ */ import { Graph } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { CircularLayout } from '@antv/layout'; const PEOPLE = [ 'Alice', @@ -44,7 +45,7 @@ export async function run(container: string | HTMLElement) { labelPlacement: 'bottom', }, }, - layout: { type: 'circular', radius: 180 }, + layout: { type: CircularLayout, radius: 180 }, behaviors: ['zoom-canvas', 'drag-canvas'], }); await graph.render(); diff --git a/site/demos/set-data.ts b/site/demos/set-data.ts index 55f48a38c..03901885b 100644 --- a/site/demos/set-data.ts +++ b/site/demos/set-data.ts @@ -4,6 +4,8 @@ */ import { Graph, type FitViewOptions } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { CircularLayout } from '@antv/layout'; +import { GridLayout } from '@antv/layout'; const DATA_A = { nodes: [ @@ -57,7 +59,7 @@ export async function run(container: string | HTMLElement) { autoFit: fit, container: el, data: DATA_A, - layout: { type: 'circular', radius: 120 }, + layout: { type: CircularLayout, radius: 120 }, behaviors: [ 'zoom-canvas', 'drag-canvas', @@ -87,9 +89,9 @@ export async function run(container: string | HTMLElement) { ui.addChoice('布局', ['circular', 'grid', 'force'], 'circular', async (value) => { log(`setLayout(${value}) + layout()`); - if (value === 'grid') graph.setLayout({ type: 'grid' }); + if (value === 'grid') graph.setLayout({ type: GridLayout }); else if (value === 'force') graph.setLayout({ type: 'd3-force' }); - else graph.setLayout({ type: 'circular', radius: 150 }); + else graph.setLayout({ type: CircularLayout, radius: 150 }); await graph.layout(); graph.fitView(fit); log(`${value} done`); diff --git a/site/demos/shapes.ts b/site/demos/shapes.ts index 2bee7b215..16497b1a4 100644 --- a/site/demos/shapes.ts +++ b/site/demos/shapes.ts @@ -2,6 +2,7 @@ * Demo: 混合图形 — 矩形节点 + 弧线边 + 粗线边 */ import { Graph } from '@antv/g6-lite'; +import { CircularLayout } from '@antv/layout'; export async function run(container: string | HTMLElement) { // --- Rect nodes with arc edges --- @@ -50,7 +51,7 @@ export async function run(container: string | HTMLElement) { curvature: 0.25, }, }, - layout: { type: 'circular', radius: 48 }, + layout: { type: CircularLayout, radius: 48 }, behaviors: ['zoom-canvas', 'drag-canvas', 'highlight-node', 'highlight-edge'], theme: 'light', }); @@ -92,7 +93,7 @@ export async function run(container: string | HTMLElement) { lineWidth: 3, }, }, - layout: { type: 'circular', radius: 48 }, + layout: { type: CircularLayout, radius: 48 }, behaviors: ['zoom-canvas', 'drag-canvas', 'highlight-node', 'highlight-edge'], theme: 'light', }); diff --git a/site/demos/theme.ts b/site/demos/theme.ts index 04b1e4a98..3b5d21fb1 100644 --- a/site/demos/theme.ts +++ b/site/demos/theme.ts @@ -3,6 +3,7 @@ */ import { Graph } from '@antv/g6-lite'; import { createDemoPanel } from './demo-panel.ts'; +import { CircularLayout } from '@antv/layout'; const data = { nodes: [{ id: '0' }, { id: '1' }, { id: '2' }, { id: '3' }, { id: '4' }], @@ -35,7 +36,7 @@ export async function run(container: string | HTMLElement) { data, node: { type: 'circle', style: { size: 14 } }, edge: { type: 'line' }, - layout: { type: 'circular', radius: 120 }, + layout: { type: CircularLayout, radius: 120 }, behaviors: ['zoom-canvas', 'drag-canvas'], theme: themeOption, }); diff --git a/site/next.config.mjs b/site/next.config.mjs index 2ed5335dd..f50acada8 100644 --- a/site/next.config.mjs +++ b/site/next.config.mjs @@ -24,12 +24,6 @@ const config = { './layoutWorker.js': '../src/layout/layoutWorker.ts', // Examples live in site/demos and import the library by package name, so // point that name at the TypeScript source instead of the built dist. - '@antv/g6-lite/layouts/mds': '../src/layout/optional/mds.ts', - '@antv/g6-lite/layouts/radial': '../src/layout/optional/radial.ts', - '@antv/g6-lite/layouts/indented': '../src/layout/optional/indented.ts', - '@antv/g6-lite/layouts/mindmap': '../src/layout/optional/mindmap.ts', - '@antv/g6-lite/layouts/fishbone': '../src/layout/optional/fishbone.ts', - '@antv/g6-lite/layouts/random': '../src/layout/optional/random.ts', '@antv/g6-lite': '../src/index.ts', }, }, diff --git a/site/scripts/check-example-documentation.mjs b/site/scripts/check-example-documentation.mjs index 261883908..ca7c3ac94 100644 --- a/site/scripts/check-example-documentation.mjs +++ b/site/scripts/check-example-documentation.mjs @@ -57,5 +57,24 @@ for (const example of definitions) { } matrix.push({ id: example.id, file: example.file, category: example.category, bilingualFields: true, documentation: docs.map((d) => d.href), reverseLinks: true, htmlAndMarkdown: true, visualScope: 'Existing runtime; representative cases are browser-checked separately' }); } + +// Check the actual production output, where class names can be minified. +const showcaseLayouts = { + 'org-chart': 'DagreLayout', + mindmap: 'MindmapLayout', + fishbone: 'FishboneLayout', +}; +for (const [id, layout] of Object.entries(showcaseLayouts)) { + for (const locale of ['zh', 'en']) { + for (const theme of ['light', 'dark']) { + const file = path.join(root, 'out/showcase', locale, id, theme, 'data.json'); + const { source } = JSON.parse(fs.readFileSync(file, 'utf8')); + if (!source.includes(`import { ${layout} } from '@antv/layout';`) || !source.includes(`"type": ${layout},`)) { + throw new Error(`Homepage example ${id} (${locale}, ${theme}) must use ${layout}`); + } + } + } +} + if (process.argv.includes('--json')) console.log(JSON.stringify(matrix, null, 2)); -else console.log(matrix.length + ' example identities, bilingual descriptions, links and exact editor/Markdown/LLM runtime sources verified.'); +else console.log(matrix.length + ' examples and homepage layout imports verified in the static output.'); diff --git a/site/src/components/homepage/bundle-measurement.json b/site/src/components/homepage/bundle-measurement.json index 25c410e02..7b75ba39e 100644 --- a/site/src/components/homepage/bundle-measurement.json +++ b/site/src/components/homepage/bundle-measurement.json @@ -1,12 +1,9 @@ { - "version": "0.1.0", - "measuredAt": "2026-11-24", - "gzipBytes": 150304, + "sourceCommit": "34bf8b6d8bdac3ee97f339e2f06314c279d7c865", + "measuredAt": "2026-11-30", + "gzipBytes": 118503, "unitBytes": 1000, "entry": "dist/g6-lite.min.js", - "compression": "Gzip -9", - "source": "https://registry.npmjs.org/@antv/g6-lite/-/g6-lite-0.1.0.tgz", - "integrity": "sha512-o3NWidrTaTEAA4RiNd56NDQOgTizoPtHwCQl6aoxipkJYk2pwnPI7d34E8LcC4UX0u6DgeyIYG8OclVBlWWI4Q==", - "gitHead": "448fc17d278693cb6bbe1886d78d43b2f4bab407", - "command": "gzip -9 -c package/dist/g6-lite.min.js | wc -c" + "compression": "Gzip", + "command": "npm run size" } diff --git a/site/src/components/homepage/content.ts b/site/src/components/homepage/content.ts index 4d86b13fe..437a2bf6c 100644 --- a/site/src/components/homepage/content.ts +++ b/site/src/components/homepage/content.ts @@ -28,7 +28,8 @@ const zh = { loadTitle: '轻量加载', loadDescription: '核心入口与布局算法分离,按需加载所选布局,降低初始化阶段的加载开销。', measured: 'UMD 包体积', - umdScope: '含内置布局和依赖', + umdScope: '含 4 种内置力导向布局和依赖', + measurementSource: '源码构建', sizeLink: '查看体积测量口径', interactionTitle: '高性能渲染', interactionDescription: @@ -182,7 +183,8 @@ const en: typeof zh = { loadTitle: 'Lightweight loading', loadDescription: 'Layout algorithms load on demand, keeping the initial core bundle small.', measured: 'UMD bundle size', - umdScope: 'Includes built-in layouts and dependencies', + umdScope: 'Includes four built-in force layouts and dependencies', + measurementSource: 'Source build', sizeLink: 'How we measure bundle size', interactionTitle: 'High-performance rendering', interactionDescription: diff --git a/site/src/components/homepage/example-showcase.tsx b/site/src/components/homepage/example-showcase.tsx index f5c8a0051..ebac5dd13 100644 --- a/site/src/components/homepage/example-showcase.tsx +++ b/site/src/components/homepage/example-showcase.tsx @@ -10,19 +10,9 @@ import { CopyPrompt } from '@/components/ai-skill/copy-prompt'; import { localizedPath, type Locale } from '@/lib/i18n'; import type { HomepageShowcaseCopy } from './content'; import styles from './homepage.module.css'; -import { - getShowcaseOptionalLayout, - showcaseIds, - type ShowcaseCode, - type ShowcaseId, -} from './showcase-catalog'; +import { showcaseIds, type ShowcaseCode, type ShowcaseId } from './showcase-catalog'; import { loadShowcaseCode } from './showcase-code-loader'; -const layoutLoaders = { - mindmap: () => import('@antv/g6-lite/layouts/mindmap'), - fishbone: () => import('@antv/g6-lite/layouts/fishbone'), -}; - const subscribeNoop = () => () => {}; export function ExampleShowcase({ @@ -80,14 +70,11 @@ export function ExampleShowcase({ const start = async () => { setStatus('loading'); try { - const layout = getShowcaseOptionalLayout(active); - const [{ Graph, register }, runtime, layoutModule] = await Promise.all([ + const [{ Graph }, runtime] = await Promise.all([ import('../../../../src/index.ts'), import('./showcase-examples'), - layout && layoutLoaders[layout](), ]); if (disposed) return; - if (layout && layoutModule) register('layout', layout, layoutModule.default); graph = new Graph({ container: mount, ...runtime.showcaseOptions(active, locale, dark) }); graphRef.current = graph; await graph.render(); diff --git a/site/src/components/homepage/homepage.tsx b/site/src/components/homepage/homepage.tsx index 162bef627..b806b1a2a 100644 --- a/site/src/components/homepage/homepage.tsx +++ b/site/src/components/homepage/homepage.tsx @@ -101,13 +101,14 @@ export async function Homepage({ locale = 'zh' }: { locale?: Locale }) { {bundleMeasurement.entry} - v{bundleMeasurement.version} · {bundleMeasurement.measuredAt} + {copy.performance.measurementSource} · {bundleMeasurement.sourceCommit.slice(0, 7)} ·{' '} + {bundleMeasurement.measuredAt} {copy.performance.sizeLink}