Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
122 lines
3.4 KiB
TypeScript
122 lines
3.4 KiB
TypeScript
import type { LayoutRect } from './treemap-layout'
|
|
|
|
export function layoutTreemap(sizes: number[], rect: LayoutRect): LayoutRect[] {
|
|
if (sizes.length === 0) return []
|
|
if (sizes.length === 1) return [rect]
|
|
|
|
const totalSize = sizes.reduce((a, b) => a + b, 0)
|
|
const normalizedSizes = sizes.map(
|
|
(s) => (s / totalSize) * rect.width * rect.height
|
|
)
|
|
|
|
const result: LayoutRect[] = []
|
|
let remaining = [...normalizedSizes]
|
|
let currentRect = { ...rect }
|
|
let totalRemaining = remaining.reduce((a, b) => a + b, 0)
|
|
|
|
while (remaining.length > 1) {
|
|
// Decide orientation: vertical if wider, horizontal if taller
|
|
const vertical = currentRect.width >= currentRect.height
|
|
|
|
// Pick items until sum > total / count
|
|
const picked: number[] = []
|
|
let sum = 0
|
|
|
|
for (const size of remaining) {
|
|
picked.push(size)
|
|
sum += size
|
|
|
|
if (vertical) {
|
|
const width = (currentRect.width * sum) / totalRemaining
|
|
if (width > (currentRect.height / picked.length) * 0.9) {
|
|
break
|
|
}
|
|
} else {
|
|
const height = (currentRect.height * sum) / totalRemaining
|
|
if (height > (currentRect.width / picked.length) * 0.9) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ensure at least one item is picked
|
|
if (picked.length === 0) {
|
|
picked.push(remaining[0])
|
|
sum = remaining[0]
|
|
}
|
|
|
|
// Calculate the space used by this row/column
|
|
const spaceRatio = sum / totalRemaining
|
|
|
|
totalRemaining -= sum
|
|
|
|
if (vertical) {
|
|
// Items stacked vertically, filling full width
|
|
const rowWidth = Math.round(spaceRatio * currentRect.width)
|
|
let offsetY = 0
|
|
|
|
for (let i = 0; i < picked.length; i++) {
|
|
const size = picked[i]
|
|
const itemHeight =
|
|
i === picked.length - 1
|
|
? Math.round(currentRect.height - offsetY)
|
|
: Math.round((size / sum) * currentRect.height)
|
|
|
|
result.push({
|
|
x: Math.round(currentRect.x),
|
|
y: Math.round(currentRect.y + offsetY),
|
|
width: rowWidth,
|
|
height: itemHeight,
|
|
})
|
|
offsetY += itemHeight
|
|
}
|
|
|
|
// Update remaining rectangle
|
|
currentRect = {
|
|
x: Math.round(currentRect.x + rowWidth),
|
|
y: Math.round(currentRect.y),
|
|
width: Math.round(currentRect.width - rowWidth),
|
|
height: Math.round(currentRect.height),
|
|
}
|
|
} else {
|
|
// Items placed horizontally, filling full height
|
|
const rowHeight = Math.round(spaceRatio * currentRect.height)
|
|
let offsetX = 0
|
|
|
|
for (let i = 0; i < picked.length; i++) {
|
|
const size = picked[i]
|
|
const itemWidth =
|
|
i === picked.length - 1
|
|
? Math.round(currentRect.width - offsetX)
|
|
: Math.round((size / sum) * currentRect.width)
|
|
|
|
result.push({
|
|
x: Math.round(currentRect.x + offsetX),
|
|
y: Math.round(currentRect.y),
|
|
width: itemWidth,
|
|
height: rowHeight,
|
|
})
|
|
offsetX += itemWidth
|
|
}
|
|
|
|
// Update remaining rectangle
|
|
currentRect = {
|
|
x: Math.round(currentRect.x),
|
|
y: Math.round(currentRect.y + rowHeight),
|
|
width: Math.round(currentRect.width),
|
|
height: Math.round(currentRect.height - rowHeight),
|
|
}
|
|
}
|
|
|
|
// Remove picked items from remaining
|
|
remaining = remaining.slice(picked.length)
|
|
}
|
|
|
|
// Last item fills remaining space
|
|
if (remaining.length === 1) {
|
|
result.push(currentRect)
|
|
}
|
|
|
|
return result
|
|
}
|