diff --git a/__tests__/unit/plots/radial-bar/utils-spec.ts b/__tests__/unit/plots/radial-bar/utils-spec.ts index 18ecac2d9f..14058a5aaa 100644 --- a/__tests__/unit/plots/radial-bar/utils-spec.ts +++ b/__tests__/unit/plots/radial-bar/utils-spec.ts @@ -1,8 +1,14 @@ -import { getScaleMax } from '../../../../src/plots/radial-bar/utils'; +import { getScaleMax, getScaleIsStackMax } from '../../../../src/plots/radial-bar/utils'; import { antvStar } from '../../../data/antv-star'; +import { populationMovementData as popData } from '../../../data/chord-population'; const yField = 'star'; +const popField = { + yField: 'value', + xField: 'source', +}; + describe('utils of radial-bar', () => { const starArr = antvStar.map((item) => item[yField]); const maxValue = Math.max(...starArr); @@ -21,4 +27,37 @@ describe('utils of radial-bar', () => { it('getScaleMax: empty array', () => { expect(getScaleMax(360, yField, [])).toBe(0); }); + + const popArr = popData + .reduce((value, item) => { + const valueItem = value.find((v) => v[popField.xField] === item[popField.xField]); + if (valueItem) { + valueItem[popField.yField] += item[popField.yField]; + } else { + value.push({ ...item }); + } + return value; + }, []) + .map((item) => item[popField.yField]); + + const isStackMaxValue = Math.max(...popArr); + + it('getScaleIsStackMax: normal', () => { + expect(getScaleIsStackMax(360, popField.yField, popField.xField, popData)).toBe(isStackMaxValue); + expect(getScaleIsStackMax(-300, popField.yField, popField.xField, popData)).toBe((isStackMaxValue * 360) / 300); + expect(getScaleIsStackMax(660, popField.yField, popField.xField, popData)).toBe((isStackMaxValue * 360) / 300); + }); + + it('getScaleIsStackMax: existed nil value', () => { + expect( + getScaleIsStackMax(-300, popField.yField, popField.xField, [...popData, { source: 'c', value: undefined }]) + ).toBe((isStackMaxValue * 360) / 300); + expect(getScaleIsStackMax(-300, popField.yField, popField.xField, [...popData, { source: 'c', value: null }])).toBe( + (isStackMaxValue * 360) / 300 + ); + }); + + it('getScaleIsStackMax: empty array', () => { + expect(getScaleIsStackMax(360, popField.yField, popField.xField, [])).toBe(0); + }); }); diff --git a/src/plots/radial-bar/adaptor.ts b/src/plots/radial-bar/adaptor.ts index e0709c8a3a..3089e45977 100644 --- a/src/plots/radial-bar/adaptor.ts +++ b/src/plots/radial-bar/adaptor.ts @@ -4,7 +4,7 @@ import { flow, deepAssign, findGeometry, transformLabel } from '../../utils'; import { interval, point } from '../../adaptor/geometries'; import { processIllegalData } from '../../utils'; import { RadialBarOptions } from './types'; -import { getScaleMax } from './utils'; +import { getScaleMax, getScaleIsStackMax } from './utils'; /** * geometry 处理 @@ -49,14 +49,16 @@ function geometry(params: Params): Params { */ export function meta(params: Params): Params { const { options } = params; - const { yField, maxAngle, data } = options; - + const { yField, xField, data, isStack, isGroup, colorField, maxAngle } = options; const processData = processIllegalData(data, yField); return flow( scale({ [yField]: { min: 0, - max: getScaleMax(maxAngle, yField, processData), + max: + isStack && !isGroup && colorField + ? getScaleIsStackMax(maxAngle, yField, xField, processData) + : getScaleMax(maxAngle, yField, processData), }, }) )(params); diff --git a/src/plots/radial-bar/types.ts b/src/plots/radial-bar/types.ts index 6bacfe18ed..aed657b895 100644 --- a/src/plots/radial-bar/types.ts +++ b/src/plots/radial-bar/types.ts @@ -24,4 +24,8 @@ export interface RadialBarOptions extends Options, Pick { + const valueItem = newData.find((v) => v[xField] === item[xField]); + if (valueItem) { + valueItem[yField] += item[yField]; + } else { + newData.push({ ...item }); + } + }); + return getScaleMax(maxAngle, yField, newData); +}