|
| 1 | +import { Bar } from '../../../../src'; |
| 2 | +import { salesByArea } from '../../../data/sales'; |
| 3 | +import { createDiv } from '../../../utils/dom'; |
| 4 | + |
| 5 | +describe('bar', () => { |
| 6 | + it('x*y', () => { |
| 7 | + const bar = new Bar(createDiv(), { |
| 8 | + width: 400, |
| 9 | + height: 300, |
| 10 | + data: salesByArea, |
| 11 | + xField: 'area', |
| 12 | + yField: 'sales', |
| 13 | + }); |
| 14 | + |
| 15 | + bar.render(); |
| 16 | + |
| 17 | + const geometry = bar.chart.geometries[0]; |
| 18 | + const positionFields = geometry.getAttribute('position').getFields(); |
| 19 | + |
| 20 | + // 类型 |
| 21 | + expect(geometry.type).toBe('interval'); |
| 22 | + // 图形元素个数 |
| 23 | + expect(bar.chart.geometries[0].elements.length).toBe(salesByArea.length); |
| 24 | + // x & y |
| 25 | + expect(positionFields).toHaveLength(2); |
| 26 | + expect(positionFields[0]).toBe('area'); |
| 27 | + expect(positionFields[1]).toBe('sales'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('x*y*color', () => { |
| 31 | + const bar = new Bar(createDiv(), { |
| 32 | + width: 400, |
| 33 | + height: 300, |
| 34 | + data: salesByArea, |
| 35 | + xField: 'area', |
| 36 | + yField: 'sales', |
| 37 | + colorField: 'area', |
| 38 | + }); |
| 39 | + |
| 40 | + bar.render(); |
| 41 | + |
| 42 | + const geometry = bar.chart.geometries[0]; |
| 43 | + const colorFields = geometry.getAttribute('color').getFields(); |
| 44 | + |
| 45 | + expect(colorFields).toHaveLength(1); |
| 46 | + expect(colorFields[0]).toBe('area'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('x*y*color with color', () => { |
| 50 | + const palette = ['red', 'yellow', 'green']; |
| 51 | + const bar = new Bar(createDiv(), { |
| 52 | + width: 400, |
| 53 | + height: 300, |
| 54 | + data: salesByArea, |
| 55 | + xField: 'area', |
| 56 | + yField: 'sales', |
| 57 | + colorField: 'area', |
| 58 | + color: palette, |
| 59 | + }); |
| 60 | + |
| 61 | + bar.render(); |
| 62 | + |
| 63 | + const geometry = bar.chart.geometries[0]; |
| 64 | + const colorAttribute = geometry.getAttribute('color'); |
| 65 | + const colorFields = colorAttribute.getFields(); |
| 66 | + |
| 67 | + expect(colorFields).toHaveLength(1); |
| 68 | + expect(colorFields[0]).toBe('area'); |
| 69 | + geometry.elements.forEach((element, index) => { |
| 70 | + const color = element.getModel().color; |
| 71 | + expect(color).toBe(palette[index % palette.length]); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments