|
1 |
| -import { isBoolean, isNumber } from '@antv/util'; |
| 1 | +import { isNumber } from '@antv/util'; |
2 | 2 | import { Params } from '../../core/adaptor';
|
3 | 3 | import { flow, deepAssign } from '../../utils';
|
4 | 4 | import { point } from '../../adaptor/geometries';
|
@@ -120,18 +120,30 @@ function axis(params: Params<ScatterOptions>): Params<ScatterOptions> {
|
120 | 120 | */
|
121 | 121 | function legend(params: Params<ScatterOptions>): Params<ScatterOptions> {
|
122 | 122 | const { chart, options } = params;
|
123 |
| - const { legend, colorField, shapeField, sizeField } = options; |
124 |
| - // legend 没有指定时根据 shapeField 和 colorField 来设置默认值 |
125 |
| - const showLegend = isBoolean(legend) ? legend : legend || !!(shapeField || colorField); |
| 123 | + const { legend, colorField, shapeField, sizeField, shapeLegend, sizeLegend } = options; |
| 124 | + |
| 125 | + /** 1. legend 不为 false, 则展示图例, 优先展示 color 分类图例 */ |
| 126 | + const showLegend = legend !== false; |
| 127 | + // 1. shapeLegend 为 false, 强制关闭 shape 映射图例 |
| 128 | + // 2.1 shapeLegend 不为空时,展示 shape 图例; 2.2 否则colorField 不存在,且 legend 存在时,可展示 shape 图例 |
| 129 | + const showShapeLegend = shapeField && shapeLegend !== false && ((showLegend && !colorField) || shapeLegend); |
| 130 | + /** 默认没有 sizeField,则隐藏连续图例 */ |
| 131 | + const showSizeLegend = sizeField && sizeLegend; |
| 132 | + |
126 | 133 | if (showLegend) {
|
127 |
| - colorField && chart.legend(colorField, legend); |
128 |
| - shapeField && chart.legend(shapeField, legend); |
129 |
| - // 隐藏连续图例 |
130 |
| - if (sizeField) { |
131 |
| - chart.legend(sizeField, false); |
132 |
| - } |
133 |
| - } else { |
| 134 | + chart.legend(colorField, legend); |
| 135 | + } |
| 136 | + if (showShapeLegend) { |
| 137 | + // 优先取 shapeLegend,否则取 legend |
| 138 | + chart.legend(shapeField, shapeLegend || legend); |
| 139 | + } |
| 140 | + if (showSizeLegend) { |
| 141 | + chart.legend(sizeField, sizeLegend); |
| 142 | + } |
| 143 | + if (!showLegend && !showShapeLegend && !showSizeLegend) { |
134 | 144 | chart.legend(false);
|
| 145 | + } else if (!showSizeLegend && sizeField) { |
| 146 | + chart.legend(sizeField, false); |
135 | 147 | }
|
136 | 148 |
|
137 | 149 | return params;
|
|
0 commit comments