@@ -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(40 , ${ height / 2 } )` )
22+ const g = svg . append ( "g" ) . attr ( "transform" , `translate(60 , ${ height / 2 } )` )
2323
2424 let i = 0
2525 const duration = 750
@@ -30,6 +30,7 @@ export default function SurveyTaxonomyTree({ height = 800, taxonomyData }) {
3030
3131 // Initialize the tree with all nodes expanded
3232 // Remove the collapse initialization - all nodes will be expanded by default
33+ console . log ( 'Tree initialized with nodes:' , root . descendants ( ) . length )
3334
3435 function collapse ( d ) {
3536 if ( d . children ) {
@@ -44,11 +45,17 @@ export default function SurveyTaxonomyTree({ height = 800, taxonomyData }) {
4445 const approx = Math . min ( maxWidth , Math . max ( 60 , text . length * 6 ) )
4546 const lines = wrapText ( text , Math . floor ( approx / 6 ) )
4647 const fontSize = level === 1 ? 11 : level === 2 ? 10 : 9
47- const lineHeight = fontSize * 1.2
48+ const lineHeight = fontSize * 1.1 // TikZ-style tighter spacing
4849 const padding = 16
50+
51+ // TikZ-style height calculation: account for actual text metrics
52+ const textHeight = lines . length === 1
53+ ? fontSize + 4 // Single line with some breathing room
54+ : fontSize + ( lines . length - 1 ) * lineHeight + 4 // Multi-line with proper spacing
55+
4956 return {
5057 w : approx + padding ,
51- h : lines . length * lineHeight + padding ,
58+ h : textHeight + padding ,
5259 lines
5360 }
5461 }
@@ -70,16 +77,32 @@ export default function SurveyTaxonomyTree({ height = 800, taxonomyData }) {
7077 }
7178
7279 function update ( source ) {
73- const treeLayout = d3 . tree ( ) . nodeSize ( [ 30 , 220 ] )
80+ const treeLayout = d3 . tree ( ) . nodeSize ( [ 30 , 180 ] ) // Adjust spacing
7481 const treeData = treeLayout ( root )
7582
7683 const nodes = treeData . descendants ( )
7784 const links = treeData . links ( )
7885
79- // Normalize for fixed-depth
86+ // Normalize for fixed-depth and adjust positioning
8087 nodes . forEach ( ( d ) => {
8188 d . _x = d . x - height / 2 // center vertically
82- d . _y = d . y
89+ // Custom positioning for each level
90+ switch ( d . depth ) {
91+ case 0 :
92+ d . _y = 0 // Root at origin
93+ break
94+ case 1 :
95+ d . _y = 140 // First level close to root
96+ break
97+ case 2 :
98+ d . _y = 300 // Second level
99+ break
100+ case 3 :
101+ d . _y = 480 // Third level
102+ break
103+ default :
104+ d . _y = 480 + ( d . depth - 3 ) * 180 // Deeper levels
105+ }
83106 } )
84107
85108 // Update the nodes
@@ -126,22 +149,25 @@ export default function SurveyTaxonomyTree({ height = 800, taxonomyData }) {
126149 nodeEnter
127150 . append ( "text" )
128151 . attr ( "text-anchor" , "middle" )
152+ . attr ( "dominant-baseline" , "middle" ) // This ensures vertical centering like TikZ
129153 . attr ( "x" , 0 )
130154 . attr ( "y" , 0 )
131155 . each ( function ( d ) {
132156 const { lines } = calcRect ( d . data . name , d . depth )
133157 const textEl = d3 . select ( this )
134158 const fontSize = d . depth === 1 ? 11 : d . depth === 2 ? 10 : 9
135- const lineHeight = fontSize * 1.2
159+ const lineHeight = fontSize * 1.1 // Slightly tighter line spacing like TikZ
136160
137- const totalTextHeight = lines . length * lineHeight
138- const startY = - ( totalTextHeight / 2 ) + ( lineHeight / 2 )
161+ // Calculate vertical centering like TikZ: center the entire text block
162+ const totalLines = lines . length
163+ const blockHeight = ( totalLines - 1 ) * lineHeight
164+ const startOffset = - blockHeight / 2
139165
140166 for ( let i = 0 ; i < lines . length ; i ++ ) {
141167 textEl
142168 . append ( "tspan" )
143169 . attr ( "x" , 0 )
144- . attr ( "y " , startY + ( i * lineHeight ) )
170+ . attr ( "dy " , i === 0 ? ` ${ startOffset } px` : ` ${ lineHeight } px` )
145171 . text ( lines [ i ] . replace ( / \s + / g, " " ) )
146172 }
147173 } )
@@ -151,7 +177,7 @@ export default function SurveyTaxonomyTree({ height = 800, taxonomyData }) {
151177 // Add expand/collapse indicator
152178 nodeEnter
153179 . append ( "circle" )
154- . attr ( "r" , 8 )
180+ . attr ( "r" , 3 )
155181 . attr ( "cx" , ( d ) => {
156182 const { w } = calcRect ( d . data . name , d . depth )
157183 return w / 2 - 8
@@ -219,7 +245,8 @@ export default function SurveyTaxonomyTree({ height = 800, taxonomyData }) {
219245 . attr ( "stroke-opacity" , 0.6 )
220246 . attr ( "d" , ( d ) => {
221247 const o = { x : source . x0 , y : source . y0 }
222- return diagonal ( o , o )
248+ // Use straight lines for ALL connections
249+ return `M${ o . y } ,${ o . x } L${ o . y } ,${ o . x } ` // Always straight line
223250 } )
224251
225252 // Transition links to their new position
@@ -229,7 +256,9 @@ export default function SurveyTaxonomyTree({ height = 800, taxonomyData }) {
229256 . attr ( "d" , ( d ) => {
230257 const src = { x : d . source . _x , y : d . source . _y }
231258 const tgt = { x : d . target . _x , y : d . target . _y }
232- return diagonal ( src , tgt )
259+
260+ // Use straight lines for ALL connections - no curves
261+ return `M${ src . y } ,${ src . x } L${ tgt . y } ,${ tgt . x } ` // Always straight line
233262 } )
234263
235264 // Transition exiting links to the parent's new position
@@ -239,7 +268,8 @@ export default function SurveyTaxonomyTree({ height = 800, taxonomyData }) {
239268 . duration ( duration )
240269 . attr ( "d" , ( d ) => {
241270 const o = { x : source . _x , y : source . _y }
242- return diagonal ( o , o )
271+ // Use straight lines for ALL connections
272+ return `M${ o . y } ,${ o . x } L${ o . y } ,${ o . x } ` // Always straight line
243273 } )
244274 . remove ( )
245275
0 commit comments