|
1 |
| -import { each, deepMix } from '@antv/util'; |
| 1 | +import { deepMix, each, map, some } from '@antv/util'; |
2 | 2 | import { theme, animation, scale } from '../../adaptor/common';
|
3 | 3 | import { Interaction } from '../../types/interaction';
|
4 | 4 | import { Params } from '../../core/adaptor';
|
5 | 5 | import { flow } from '../../utils';
|
6 | 6 | import { getOption } from './util/option';
|
7 | 7 | import { drawSingleGeometry } from './util/geometry';
|
8 |
| -import { DualAxesOption } from './types'; |
| 8 | +import { DualAxesOption, GeometryConfig } from './types'; |
9 | 9 |
|
10 | 10 | /**
|
11 | 11 | * 获取默认参数设置
|
@@ -173,21 +173,48 @@ export function interaction(params: Params<DualAxesOption>): Params<DualAxesOpti
|
173 | 173 | */
|
174 | 174 | export function legend(params: Params<DualAxesOption>): Params<DualAxesOption> {
|
175 | 175 | 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); |
190 | 216 | }
|
| 217 | + |
191 | 218 | return params;
|
192 | 219 | }
|
193 | 220 |
|
|
0 commit comments