Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| | | | | mix |


## 🏗️ How It Works
Expand Down
1 change: 1 addition & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ G2Plot v3 覆盖 36+ 个常用图表类型,更多类型,欢迎提交 PR:
| line 折线图 | column 柱状图 | pie 饼图 | scatter 散点图 | dual-axes 双轴图 |
| area 面积图 | bar 条形图 | | heatmap 热力图 | funnel 漏斗图 |
| | | | | radar 雷达图 |
| | | | | mix 混合图 |


## 🏗️ 工作原理
Expand Down
173 changes: 173 additions & 0 deletions charts/mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# Mix 混合图

> 组件用途:在同一个坐标系里叠加两种及以上图形标记(柱 + 线、面积 + 线 + 点等),用不同视觉编码同时表达多个指标或同一指标的多层信息。

## 何时使用

- 柱线组合:柱表达绝对量,线表达目标、比率或趋势(实际营收 vs 目标营收)
- 面积 + 折线 + 散点:面积给量级氛围、折线强调走势、散点标出采样点
- 堆叠柱 + 总量折线:既看构成,又看总量趋势
- 主图上叠加参考线/阈值线(目标线、均值线、警戒线)

## 何时不用

- 只有一种 mark → 直接用对应图表(line / column / area),不要为组合而组合
- 两个指标量级或单位差异大、需要各自刻度 → 用 [dual-axes 双轴图](./dual-axes.md)
- 需要多个独立坐标系并排或分面 → 用 G2 的 `spaceFlex` / 分面组合,而不是单 view 叠加
- 占比构成 → 用 [pie 饼图](./pie.md);相关性 → 用 [scatter 散点图](./scatter.md)

## 数据要求

- 长表数据,每行一个 x 观测点,见 [数据模式](../references/data-patterns.md)
- `x` 字段:时间字符串或有序类别,各层共用
- 共享数据时:多个数值字段并列(如 `actual` / `target`),每个子 mark 各取一个 `y`
- 子 mark 独立数据时:在 child 上直接写 `data`(如总量折线单独聚合)
- 堆叠场景:长表增加系列字段(如 `channel`),用 `encode.color` 分组 + `transform: [{ type: 'stackY' }]`
- 数值字段必须为 number;字符串数字先 `Number()` 转换

## 基础实现(G2 v5)

混合图 = `type: 'view'` 容器 + `children` 数组;每个 child 是一个 mark(必须有 `type`),按数组顺序渲染(后面的在上层)。默认共享 x/y 比例尺,因此同单位指标可直接叠加。

```ts
import { Chart } from '@antv/g2';

const data = [
{ month: 'Jan', actual: 320, target: 300 },
{ month: 'Feb', actual: 420, target: 380 },
{ month: 'Mar', actual: 380, target: 410 },
{ month: 'Apr', actual: 520, target: 460 },
{ month: 'May', actual: 480, target: 500 },
{ month: 'Jun', actual: 610, target: 550 },
];

const chart = new Chart({
container: 'container',
autoFit: true,
});

chart.options({
type: 'view',
data,
children: [
{
type: 'interval',
encode: { x: 'month', y: 'actual' },
style: { fill: '#5B8FF9', radiusTopLeft: 4, radiusTopRight: 4 },
},
{
type: 'line',
encode: { x: 'month', y: 'target', shape: 'smooth' },
style: { stroke: '#F6BD16', lineWidth: 2, lineDash: [6, 4] },
},
],
axis: { x: { title: '月份' }, y: { title: '营收(万元)' } },
legend: false,
});

chart.render();
```

## 常见变体

| 变体 | 关键改动 | 完整案例 |
|---|---|---|
| 柱线混合 | children:`interval` + `line`,共享单轴 | [basic.ts](../examples/mix/basic.ts) |
| 面积 + 折线 + 散点 | 父级 `encode` 写一次,三层 child 继承 | [area-line-point.ts](../examples/mix/area-line-point.ts) |
| 堆叠柱 + 总量折线 | `transform: [{ type: 'stackY' }]` + line child 独立 `data` | [stacked-column-line.ts](../examples/mix/stacked-column-line.ts) |
| 柱 + 目标参考线 | 叠加 `lineY` mark,`encode: { y: 'threshold' }` | [target-line.ts](../examples/mix/target-line.ts) |
| 柱线双轴 | 第二个 child 加 `scale: { y: { independent: true } }` | [column-line.ts](../examples/dual-axes/column-line.ts) |

多层叠加的核心是父级 `encode` 继承(数据需含 `date` / `dau` 字段):

```ts-snippet
chart.options({
type: 'view',
data,
encode: { x: 'date', y: 'dau' },
children: [
{ type: 'area', style: { fill: '#5B8FF9', fillOpacity: 0.15 } },
{ type: 'line', style: { stroke: '#5B8FF9', lineWidth: 2 } },
{ type: 'point', encode: { shape: 'circle', size: 3 }, tooltip: false },
],
});
```

子 mark 需要不同数据时,直接在该 child 上写 `data`(不要在 `children` 里再套一层 `view`):

```ts-snippet
children: [
{
type: 'interval',
data: revenueByChannel,
encode: { x: 'month', y: 'revenue', color: 'channel' },
transform: [{ type: 'stackY' }],
},
{
type: 'line',
data: monthlyTotal,
encode: { x: 'month', y: 'total' },
},
],
```

## 样式自定义

| 定制项 | 写法 | 说明 |
|---|---|---|
| 共享坐标轴 | view 级 `axis: { x: { title }, y: { title } }` | 一次配置作用于所有 child |
| 图例位置 | view 级 `legend: { color: { position: 'top' } }` | 多系列堆叠时常用 |
| 系列配色 | view 级 `scale: { color: { range: [...] } }` | 堆叠/分组按系列值序着色 |
| mark 专属颜色 | 各 child 的 `style.fill` / `style.stroke` | 单系列柱、单条折线固定颜色 |
| 柱圆角 | `style: { radiusTopLeft: 4, radiusTopRight: 4 }` | interval 专属 |
| 折线平滑/虚线 | `encode: { shape: 'smooth' }`、`style: { lineDash: [6, 4] }` | 目标线常用虚线区分 |
| 参考线 | `type: 'lineY'` + `encode: { y: 'threshold' }` | 横向铺满坐标系 |
| 数据标签 | 各 child 的 `labels: [{ text: '字段', position: 'top' }]` | 柱顶/线点旁标数值 |
| tooltip 格式化 | 各 child 的 `tooltip.items` + `valueFormatter` | 辅助 mark 设 `tooltip: false` 去重 |
| 图表标题 | view 级 `title: { title, subtitle, align }` | `title` 必须是对象,不能直接写字符串 |
| 内置主题 | view 级 `theme: 'light' \| 'dark' \| 'classic' \| 'classicDark' \| 'academy'` | 暗色场景用 `'classicDark'` |

标题、轴样式与 tooltip 组合,完整示例见 [theme.ts](../examples/mix/theme.ts):

```ts-snippet
title: {
title: '营收与利润',
subtitle: '单位:万元',
align: 'left',
},
axis: {
x: { title: '月份', labelFontSize: 12, labelFill: '#666' },
y: { title: '金额', gridStroke: '#e8e8e8' },
},
```

双轴不属于混合图的默认行为:只有量级/单位确实不同才给第二个 child 加 `scale: { y: { independent: true } }`,并分别命名左右轴(详见 [dual-axes.md](./dual-axes.md))。

## 易错点(v4 → v5)

| ❌ 错误(幻觉高发) | ✅ 正确 |
|---|---|
| `type: 'mix'`(不存在的 mark) | `type: 'view'` + `children: [mark1, mark2]` |
| `new Mix('container', { plots: [...] })`(G2Plot v2) | `new Chart({ container })` + view children |
| `plots` / `views` 配置(v2 Mix 专属) | `children` 数组;各层数据挂在对应 child 的 `data` |
| 连续调用两次 `chart.options()` 叠加图层 | 后一次会覆盖前一次;合并为单个 view spec |
| 在 `children` 里再嵌套 `type: 'view'` | children 元素必须是 mark;独立数据直接写在 mark 上 |
| 双轴漏配 `scale: { y: { independent: true } }` | 两系列共用一根轴,量级小的会被压平 |
| 两个量级悬殊的指标硬塞进单轴混合图 | 改用 [dual-axes](./dual-axes.md) 双轴图 |
| `chart.line()` 之后再 `chart.interval()` 期望叠加 | 多 mark 叠加统一走 `type: 'view'` + children |
| `seriesField: 'channel'`(v2 配置) | `encode: { color: 'channel' }` |
| `chart.source(data)`(v4) | `chart.options({ data })`,或 child 内 `data` |

更多对照见 [v4-to-v5-migration.md](../references/v4-to-v5-migration.md)。

## 完整案例

每个案例的元信息(场景与关键配置),代码均通过 `npm run validate` 校验:

| 案例 | 场景 | 关键配置 |
|---|---|---|
| [basic.ts](../examples/mix/basic.ts) | 实际营收(柱)vs 目标营收(线),最简柱线混合 | `type: 'view'` + interval/line children |
| [area-line-point.ts](../examples/mix/area-line-point.ts) | 日活趋势:面积 + 折线 + 采样点三层叠加 | 父级 `encode` 继承、`point.tooltip: false` |
| [stacked-column-line.ts](../examples/mix/stacked-column-line.ts) | 渠道营收构成 + 总营收趋势 | `transform: [{ type: 'stackY' }]`、line child 独立 `data` |
| [target-line.ts](../examples/mix/target-line.ts) | 月度完成量 vs 固定目标线 | `type: 'lineY'` + `encode: { y: 'threshold' }` + `lineDash` |
| [theme.ts](../examples/mix/theme.ts) | 品牌化柱线混合图:标题、轴、tooltip、主题 | view 级 `title`/`axis`、child 级 `tooltip.items`、`theme: 'classic'` |
15 changes: 15 additions & 0 deletions evals/cases/mix.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{"id": "mix-column-line-basic", "prompt": "做一个月度经营看板,用柱状图展示实际营收,再叠一条折线表示目标营收,方便对比达成情况", "groundTruth": "type: 'view' + children;柱子用 type: 'interval' 并 encode x: 'month', y: 'actual';折线用 type: 'line' 并 encode x: 'month', y: 'target';两个子 mark 共享同一套 x/y 比例尺"}
{"id": "mix-area-line-point", "prompt": "日活趋势图,希望既有面积填充的氛围感,又有折线强调走势,还能看到每个采样点", "groundTruth": "type: 'view' 的 children 依次放 area、line、point;父级 encode { x, y } 让三个子 mark 继承;point 可设 tooltip: false 避免重复提示"}
{"id": "mix-stacked-line-total", "prompt": "各渠道营收用堆叠柱展示构成,同时在柱子上方叠一条总营收折线", "groundTruth": "view + children;interval 配 transform: [{ type: 'stackY' }] 且 encode.color 映射渠道;line 使用独立 data(每月总量)并 encode y 为 total;不要在 children 内嵌套 view"}
{"id": "mix-target-line", "prompt": "月度订单量柱状图,加一条 500 的目标线,一眼看出哪些月没达标", "groundTruth": "view 的 children 为 interval + lineY;lineY 使用独立 data(如 [{ threshold: 500 }]),encode: { y: 'threshold' },style 配 lineDash 画虚线;参考线设 tooltip: false"}
{"id": "mix-dual-axis-column-line", "prompt": "销售额和增长率量级差很多,想在一张图里用柱子和折线分别表示,左右各一根 y 轴", "groundTruth": "view children:interval(销售额)+ line(增长率);第二个 child 设置 scale: { y: { independent: true } } 启用独立右轴;各自配置 axis.y.title"}
{"id": "mix-brand-color", "prompt": "品牌主色是 #FF6B35,用它画柱状图,再叠一条蓝色折线表示利润率,颜色要区分清楚", "groundTruth": "view children 分别设置 style.fill(柱)与 style.stroke(线);也可在 view 级用 scale.color.range 指定颜色数组;type: 'view' + children 结构"}
{"id": "mix-dark-theme-title", "prompt": "大屏用的暗色混合图:柱状营收加折线利润,标题「营收与利润」、副标题「单位:万元」,左对齐", "groundTruth": "view 级设置 theme: 'classicDark'(或 'dark');title 为对象,包含 title、subtitle、align: 'left';children 为 interval + line"}
{"id": "mix-tooltip-unit", "prompt": "混合图 tooltip 里柱子和折线两个系列都要带「万元」单位,数值保留一位小数", "groundTruth": "各 child 的 tooltip.items 配置 valueFormatter,拼接「万元」并用 toFixed(1) 格式化;整体为 view + children 结构"}
{"id": "mix-labels-both", "prompt": "柱线混合图,柱顶显示营收数值,折线上也标出利润数值", "groundTruth": "分别在 interval 与 line 两个 child 上配置 labels: [{ text: 'revenue' / 'profit', position: 'top' }];使用 type: 'view' + children"}
{"id": "mix-legend-top", "prompt": "多系列堆叠柱加总量折线,图例放到顶部,别挡住图形", "groundTruth": "view 级配置 legend: { color: { position: 'top' } };interval 用 encode.color 映射系列并配 transform: [{ type: 'stackY' }];line 表示总量"}
{"id": "mix-transpose-bar-line", "prompt": "各品类销量用横向条形展示,并叠一条目标销量折线,整体横向排布", "groundTruth": "view 级配置 coordinate: { transform: [{ type: 'transpose' }] } 转置坐标系;children 为 interval + line,均 encode x 为品类、y 为销量/目标销量"}
{"id": "mix-smooth-line", "prompt": "柱状图表示每月新增用户,上面叠一条平滑折线看增长趋势", "groundTruth": "line child 的 encode 增加 shape: 'smooth';interval 与 line 同时放在 type: 'view' 的 children 中"}
{"id": "mix-shared-axis", "prompt": "混合图里柱子和折线是同一个单位,想共用一根 y 轴,不要双轴", "groundTruth": "view children 的 y 通道保持默认共享比例尺;不要设置 scale.y.independent;只保留左侧一根 y 轴"}
{"id": "mix-options-override", "prompt": "我在一个图表里先画柱状图再画折线,结果只剩折线了,怎么把两种图叠加起来?", "groundTruth": "不要连续调用 chart.options(后一次会覆盖前一次);改为单个 type: 'view' spec,children 数组同时放 interval 和 line"}
{"id": "mix-auxiliary-tooltip", "prompt": "混合图里的散点只是辅助标记,鼠标悬浮时不想出现重复的提示条目", "groundTruth": "给辅助 mark(如 point)设置 tooltip: false;主 mark(interval/line)保留 tooltip 配置"}
46 changes: 46 additions & 0 deletions examples/mix/area-line-point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 面积 + 折线 + 散点:三层叠加
* 场景:日活趋势既要量级氛围(面积)、走势(折线),又要标出每个采样点(散点)
* 要点:父级 encode 只写一次,子 mark 自动继承;散点关掉 tooltip 避免重复提示
*/
import { Chart } from '@antv/g2';

const data = [
{ date: '06-01', dau: 18200 },
{ date: '06-02', dau: 19600 },
{ date: '06-03', dau: 18900 },
{ date: '06-04', dau: 21400 },
{ date: '06-05', dau: 23100 },
{ date: '06-06', dau: 22800 },
{ date: '06-07', dau: 24900 },
];

const chart = new Chart({
container: 'container',
autoFit: true,
});

chart.options({
type: 'view',
data,
encode: { x: 'date', y: 'dau' },
children: [
{
type: 'area',
style: { fill: '#5B8FF9', fillOpacity: 0.15 },
},
{
type: 'line',
style: { stroke: '#5B8FF9', lineWidth: 2 },
},
{
type: 'point',
encode: { shape: 'circle', size: 3 },
style: { fill: '#fff', stroke: '#5B8FF9', lineWidth: 2 },
tooltip: false,
},
],
axis: { x: { title: '日期' }, y: { title: '日活用户数' } },
});

chart.render();
42 changes: 42 additions & 0 deletions examples/mix/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 基础混合图:柱 + 折线
* 场景:同一 x 轴上对比「实际营收」与「目标营收」,柱看绝对量、线看目标走势
* 要点:type 为 'view',children 按渲染顺序排列(后面的在上层);
* 两个子 mark 共享 x/y 比例尺,无需额外配置
*/
import { Chart } from '@antv/g2';

const data = [
{ month: 'Jan', actual: 320, target: 300 },
{ month: 'Feb', actual: 420, target: 380 },
{ month: 'Mar', actual: 380, target: 410 },
{ month: 'Apr', actual: 520, target: 460 },
{ month: 'May', actual: 480, target: 500 },
{ month: 'Jun', actual: 610, target: 550 },
];

const chart = new Chart({
container: 'container',
autoFit: true,
});

chart.options({
type: 'view',
data,
children: [
{
type: 'interval',
encode: { x: 'month', y: 'actual' },
style: { fill: '#5B8FF9', radiusTopLeft: 4, radiusTopRight: 4 },
},
{
type: 'line',
encode: { x: 'month', y: 'target', shape: 'smooth' },
style: { stroke: '#F6BD16', lineWidth: 2, lineDash: [6, 4] },
},
],
axis: { x: { title: '月份' }, y: { title: '营收(万元)' } },
legend: false,
});

chart.render();
58 changes: 58 additions & 0 deletions examples/mix/stacked-column-line.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 堆叠柱 + 总量折线:子 mark 独立数据
* 场景:各渠道营收构成(堆叠柱)与总营收趋势(折线)同图对比
* 要点:interval 用 transform stackY 堆叠;折线 mark 直接挂独立 data,
* 不要在 children 里再套一层 view
*/
import { Chart } from '@antv/g2';

const revenueByChannel = [
{ month: 'Jan', channel: '线上', revenue: 220 },
{ month: 'Feb', channel: '线上', revenue: 280 },
{ month: 'Mar', channel: '线上', revenue: 250 },
{ month: 'Apr', channel: '线上', revenue: 320 },
{ month: 'May', channel: '线上', revenue: 360 },
{ month: 'Jan', channel: '线下', revenue: 120 },
{ month: 'Feb', channel: '线下', revenue: 150 },
{ month: 'Mar', channel: '线下', revenue: 180 },
{ month: 'Apr', channel: '线下', revenue: 140 },
{ month: 'May', channel: '线下', revenue: 160 },
];

// 总量独立维护:折线不等于任一通道,需单独聚合
const monthlyTotal = [
{ month: 'Jan', total: 340 },
{ month: 'Feb', total: 430 },
{ month: 'Mar', total: 430 },
{ month: 'Apr', total: 460 },
{ month: 'May', total: 520 },
];

const chart = new Chart({
container: 'container',
autoFit: true,
});

chart.options({
type: 'view',
children: [
{
type: 'interval',
data: revenueByChannel,
encode: { x: 'month', y: 'revenue', color: 'channel' },
transform: [{ type: 'stackY' }],
style: { radiusTopLeft: 4, radiusTopRight: 4 },
},
{
type: 'line',
data: monthlyTotal,
encode: { x: 'month', y: 'total', shape: 'smooth' },
style: { stroke: '#1d1d1d', lineWidth: 2 },
},
],
scale: { color: { range: ['#5B8FF9', '#5AD8A6'] } },
axis: { x: { title: '月份' }, y: { title: '营收(万元)' } },
legend: { color: { position: 'top' } },
});

chart.render();
Loading
Loading