@@ -7,35 +7,29 @@ export interface BeautifulTreeProps {
7
7
readonly svgProps : {
8
8
readonly width : number
9
9
readonly height : number
10
+ readonly sizeUnit ?: '%' | 'em' | 'px' | 'rem'
10
11
}
11
12
readonly tree : Tree
12
13
}
13
14
14
- export function BeautifulTree (
15
- props : Readonly < BeautifulTreeProps > ,
16
- ) : JSX . Element {
17
- const treeWithLayout = computeLeftShiftLayout ( props . tree )
15
+ export function BeautifulTree ( {
16
+ id,
17
+ svgProps,
18
+ tree,
19
+ } : Readonly < BeautifulTreeProps > ) : JSX . Element {
20
+ const { tree : treeWithLayout , maxX, maxY } = computeLeftShiftLayout ( tree )
18
21
const orderedNodes = [ ...postOrderIterator ( treeWithLayout ) ]
19
22
20
- let maxX = 0
21
- let maxY = 0
22
- for ( const node of orderedNodes ) {
23
- if ( node . meta . abstractPosition . x > maxX ) {
24
- maxX = node . meta . abstractPosition . x
25
- }
26
- if ( node . meta . abstractPosition . y > maxY ) {
27
- maxY = node . meta . abstractPosition . y
28
- }
29
- }
23
+ const { width, height, sizeUnit = 'px' } = svgProps
30
24
31
25
return (
32
26
< svg
33
27
xmlns = "http://www.w3.org/2000/svg"
34
- id = { props . id }
35
- viewBox = { `0 0 ${ props . svgProps . width } ${ props . svgProps . height } ` }
28
+ id = { id }
29
+ viewBox = { `0 0 ${ width } ${ height } ` }
36
30
style = { {
37
- width : `${ props . svgProps . width } px ` ,
38
- height : `${ props . svgProps . height } px ` ,
31
+ width : `${ width } ${ sizeUnit } ` ,
32
+ height : `${ height } ${ sizeUnit } ` ,
39
33
} }
40
34
className = { 'beautiful-tree-react' }
41
35
>
@@ -44,11 +38,12 @@ export function BeautifulTree(
44
38
const aY = node . meta . abstractPosition . y
45
39
return (
46
40
< circle
47
- key = { `${ props . id } -node-${ idx } ` }
48
- cx = { ( ( aX + 1 ) * props . svgProps . width ) / ( maxX + 2 ) }
49
- cy = { ( ( aY + 1 ) * props . svgProps . height ) / ( maxY + 2 ) }
50
- stroke = "blue"
51
- fill = "purple"
41
+ key = { `${ id } -node-${ idx } ` }
42
+ className = { 'beautiful-tree-node' }
43
+ cx = { ( ( aX + 1 ) * width ) / ( maxX + 2 ) }
44
+ cy = { ( ( aY + 1 ) * height ) / ( maxY + 2 ) }
45
+ stroke = "black"
46
+ fill = "white"
52
47
r = "5"
53
48
/>
54
49
)
0 commit comments