From 3966af83865b861058f35dc2e29a415a18219cf7 Mon Sep 17 00:00:00 2001 From: Ovilia Date: Wed, 7 Apr 2021 12:20:14 +0800 Subject: [PATCH] refactor(legend): render legend in legendView when icon is defined --- src/chart/line/LineSeries.ts | 34 ++++++++++++----------------- src/component/legend/LegendModel.ts | 5 ----- src/component/legend/LegendView.ts | 8 +++---- 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/chart/line/LineSeries.ts b/src/chart/line/LineSeries.ts index 9441dc3d3..bd8ae1919 100644 --- a/src/chart/line/LineSeries.ts +++ b/src/chart/line/LineSeries.ts @@ -39,10 +39,8 @@ import { import List from '../../data/List'; import type Cartesian2D from '../../coord/cartesian/Cartesian2D'; import type Polar from '../../coord/polar/Polar'; -import {PathStyleProps} from 'zrender/src/graphic/Path'; import {createSymbol, ECSymbol} from '../../util/symbol'; import {Group} from '../../util/graphic'; -import {LineStyleProps} from '../../model/mixin/lineStyle'; import {LegendSymbolParams} from '../../component/legend/LegendModel'; type LineDataValue = OptionDataValue | OptionDataValue[]; @@ -217,27 +215,23 @@ class LineSeriesModel extends SeriesModel { getLegendIcon(opt: LegendSymbolParams): ECSymbol | Group { const group = new Group(); - // Draw line only if legend.icon is not defined - if (!opt.legendIconType) { - const line = createSymbol( - 'line', - 0, - opt.itemHeight / 2, - opt.itemWidth, - 0, - opt.lineStyle.stroke, - false - ); - group.add(line); - line.setStyle(opt.lineStyle); - } + const line = createSymbol( + 'line', + 0, + opt.itemHeight / 2, + opt.itemWidth, + 0, + opt.lineStyle.stroke, + false + ); + group.add(line); + line.setStyle(opt.lineStyle); - const visualType = opt.series.getData().getVisual('symbol'); - const symbolType = opt.legendIconType - || (visualType === 'none' ? 'circle' : visualType); + const visualType = this.getData().getVisual('symbol'); + const symbolType = visualType === 'none' ? 'circle' : visualType; // Symbol size is 80% when there is a line - const size = opt.itemHeight * (opt.legendIconType ? 1 : 0.8); + const size = opt.itemHeight * 0.8; const symbol = createSymbol( symbolType, (opt.itemWidth - size) / 2, diff --git a/src/component/legend/LegendModel.ts b/src/component/legend/LegendModel.ts index dfb7f3fce..ca5e09466 100644 --- a/src/component/legend/LegendModel.ts +++ b/src/component/legend/LegendModel.ts @@ -140,17 +140,12 @@ export interface LegendTooltipFormatterParams { } export interface LegendSymbolParams { - series: SeriesModel, itemWidth: number, itemHeight: number, /** * symbolType is from legend.icon, legend.data.icon, or series visual */ symbolType: string, - /** - * legendIconType is from legend.icon or legend.data.icon - */ - legendIconType: string, symbolKeepAspect: boolean, itemStyle: PathStyleProps, lineStyle: LineStyleProps diff --git a/src/component/legend/LegendView.ts b/src/component/legend/LegendView.ts index df979138d..8b0a77522 100644 --- a/src/component/legend/LegendView.ts +++ b/src/component/legend/LegendView.ts @@ -353,14 +353,14 @@ class LegendView extends ComponentView { const textStyleModel = itemModel.getModel('textStyle'); - if (typeof seriesModel.getLegendIcon === 'function') { + if (typeof seriesModel.getLegendIcon === 'function' + && !legendIconType + ) { // Series has specific way to define legend icon itemGroup.add(seriesModel.getLegendIcon({ - series: seriesModel, itemWidth, itemHeight, symbolType, - legendIconType: legendIconType, symbolKeepAspect, itemStyle: style.itemStyle, lineStyle: style.lineStyle @@ -369,11 +369,9 @@ class LegendView extends ComponentView { else { // Use default legend icon policy for most series itemGroup.add(getDefaultLegendIcon({ - series: seriesModel, itemWidth, itemHeight, symbolType, - legendIconType, symbolKeepAspect, itemStyle: style.itemStyle, lineStyle: style.lineStyle -- GitLab