Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions __tests__/unit/designs/structures/compare-quadrant.test.tsx
Original file line number Diff line number Diff line change
@@ -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,
}) => (
<Rect
data-quadrant-item={indexes[0]}
x={x}
y={y}
width={Number(datum.width) + (positionH === 'flipped' ? 20 : 0)}
height={Number(datum.height)}
/>
);

function render(
items: Array<{ width: number; height: number }>,
dimensions = {},
) {
const data = { items } as unknown as ParsedData;
const svg = renderSVG(
<Quadrant
Item={Item}
Items={[]}
data={data}
options={{ data } as ParsedInfographicOptions}
{...dimensions}
/>,
);
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 }) => (
<Rect data-quadrant-item="same" x={x} y={y} width={80} height={40} />
);
const data = { items: [{}, {}, {}, {}] } as ParsedData;
const doc = new DOMParser().parseFromString(
renderSVG(
<Quadrant
Item={PlainItem}
Items={[]}
data={data}
options={{ data } as ParsedInfographicOptions}
/>,
),
'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([]);
});
});
51 changes: 24 additions & 27 deletions src/designs/structures/compare-quadrant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,31 @@ export const Quadrant: ComponentType<QuadrantProps> = (props) => {
// 限制只显示4个象限数据,不足时填充空值
const quadrantItems = Array.from({ length: 4 }, (_, i) => items[i]);

// 计算单个象限项的尺寸 - 使用可选链处理空值
const itemBounds = quadrantItems[0]
? getElementBounds(
<Item indexes={[0]} data={data} datum={quadrantItems[0]} />,
)
: { width: 0, height: 0 };
// 按照实际渲染方向测量每个象限项。
const itemBounds = quadrantItems.map((item, index) =>
item
? getElementBounds(
<Item
indexes={[index]}
data={data}
datum={item}
positionH={index % 2 ? 'flipped' : 'normal'}
positionV={index < 2 ? 'normal' : 'flipped'}
/>,
)
: { 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(
Expand Down Expand Up @@ -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', {
Expand Down