@@ -19,7 +19,7 @@ export default function SurveyTaxonomyTree({ height = 800, taxonomyData }) {
1919 . attr ( "height" , height )
2020 . style ( "font" , "12px" )
2121
22- const g = svg . append ( "g" ) . attr ( "transform" , `translate(60 , ${ height / 2 } )` )
22+ const g = svg . append ( "g" ) . attr ( "transform" , `translate(${ width / 2 } , ${ height / 2 } )` )
2323
2424 let i = 0
2525 const duration = 750
@@ -83,28 +83,52 @@ export default function SurveyTaxonomyTree({ height = 800, taxonomyData }) {
8383 const nodes = treeData . descendants ( )
8484 const links = treeData . links ( )
8585
86- // Normalize for fixed-depth and adjust positioning
86+ // First, set up the hierarchy positions
8787 nodes . forEach ( ( d ) => {
88- d . _x = d . x - height / 2 // center vertically
89- // Custom positioning for each level
88+ // Custom Y positioning for taxonomy hierarchy
9089 switch ( d . depth ) {
9190 case 0 :
92- d . _y = 0 // Root at origin
91+ d . y = 0 // Root at origin
9392 break
9493 case 1 :
95- d . _y = 140 // First level close to root
94+ d . y = 140 // First level close to root
9695 break
9796 case 2 :
98- d . _y = 300 // Second level
97+ d . y = 300 // Second level
9998 break
10099 case 3 :
101- d . _y = 480 // Third level
100+ d . y = 480 // Third level
102101 break
103102 default :
104- d . _y = 480 + ( d . depth - 3 ) * 180 // Deeper levels
103+ d . y = 480 + ( d . depth - 3 ) * 180 // Deeper levels
105104 }
106105 } )
107106
107+ // Calculate bounds of ALL nodes including their rectangle sizes
108+ let minX = Infinity , maxX = - Infinity
109+ let minY = Infinity , maxY = - Infinity
110+
111+ nodes . forEach ( ( d ) => {
112+ const { w, h } = calcRect ( d . data . name , d . depth )
113+ minX = Math . min ( minX , d . x - w / 2 )
114+ maxX = Math . max ( maxX , d . x + w / 2 )
115+ minY = Math . min ( minY , d . y - h / 2 )
116+ maxY = Math . max ( maxY , d . y + h / 2 )
117+ } )
118+
119+ // Calculate the center point of ALL nodes
120+ const treeCenterX = ( minX + maxX ) / 2
121+ const treeCenterY = ( minY + maxY ) / 2
122+
123+ console . log ( 'Tree bounds:' , { minX, maxX, minY, maxY } )
124+ console . log ( 'Tree center:' , { treeCenterX, treeCenterY } )
125+
126+ // Now apply centering offset to ALL nodes
127+ nodes . forEach ( ( d ) => {
128+ d . _x = d . x - treeCenterX // Center horizontally based on all nodes
129+ d . _y = d . y - treeCenterY // Center vertically based on all nodes
130+ } )
131+
108132 // Update the nodes
109133 const node = g . selectAll ( "g.node" ) . data ( nodes , ( d ) => d . id || ( d . id = ++ i ) )
110134
0 commit comments