|
| 1 | +import { Band, Constant } from '@antv/scale'; |
| 2 | +import { groupNameOf, dataOf, seriesOf } from '../../../src/utils/helper'; |
| 3 | +import { Chart } from '../../../src'; |
| 4 | +import { createNodeGCanvas } from '../../integration/utils/createNodeGCanvas'; |
| 5 | +import { G2Element } from '../../../src/utils/selection'; |
| 6 | + |
| 7 | +describe('groupNameOf', () => { |
| 8 | + it('should handle band series scale', () => { |
| 9 | + const seriesScale = new Band(); |
| 10 | + seriesScale.update({ domain: ['A', 'B', 'C', 'D'], range: [0, 1] }); |
| 11 | + const scale = { series: seriesScale }; |
| 12 | + const datum = { series: 0.25 }; |
| 13 | + expect(groupNameOf(scale, datum)).toBe('B'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return null for constant scale', () => { |
| 17 | + const seriesScale = new Constant(); |
| 18 | + seriesScale.update({ domain: [1], range: [1] }); |
| 19 | + const scale = { series: seriesScale }; |
| 20 | + const datum = { series: 1 }; |
| 21 | + expect(groupNameOf(scale, datum)).toBeNull(); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('seriesOf', () => { |
| 26 | + const chart = new Chart({ |
| 27 | + canvas: createNodeGCanvas(640, 480), |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return null for non-series mark', async () => { |
| 31 | + const data = [ |
| 32 | + { x: 1, y: 10 }, |
| 33 | + { x: 2, y: 20 }, |
| 34 | + ]; |
| 35 | + chart.options({ |
| 36 | + type: 'interval', |
| 37 | + data, |
| 38 | + encode: { |
| 39 | + x: 'x', |
| 40 | + y: 'y', |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + await chart.render(); |
| 45 | + |
| 46 | + const rects = chart |
| 47 | + .getContext() |
| 48 | + ?.canvas?.document.getElementsByClassName('element'); |
| 49 | + const firstRect = rects?.[0] as G2Element; |
| 50 | + const name = seriesOf(firstRect); |
| 51 | + |
| 52 | + expect(name).toBe(null); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should get series value from grouped data', async () => { |
| 56 | + const data = [ |
| 57 | + { year: '2020', value: 10, type: 'A' }, |
| 58 | + { year: '2020', value: 20, type: 'B' }, |
| 59 | + { year: '2021', value: 15, type: 'A' }, |
| 60 | + { year: '2021', value: 25, type: 'B' }, |
| 61 | + ]; |
| 62 | + |
| 63 | + chart.options({ |
| 64 | + type: 'interval', |
| 65 | + data, |
| 66 | + encode: { |
| 67 | + x: 'year', |
| 68 | + y: 'value', |
| 69 | + color: 'type', |
| 70 | + series: 'type', |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + await chart.render(); |
| 75 | + |
| 76 | + const rects = chart |
| 77 | + .getContext() |
| 78 | + ?.canvas?.document.getElementsByClassName('element'); |
| 79 | + const firstRect = rects?.[1] as G2Element; |
| 80 | + const name = seriesOf(firstRect); |
| 81 | + |
| 82 | + expect(name).toBe('B'); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('dataOf', () => { |
| 87 | + const chart = new Chart({ |
| 88 | + canvas: createNodeGCanvas(640, 480), |
| 89 | + }); |
| 90 | + |
| 91 | + it('should get single data point from interval element', async () => { |
| 92 | + const data = [ |
| 93 | + { x: 1, y: 10 }, |
| 94 | + { x: 2, y: 20 }, |
| 95 | + ]; |
| 96 | + chart.options({ |
| 97 | + type: 'interval', |
| 98 | + data, |
| 99 | + encode: { |
| 100 | + x: 'x', |
| 101 | + y: 'y', |
| 102 | + }, |
| 103 | + }); |
| 104 | + |
| 105 | + await chart.render(); |
| 106 | + |
| 107 | + const rects = chart |
| 108 | + .getContext() |
| 109 | + ?.canvas?.document.getElementsByClassName('element'); |
| 110 | + const firstRect = rects?.[0] as G2Element; |
| 111 | + const originalData = dataOf(firstRect); |
| 112 | + |
| 113 | + expect(originalData).toEqual(data[0]); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should get series data from grouped interval element', async () => { |
| 117 | + const data = [ |
| 118 | + { year: '2020', value: 10, type: 'A' }, |
| 119 | + { year: '2020', value: 20, type: 'B' }, |
| 120 | + { year: '2021', value: 15, type: 'A' }, |
| 121 | + { year: '2021', value: 25, type: 'B' }, |
| 122 | + ]; |
| 123 | + |
| 124 | + chart.options({ |
| 125 | + type: 'interval', |
| 126 | + data, |
| 127 | + encode: { |
| 128 | + x: 'year', |
| 129 | + y: 'value', |
| 130 | + color: 'type', |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + await chart.render(); |
| 135 | + |
| 136 | + const rects = chart |
| 137 | + .getContext() |
| 138 | + ?.canvas?.document.getElementsByClassName('element'); |
| 139 | + const firstRect = rects?.[0] as G2Element; |
| 140 | + const seriesData = dataOf(firstRect); |
| 141 | + |
| 142 | + expect(seriesData).toEqual({ year: '2020', value: 10, type: 'A' }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments