From cd3be27cff74ca58f62ed54af415d262afe5bd37 Mon Sep 17 00:00:00 2001 From: Kasmine <736929286@qq.com> Date: Sun, 19 Jul 2020 13:55:40 +0800 Subject: [PATCH] feat(v2/pie-label): add pie options (#1296) Co-authored-by: kasmine<736929286@qq.com> --- __tests__/unit/plots/pie/index-spec.ts | 3 - __tests__/unit/plots/pie/label-spec.ts | 98 ++++++++++++++++++++++++++ src/plots/pie/adaptor.ts | 25 ++++++- 3 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 __tests__/unit/plots/pie/label-spec.ts diff --git a/__tests__/unit/plots/pie/index-spec.ts b/__tests__/unit/plots/pie/index-spec.ts index 8b76ca1294..ab5bc3b4c5 100644 --- a/__tests__/unit/plots/pie/index-spec.ts +++ b/__tests__/unit/plots/pie/index-spec.ts @@ -62,9 +62,6 @@ describe('pie', () => { const polarRadius = coordinate.getRadius(); expect(radius).toBeUndefined(); expect(polarRadius).toBeGreaterThan(0); - - const geometry = pie.chart.geometries[0]; - const elements = geometry.elements; }); it('innerRadius', () => { diff --git a/__tests__/unit/plots/pie/label-spec.ts b/__tests__/unit/plots/pie/label-spec.ts new file mode 100644 index 0000000000..3625d44d61 --- /dev/null +++ b/__tests__/unit/plots/pie/label-spec.ts @@ -0,0 +1,98 @@ +import { Pie } from '../../../../src'; +import { POSITIVE_NEGATIVE_DATA } from '../../../data/common'; +import { createDiv } from '../../../utils/dom'; +import { IGroup } from '@antv/g2/lib/dependents'; + +describe('pie label', () => { + const data = POSITIVE_NEGATIVE_DATA.filter((o) => o.value > 0).map((d, idx) => + idx === 1 ? { ...d, type: 'item1' } : d + ); + + const config = { + width: 400, + height: 300, + data, + angleField: 'value', + colorField: 'type', + radius: 0.8, + label: {}, + }; + + it('label: visible', () => { + const pie = new Pie(createDiv(), config); + + pie.render(); + + const geometry = pie.chart.geometries[0]; + const elements = geometry.elements; + const labelGroups = geometry.labelsContainer.getChildren(); + + expect(elements.length).toBe(data.length); + expect(labelGroups.length).toBe(data.length); + }); + + it.skip('label: single color(todo-hustcc: 没有 color 字段,label 显示错误)', () => { + const pie = new Pie(createDiv(), { + ...config, + colorField: null, + }); + + pie.render(); + + const geometry = pie.chart.geometries[0]; + const labelGroups = geometry.labelsContainer.getChildren(); + + expect(labelGroups.length).toBe(data.length); + }); + + it('label: custom content', () => { + const pie = new Pie(createDiv(), { + ...config, + label: { + content: (data, item, idx) => { + if (idx === 0 || idx === 1) { + return 'hello'; + } + const { type, value } = data; + return `${type}: ${value}`; + }, + }, + }); + + pie.render(); + + const geometry = pie.chart.geometries[0]; + const labelGroups = geometry.labelsContainer.getChildren(); + expect(labelGroups.length).toBe(data.length); + const label1 = (labelGroups[0] as IGroup).getChildren(); + expect(label1[0].get('type')).toBe('text'); + expect(label1[1].get('type')).toBe('path'); + expect(label1[0].attr('text')).toBe('hello'); + const label2 = (labelGroups[1] as IGroup).getChildren(); + expect(label2[0].attr('text')).toBe('hello'); + }); + + it('label: custom callback', () => { + const pie = new Pie(createDiv(), { + ...config, + label: { + callback: (value, type) => { + return { + content: `${type}: ${value}`, + }; + }, + }, + }); + + pie.render(); + + const geometry = pie.chart.geometries[0]; + const labelGroups = geometry.labelsContainer.getChildren(); + + expect(labelGroups.length).toBe(data.length); + const label1 = (labelGroups[0] as IGroup).getChildren(); + const label3 = (labelGroups[2] as IGroup).getChildren(); + expect(label1[0].attr('text')).toBe(`${data[0].type}: ${data[0].value}`); + expect(label3[0].attr('text')).toBe(`${data[2].type}: ${data[2].value}`); + }); +}); diff --git a/src/plots/pie/adaptor.ts b/src/plots/pie/adaptor.ts index f4335aaf13..1796c8b098 100644 --- a/src/plots/pie/adaptor.ts +++ b/src/plots/pie/adaptor.ts @@ -74,6 +74,29 @@ function legend(params: Params): Params { return params; } +/** + * label 配置 + * @param params + */ +function label(params: Params): Params { + const { chart, options } = params; + const { label, colorField, angleField } = options; + + const geometry = chart.geometries[0]; + // label 为 false, 空 则不显示 label + if (!label) { + geometry.label(false); + } else { + const { callback, ...cfg } = label; + geometry.label({ + fields: [angleField, colorField], + callback, + cfg, + }); + } + return params; +} + /** * style 配置 * @param params @@ -102,5 +125,5 @@ function style(params: Params): Params { */ export function adaptor(params: Params) { // flow 的方式处理所有的配置到 G2 API - flow(field, meta, coord, legend, tooltip, style)(params); + flow(field, meta, coord, legend, tooltip, label, style)(params); }