Skip to content

Commit

Permalink
Merge pull request #411 from kitsuyui/refurb-treemap
Browse files Browse the repository at this point in the history
refurb: treemap
  • Loading branch information
kitsuyui authored May 4, 2024
2 parents d14906b + 4d8e3a0 commit fa1e5c1
Showing 1 changed file with 7 additions and 31 deletions.
38 changes: 7 additions & 31 deletions packages/treemap/src/treemap.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useEffect, useState } from 'react'
import { useMeasure } from 'react-use'
import { dividing } from '@kitsuyui/rectangle-dividing'

interface Rect {
x: number
Expand Down Expand Up @@ -68,19 +69,6 @@ const previousBoustrophedonDirection = (rect1: Rect, rect2: Rect): Direction =>
return nextBoustrophedonDirection(rect2, rect1)
}

const invertDirection = (direction: Direction): Direction => {
switch (direction) {
case 'right':
return 'left'
case 'down':
return 'up'
case 'left':
return 'right'
case 'up':
return 'down'
}
}

const rectsToRectInfos = (rects: Rect[]): RectInfo[] => {
const rectInfos: RectInfo[] = rects.map((rect, index) => ({
...rect,
Expand Down Expand Up @@ -111,6 +99,9 @@ const rectsToRectInfos = (rects: Rect[]): RectInfo[] => {
}


const DEFAULT_ASPECT_RATIO = 16 / 9


export const Treemap = (props: {
weightedItems: WeightedItem[]
verticalFirst?: boolean
Expand All @@ -120,29 +111,14 @@ export const Treemap = (props: {
const [ref, { width, height }] = useMeasure<HTMLDivElement>()
const { weightedItems } = props
const verticalFirst = props.verticalFirst ?? true
const aspectRatio = props.aspectRatio ?? 16 / 9
const aspectRatio = props.aspectRatio ?? DEFAULT_ASPECT_RATIO
const boustrophedon = props.boustrophedon ?? false
const [inAreas, setInAreas] = useState<Rect[]>([])
const [dividing, setDividing] = useState<
typeof import('@kitsuyui/rectangle-dividing') | null
>(null)

useEffect(() => {
; (async () => {
if (dividing) return
const d = await import('@kitsuyui/rectangle-dividing')
setDividing(d)
})()
})

useEffect(() => {
if (!dividing) {
setInAreas([])
return
}
const rect: Rect = { x: 0, y: 0, w: width, h: height }
const weights = new Float32Array(weightedItems.map(({ weight }) => weight))
const ia: Rect[] = dividing.dividing(
const ia: Rect[] = dividing(
rect,
weights,
aspectRatio,
Expand All @@ -159,7 +135,6 @@ export const Treemap = (props: {
aspectRatio,
verticalFirst,
boustrophedon,
dividing,
])

const rectInfos = rectsToRectInfos(inAreas)
Expand Down Expand Up @@ -196,6 +171,7 @@ const TreemapByRect = (props: { items: RectItem[] }) => {
const { x, y, w, h, index } = rect
return (<TreemapContext.Provider value={rect} key={index}>
<div
key={index}
style={{
position: 'absolute',
overflow: 'hidden',
Expand Down

0 comments on commit fa1e5c1

Please sign in to comment.