|
| 1 | +import { Pie } from '../../../../src'; |
| 2 | +import { POSITIVE_NEGATIVE_DATA } from '../../../data/common'; |
| 3 | +import { createDiv } from '../../../utils/dom'; |
| 4 | +import { IGroup } from '@antv/g2/lib/dependents'; |
| 5 | + |
| 6 | +describe('pie label', () => { |
| 7 | + const data = POSITIVE_NEGATIVE_DATA.filter((o) => o.value > 0).map((d, idx) => |
| 8 | + idx === 1 ? { ...d, type: 'item1' } : d |
| 9 | + ); |
| 10 | + |
| 11 | + const config = { |
| 12 | + width: 400, |
| 13 | + height: 300, |
| 14 | + data, |
| 15 | + angleField: 'value', |
| 16 | + colorField: 'type', |
| 17 | + radius: 0.8, |
| 18 | + label: {}, |
| 19 | + }; |
| 20 | + |
| 21 | + it('label: visible', () => { |
| 22 | + const pie = new Pie(createDiv(), config); |
| 23 | + |
| 24 | + pie.render(); |
| 25 | + |
| 26 | + const geometry = pie.chart.geometries[0]; |
| 27 | + const elements = geometry.elements; |
| 28 | + const labelGroups = geometry.labelsContainer.getChildren(); |
| 29 | + |
| 30 | + expect(elements.length).toBe(data.length); |
| 31 | + expect(labelGroups.length).toBe(data.length); |
| 32 | + }); |
| 33 | + |
| 34 | + it.skip('label: single color(todo-hustcc: 没有 color 字段,label 显示错误)', () => { |
| 35 | + const pie = new Pie(createDiv(), { |
| 36 | + ...config, |
| 37 | + colorField: null, |
| 38 | + }); |
| 39 | + |
| 40 | + pie.render(); |
| 41 | + |
| 42 | + const geometry = pie.chart.geometries[0]; |
| 43 | + const labelGroups = geometry.labelsContainer.getChildren(); |
| 44 | + |
| 45 | + expect(labelGroups.length).toBe(data.length); |
| 46 | + }); |
| 47 | + |
| 48 | + it('label: custom content', () => { |
| 49 | + const pie = new Pie(createDiv(), { |
| 50 | + ...config, |
| 51 | + label: { |
| 52 | + content: (data, item, idx) => { |
| 53 | + if (idx === 0 || idx === 1) { |
| 54 | + return 'hello'; |
| 55 | + } |
| 56 | + const { type, value } = data; |
| 57 | + return `${type}: ${value}`; |
| 58 | + }, |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + pie.render(); |
| 63 | + |
| 64 | + const geometry = pie.chart.geometries[0]; |
| 65 | + const labelGroups = geometry.labelsContainer.getChildren(); |
| 66 | + expect(labelGroups.length).toBe(data.length); |
| 67 | + const label1 = (labelGroups[0] as IGroup).getChildren(); |
| 68 | + expect(label1[0].get('type')).toBe('text'); |
| 69 | + expect(label1[1].get('type')).toBe('path'); |
| 70 | + expect(label1[0].attr('text')).toBe('hello'); |
| 71 | + const label2 = (labelGroups[1] as IGroup).getChildren(); |
| 72 | + expect(label2[0].attr('text')).toBe('hello'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('label: custom callback', () => { |
| 76 | + const pie = new Pie(createDiv(), { |
| 77 | + ...config, |
| 78 | + label: { |
| 79 | + callback: (value, type) => { |
| 80 | + return { |
| 81 | + content: `${type}: ${value}`, |
| 82 | + }; |
| 83 | + }, |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + pie.render(); |
| 88 | + |
| 89 | + const geometry = pie.chart.geometries[0]; |
| 90 | + const labelGroups = geometry.labelsContainer.getChildren(); |
| 91 | + |
| 92 | + expect(labelGroups.length).toBe(data.length); |
| 93 | + const label1 = (labelGroups[0] as IGroup).getChildren(); |
| 94 | + const label3 = (labelGroups[2] as IGroup).getChildren(); |
| 95 | + expect(label1[0].attr('text')).toBe(`${data[0].type}: ${data[0].value}`); |
| 96 | + expect(label3[0].attr('text')).toBe(`${data[2].type}: ${data[2].value}`); |
| 97 | + }); |
| 98 | +}); |
0 commit comments