Skip to content

Commit 397d864

Browse files
authored
fix:修复 玉珏图没有考虑isStack的情况 重新计算最大值 (#2685)
* fix:修复 玉珏图没有考虑isStack的情况 重新计算最大值 * fix: fix:修复 玉珏图没有考虑isStack的情况 重新计算最大值 -2 * fix:修复 玉珏图没有考虑isStack的情况 重新计算最大值 -2 * fix:修复 玉珏图没有考虑isStack的情况 重新计算最大值 -2 Co-authored-by: ai-qing-hai <[email protected]>
1 parent 86e2309 commit 397d864

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

__tests__/unit/plots/radial-bar/utils-spec.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { getScaleMax } from '../../../../src/plots/radial-bar/utils';
1+
import { getScaleMax, getScaleIsStackMax } from '../../../../src/plots/radial-bar/utils';
22
import { antvStar } from '../../../data/antv-star';
3+
import { populationMovementData as popData } from '../../../data/chord-population';
34

45
const yField = 'star';
56

7+
const popField = {
8+
yField: 'value',
9+
xField: 'source',
10+
};
11+
612
describe('utils of radial-bar', () => {
713
const starArr = antvStar.map((item) => item[yField]);
814
const maxValue = Math.max(...starArr);
@@ -21,4 +27,37 @@ describe('utils of radial-bar', () => {
2127
it('getScaleMax: empty array', () => {
2228
expect(getScaleMax(360, yField, [])).toBe(0);
2329
});
30+
31+
const popArr = popData
32+
.reduce((value, item) => {
33+
const valueItem = value.find((v) => v[popField.xField] === item[popField.xField]);
34+
if (valueItem) {
35+
valueItem[popField.yField] += item[popField.yField];
36+
} else {
37+
value.push({ ...item });
38+
}
39+
return value;
40+
}, [])
41+
.map((item) => item[popField.yField]);
42+
43+
const isStackMaxValue = Math.max(...popArr);
44+
45+
it('getScaleIsStackMax: normal', () => {
46+
expect(getScaleIsStackMax(360, popField.yField, popField.xField, popData)).toBe(isStackMaxValue);
47+
expect(getScaleIsStackMax(-300, popField.yField, popField.xField, popData)).toBe((isStackMaxValue * 360) / 300);
48+
expect(getScaleIsStackMax(660, popField.yField, popField.xField, popData)).toBe((isStackMaxValue * 360) / 300);
49+
});
50+
51+
it('getScaleIsStackMax: existed nil value', () => {
52+
expect(
53+
getScaleIsStackMax(-300, popField.yField, popField.xField, [...popData, { source: 'c', value: undefined }])
54+
).toBe((isStackMaxValue * 360) / 300);
55+
expect(getScaleIsStackMax(-300, popField.yField, popField.xField, [...popData, { source: 'c', value: null }])).toBe(
56+
(isStackMaxValue * 360) / 300
57+
);
58+
});
59+
60+
it('getScaleIsStackMax: empty array', () => {
61+
expect(getScaleIsStackMax(360, popField.yField, popField.xField, [])).toBe(0);
62+
});
2463
});

src/plots/radial-bar/adaptor.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { flow, deepAssign, findGeometry, transformLabel } from '../../utils';
44
import { interval, point } from '../../adaptor/geometries';
55
import { processIllegalData } from '../../utils';
66
import { RadialBarOptions } from './types';
7-
import { getScaleMax } from './utils';
7+
import { getScaleMax, getScaleIsStackMax } from './utils';
88

99
/**
1010
* geometry 处理
@@ -49,14 +49,16 @@ function geometry(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
4949
*/
5050
export function meta(params: Params<RadialBarOptions>): Params<RadialBarOptions> {
5151
const { options } = params;
52-
const { yField, maxAngle, data } = options;
53-
52+
const { yField, xField, data, isStack, isGroup, colorField, maxAngle } = options;
5453
const processData = processIllegalData(data, yField);
5554
return flow(
5655
scale({
5756
[yField]: {
5857
min: 0,
59-
max: getScaleMax(maxAngle, yField, processData),
58+
max:
59+
isStack && !isGroup && colorField
60+
? getScaleIsStackMax(maxAngle, yField, xField, processData)
61+
: getScaleMax(maxAngle, yField, processData),
6062
},
6163
})
6264
)(params);

src/plots/radial-bar/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ export interface RadialBarOptions extends Options, Pick<BarOptions, 'barBackgrou
2424
readonly colorField?: string;
2525
/** 类型 */
2626
readonly type?: string;
27+
/** 叠加 */
28+
readonly isStack?: boolean;
29+
/** 分组 */
30+
readonly isGroup?: boolean;
2731
}

src/plots/radial-bar/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,17 @@ export function getScaleMax(maxAngle: number, yField: string, data: Data): numbe
99
}
1010
return (maxValue * 360) / formatRadian;
1111
}
12+
13+
// 获取isStack 下的最大值
14+
export function getScaleIsStackMax(maxAngle: number, yField: string, xField: string, data: Data) {
15+
const newData: Data = [];
16+
data.forEach((item) => {
17+
const valueItem = newData.find((v) => v[xField] === item[xField]);
18+
if (valueItem) {
19+
valueItem[yField] += item[yField];
20+
} else {
21+
newData.push({ ...item });
22+
}
23+
});
24+
return getScaleMax(maxAngle, yField, newData);
25+
}

0 commit comments

Comments
 (0)