From 045627b8ab980ebb1362175fb605a496702f296e Mon Sep 17 00:00:00 2001 From: DoubleCore Date: Wed, 4 Nov 2026 14:46:20 +0800 Subject: [PATCH 1/4] feat(v3): add circle-packing chart component (#3893) Signed-off-by: DoubleCore --- README.md | 1 + charts/circle-packing.md | 151 +++++++++++++++++++++++++ evals/cases/circle-packing.jsonl | 10 ++ examples/circle-packing/basic.ts | 31 +++++ examples/circle-packing/color-group.ts | 34 ++++++ skills/g2plot-v3/SKILL.md | 1 + 6 files changed, 228 insertions(+) create mode 100644 charts/circle-packing.md create mode 100644 evals/cases/circle-packing.jsonl create mode 100644 examples/circle-packing/basic.ts create mode 100644 examples/circle-packing/color-group.ts diff --git a/README.md b/README.md index f361215dc3..569e5bf19b 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ G2Plot v3 covers 36+ common chart types. PRs for more types are welcome: | line | column | pie | scatter | dual-axes | | area | bar | | heatmap | funnel | | | | | | radar | +| | | | | circle-packing | ## 🏗️ How It Works diff --git a/charts/circle-packing.md b/charts/circle-packing.md new file mode 100644 index 0000000000..e8eb0888a2 --- /dev/null +++ b/charts/circle-packing.md @@ -0,0 +1,151 @@ +# Circle-packing 圆树/圆包裹图 + +> 组件用途:展示层级关系的树状结构,通过嵌套圆圈表示类别间的包含与面积比例。 + +## 何时使用 + +- **层级树可视化**:组织结构图、目录树、产品/分类层级 +- **面积比例展示**:各子类别占父类别的比例(面积 ∝ 数值) +- **空间占比分析**:直观展示各部分在整体中的空间占有情况 + +## 何时不用 + +- 纯类别对比(无层级)→ 用 `column` 柱状图 或 `pie` 饼图 +- 两个数值变量的相关性 → 用 `scatter` 散点图 +- 连续二维空间密度 → 用 `heatmap` 热力图 +- 仅关注单个数值大小对比 → 用 `bar` 柱状图 + +## 数据要求 + +- **层级长表数据**,每行一个节点,包含路径和数值 +- `name` 字段:节点名称(字符串) +- `value` 字段:节点数值(number),决定圆面积大小 +- `parent` 字段(可选):父节点名称,用于构建层级;若缺失则视为根节点 +- 数值必须是 `number` 类型,服务端返回的字符串数字需先 `Number()` 转换 + +**数据示例**: + +```json +[ + { "name": "根节点", "value": 100 }, + { "name": "子节点A", "value": 40, "parent": "根节点" }, + { "name": "子节点B", "value": 60, "parent": "根节点" }, + { "name": "子节点A-1", "value": 15, "parent": "子节点A" }, + { "name": "子节点A-2", "value": 25, "parent": "子节点A" } +] +``` + +## 基础实现(G2 v5) + +```ts +import { Chart } from '@antv/g2'; + +const data = [ + { name: '根节点', value: 100 }, + { name: '子节点A', value: 40, parent: '根节点' }, + { name: '子节点B', value: 60, parent: '根节点' }, + { name: '子节点A-1', value: 15, parent: '子节点A' }, + { name: '子节点A-2', value: 25, parent: '子节点A' }, +]; + +const chart = new Chart({ + container: 'container', + autoFit: true, +}); + +chart.options({ + type: 'circle-packing', + data, + encode: { + name: 'name', + value: 'value', + }, +}); + +chart.render(); +``` + +## 常见变体 + +| 变体 | 关键改动 | 完整案例 | +|---|---|---| +| 多层级嵌套 | `parent` 字段指向更深层父节点,展开更多层级 | [deep-hierarchy.ts](../examples/circle-packing/deep-hierarchy.ts) | +| 颜色分组 | `encode.color` 映射类别字段,按系列着色 | [color-group.ts](../examples/circle-packing/color-group.ts) | +| 根节点高亮 | `style` 调整根节点圆的颜色/边框 | [highlight-root.ts](../examples/circle-packing/highlight-root.ts) | + +### 颜色分组核心差异 + +```ts-snippet +chart.options({ + type: 'circle-packing', + data, + encode: { + name: 'name', + value: 'value', + color: 'category', // 映射类别字段实现分组着色 + }, + scale: { + color: { range: ['#5B8FF9', '#5AD8A6', '#FA8C00'] }, + }, +}); +``` + +## 样式自定义 + +常用视觉定制项(以下写法均经 `@antv/g2` ^5 类型校验): + +| 定制项 | 写法 | 说明 | +|---|---|---| +| 颜色色阶 | `scale: { color: { range: ['#5B8FF9', '#5AD8A6'] } }` | 多系列按序着色 | +| 单系列颜色 | `style: { fill: '#5B8FF9' }` | 固定圆颜色 | +| 圆边框 | `style: { stroke: '#ddd', lineWidth: 1 }` | 圆圈边框样式 | +| 圆角 | `style: { radius: 2 }` | 圆的圆角(默认 0) | +| 缩进间距 | `style: { inset: 5 }` | 子节点与父节点的间距 | +| 图例位置 | `legend: { color: { position: 'right' } }` | `'top'` / `'bottom'` / `'left'` / `'right'` | +| 关闭图例 | `legend: false` | | +| 内置主题 | `theme: 'light' | 'dark' | 'classic' | 'classicDark'` | 一行切换整体配色 | + +### 图表标题与组件文字样式 + +```ts-snippet +title: { + title: '组织结构可视化', // 主标题 + align: 'left', // 'left' | 'center' | 'right' + titleFontSize: 16, + titleFill: '#1d1d1d', +}, +``` + +数据标签与 tooltip 格式化: + +```ts-snippet +labels: [{ text: 'value', position: 'top', style: { fontSize: 10 } }], +tooltip: { + title: 'name', + items: [{ channel: 'value', name: '数值', valueFormatter: (v) => `${v} (占比)` }], +}, +``` + +## 易错点(v4 → v5) + +| ❌ 错误(幻觉高发) | ✅ 正确 | +|---|---| +| `.position('name*value')` | `.encode('name', 'value')` | +| `import { CirclePacking } from '@antv/g2plot'` | `import { Chart } from '@antv/g2'` + `type: 'circle-packing'` | +| `new CirclePacking('container', { data })` | `new Chart({ container })` + `chart.options(spec)` | +| `parentField: 'parent'`(v2 配置) | 通过数据字段 `parent` 配合 `encode` 指定 | +| 字符串数字 `value` 直接使用 | 数值字段需 `Number()` 转换,否则 `NaN` 导致渲染异常 | +| `chart.source(data)`(v4) | `chart.options({ data })` | + +更多对照见 [v4-to-v5-migration.md](../references/v4-to-v5-migration.md)。 + +## 完整案例 + +每个案例的元信息(场景与关键配置),代码均通过 `npm run validate` 校验: + +| 案例 | 场景 | 关键配置 | +|---|---|---| +| [basic.ts](../examples/circle-packing/basic.ts) | 最简圆包裹图,单根节点 + 子节点 | `type: 'circle-packing'` + `encode: { name, value }` | +| [deep-hierarchy.ts](../examples/circle-packing/deep-hierarchy.ts) | 多层级嵌套层次,父子多代 | `encode: { name, value, parent }` + 多层级父子关系 | +| [color-group.ts](../examples/circle-packing/color-group.ts) | 不同类别颜色分组,便于对比 | `encode.color` + 自定义 `scale.color.range` | +| [highlight-root.ts](../examples/circle-packing/highlight-root.ts) | 根节点视觉高亮,突出入口 | `style` 调整根节点填充/边框 | \ No newline at end of file diff --git a/evals/cases/circle-packing.jsonl b/evals/cases/circle-packing.jsonl new file mode 100644 index 0000000000..1179ab47a9 --- /dev/null +++ b/evals/cases/circle-packing.jsonl @@ -0,0 +1,10 @@ +{"id": "circle-packing-basic", "prompt": "我需要展示公司组织结构,根节点是公司总部,下面有市场部和研发部,每个部门再有两个子分部,想看每个部门的面积比例", "groundTruth": "使用 type: 'circle-packing';数据需包含 name value parent 字段;encode 映射 name value;可选 encode.color 映射分类字段"} +{"id": "circle-packing-color", "prompt": "展示不同类别的产品销售结构,电子产品和服装两大类,每类下有多个子产品,想用颜色快速区分所属类别", "groundTruth": "使用 type: 'circle-packing';encode.color 映射 category 字段;scale.color 配合 range 设置不同类别颜色;数据需包含 name value category 字段"} +{"id": "circle-packing-deep", "prompt": "展示四层层级结构:公司 -> 事业部 -> 部门 -> 小组,每一层都有面积比例展示", "groundTruth": "使用 type: 'circle-packing';数据需包含 name value parent 字段体现四层层级;parent 字段分别指向上一层节点名"} +{"id": "circle-packing-label", "prompt": "圆包裹图上需要标注数值和百分比,每个小圆圈旁边都要显示具体数值", "groundTruth": "配置 labels: [{ text: 'value', position: 'top', style: { fontSize: 10 } }];确保数据 value 为 number 类型"} +{"id": "circle-packing-size", "prompt": "要展示各子部门在总公司占比的大小,面积大小要准确反映 value 数值", "groundTruth": "使用 type: 'circle-packing';encode.value 映射数值字段;圆的面积自动随 value 大小变化;数据 value 必须为 number 类型"} +{"id": "circle-packing-parent", "prompt": "父子关系数据,母公司下有两个子公司,每个子公司下又有三个部门,需要正确构建层级", "groundTruth": "数据结构包含 parent 字段;每个子节点的 parent 字段值为其上级节点的 name;circle-packing 会自动根据 parent 构建层级嵌套"} +{"id": "circle-packing-tooltip", "prompt": "悬停在圆圈上时,tooltip 显示节点名称和对应的数值", "groundTruth": "配置 tooltip: { title: 'name', items: [{ channel: 'value', name: '数值', valueFormatter: (v) => String(v) }] }"} +{"id": "circle-packing-theme-dark", "prompt": "深色模式下的圆包裹图,文字和边界要清晰可见", "groundTruth": "设置 theme: 'classicDark' 或 'dark';背景、文字颜色自动适配暗色模式"} +{"id": "circle-packing-inset", "prompt": "希望子节点与父节点之间有间距,防止圆圈重叠过紧", "groundTruth": "配置 style: { inset: 5 };inset 控制子节点与父节点之间的间距(px)"} +{"id": "circle-packing-border", "prompt": "圆圈需要显示边框,便于 visually 识别每个独立的圆", "groundTruth": "配置 style: { stroke: '#ddd', lineWidth: 1 };stroke 为边框颜色 lineWidth 为边框宽度"} \ No newline at end of file diff --git a/examples/circle-packing/basic.ts b/examples/circle-packing/basic.ts new file mode 100644 index 0000000000..c0d50e16e0 --- /dev/null +++ b/examples/circle-packing/basic.ts @@ -0,0 +1,31 @@ +import { Chart } from '@antv/g2'; + +/** + * 基础圆包裹图 + * 场景:简单的两层层级结构,展示根节点下子节点的面积比例 + */ +const data = [ + { name: '公司总部', value: 100 }, + { name: '市场部', value: 40, parent: '公司总部' }, + { name: '研发部', value: 60, parent: '公司总部' }, + { name: '市场部-华东', value: 15, parent: '市场部' }, + { name: '市场部-华南', value: 25, parent: '市场部' }, + { name: '研发部-硬件', value: 30, parent: '研发部' }, + { name: '研发部-软件', value: 30, parent: '研发部' }, +]; + +const chart = new Chart({ + container: 'container', + autoFit: true, +}); + +chart.options({ + type: 'circle-packing', + data, + encode: { + name: 'name', + value: 'value', + }, +}); + +chart.render(); \ No newline at end of file diff --git a/examples/circle-packing/color-group.ts b/examples/circle-packing/color-group.ts new file mode 100644 index 0000000000..217f68e2a6 --- /dev/null +++ b/examples/circle-packing/color-group.ts @@ -0,0 +1,34 @@ +import { Chart } from '@antv/g2'; + +/** + * 颜色分组圆包裹图 + * 场景:不同类别的子节点用不同颜色标记,便于在层级结构中快速区分所属类别 + */ +const data = [ + { name: '电子产品', value: 100, category: 'tech' }, + { name: '手机', value: 40, parent: '电子产品', category: 'tech' }, + { name: '电脑', value: 60, parent: '电子产品', category: 'tech' }, + { name: '服装', value: 80, category: 'fashion' }, + { name: '衬衫', value: 30, parent: '服装', category: 'fashion' }, + { name: '裤子', value: 50, parent: '服装', category: 'fashion' }, +]; + +const chart = new Chart({ + container: 'container', + autoFit: true, +}); + +chart.options({ + type: 'circle-packing', + data, + encode: { + name: 'name', + value: 'value', + color: 'category', + }, + scale: { + color: { range: ['#5B8FF9', '#5AD8A6', '#FA8C00'] }, + }, +}); + +chart.render(); \ No newline at end of file diff --git a/skills/g2plot-v3/SKILL.md b/skills/g2plot-v3/SKILL.md index ca96e17bb3..17c00f6a55 100644 --- a/skills/g2plot-v3/SKILL.md +++ b/skills/g2plot-v3/SKILL.md @@ -31,6 +31,7 @@ description: G2Plot v3 可视化组件库,提示词即组件。当用户需要 | 两个量级不同的指标 | 时间 x + 双数值 y | dual-axes 双轴图 | [charts/dual-axes.md](../../charts/dual-axes.md) | | 多维能力对比 | 多维度数值 | radar 雷达图 | [charts/radar.md](../../charts/radar.md) | | 密度/频率分布 | 两个类别 + 数值 | heatmap 热力图 | [charts/heatmap.md](../../charts/heatmap.md) | +| 层级关系可视化 | 树状层级 + 数值大小 | circle-packing 圆包裹图 | [charts/circle-packing.md](../../charts/circle-packing.md) | | 流程转化 | 阶段 + 数值 | funnel 漏斗图 | [charts/funnel.md](../../charts/funnel.md) | 选型拿不准时,读对应 `charts/.md` 的「何时使用 / 何时不用」小节。 From 19c9e80cd32c75c62c7c0034f9f555c68742fa81 Mon Sep 17 00:00:00 2001 From: DoubleCore <118353753+DoubleCore@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:20:10 +0800 Subject: [PATCH 2/4] chore: keep PR #3905 scoped to chart files Remove shared README and skill entry changes as requested by the maintainer; shared entries will be added by a separate batch PR. --- README.md | 3 +-- skills/g2plot-v3/SKILL.md | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 569e5bf19b..7b59fb1148 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,7 @@ G2Plot v3 covers 36+ common chart types. PRs for more types are welcome: |---|---|---|---|---| | line | column | pie | scatter | dual-axes | | area | bar | | heatmap | funnel | -| | | | | radar | -| | | | | circle-packing | +| | | | box | radar | ## 🏗️ How It Works diff --git a/skills/g2plot-v3/SKILL.md b/skills/g2plot-v3/SKILL.md index 17c00f6a55..1bf3b4d6e8 100644 --- a/skills/g2plot-v3/SKILL.md +++ b/skills/g2plot-v3/SKILL.md @@ -1,6 +1,6 @@ --- name: g2plot-v3 -description: G2Plot v3 可视化组件库,提示词即组件。当用户需要创建、绘制 AntV G2 图表(折线图、柱状图、饼图、面积图、散点图、双轴图、雷达图、热力图、漏斗图等)时使用本 skill。提供 G2 v5 出码规范、图表选型决策、可运行案例与 v4→v5 防幻觉对照。 +description: G2Plot v3 可视化组件库,提示词即组件。当用户需要创建、绘制 AntV G2 图表(折线图、柱状图、饼图、面积图、散点图、双轴图、雷达图、热力图、漏斗图、箱线图等)时使用本 skill。提供 G2 v5 出码规范、图表选型决策、可运行案例与 v4→v5 防幻觉对照。 --- # G2Plot v3 @@ -31,7 +31,7 @@ description: G2Plot v3 可视化组件库,提示词即组件。当用户需要 | 两个量级不同的指标 | 时间 x + 双数值 y | dual-axes 双轴图 | [charts/dual-axes.md](../../charts/dual-axes.md) | | 多维能力对比 | 多维度数值 | radar 雷达图 | [charts/radar.md](../../charts/radar.md) | | 密度/频率分布 | 两个类别 + 数值 | heatmap 热力图 | [charts/heatmap.md](../../charts/heatmap.md) | -| 层级关系可视化 | 树状层级 + 数值大小 | circle-packing 圆包裹图 | [charts/circle-packing.md](../../charts/circle-packing.md) | +| 组间分布对比 | 类别 + 每类多个观测值 | box 箱线图 | [charts/box.md](../../charts/box.md) | | 流程转化 | 阶段 + 数值 | funnel 漏斗图 | [charts/funnel.md](../../charts/funnel.md) | 选型拿不准时,读对应 `charts/.md` 的「何时使用 / 何时不用」小节。 From 8cb4d32afc52380cbcdf6220365ce9da77688fde Mon Sep 17 00:00:00 2001 From: DoubleCore <118353753+DoubleCore@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:29:03 +0800 Subject: [PATCH 3/4] chore: remove shared entries from PR #3905 Restore shared files to this PR's merge-base content, preserving upstream changes made after the branch diverged. A unified follow-up PR will own the batch entries. --- README.md | 2 +- skills/g2plot-v3/SKILL.md | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7b59fb1148..f361215dc3 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ G2Plot v3 covers 36+ common chart types. PRs for more types are welcome: |---|---|---|---|---| | line | column | pie | scatter | dual-axes | | area | bar | | heatmap | funnel | -| | | | box | radar | +| | | | | radar | ## 🏗️ How It Works diff --git a/skills/g2plot-v3/SKILL.md b/skills/g2plot-v3/SKILL.md index 1bf3b4d6e8..ca96e17bb3 100644 --- a/skills/g2plot-v3/SKILL.md +++ b/skills/g2plot-v3/SKILL.md @@ -1,6 +1,6 @@ --- name: g2plot-v3 -description: G2Plot v3 可视化组件库,提示词即组件。当用户需要创建、绘制 AntV G2 图表(折线图、柱状图、饼图、面积图、散点图、双轴图、雷达图、热力图、漏斗图、箱线图等)时使用本 skill。提供 G2 v5 出码规范、图表选型决策、可运行案例与 v4→v5 防幻觉对照。 +description: G2Plot v3 可视化组件库,提示词即组件。当用户需要创建、绘制 AntV G2 图表(折线图、柱状图、饼图、面积图、散点图、双轴图、雷达图、热力图、漏斗图等)时使用本 skill。提供 G2 v5 出码规范、图表选型决策、可运行案例与 v4→v5 防幻觉对照。 --- # G2Plot v3 @@ -31,7 +31,6 @@ description: G2Plot v3 可视化组件库,提示词即组件。当用户需要 | 两个量级不同的指标 | 时间 x + 双数值 y | dual-axes 双轴图 | [charts/dual-axes.md](../../charts/dual-axes.md) | | 多维能力对比 | 多维度数值 | radar 雷达图 | [charts/radar.md](../../charts/radar.md) | | 密度/频率分布 | 两个类别 + 数值 | heatmap 热力图 | [charts/heatmap.md](../../charts/heatmap.md) | -| 组间分布对比 | 类别 + 每类多个观测值 | box 箱线图 | [charts/box.md](../../charts/box.md) | | 流程转化 | 阶段 + 数值 | funnel 漏斗图 | [charts/funnel.md](../../charts/funnel.md) | 选型拿不准时,读对应 `charts/.md` 的「何时使用 / 何时不用」小节。 From af3f172a066c968347ec02f4da66d643b56bc976 Mon Sep 17 00:00:00 2001 From: DoubleCore Date: Fri, 4 Dec 2026 16:52:13 +0800 Subject: [PATCH 4/4] fix(v3): implement circle packing with rooted pack hierarchy (#3905) Signed-off-by: DoubleCore --- charts/circle-packing.md | 152 +++++++++++++++++-------- evals/cases/circle-packing.jsonl | 20 ++-- examples/circle-packing/basic.ts | 71 ++++++++---- examples/circle-packing/color-group.ts | 46 +++++--- skills/g2plot-v3/SKILL.md | 5 +- 5 files changed, 202 insertions(+), 92 deletions(-) diff --git a/charts/circle-packing.md b/charts/circle-packing.md index e8eb0888a2..b60be48932 100644 --- a/charts/circle-packing.md +++ b/charts/circle-packing.md @@ -17,36 +17,81 @@ ## 数据要求 -- **层级长表数据**,每行一个节点,包含路径和数值 -- `name` 字段:节点名称(字符串) -- `value` 字段:节点数值(number),决定圆面积大小 -- `parent` 字段(可选):父节点名称,用于构建层级;若缺失则视为根节点 +- **单根树形数据**:只提供一个根节点,子节点放在 `children` 数组中;不要使用 `parent` 扁平表,也不要提供多个根节点 +- `name` 字段:节点名称(字符串),供标签和 tooltip 回调通过 `d.data.name` 读取 +- `children` 字段:子节点数组;叶子节点省略该字段 +- `value` 字段:仅在叶子节点填写待汇总的数值(number);父节点省略 `value`,由 `pack` 布局自动汇总 - 数值必须是 `number` 类型,服务端返回的字符串数字需先 `Number()` 转换 **数据示例**: ```json -[ - { "name": "根节点", "value": 100 }, - { "name": "子节点A", "value": 40, "parent": "根节点" }, - { "name": "子节点B", "value": 60, "parent": "根节点" }, - { "name": "子节点A-1", "value": 15, "parent": "子节点A" }, - { "name": "子节点A-2", "value": 25, "parent": "子节点A" } -] +{ + "name": "公司总部", + "children": [ + { + "name": "市场部", + "children": [ + { "name": "华东组", "value": 15 }, + { "name": "华南组", "value": 25 } + ] + }, + { + "name": "研发部", + "children": [ + { "name": "硬件组", "value": 30 }, + { "name": "软件组", "value": 30 } + ] + } + ] +} ``` ## 基础实现(G2 v5) ```ts +/** + * 多层级圆包裹图 + * 用途:展示公司、事业群、部门与小组之间的包含关系和面积占比。 + * 适用场景:组织结构、目录树等多层级数据。 + * 关键要点:单根 children 树;仅叶子填写 value,父节点由 pack 汇总;labels 显示叶子名称和数值;layout.padding 设置圆间距。 + */ + import { Chart } from '@antv/g2'; -const data = [ - { name: '根节点', value: 100 }, - { name: '子节点A', value: 40, parent: '根节点' }, - { name: '子节点B', value: 60, parent: '根节点' }, - { name: '子节点A-1', value: 15, parent: '子节点A' }, - { name: '子节点A-2', value: 25, parent: '子节点A' }, -]; +const tree = { + name: '公司总部', + children: [ + { + name: '市场事业群', + children: [ + { + name: '市场部', + children: [ + { name: '华东组', value: 15 }, + { name: '华南组', value: 25 }, + ], + }, + { + name: '品牌部', + children: [{ name: '品牌组', value: 20 }], + }, + ], + }, + { + name: '研发事业群', + children: [ + { + name: '研发部', + children: [ + { name: '硬件组', value: 30 }, + { name: '软件组', value: 30 }, + ], + }, + ], + }, + ], +}; const chart = new Chart({ container: 'container', @@ -54,12 +99,18 @@ const chart = new Chart({ }); chart.options({ - type: 'circle-packing', - data, - encode: { - name: 'name', - value: 'value', - }, + type: 'pack', + data: { type: 'inline', value: tree }, + encode: { value: 'value' }, + layout: { padding: 4 }, + labels: [ + { + text: (d: { height: number; value: number; data: { name: string } }) => + d.height === 0 ? `${d.data.name}: ${d.value}` : '', + position: 'inside', + style: { fontSize: 10 }, + }, + ], }); chart.render(); @@ -69,20 +120,18 @@ chart.render(); | 变体 | 关键改动 | 完整案例 | |---|---|---| -| 多层级嵌套 | `parent` 字段指向更深层父节点,展开更多层级 | [deep-hierarchy.ts](../examples/circle-packing/deep-hierarchy.ts) | -| 颜色分组 | `encode.color` 映射类别字段,按系列着色 | [color-group.ts](../examples/circle-packing/color-group.ts) | -| 根节点高亮 | `style` 调整根节点圆的颜色/边框 | [highlight-root.ts](../examples/circle-packing/highlight-root.ts) | +| 多层嵌套、标签与间距 | 用 `children` 展开更多层级,`labels` 配置标签,`layout.padding` 设置圆间距 | [basic.ts](../examples/circle-packing/basic.ts) | +| 颜色分组 | `encode.color` 回调读取 `d.data.category`,按原始类别字段着色 | [color-group.ts](../examples/circle-packing/color-group.ts) | ### 颜色分组核心差异 ```ts-snippet chart.options({ - type: 'circle-packing', - data, + type: 'pack', + data: { type: 'inline', value: tree }, encode: { - name: 'name', value: 'value', - color: 'category', // 映射类别字段实现分组着色 + color: (d: { data: { category: string } }) => d.data.category, }, scale: { color: { range: ['#5B8FF9', '#5AD8A6', '#FA8C00'] }, @@ -90,20 +139,21 @@ chart.options({ }); ``` +`pack` 的回调参数是层次节点:原始字段位于 `d.data`,汇总值位于 `d.value`,`d.height === 0` 表示叶子节点。 + ## 样式自定义 常用视觉定制项(以下写法均经 `@antv/g2` ^5 类型校验): | 定制项 | 写法 | 说明 | |---|---|---| -| 颜色色阶 | `scale: { color: { range: ['#5B8FF9', '#5AD8A6'] } }` | 多系列按序着色 | +| 颜色色阶 | `scale: { color: { range: ['#5B8FF9', '#5AD8A6'] } }` | 多类别按序着色 | | 单系列颜色 | `style: { fill: '#5B8FF9' }` | 固定圆颜色 | | 圆边框 | `style: { stroke: '#ddd', lineWidth: 1 }` | 圆圈边框样式 | -| 圆角 | `style: { radius: 2 }` | 圆的圆角(默认 0) | -| 缩进间距 | `style: { inset: 5 }` | 子节点与父节点的间距 | +| 圆间距 | `layout: { padding: 4 }` | 控制父子节点及兄弟节点之间的间距,不是 `style.inset` | | 图例位置 | `legend: { color: { position: 'right' } }` | `'top'` / `'bottom'` / `'left'` / `'right'` | | 关闭图例 | `legend: false` | | -| 内置主题 | `theme: 'light' | 'dark' | 'classic' | 'classicDark'` | 一行切换整体配色 | +| 内置主题 | `theme: 'classicDark'` | 暗色场景使用 `classicDark`,也可按项目配置选择其他内置主题 | ### 图表标题与组件文字样式 @@ -119,10 +169,17 @@ title: { 数据标签与 tooltip 格式化: ```ts-snippet -labels: [{ text: 'value', position: 'top', style: { fontSize: 10 } }], +labels: [ + { + text: (d: { height: number; value: number; data: { name: string } }) => + d.height === 0 ? `${d.data.name}: ${d.value}` : '', + position: 'inside', + style: { fontSize: 10 }, + }, +], tooltip: { - title: 'name', - items: [{ channel: 'value', name: '数值', valueFormatter: (v) => `${v} (占比)` }], + title: (d) => d.data.name, + items: [{ field: 'value', name: '数值', valueFormatter: (v) => `${v}` }], }, ``` @@ -130,11 +187,16 @@ tooltip: { | ❌ 错误(幻觉高发) | ✅ 正确 | |---|---| -| `.position('name*value')` | `.encode('name', 'value')` | -| `import { CirclePacking } from '@antv/g2plot'` | `import { Chart } from '@antv/g2'` + `type: 'circle-packing'` | +| `.position('name*value')` | `.encode('value', 'value')` | +| `import { CirclePacking } from '@antv/g2plot'` | `import { Chart } from '@antv/g2'` + `type: 'pack'` | | `new CirclePacking('container', { data })` | `new Chart({ container })` + `chart.options(spec)` | -| `parentField: 'parent'`(v2 配置) | 通过数据字段 `parent` 配合 `encode` 指定 | -| 字符串数字 `value` 直接使用 | 数值字段需 `Number()` 转换,否则 `NaN` 导致渲染异常 | +| `type: 'circle-packing'` | `type: 'pack'` | +| `parentField: 'parent'`(v2 配置) | 转换为单根 `children` 树,不使用 `parent` 自动组树 | +| 多个根节点或多棵树组成的数组 | 提供唯一根节点,根节点下通过 `children` 连接所有节点 | +| 给父节点也填写 `value` | 只在叶子填写 `value`,父节点由布局汇总 | +| `style: { inset: 5 }` | `layout: { padding: 5 }` | +| 标签或颜色直接读取 `d.name` / `d.category` | 从 `d.data.name` / `d.data.category` 读取原始字段 | +| 字符串数字 `value` 直接使用 | 叶子数值需为 `number`,否则可能得到 `NaN` | | `chart.source(data)`(v4) | `chart.options({ data })` | 更多对照见 [v4-to-v5-migration.md](../references/v4-to-v5-migration.md)。 @@ -145,7 +207,5 @@ tooltip: { | 案例 | 场景 | 关键配置 | |---|---|---| -| [basic.ts](../examples/circle-packing/basic.ts) | 最简圆包裹图,单根节点 + 子节点 | `type: 'circle-packing'` + `encode: { name, value }` | -| [deep-hierarchy.ts](../examples/circle-packing/deep-hierarchy.ts) | 多层级嵌套层次,父子多代 | `encode: { name, value, parent }` + 多层级父子关系 | -| [color-group.ts](../examples/circle-packing/color-group.ts) | 不同类别颜色分组,便于对比 | `encode.color` + 自定义 `scale.color.range` | -| [highlight-root.ts](../examples/circle-packing/highlight-root.ts) | 根节点视觉高亮,突出入口 | `style` 调整根节点填充/边框 | \ No newline at end of file +| [basic.ts](../examples/circle-packing/basic.ts) | 公司四层组织结构的多层嵌套,展示叶子名称、数值与圆间距 | `type: 'pack'` + 单根 `children` 树 + `encode: { value }` + `labels` + `layout.padding` | +| [color-group.ts](../examples/circle-packing/color-group.ts) | 不同产品类别颜色分组,便于对比 | `encode.color` 读取 `d.data.category` + 自定义 `scale.color.range` | diff --git a/evals/cases/circle-packing.jsonl b/evals/cases/circle-packing.jsonl index 1179ab47a9..a0cfc50277 100644 --- a/evals/cases/circle-packing.jsonl +++ b/evals/cases/circle-packing.jsonl @@ -1,10 +1,10 @@ -{"id": "circle-packing-basic", "prompt": "我需要展示公司组织结构,根节点是公司总部,下面有市场部和研发部,每个部门再有两个子分部,想看每个部门的面积比例", "groundTruth": "使用 type: 'circle-packing';数据需包含 name value parent 字段;encode 映射 name value;可选 encode.color 映射分类字段"} -{"id": "circle-packing-color", "prompt": "展示不同类别的产品销售结构,电子产品和服装两大类,每类下有多个子产品,想用颜色快速区分所属类别", "groundTruth": "使用 type: 'circle-packing';encode.color 映射 category 字段;scale.color 配合 range 设置不同类别颜色;数据需包含 name value category 字段"} -{"id": "circle-packing-deep", "prompt": "展示四层层级结构:公司 -> 事业部 -> 部门 -> 小组,每一层都有面积比例展示", "groundTruth": "使用 type: 'circle-packing';数据需包含 name value parent 字段体现四层层级;parent 字段分别指向上一层节点名"} -{"id": "circle-packing-label", "prompt": "圆包裹图上需要标注数值和百分比,每个小圆圈旁边都要显示具体数值", "groundTruth": "配置 labels: [{ text: 'value', position: 'top', style: { fontSize: 10 } }];确保数据 value 为 number 类型"} -{"id": "circle-packing-size", "prompt": "要展示各子部门在总公司占比的大小,面积大小要准确反映 value 数值", "groundTruth": "使用 type: 'circle-packing';encode.value 映射数值字段;圆的面积自动随 value 大小变化;数据 value 必须为 number 类型"} -{"id": "circle-packing-parent", "prompt": "父子关系数据,母公司下有两个子公司,每个子公司下又有三个部门,需要正确构建层级", "groundTruth": "数据结构包含 parent 字段;每个子节点的 parent 字段值为其上级节点的 name;circle-packing 会自动根据 parent 构建层级嵌套"} -{"id": "circle-packing-tooltip", "prompt": "悬停在圆圈上时,tooltip 显示节点名称和对应的数值", "groundTruth": "配置 tooltip: { title: 'name', items: [{ channel: 'value', name: '数值', valueFormatter: (v) => String(v) }] }"} -{"id": "circle-packing-theme-dark", "prompt": "深色模式下的圆包裹图,文字和边界要清晰可见", "groundTruth": "设置 theme: 'classicDark' 或 'dark';背景、文字颜色自动适配暗色模式"} -{"id": "circle-packing-inset", "prompt": "希望子节点与父节点之间有间距,防止圆圈重叠过紧", "groundTruth": "配置 style: { inset: 5 };inset 控制子节点与父节点之间的间距(px)"} -{"id": "circle-packing-border", "prompt": "圆圈需要显示边框,便于 visually 识别每个独立的圆", "groundTruth": "配置 style: { stroke: '#ddd', lineWidth: 1 };stroke 为边框颜色 lineWidth 为边框宽度"} \ No newline at end of file +{"id": "circle-packing-basic", "prompt": "我需要展示公司组织结构,根节点是公司总部,下面有市场部和研发部,每个部门再有两个子分部,想看每个部门的面积比例", "groundTruth": "使用 type: 'pack';data 使用 { type: 'inline', value: tree },tree 为唯一根节点并通过 children 连接部门与子分部;仅叶子填写 number 类型的 value,非叶子节点不填写 value 并由布局汇总;encode: { value: 'value' }"} +{"id": "circle-packing-color", "prompt": "展示不同类别的产品销售结构,电子产品和服装两大类,每类下有多个子产品,想用颜色快速区分所属类别", "groundTruth": "使用 type: 'pack' 和单根 children 树,仅叶子填写 value;encode: { value: 'value', color: (d) => d.data.category },原始分类字段从 d.data 读取;scale.color.range 设置不同类别颜色;data 使用 { type: 'inline', value: tree }"} +{"id": "circle-packing-deep", "prompt": "展示四层层级结构:公司 -> 事业部 -> 部门 -> 小组,每一层都有面积比例展示", "groundTruth": "使用 type: 'pack';公司、事业部、部门、小组用四层嵌套 children 表达,只有叶子小组填写 value;data: { type: 'inline', value: tree };encode: { value: 'value' },各层数值由布局汇总"} +{"id": "circle-packing-label", "prompt": "圆包裹图上需要标注数值和百分比,每个小圆圈都要显示具体数值", "groundTruth": "使用 type: 'pack';labels 使用回调从 d.data.name 读取名称、从 d.value 读取数值,例如 text: (d) => `${d.data.name}: ${d.value}`,position: 'inside';叶子 value 为 number;百分比可按叶子 value 除以根节点汇总值计算"} +{"id": "circle-packing-size", "prompt": "要展示各子部门在总公司占比的大小,面积大小要准确反映 value 数值", "groundTruth": "使用 type: 'pack' 和 encode: { value: 'value' };数据为单根 children 树,仅叶子填写 number 类型 value,非叶子节点由布局自动汇总;圆面积随汇总 value 变化"} +{"id": "circle-packing-parent", "prompt": "父子关系数据,母公司下有两个子公司,每个子公司下又有三个部门,需要正确构建层级", "groundTruth": "使用 type: 'pack';将母公司、子公司和部门组织为唯一根节点的 children 树,子公司和部门作为嵌套节点,仅最末级部门填写 value;data: { type: 'inline', value: tree };encode: { value: 'value' }"} +{"id": "circle-packing-tooltip", "prompt": "悬停在圆圈上时,tooltip 显示节点名称和对应的数值", "groundTruth": "使用 type: 'pack' 和单根 children 树;tooltip.title 使用 (d) => d.data.name 读取节点名称;items 可配置 [{ field: 'value', name: '数值', valueFormatter: (v) => String(v) }];叶子 value 为 number"} +{"id": "circle-packing-theme-dark", "prompt": "深色模式下的圆包裹图,文字和边界要清晰可见", "groundTruth": "使用 type: 'pack'、单根 children 树和 encode: { value: 'value' };设置 theme: 'classicDark' 或 'dark';必要时通过 style.stroke 和 style.lineWidth 调整边界,标签文字颜色随主题或 label style 配置"} +{"id": "circle-packing-inset", "prompt": "希望子节点与父节点之间有间距,防止圆圈重叠过紧", "groundTruth": "使用 type: 'pack' 和单根 children 树;配置 layout: { padding: 5 },由 layout.padding 控制节点之间的间距;encode: { value: 'value' },仅叶子填写 value"} +{"id": "circle-packing-border", "prompt": "圆圈需要显示边框,便于 visually 识别每个独立的圆", "groundTruth": "使用 type: 'pack'、单根 children 树和 encode: { value: 'value' };配置 style: { stroke: '#ddd', lineWidth: 1 };仅叶子填写 number 类型 value,非叶子节点由布局汇总"} diff --git a/examples/circle-packing/basic.ts b/examples/circle-packing/basic.ts index c0d50e16e0..66d975c17f 100644 --- a/examples/circle-packing/basic.ts +++ b/examples/circle-packing/basic.ts @@ -1,18 +1,45 @@ -import { Chart } from '@antv/g2'; - /** - * 基础圆包裹图 - * 场景:简单的两层层级结构,展示根节点下子节点的面积比例 + * 多层级圆包裹图 + * 用途:展示公司、事业群、部门与小组之间的包含关系和面积占比。 + * 适用场景:组织结构、目录树等多层级数据。 + * 关键要点:单根 children 树;仅叶子填写 value,父节点由 pack 汇总;labels 显示叶子名称和数值;layout.padding 设置圆间距。 */ -const data = [ - { name: '公司总部', value: 100 }, - { name: '市场部', value: 40, parent: '公司总部' }, - { name: '研发部', value: 60, parent: '公司总部' }, - { name: '市场部-华东', value: 15, parent: '市场部' }, - { name: '市场部-华南', value: 25, parent: '市场部' }, - { name: '研发部-硬件', value: 30, parent: '研发部' }, - { name: '研发部-软件', value: 30, parent: '研发部' }, -]; + +import { Chart } from '@antv/g2'; + +const tree = { + name: '公司总部', + children: [ + { + name: '市场事业群', + children: [ + { + name: '市场部', + children: [ + { name: '华东组', value: 15 }, + { name: '华南组', value: 25 }, + ], + }, + { + name: '品牌部', + children: [{ name: '品牌组', value: 20 }], + }, + ], + }, + { + name: '研发事业群', + children: [ + { + name: '研发部', + children: [ + { name: '硬件组', value: 30 }, + { name: '软件组', value: 30 }, + ], + }, + ], + }, + ], +}; const chart = new Chart({ container: 'container', @@ -20,12 +47,18 @@ const chart = new Chart({ }); chart.options({ - type: 'circle-packing', - data, - encode: { - name: 'name', - value: 'value', - }, + type: 'pack', + data: { type: 'inline', value: tree }, + encode: { value: 'value' }, + layout: { padding: 4 }, + labels: [ + { + text: (d: { height: number; value: number; data: { name: string } }) => + d.height === 0 ? `${d.data.name}: ${d.value}` : '', + position: 'inside', + style: { fontSize: 10 }, + }, + ], }); chart.render(); \ No newline at end of file diff --git a/examples/circle-packing/color-group.ts b/examples/circle-packing/color-group.ts index 217f68e2a6..bfb9093a1b 100644 --- a/examples/circle-packing/color-group.ts +++ b/examples/circle-packing/color-group.ts @@ -1,17 +1,34 @@ -import { Chart } from '@antv/g2'; - /** * 颜色分组圆包裹图 - * 场景:不同类别的子节点用不同颜色标记,便于在层级结构中快速区分所属类别 + * 用途:用颜色区分不同产品类别,并展示各类别下产品的销售规模。 + * 适用场景:多层产品结构、业务分类等需要同时看层级和分组的场景。 + * 关键要点:使用单根 children 树,仅叶子填写 value;颜色回调通过 d.data.category 读取原始分类字段。 */ -const data = [ - { name: '电子产品', value: 100, category: 'tech' }, - { name: '手机', value: 40, parent: '电子产品', category: 'tech' }, - { name: '电脑', value: 60, parent: '电子产品', category: 'tech' }, - { name: '服装', value: 80, category: 'fashion' }, - { name: '衬衫', value: 30, parent: '服装', category: 'fashion' }, - { name: '裤子', value: 50, parent: '服装', category: 'fashion' }, -]; + +import { Chart } from '@antv/g2'; + +const tree = { + name: '全部产品', + category: 'total', + children: [ + { + name: '电子产品', + category: 'tech', + children: [ + { name: '手机', value: 40, category: 'tech' }, + { name: '电脑', value: 60, category: 'tech' }, + ], + }, + { + name: '服装', + category: 'fashion', + children: [ + { name: '衬衫', value: 30, category: 'fashion' }, + { name: '裤子', value: 50, category: 'fashion' }, + ], + }, + ], +}; const chart = new Chart({ container: 'container', @@ -19,12 +36,11 @@ const chart = new Chart({ }); chart.options({ - type: 'circle-packing', - data, + type: 'pack', + data: { type: 'inline', value: tree }, encode: { - name: 'name', value: 'value', - color: 'category', + color: (d: { data: { category: string } }) => d.data.category, }, scale: { color: { range: ['#5B8FF9', '#5AD8A6', '#FA8C00'] }, diff --git a/skills/g2plot-v3/SKILL.md b/skills/g2plot-v3/SKILL.md index ca96e17bb3..98147c127f 100644 --- a/skills/g2plot-v3/SKILL.md +++ b/skills/g2plot-v3/SKILL.md @@ -1,6 +1,6 @@ --- name: g2plot-v3 -description: G2Plot v3 可视化组件库,提示词即组件。当用户需要创建、绘制 AntV G2 图表(折线图、柱状图、饼图、面积图、散点图、双轴图、雷达图、热力图、漏斗图等)时使用本 skill。提供 G2 v5 出码规范、图表选型决策、可运行案例与 v4→v5 防幻觉对照。 +description: G2Plot v3 可视化组件库,提示词即组件。当用户需要创建、绘制 AntV G2 图表(折线图、柱状图、饼图、面积图、散点图、双轴图、雷达图、热力图、漏斗图、圆包裹图等)时使用本 skill。提供 G2 v5 出码规范、图表选型决策、可运行案例与 v4→v5 防幻觉对照。 --- # G2Plot v3 @@ -31,6 +31,7 @@ description: G2Plot v3 可视化组件库,提示词即组件。当用户需要 | 两个量级不同的指标 | 时间 x + 双数值 y | dual-axes 双轴图 | [charts/dual-axes.md](../../charts/dual-axes.md) | | 多维能力对比 | 多维度数值 | radar 雷达图 | [charts/radar.md](../../charts/radar.md) | | 密度/频率分布 | 两个类别 + 数值 | heatmap 热力图 | [charts/heatmap.md](../../charts/heatmap.md) | +| 层级包含与面积占比 | 单根 `children` 树 + 叶子 `value` | circle-packing 圆包裹图 | [charts/circle-packing.md](../../charts/circle-packing.md) | | 流程转化 | 阶段 + 数值 | funnel 漏斗图 | [charts/funnel.md](../../charts/funnel.md) | 选型拿不准时,读对应 `charts/.md` 的「何时使用 / 何时不用」小节。 @@ -71,6 +72,6 @@ curl "https://sive.antv.antgroup.com/api/v1/context/retrieve?query=bar+chart+sta 1. 只用 G2 v5 API:`import { Chart } from '@antv/g2'`,`new Chart()` + `chart.options(spec)` + `chart.render()` 2. 禁止 v4 语法糖:`.position('x*y')`、`.shape('smooth')`、`chart.source(data)` 3. 禁止 G2Plot v2 写法:`new Line()`、`xField` / `yField` / `seriesField` -4. 数据用长表,数值字段确保 `number` 类型 +4. 普通统计图使用长表;圆包裹图等层级图按组件文档使用单根 `children` 树。数值字段确保 `number` 类型 5. 生成代码自包含:示例数据内联,或明确标注数据来源与字段名 6. 布局用 `autoFit: true`(`new Chart({ container, autoFit: true })`),**不要**写死 `width` / `height`(autoFit 已自适应容器,写死会留白或裁剪),也**不要**手动设置 spec `padding`:保持默认 `auto`,G2 会自动为坐标轴、图例留出空间,手动 padding 容易导致组件被遮挡/裁剪