Skip to content

Commit f75cbf0

Browse files
authored
feat(legend): add legend to dual-axes (#1533)
* feat(legend): 双轴图添加 legend 自定义交互 * fix: 修复 ci 错误 & 删除无用变量
1 parent 2750736 commit f75cbf0

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

src/core/plot.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export abstract class Plot<O extends PickOptions> extends EE {
4848
this.chart = new Chart({
4949
container: this.container,
5050
autoFit: false, // G2Plot 使用 size-sensor 进行 autoFit
51-
...this.getChartSize(width, height, autoFit),
51+
...this.getChartSize(width, height),
5252
padding,
5353
appendPadding,
5454
renderer,
@@ -61,9 +61,8 @@ export abstract class Plot<O extends PickOptions> extends EE {
6161
* 计算默认的 chart 大小。逻辑简化:如果存在 width 或 height,则直接使用,否则使用容器大小
6262
* @param width
6363
* @param height
64-
* @param autoFit
6564
*/
66-
private getChartSize(width: number, height: number, autoFit: boolean): Size {
65+
private getChartSize(width: number, height: number): Size {
6766
const chartSize = getContainerSize(this.container);
6867
return { width: width || chartSize.width, height: height || chartSize.height };
6968
}

src/plots/dual-axes/adaptor.ts

+43-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { each, deepMix } from '@antv/util';
1+
import { deepMix, each, map, some } from '@antv/util';
22
import { theme, animation, scale } from '../../adaptor/common';
33
import { Interaction } from '../../types/interaction';
44
import { Params } from '../../core/adaptor';
55
import { flow } from '../../utils';
66
import { getOption } from './util/option';
77
import { drawSingleGeometry } from './util/geometry';
8-
import { DualAxesOption } from './types';
8+
import { DualAxesOption, GeometryConfig } from './types';
99

1010
/**
1111
* 获取默认参数设置
@@ -173,21 +173,48 @@ export function interaction(params: Params<DualAxesOption>): Params<DualAxesOpti
173173
*/
174174
export function legend(params: Params<DualAxesOption>): Params<DualAxesOption> {
175175
const { chart, options } = params;
176-
const { legend, geometryOptions } = options;
177-
const [leftGeometryOption, rightGeometryOption] = geometryOptions;
178-
if (legend) {
179-
if (leftGeometryOption.seriesField) {
180-
// TOFIX: G2 中图例无法绑定在子 view 中,但绑定在 chart 上回换行。暂时先放在下方
181-
chart.legend(leftGeometryOption.seriesField, {
182-
position: 'bottom',
183-
});
184-
}
185-
if (rightGeometryOption.seriesField) {
186-
chart.legend(rightGeometryOption.seriesField, {
187-
position: 'bottom',
188-
});
189-
}
176+
const { legend, geometryOptions, yField } = options;
177+
178+
if (legend === false) {
179+
chart.legend(false);
180+
} else if (!legend && !some(geometryOptions, (opt) => !!opt.seriesField)) {
181+
// 没有任何分类字段的时候,内置自定义图例
182+
chart.legend({
183+
custom: true,
184+
// todo 修改类型定义
185+
// @ts-ignore
186+
items: map(geometryOptions, (opt: GeometryConfig, idx: number) => {
187+
const defaultColor = chart.getTheme().defaultColor;
188+
const geometryType = opt.geometry;
189+
const marker =
190+
geometryType === 'line'
191+
? { symbol: 'line', style: { stroke: defaultColor, lineWidth: 2 } }
192+
: { symbol: 'square' };
193+
return {
194+
value: yField[idx],
195+
name: geometryType,
196+
marker,
197+
};
198+
}),
199+
});
200+
201+
// 自定义图例交互
202+
chart.on('legend-item:click', (evt) => {
203+
const delegateObject = evt.gEvent.delegateObject;
204+
if (delegateObject && delegateObject.item) {
205+
const idx = delegateObject.index;
206+
const view = chart.views[idx];
207+
const field = yField[idx];
208+
if (view.getScaleByField(field)) {
209+
view.filter(field, () => !delegateObject.item.unchecked);
210+
}
211+
view.render(true);
212+
}
213+
});
214+
} else if (legend) {
215+
chart.legend(legend);
190216
}
217+
191218
return params;
192219
}
193220

0 commit comments

Comments
 (0)