|
| 1 | +import { Pie } from '../../../../src'; |
| 2 | +import { POSITIVE_NEGATIVE_DATA } from '../../../data/common'; |
| 3 | +import { createDiv } from '../../../utils/dom'; |
| 4 | + |
| 5 | +describe('pie', () => { |
| 6 | + const data = POSITIVE_NEGATIVE_DATA.filter((o) => o.value > 0).map((d, idx) => |
| 7 | + idx === 1 ? { ...d, type: 'item1' } : d |
| 8 | + ); |
| 9 | + it('angleField: single color', () => { |
| 10 | + const pie = new Pie(createDiv(), { |
| 11 | + width: 400, |
| 12 | + height: 300, |
| 13 | + data, |
| 14 | + angleField: 'value', |
| 15 | + radius: 0.8, |
| 16 | + }); |
| 17 | + |
| 18 | + pie.render(); |
| 19 | + |
| 20 | + const geometry = pie.chart.geometries[0]; |
| 21 | + const elements = geometry.elements; |
| 22 | + |
| 23 | + expect(elements.length).toBe(data.length); |
| 24 | + expect(elements[0].getModel().color).toBe(elements[1].getModel().color); |
| 25 | + }); |
| 26 | + |
| 27 | + it('angleField with colorField: multiple colors', () => { |
| 28 | + const pie = new Pie(createDiv(), { |
| 29 | + width: 400, |
| 30 | + height: 300, |
| 31 | + data, |
| 32 | + angleField: 'value', |
| 33 | + colorField: 'type', |
| 34 | + color: ['blue', 'red', 'yellow', 'lightgreen', 'lightblue', 'pink'], |
| 35 | + radius: 0.8, |
| 36 | + }); |
| 37 | + |
| 38 | + pie.render(); |
| 39 | + |
| 40 | + const geometry = pie.chart.geometries[0]; |
| 41 | + const elements = geometry.elements; |
| 42 | + // @ts-ignore |
| 43 | + expect(elements.length).toBe(data.length); |
| 44 | + // 绘图数据 |
| 45 | + expect(elements[0].getModel().style?.fill || elements[0].getModel().color).toBe('blue'); |
| 46 | + expect(elements[1].getModel().style?.fill || elements[1].getModel().color).toBe('red'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('no radius', () => { |
| 50 | + const pie = new Pie(createDiv(), { |
| 51 | + width: 400, |
| 52 | + height: 300, |
| 53 | + data, |
| 54 | + angleField: 'value', |
| 55 | + colorField: 'type', |
| 56 | + }); |
| 57 | + |
| 58 | + pie.render(); |
| 59 | + |
| 60 | + const coordinate = pie.chart.getCoordinate(); |
| 61 | + const { radius } = coordinate; |
| 62 | + const polarRadius = coordinate.getRadius(); |
| 63 | + expect(radius).toBeUndefined(); |
| 64 | + expect(polarRadius).toBeGreaterThan(0); |
| 65 | + |
| 66 | + const geometry = pie.chart.geometries[0]; |
| 67 | + const elements = geometry.elements; |
| 68 | + }); |
| 69 | + |
| 70 | + it('innerRadius', () => { |
| 71 | + const pie = new Pie(createDiv(), { |
| 72 | + width: 400, |
| 73 | + height: 300, |
| 74 | + data, |
| 75 | + angleField: 'value', |
| 76 | + colorField: 'type', |
| 77 | + color: ['blue', 'red', 'yellow', 'lightgreen', 'lightblue', 'pink'], |
| 78 | + radius: 0.8, |
| 79 | + innerRadius: 0.5, |
| 80 | + }); |
| 81 | + |
| 82 | + pie.render(); |
| 83 | + |
| 84 | + const coordinate = pie.chart.getCoordinate(); |
| 85 | + const { innerRadius, radius } = coordinate; |
| 86 | + expect(innerRadius).toBe((radius / 0.8) * 0.5); |
| 87 | + }); |
| 88 | + |
| 89 | + it('pieStyle: custom style of pie', () => { |
| 90 | + const pie = new Pie(createDiv(), { |
| 91 | + width: 400, |
| 92 | + height: 300, |
| 93 | + data, |
| 94 | + angleField: 'value', |
| 95 | + colorField: 'type', |
| 96 | + color: ['blue', 'red', 'yellow', 'lightgreen', 'lightblue', 'pink'], |
| 97 | + radius: 0.8, |
| 98 | + innerRadius: 0.5, |
| 99 | + pieStyle: { |
| 100 | + fill: 'red', |
| 101 | + lineWidth: 3, |
| 102 | + stroke: 'yellow', |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + pie.render(); |
| 107 | + |
| 108 | + const geometry = pie.chart.geometries[0]; |
| 109 | + const elements = geometry.elements; |
| 110 | + expect(elements[0].getModel().style?.fill).toBe('red'); |
| 111 | + expect(elements[1].getModel().style?.fill).toBe('red'); |
| 112 | + expect(elements[1].getModel().style?.lineWidth).toBe(3); |
| 113 | + expect(elements[1].getModel().style?.stroke).toBe('yellow'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('pieStyle: with callback', () => { |
| 117 | + const pie = new Pie(createDiv(), { |
| 118 | + width: 400, |
| 119 | + height: 300, |
| 120 | + data, |
| 121 | + angleField: 'value', |
| 122 | + colorField: 'type', |
| 123 | + color: ['blue', 'red', 'yellow', 'lightgreen', 'lightblue', 'pink'], |
| 124 | + radius: 0.8, |
| 125 | + innerRadius: 0.5, |
| 126 | + pieStyle: (item) => ({ |
| 127 | + fill: item === 'item1' ? 'blue' : 'red', |
| 128 | + lineWidth: 3, |
| 129 | + stroke: 'yellow', |
| 130 | + }), |
| 131 | + }); |
| 132 | + |
| 133 | + pie.render(); |
| 134 | + |
| 135 | + const geometry = pie.chart.geometries[0]; |
| 136 | + const elements = geometry.elements; |
| 137 | + expect(elements[0].getModel().style?.fill).toBe('red'); |
| 138 | + expect(elements[1].getModel().style?.fill).toBe('blue'); |
| 139 | + expect(elements[2].getModel().style?.fill).toBe('red'); |
| 140 | + }); |
| 141 | +}); |
0 commit comments