Skip to content

Commit

Permalink
refactor(legend): render legend in legendView when icon is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovilia committed Apr 7, 2021
1 parent da6d040 commit 3966af8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
34 changes: 14 additions & 20 deletions src/chart/line/LineSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -217,27 +215,23 @@ class LineSeriesModel extends SeriesModel<LineSeriesOption> {
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,
Expand Down
5 changes: 0 additions & 5 deletions src/component/legend/LegendModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/component/legend/LegendView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 3966af8

Please sign in to comment.