Skip to content

Commit

Permalink
chore(rose): 完善玫瑰图单侧
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhonghe committed Oct 9, 2020
1 parent bff81c2 commit fa7b176
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 5 deletions.
114 changes: 114 additions & 0 deletions __tests__/unit/plots/rose/label-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,120 @@ describe('rose label', () => {
expect(geometry.labelOption.cfg).toBeUndefined();
});

it('null', () => {
const rose = new Rose(
createDiv('null'),
deepMix({}, options, {
label: null,
})
);

rose.render();
const geometry = rose.chart.geometries[0];

// @ts-ignore
expect(geometry.labelOption.cfg).toBeUndefined();
});

it('offset less 0', () => {
const rose = new Rose(
createDiv('offset'),
deepMix({}, options, {
label: {
offset: -1,
},
})
);

rose.render();
const geometry = rose.chart.geometries[0];

// @ts-ignore
expect(geometry.labelOption.cfg).toEqual({
offset: -1,
layout: { type: 'limit-in-shape' }, // 默认配置
});
});

it('layout is object', () => {
const rose = new Rose(
createDiv('layout'),
deepMix({}, options, {
label: {
fields: ['sales'],
layout: {
type: 'other',
},
},
})
);

rose.render();

const geometry = rose.chart.geometries[0];

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

it('layout is array', () => {
const rose = new Rose(
createDiv('layout'),
deepMix({}, options, {
label: {
fields: ['sales'],
position: 'top',
layout: [
{
type: 'limit-in-shape',
},
{
type: 'other',
},
],
},
})
);

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', () => {
const rose = new Rose(
createDiv('layout'),
deepMix({}, options, {
label: {
fields: ['sales'],
layout: null,
},
})
);

rose.render();

const geometry = rose.chart.geometries[0];

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

it('position top', () => {
const rose = new Rose(
createDiv('position top'),
Expand Down
13 changes: 8 additions & 5 deletions src/plots/rose/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeometryLabelLayoutCfg } from '@antv/g2/lib/interface';
import { deepMix, each, isObject, isArray } from '@antv/util';
import { deepMix, filter, isObject, isArray } from '@antv/util';
import { Params } from '../../core/adaptor';
import { flow, findGeometry, log, LEVEL } from '../../utils';
import { tooltip, interaction, animation, theme, scale, annotation, state } from '../../adaptor/common';
Expand Down Expand Up @@ -47,28 +47,31 @@ function label(params: Params<RoseOptions>): Params<RoseOptions> {
} else if (isObject(label)) {
const { callback, fields, ...cfg } = label;
const { offset, layout } = cfg;

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

geometry.label({
fields: fields || [xField],
callback,
cfg,
});
} else {
log(LEVEL.WARN, label === null || label === undefined, 'the label option must be an Object.');
log(LEVEL.WARN, label === null, 'the label option must be an Object.');
geometry.label({ fields: [xField] });
}

Expand Down

0 comments on commit fa7b176

Please sign in to comment.