Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(legend): support legend.symbolRotate and inherit from series by default #14876

Merged
merged 7 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor(legend): rename variables
  • Loading branch information
Ovilia committed May 14, 2021
commit 8d047cb9238b4ea54d7e9d2606f38cff22ea2442
8 changes: 4 additions & 4 deletions src/chart/line/LineSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import type Cartesian2D from '../../coord/cartesian/Cartesian2D';
import type Polar from '../../coord/polar/Polar';
import {createSymbol, ECSymbol} from '../../util/symbol';
import {Group} from '../../util/graphic';
import {LegendSymbolParams} from '../../component/legend/LegendModel';
import {LegendIconParams} from '../../component/legend/LegendModel';

type LineDataValue = OptionDataValue | OptionDataValue[];

Expand Down Expand Up @@ -210,7 +210,7 @@ class LineSeriesModel extends SeriesModel<LineSeriesOption> {
hoverLayerThreshold: Infinity
};

getLegendIcon(opt: LegendSymbolParams): ECSymbol | Group {
getLegendIcon(opt: LegendIconParams): ECSymbol | Group {
const group = new Group();

const line = createSymbol(
Expand Down Expand Up @@ -243,9 +243,9 @@ class LineSeriesModel extends SeriesModel<LineSeriesOption> {

symbol.setStyle(opt.itemStyle);

const symbolRotate = opt.symbolRotate === 'inherit'
const symbolRotate = opt.iconRotate === 'inherit'
? visualRotate
: (opt.symbolRotate || 0);
: (opt.iconRotate || 0);
symbol.rotation = symbolRotate * Math.PI / 180;
symbol.setOrigin([opt.itemWidth / 2, opt.itemHeight / 2]);

Expand Down
25 changes: 13 additions & 12 deletions src/chart/map/MapSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import Model from '../../model/Model';
import Geo from '../../coord/geo/Geo';
import { createTooltipMarkup } from '../../component/tooltip/tooltipMarkup';
import {createSymbol, ECSymbol} from '../../util/symbol';
import {LegendSymbolParams} from '../../component/legend/LegendModel';
import {LegendIconParams} from '../../component/legend/LegendModel';
import {Group} from '../../util/graphic';

export interface MapStateOption {
Expand Down Expand Up @@ -227,27 +227,28 @@ class MapSeries extends SeriesModel<MapSeriesOption> {
this.option.center = center;
}

getLegendIcon(opt: LegendSymbolParams): ECSymbol | Group {
const symbolType = opt.symbolType || 'roundRect';
const symbol = createSymbol(
symbolType,
getLegendIcon(opt: LegendIconParams): ECSymbol | Group {
const iconType = opt.icon || 'roundRect';
const icon = createSymbol(
iconType,
0,
0,
opt.itemWidth,
opt.itemHeight,
opt.itemStyle.fill
);

symbol.setStyle(opt.itemStyle);
icon.setStyle(opt.itemStyle);
// Map do not use itemStyle.borderWidth as border width
symbol.style.stroke = 'none';
icon.style.stroke = 'none';
// No rotation because no series visual symbol for map

if (symbolType.indexOf('empty') > -1) {
symbol.style.stroke = symbol.style.fill;
symbol.style.fill = '#fff';
symbol.style.lineWidth = 2;
if (iconType.indexOf('empty') > -1) {
icon.style.stroke = icon.style.fill;
icon.style.fill = '#fff';
icon.style.lineWidth = 2;
}
return symbol;
return icon;
}

static defaultOption: MapSeriesOption = {
Expand Down
6 changes: 3 additions & 3 deletions src/component/legend/LegendModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ export interface LegendTooltipFormatterParams {
$vars: ['name']
}

export interface LegendSymbolParams {
export interface LegendIconParams {
itemWidth: number,
itemHeight: number,
/**
* symbolType is from legend.icon, legend.data.icon, or series visual
*/
symbolType: string,
symbolRotate: number | 'inherit',
icon: string,
iconRotate: number | 'inherit',
itemStyle: PathStyleProps,
lineStyle: LineStyleProps
}
Expand Down
61 changes: 31 additions & 30 deletions src/component/legend/LegendView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import LegendModel, {
LegendLineStyleOption,
LegendOption,
LegendSelectorButtonOption,
LegendSymbolParams,
LegendIconParams,
LegendTooltipFormatterParams
} from './LegendModel';
import GlobalModel from '../../model/Global';
Expand Down Expand Up @@ -334,7 +334,7 @@ class LegendView extends ComponentView {
seriesModel: SeriesModel<SeriesOption & SymbolOptionMixin>,
name: string,
dataIndex: number,
itemModel: LegendModel['_data'][number],
legendItemModel: LegendModel['_data'][number],
legendModel: LegendModel,
itemAlign: LegendOption['align'],
lineVisualStyle: LineStyleProps,
Expand All @@ -347,15 +347,15 @@ class LegendView extends ComponentView {
const itemHeight = legendModel.get('itemHeight');
const isSelected = legendModel.isSelected(name);

let symbolRotate = itemModel.get('symbolRotate');
let iconRotate = legendItemModel.get('symbolRotate');

const legendIconType = itemModel.get('icon');
const legendIconType = legendItemModel.get('icon');
legendIcon = legendIconType || legendIcon || 'roundRect';

const legendLineStyle = legendModel.getModel('lineStyle');
const style = getLegendStyle(
legendIcon,
itemModel,
legendItemModel,
legendLineStyle,
lineVisualStyle,
itemVisualStyle,
Expand All @@ -365,7 +365,7 @@ class LegendView extends ComponentView {

const itemGroup = new Group();

const textStyleModel = itemModel.getModel('textStyle');
const textStyleModel = legendItemModel.getModel('textStyle');

if (typeof seriesModel.getLegendIcon === 'function'
&& (!legendIconType || legendIconType === 'inherit')
Expand All @@ -374,24 +374,25 @@ class LegendView extends ComponentView {
itemGroup.add(seriesModel.getLegendIcon({
itemWidth,
itemHeight,
symbolType: legendIcon,
symbolRotate,
icon: legendIcon,
iconRotate: iconRotate,
itemStyle: style.itemStyle,
lineStyle: style.lineStyle
}));
}
else {
// Use default legend icon policy for most series
const rotate = legendIconType === 'inherit' && legendIcon
? (symbolRotate === 'inherit'
? seriesModel.get('symbolRotate')
: symbolRotate)
const rotate = legendIconType === 'inherit' && seriesModel.getData().getVisual('symbol')
? (iconRotate === 'inherit'
? seriesModel.getData().getVisual('symbolRotate')
: iconRotate
)
: 0; // No rotation for no icon
itemGroup.add(getDefaultLegendIcon({
itemWidth,
itemHeight,
symbolType: legendIcon,
symbolRotate: rotate,
icon: legendIcon,
iconRotate: rotate,
itemStyle: style.itemStyle,
lineStyle: style.lineStyle
}));
Expand All @@ -409,7 +410,7 @@ class LegendView extends ComponentView {
content = formatter(name);
}

const inactiveColor = itemModel.get('inactiveColor');
const inactiveColor = legendItemModel.get('inactiveColor');
itemGroup.add(new graphic.Text({
style: createTextStyle(textStyleModel, {
text: content,
Expand All @@ -427,7 +428,7 @@ class LegendView extends ComponentView {
invisible: true
});

const tooltipModel = itemModel.getModel('tooltip') as Model<CommonTooltipOption<LegendTooltipFormatterParams>>;
const tooltipModel = legendItemModel.getModel('tooltip') as Model<CommonTooltipOption<LegendTooltipFormatterParams>>;
if (tooltipModel.get('show')) {
graphic.setTooltipConfig({
el: hitRect,
Expand Down Expand Up @@ -536,7 +537,7 @@ class LegendView extends ComponentView {
}

function getLegendStyle(
symbolType: string,
iconType: string,
legendModel: LegendModel['_data'][number],
legendLineStyle: Model<LegendLineStyleOption>,
lineVisualStyle: LineStyleProps,
Expand Down Expand Up @@ -573,11 +574,11 @@ function getLegendStyle(

case 'stroke':
/**
* symbol type with "emptyXXX" should use fill color
* icon type with "emptyXXX" should use fill color
* in visual style
*/
itemStyle.stroke = itemVisualStyle[
symbolType.lastIndexOf('empty', 0) === 0 ? 'fill' : 'stroke'
iconType.lastIndexOf('empty', 0) === 0 ? 'fill' : 'stroke'
];
break;

Expand Down Expand Up @@ -636,7 +637,7 @@ function getLegendStyle(
* there is no border in series but border in legend, so we need to
* use border only when series has border if is set to be auto
*/
const visualHasBorder = itemStyle[symbolType.indexOf('empty') > -1 ? 'fill' : 'stroke'];
const visualHasBorder = itemStyle[iconType.indexOf('empty') > -1 ? 'fill' : 'stroke'];
itemStyle.lineWidth = borderWidth === 'auto'
? (itemVisualStyle.lineWidth > 0 && visualHasBorder ? 2 : 0)
: itemStyle.lineWidth;
Expand All @@ -648,9 +649,9 @@ function getLegendStyle(
return { itemStyle, lineStyle };
}

function getDefaultLegendIcon(opt: LegendSymbolParams): ECSymbol {
const symboType = opt.symbolType || 'roundRect';
const symbol = createSymbol(
function getDefaultLegendIcon(opt: LegendIconParams): ECSymbol {
const symboType = opt.icon || 'roundRect';
const icon = createSymbol(
symboType,
0,
0,
Expand All @@ -659,18 +660,18 @@ function getDefaultLegendIcon(opt: LegendSymbolParams): ECSymbol {
opt.itemStyle.fill
);

symbol.setStyle(opt.itemStyle);
icon.setStyle(opt.itemStyle);

symbol.rotation = (opt.symbolRotate as number || 0) * Math.PI / 180;
symbol.setOrigin([opt.itemWidth / 2, opt.itemHeight / 2]);
icon.rotation = (opt.iconRotate as number || 0) * Math.PI / 180;
icon.setOrigin([opt.itemWidth / 2, opt.itemHeight / 2]);

if (symboType.indexOf('empty') > -1) {
symbol.style.stroke = symbol.style.fill;
symbol.style.fill = '#fff';
symbol.style.lineWidth = 2;
icon.style.stroke = icon.style.fill;
icon.style.fill = '#fff';
icon.style.lineWidth = 2;
}

return symbol;
return icon;
}

function dispatchSelectAction(
Expand Down
4 changes: 2 additions & 2 deletions src/model/Series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { Source } from '../data/Source';
import { defaultSeriesFormatTooltip } from '../component/tooltip/seriesFormatTooltip';
import {ECSymbol} from '../util/symbol';
import {Group} from '../util/graphic';
import {LegendSymbolParams} from '../component/legend/LegendModel';
import {LegendIconParams} from '../component/legend/LegendModel';

const inner = modelUtil.makeInner<{
data: List
Expand Down Expand Up @@ -96,7 +96,7 @@ interface SeriesModel {
/**
* Get legend icon symbol according to each series type
*/
getLegendIcon(opt: LegendSymbolParams): ECSymbol | Group;
getLegendIcon(opt: LegendIconParams): ECSymbol | Group;

/**
* See `component/brush/selector.js`
Expand Down