diff --git a/charts/circle-packing.md b/charts/circle-packing.md new file mode 100644 index 0000000000..b60be48932 --- /dev/null +++ b/charts/circle-packing.md @@ -0,0 +1,211 @@ +# Circle-packing 圆树/圆包裹图 + +> 组件用途:展示层级关系的树状结构,通过嵌套圆圈表示类别间的包含与面积比例。 + +## 何时使用 + +- **层级树可视化**:组织结构图、目录树、产品/分类层级 +- **面积比例展示**:各子类别占父类别的比例(面积 ∝ 数值) +- **空间占比分析**:直观展示各部分在整体中的空间占有情况 + +## 何时不用 + +- 纯类别对比(无层级)→ 用 `column` 柱状图 或 `pie` 饼图 +- 两个数值变量的相关性 → 用 `scatter` 散点图 +- 连续二维空间密度 → 用 `heatmap` 热力图 +- 仅关注单个数值大小对比 → 用 `bar` 柱状图 + +## 数据要求 + +- **单根树形数据**:只提供一个根节点,子节点放在 `children` 数组中;不要使用 `parent` 扁平表,也不要提供多个根节点 +- `name` 字段:节点名称(字符串),供标签和 tooltip 回调通过 `d.data.name` 读取 +- `children` 字段:子节点数组;叶子节点省略该字段 +- `value` 字段:仅在叶子节点填写待汇总的数值(number);父节点省略 `value`,由 `pack` 布局自动汇总 +- 数值必须是 `number` 类型,服务端返回的字符串数字需先 `Number()` 转换 + +**数据示例**: + +```json +{ + "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 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', + autoFit: true, +}); + +chart.options({ + 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(); +``` + +## 常见变体 + +| 变体 | 关键改动 | 完整案例 | +|---|---|---| +| 多层嵌套、标签与间距 | 用 `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: 'pack', + data: { type: 'inline', value: tree }, + encode: { + value: 'value', + color: (d: { data: { category: string } }) => d.data.category, + }, + scale: { + color: { range: ['#5B8FF9', '#5AD8A6', '#FA8C00'] }, + }, +}); +``` + +`pack` 的回调参数是层次节点:原始字段位于 `d.data`,汇总值位于 `d.value`,`d.height === 0` 表示叶子节点。 + +## 样式自定义 + +常用视觉定制项(以下写法均经 `@antv/g2` ^5 类型校验): + +| 定制项 | 写法 | 说明 | +|---|---|---| +| 颜色色阶 | `scale: { color: { range: ['#5B8FF9', '#5AD8A6'] } }` | 多类别按序着色 | +| 单系列颜色 | `style: { fill: '#5B8FF9' }` | 固定圆颜色 | +| 圆边框 | `style: { stroke: '#ddd', lineWidth: 1 }` | 圆圈边框样式 | +| 圆间距 | `layout: { padding: 4 }` | 控制父子节点及兄弟节点之间的间距,不是 `style.inset` | +| 图例位置 | `legend: { color: { position: 'right' } }` | `'top'` / `'bottom'` / `'left'` / `'right'` | +| 关闭图例 | `legend: false` | | +| 内置主题 | `theme: 'classicDark'` | 暗色场景使用 `classicDark`,也可按项目配置选择其他内置主题 | + +### 图表标题与组件文字样式 + +```ts-snippet +title: { + title: '组织结构可视化', // 主标题 + align: 'left', // 'left' | 'center' | 'right' + titleFontSize: 16, + titleFill: '#1d1d1d', +}, +``` + +数据标签与 tooltip 格式化: + +```ts-snippet +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: (d) => d.data.name, + items: [{ field: 'value', name: '数值', valueFormatter: (v) => `${v}` }], +}, +``` + +## 易错点(v4 → v5) + +| ❌ 错误(幻觉高发) | ✅ 正确 | +|---|---| +| `.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)` | +| `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)。 + +## 完整案例 + +每个案例的元信息(场景与关键配置),代码均通过 `npm run validate` 校验: + +| 案例 | 场景 | 关键配置 | +|---|---|---| +| [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 new file mode 100644 index 0000000000..a0cfc50277 --- /dev/null +++ b/evals/cases/circle-packing.jsonl @@ -0,0 +1,10 @@ +{"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 new file mode 100644 index 0000000000..66d975c17f --- /dev/null +++ b/examples/circle-packing/basic.ts @@ -0,0 +1,64 @@ +/** + * 多层级圆包裹图 + * 用途:展示公司、事业群、部门与小组之间的包含关系和面积占比。 + * 适用场景:组织结构、目录树等多层级数据。 + * 关键要点:单根 children 树;仅叶子填写 value,父节点由 pack 汇总;labels 显示叶子名称和数值;layout.padding 设置圆间距。 + */ + +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', + autoFit: true, +}); + +chart.options({ + 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 new file mode 100644 index 0000000000..bfb9093a1b --- /dev/null +++ b/examples/circle-packing/color-group.ts @@ -0,0 +1,50 @@ +/** + * 颜色分组圆包裹图 + * 用途:用颜色区分不同产品类别,并展示各类别下产品的销售规模。 + * 适用场景:多层产品结构、业务分类等需要同时看层级和分组的场景。 + * 关键要点:使用单根 children 树,仅叶子填写 value;颜色回调通过 d.data.category 读取原始分类字段。 + */ + +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', + autoFit: true, +}); + +chart.options({ + type: 'pack', + data: { type: 'inline', value: tree }, + encode: { + value: 'value', + color: (d: { data: { category: string } }) => d.data.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 1bf3b4d6e8..04070eaeb9 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 @@ -32,6 +32,7 @@ description: G2Plot v3 可视化组件库,提示词即组件。当用户需要 | 多维能力对比 | 多维度数值 | radar 雷达图 | [charts/radar.md](../../charts/radar.md) | | 密度/频率分布 | 两个类别 + 数值 | heatmap 热力图 | [charts/heatmap.md](../../charts/heatmap.md) | | 组间分布对比 | 类别 + 每类多个观测值 | box 箱线图 | [charts/box.md](../../charts/box.md) | +| 层级包含与面积占比 | 单根 `children` 树 + 叶子 `value` | circle-packing 圆包裹图 | [charts/circle-packing.md](../../charts/circle-packing.md) | | 流程转化 | 阶段 + 数值 | funnel 漏斗图 | [charts/funnel.md](../../charts/funnel.md) | 选型拿不准时,读对应 `charts/.md` 的「何时使用 / 何时不用」小节。 @@ -72,6 +73,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 容易导致组件被遮挡/裁剪