diff --git a/__tests__/unit/designs/structures/compare-quadrant.test.tsx b/__tests__/unit/designs/structures/compare-quadrant.test.tsx new file mode 100644 index 000000000..4df495f14 --- /dev/null +++ b/__tests__/unit/designs/structures/compare-quadrant.test.tsx @@ -0,0 +1,132 @@ +/** @jsxImportSource ../../../../src */ +import { describe, expect, it } from 'vitest'; +import { + Rect, + renderSVG, + type ParsedInfographicOptions, +} from '../../../../src'; +import type { BaseStructureProps } from '../../../../src/designs/structures'; +import { Quadrant } from '../../../../src/designs/structures/compare-quadrant'; +import type { ParsedData } from '../../../../src/types'; + +const Item: BaseStructureProps['Item'] = ({ + x = 0, + y = 0, + datum, + indexes, + positionH, +}) => ( + +); + +function render( + items: Array<{ width: number; height: number }>, + dimensions = {}, +) { + const data = { items } as unknown as ParsedData; + const svg = renderSVG( + , + ); + return new DOMParser().parseFromString(svg, 'image/svg+xml'); +} + +function itemBounds(doc: Document) { + return [...doc.querySelectorAll('[data-quadrant-item]')].map((el) => ({ + x: Number(el.getAttribute('x')), + y: Number(el.getAttribute('y')), + width: Number(el.getAttribute('width')), + height: Number(el.getAttribute('height')), + })); +} + +const varied = [ + { width: 80, height: 40 }, + { width: 160, height: 120 }, + { width: 100, height: 200 }, + { width: 40, height: 60 }, +]; + +describe('Quadrant item bounds', () => { + it('sizes all quadrants to contain their largest rendered item', () => { + const doc = render(varied); + // The flipped second item is 180px wide, so each quadrant is 200x220. + const bounds = itemBounds(doc); + bounds.forEach((b, index) => { + const left = (index % 2) * 200; + const top = Math.floor(index / 2) * 220; + expect(b.x).toBeGreaterThanOrEqual(left + 10); + expect(b.y).toBeGreaterThanOrEqual(top + 10); + expect(b.x + b.width).toBeLessThanOrEqual(left + 190); + expect(b.y + b.height).toBeLessThanOrEqual(top + 210); + }); + }); + + it('centers each item using its own dimensions and rendered orientation', () => { + const bounds = itemBounds(render(varied)); + expect(bounds.map((b) => [b.x + b.width / 2, b.y + b.height / 2])).toEqual([ + [100, 110], + [300, 110], + [100, 330], + [300, 330], + ]); + }); + + it('preserves explicit quadrant dimensions while centering variable items', () => { + const bounds = itemBounds( + render(varied, { quadrantWidth: 300, quadrantHeight: 250 }), + ); + expect(bounds.map((b) => [b.x + b.width / 2, b.y + b.height / 2])).toEqual([ + [150, 125], + [450, 125], + [150, 375], + [450, 375], + ]); + }); + + it('only measures and renders the first four data items', () => { + const doc = render([...varied, { width: 10000, height: 10000 }]); + const bounds = itemBounds(doc); + expect(bounds).toHaveLength(4); + expect(bounds[0].x + bounds[0].width / 2).toBe(100); + }); + + it('keeps the existing padding for identical rendered dimensions', () => { + const PlainItem: BaseStructureProps['Item'] = ({ x = 0, y = 0 }) => ( + + ); + const data = { items: [{}, {}, {}, {}] } as ParsedData; + const doc = new DOMParser().parseFromString( + renderSVG( + , + ), + 'image/svg+xml', + ); + expect(itemBounds(doc).map(({ x, y }) => [x, y])).toEqual([ + [10, 10], + [110, 10], + [10, 70], + [110, 70], + ]); + }); + + it('does not render placeholder items for empty data', () => { + expect(itemBounds(render([]))).toEqual([]); + }); +}); diff --git a/src/designs/structures/compare-quadrant.tsx b/src/designs/structures/compare-quadrant.tsx index 16ab23209..855678b5e 100644 --- a/src/designs/structures/compare-quadrant.tsx +++ b/src/designs/structures/compare-quadrant.tsx @@ -42,20 +42,31 @@ export const Quadrant: ComponentType = (props) => { // 限制只显示4个象限数据,不足时填充空值 const quadrantItems = Array.from({ length: 4 }, (_, i) => items[i]); - // 计算单个象限项的尺寸 - 使用可选链处理空值 - const itemBounds = quadrantItems[0] - ? getElementBounds( - , - ) - : { width: 0, height: 0 }; + // 按照实际渲染方向测量每个象限项。 + const itemBounds = quadrantItems.map((item, index) => + item + ? getElementBounds( + , + ) + : { width: 0, height: 0 }, + ); // 基于 Item 包围盒计算默认象限尺寸 // 横向基于 Item 的宽,纵向基于 Item 的高,并加上额外的间距 const quadrantWidth = - userQuadrantWidth ?? itemBounds.width + QUADRANT_CONFIG.defaultExtraSpacing; + userQuadrantWidth ?? + Math.max(...itemBounds.map((bounds) => bounds.width)) + + QUADRANT_CONFIG.defaultExtraSpacing; const quadrantHeight = userQuadrantHeight ?? - itemBounds.height + QUADRANT_CONFIG.defaultExtraSpacing; + Math.max(...itemBounds.map((bounds) => bounds.height)) + + QUADRANT_CONFIG.defaultExtraSpacing; // 计算象限位置 - 默认使用居中对齐 const quadrantPositions = calculateQuadrantPositions( @@ -210,30 +221,16 @@ function createAxisElements( function calculateQuadrantPositions( quadrantWidth: number, quadrantHeight: number, - itemBounds: { width: number; height: number }, + itemBounds: Array<{ width: number; height: number }>, ): Array<{ x: number; y: number }> { // 计算每个象限的中心点,然后将 item 居中放置 const centerX = quadrantWidth / 2; const centerY = quadrantHeight / 2; - return [ - { - x: centerX - itemBounds.width / 2, - y: centerY - itemBounds.height / 2, - }, // 第一象限 (左上) - { - x: quadrantWidth + centerX - itemBounds.width / 2, - y: centerY - itemBounds.height / 2, - }, // 第二象限 (右上) - { - x: centerX - itemBounds.width / 2, - y: quadrantHeight + centerY - itemBounds.height / 2, - }, // 第三象限 (左下) - { - x: quadrantWidth + centerX - itemBounds.width / 2, - y: quadrantHeight + centerY - itemBounds.height / 2, - }, // 第四象限 (右下) - ]; + return itemBounds.map((bounds, index) => ({ + x: (index % 2) * quadrantWidth + centerX - bounds.width / 2, + y: Math.floor(index / 2) * quadrantHeight + centerY - bounds.height / 2, + })); } registerStructure('quadrant', {