@@ -3,6 +3,7 @@ import { Group } from '@visx/group';
33import { hierarchy , Tree } from '@visx/hierarchy' ;
44import { LinearGradient } from '@visx/gradient' ;
55import { pointRadial } from 'd3-shape' ;
6+ import { useTooltipInPortal } from '@visx/tooltip' ;
67import LinkControls from './axLinkControls' ;
78import getLinkComponent from './getAxLinkComponents' ;
89import { useTooltip , useTooltipInPortal , defaultStyles } from '@visx/tooltip' ;
@@ -81,7 +82,7 @@ const data: TreeNode = {
8182 } ] ,
8283} ;
8384
84- const dataArray = [
85+ const nodeAxArr = [
8586 {
8687 name : {
8788 sources : [ { attribute : 'aria-labelledby' , type : 'relatedElement' } ] ,
@@ -153,11 +154,18 @@ export type LinkTypesProps = {
153154 margin ?: { top : number ; right : number ; bottom : number ; left : number } ;
154155} ;
155156
156- export default function AxTree ( props , {
157- width : totalWidth ,
158- height : totalHeight ,
159- margin = defaultMargin ,
160- } : LinkTypesProps ) {
157+ export default function AxTree ( props ) {
158+ const {
159+ currLocation,
160+ axSnapshots,
161+ width,
162+ height
163+ } = props ;
164+
165+ let margin = defaultMargin ;
166+ let totalWidth = width ;
167+ let totalHeight = height ;
168+
161169
162170
163171 const toolTipTimeoutID = useRef ( null ) ; //useRef stores stateful data that’s not needed for rendering.
@@ -207,8 +215,6 @@ export default function AxTree(props, {
207215 const [ linkType , setLinkType ] = useState ( 'diagonal' ) ;
208216 const [ stepPercent , setStepPercent ] = useState ( 0.5 ) ;
209217
210- console . log ( 'total height access: ' , totalHeight ) ;
211-
212218 const innerWidth : number = totalWidth - margin . left - margin . right ;
213219 const innerHeight : number = totalHeight - margin . top - margin . bottom - 60 ;
214220
@@ -234,74 +240,64 @@ export default function AxTree(props, {
234240 }
235241 }
236242
237- const LinkComponent = getLinkComponent ( { layout , linkType , orientation } ) ;
243+ console . log ( 'size width height ax: ' , sizeWidth , sizeHeight ) ;
238244
239- const {
240- currLocation,
241- axSnapshots
242- } = props ;
245+ const LinkComponent = getLinkComponent ( { layout, linkType, orientation } ) ;
243246
244247 const currAxSnapshot = JSON . parse ( JSON . stringify ( axSnapshots [ currLocation . index ] ) ) ;
248+ console . log ( 'currAxSnapshot: ' , currAxSnapshot ) ;
245249
246250 // root node of currAxSnapshot
247251 const rootAxNode = JSON . parse ( JSON . stringify ( currAxSnapshot [ 0 ] ) ) ;
248252
249253 // // array that holds the ax tree as a nested object and the root node initially
250254 const nodeAxArr = [ ] ;
251255
252- // const organizeAxTree = (currNode, currAxSnapshot) => {
253- // for (let i = 0; i < currNode.length; i++) {
254- // if (currNode.childIds.length > 0) {
255- // currNode.children = [];
256-
256+ // currNode.children = [];
257+ // // checks if there is more than 1 child
258+ // if (currNode.childIds.length > 1) {
259+ // for (let m = 0; m < currNode.childIds.length; m++) {
260+ // for (let j = 0; j < currAxSnapshot.length; j++) {
261+ // if (currNode.childIds.includes(currAxSnapshot[j].nodeId)) {
262+ // currNode.children.push(currAxSnapshot[j]);
263+ // }
264+ // }
265+ // }
266+ // } else if (currNode.childIds.length === 1) {
257267 // for (let j = 0; j < currAxSnapshot.length; j++) {
258-
259268 // if (currNode.childIds.includes(currAxSnapshot[j].nodeId)) {
260269 // currNode.children.push(currAxSnapshot[j]);
261-
262- // organizeAxTree(currNode.children, currAxSnapshot);
263270 // }
264271 // }
272+ // organizeAxTree(currNode.children[0], currAxSnapshot);
265273 // }
274+ // organizeAxTree(currNode.children, currAxSnapshot);
266275 // }
267- // }
268-
269- // organizeAxTree(nodeAxArr, currAxSnapshot);
270-
271-
272- // attempted refactored organizeAxTree
273- // const organizeAxTree2 = (currNode, currAxSnapshot) => {
274- // console.log('currNode: ', currNode);
275- // if (currNode.childIds.length > 0) {
276- // currNode.children = [];
277- // for (let j = 0; j < currAxSnapshot.length; j++) {
278- // if (currNode.childIds.includes(currAxSnapshot[j].nodeId)) {
279- // currNode.children.push(currAxSnapshot[j]);
280- // organizeAxTree2(currNode.children, currAxSnapshot);
281- // }
282- // }
283- // }
284- // }
285- // organizeAxTree2(rootAxNode, currAxSnapshot);
286276
277+ // organizeAxTree([rootAxNode], currAxSnapshot);
287278 const organizeAxTree = ( currNode , currAxSnapshot ) => {
288279 if ( currNode . childIds && currNode . childIds . length > 0 ) {
289280 currNode . children = [ ] ;
290281 for ( let j = 0 ; j < currAxSnapshot . length ; j ++ ) {
291- if ( currNode . childIds . includes ( currAxSnapshot [ j ] . nodeId ) ) {
292- currNode . children . push ( currAxSnapshot [ j ] ) ;
293- organizeAxTree ( currAxSnapshot [ j ] , currAxSnapshot ) ;
282+ for ( const childEle of currNode . childIds ) {
283+ if ( childEle === currAxSnapshot [ j ] . nodeId ) {
284+ currNode . children . push ( currAxSnapshot [ j ] ) ;
285+ organizeAxTree ( currAxSnapshot [ j ] , currAxSnapshot ) ;
286+ }
294287 }
288+
295289 }
296290 }
297291 }
298292
299293 organizeAxTree ( rootAxNode , currAxSnapshot ) ;
300- console . log ( 'nestedAxTree: ' , rootAxNode ) ;
294+
295+ console . log ( 'rootAxNode: ' , rootAxNode ) ;
301296
302297 // store each individual node, now with children property in nodeAxArr
303298 // need to consider order, iterate through the children property first?
304299 const populateNodeAxArr = ( currNode ) => {
300+ nodeAxArr . splice ( 0 , nodeAxArr . length ) ;
305301 nodeAxArr . push ( currNode ) ;
306302 for ( let i = 0 ; i < nodeAxArr . length ; i += 1 ) {
307303 // iterate through the nodeList that contains our snapshot
@@ -319,6 +315,14 @@ export default function AxTree(props, {
319315 populateNodeAxArr ( rootAxNode ) ;
320316 console . log ( 'nodeAxArr: ' , nodeAxArr ) ;
321317
318+ const {
319+ containerRef // Access to the container's bounding box. This will be empty on first render.
320+ } = useTooltipInPortal ( {
321+ // Visx hook
322+ detectBounds : true , // use TooltipWithBounds
323+ scroll : true , // when tooltip containers are scrolled, this will correctly update the Tooltip position
324+ } ) ;
325+
322326 return totalWidth < 10 ? null : (
323327 < div >
324328 < LinkControls
@@ -331,14 +335,22 @@ export default function AxTree(props, {
331335 setLinkType = { setLinkType }
332336 setStepPercent = { setStepPercent }
333337 />
334- < svg width = { totalWidth } height = { totalHeight + 0 } >
335- < LinearGradient id = "links-gradient" from = "#fd9b93" to = "#fe6e9e" />
336- < rect width = { totalWidth } height = { totalHeight } rx = { 14 } fill = "#272b4d" onClick = { ( ) => {
338+
339+ { /* svg references purple background */ }
340+ < svg ref = { containerRef } width = { totalWidth } height = { totalHeight + 0 } >
341+ < LinearGradient id = 'root-gradient' from = '#488689' to = '#3c6e71' />
342+ < LinearGradient id = 'parent-gradient' from = '#488689' to = '#3c6e71' />
343+ < rect
344+ className = 'componentMapContainer'
345+ width = { sizeWidth / aspect }
346+ height = { sizeHeight / aspect + 0 }
347+ rx = { 14 }
348+ onClick = { ( ) => {
337349 hideTooltip ( ) ;
338350 } } />
339- < Group top = { margin . top } left = { margin . left } >
351+ < Group transform = { `scale( ${ aspect } )` } top = { margin . top } left = { margin . left } >
340352 < Tree
341- root = { hierarchy ( data , ( d ) => ( d . isExpanded ? null : d . children ) ) }
353+ root = { hierarchy ( nodeAxArr [ 0 ] , ( d ) => ( d . isExpanded ? null : d . children ) ) }
342354 size = { [ sizeWidth / aspect , sizeHeight / aspect ] }
343355 separation = { ( a , b ) => ( a . parent === b . parent ? 0.5 : 0.5 ) / a . depth }
344356 >
@@ -349,21 +361,21 @@ export default function AxTree(props, {
349361 key = { i }
350362 data = { link }
351363 percent = { stepPercent }
364+ stroke = "rgb(254,110,158,0.6)"
352365 strokeWidth = "1"
353366 fill = "none"
354367 />
355368 ) ) }
356369
357370 // code relating to each node in tree
358371 { tree . descendants ( ) . map ( ( node , key ) => {
359- console . log ( 'node.data: ' , node . data ) ;
360372 const widthFunc = ( name ) : number => {
361373 //returns a number that is related to the length of the name. Used for determining the node width.
362374 const nodeLength = name . length ;
363375 //return nodeLength * 7 + 20; //uncomment this line if we want each node to be directly proportional to the name.length (instead of nodes of similar sizes to snap to the same width)
364- if ( nodeLength <= 5 ) return nodeLength + 50 ;
365- if ( nodeLength <= 10 ) return nodeLength + 120 ;
366- return nodeLength + 140 ;
376+ if ( nodeLength <= 5 ) return nodeLength + 60 ;
377+ if ( nodeLength <= 10 ) return nodeLength + 130 ;
378+ return nodeLength + 160 ;
367379 } ;
368380
369381 const width : number = widthFunc ( node . data . name . value ) ; // the width is determined by the length of the node.name
@@ -384,14 +396,14 @@ export default function AxTree(props, {
384396 }
385397
386398 //setup a nodeCoords Object that will have keys of unique y coordinates and value arrays of all the left and right x coordinates of the nodes on that level
387- count < dataArray . length
399+ count < nodeAxArr . length
388400 ? ! nodeCoords [ top ]
389401 ? ( nodeCoords [ top ] = [ left - width / 2 , left + width / 2 ] )
390402 : nodeCoords [ top ] . push ( left - width / 2 , left + width / 2 )
391403 : null ;
392404 count ++ ;
393405
394- if ( count === dataArray . length ) {
406+ if ( count === nodeAxArr . length ) {
395407 //check if there is still a tier of the node tree to collision check
396408 while ( Object . values ( nodeCoords ) [ nodeCoordTier ] ) {
397409 //check if there are atleast two nodes on the current tier
@@ -466,10 +478,16 @@ export default function AxTree(props, {
466478 return (
467479 < Group top = { top } left = { left } key = { key } className = 'rect' >
468480 { node . depth === 0 && (
469- < circle
470- className = 'compMapRoot'
471- r = { 25 }
472- fill = "url('#root-gradient')"
481+ < rect
482+ className = { node . children ? 'compMapParent' : 'compMapChild' }
483+ height = { height }
484+ width = { width }
485+ y = { - height / 2 }
486+ x = { - width / 2 }
487+ fill = "url('#parent-gradient')"
488+ strokeWidth = { 1.5 }
489+ strokeOpacity = '1'
490+ rx = { node . children ? 4 : 10 }
473491 onClick = { ( ) => {
474492 node . data . isExpanded = ! node . data . isExpanded ;
475493 hideTooltip ( ) ;
0 commit comments