Skip to content

Commit

Permalink
chore: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhonghe committed Oct 10, 2020
1 parent 496a39b commit 14e2346
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
11 changes: 2 additions & 9 deletions __tests__/unit/plots/rose/label-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('rose label', () => {

// @ts-ignore
expect(geometry.labelOption.cfg).toEqual({
layout: { type: 'other' },
layout: [{ type: 'other' }],
});
});

Expand All @@ -113,17 +113,12 @@ describe('rose label', () => {
rose.render();

const geometry = rose.chart.geometries[0];
const labelGroups = geometry.labelsContainer.getChildren();

// @ts-ignore
expect(geometry.labelOption.cfg).toEqual({
position: 'top',
layout: [{ type: 'other' }],
});
expect(labelGroups).toHaveLength(salesByArea.length);
labelGroups.forEach((label, index) => {
expect(label.get('children')[0].attr('text')).toBe(`${Math.floor(salesByArea[index].sales / 10000)}万`);
});
});

it('layout is null', () => {
Expand All @@ -142,9 +137,7 @@ describe('rose label', () => {
const geometry = rose.chart.geometries[0];

// @ts-ignore
expect(geometry.labelOption.cfg).toEqual({
layout: null,
});
expect(geometry.labelOption.cfg).toEqual({});
});

it('position top', () => {
Expand Down
19 changes: 5 additions & 14 deletions src/plots/rose/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { GeometryLabelLayoutCfg } from '@antv/g2/lib/interface';
import { deepMix, filter, isObject, isArray } from '@antv/util';
import { Params } from '../../core/adaptor';
import { flow, findGeometry, log, LEVEL } from '../../utils';
Expand Down Expand Up @@ -46,23 +45,15 @@ function label(params: Params<RoseOptions>): Params<RoseOptions> {
geometry.label(false);
} else if (isObject(label)) {
const { callback, fields, ...cfg } = label;
const { offset, layout } = cfg;
const { offset } = cfg;
let layout = cfg.layout;

// 当 label 在 shape 外部显示时,设置 'limit-in-shape' 会
// 造成 label 不显示。
if (offset === undefined || offset >= 0) {
if (isArray(layout)) {
cfg.layout = filter(layout, (value: GeometryLabelLayoutCfg) => {
if (value.type === 'limit-in-shape') {
return false;
}
return true;
});
} else if (isObject(layout)) {
if (layout.type === 'limit-in-shape') {
cfg.layout = undefined;
}
}
layout = layout ? (isArray(layout) ? layout : [layout]) : [];
cfg.layout = filter(layout, (v) => v.type !== 'limit-in-shape');
cfg.layout.length || delete cfg.layout;
}

geometry.label({
Expand Down

0 comments on commit 14e2346

Please sign in to comment.