Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dnyamic node height position y axis #459

Open
charlesnoqx opened this issue Nov 7, 2024 · 1 comment
Open

Dnyamic node height position y axis #459

charlesnoqx opened this issue Nov 7, 2024 · 1 comment

Comments

@charlesnoqx
Copy link

charlesnoqx commented Nov 7, 2024

Screenshot 2024-11-07 at 3 33 14 PM


const getLayoutedElements = (nodes, edges, useNodeHeight = false, direction = 'TB') => {
  const isHorizontal = direction === 'LR'

  const dagreGraph = new dagre.graphlib.Graph().setDefaultEdgeLabel(() => ({}))
  dagreGraph.setGraph({ rankdir: direction })

  let newNodes = nodes.map((node) => ({ ...node }))

  newNodes.forEach((node) => {
    return dagreGraph.setNode(node.id, {
      width: nodeWidth,
      height: (useNodeHeight ? node.height : nodeHeight) + nodeHeight / 2
    })
  })

  edges.forEach((edge) => {
    dagreGraph.setEdge(edge.source, edge.target)
  })

  dagre.layout(dagreGraph)

  newNodes = newNodes.map((node) => {
    const nodeWithPosition = dagreGraph.node(node.id)
    node.targetPosition = isHorizontal ? 'left' : 'top'
    node.sourcePosition = isHorizontal ? 'right' : 'bottom'

    console.log(
      nodeWithPosition,
      '<<nodeWithPosition.height',
      nodeWithPosition.y - nodeWithPosition.height / 2
    )
    node.position = {
      x: nodeWithPosition.x - nodeWidth / 2,
      y: nodeWithPosition.y - nodeWithPosition.height / 2
    }

    return node
  })

  return { nodes: newNodes, edges }
}


  const nodesInitialized = useNodesInitialized({})
  
 useEffect(() => {
    if (nodesInitialized) {
      const { nodes: layoutNodes, edges: layoutEdges } = getLayoutedElements(
        getNodes(),
        activeGraph.edges || [],
        true
      )

      setNodes(layoutNodes)
      setEdges(layoutEdges)
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [nodesInitialized])
  

I used useNodesInitialized to recalculate the height and position, based on the actual height of the node

My problem is each rank/heirarchy doesnt go on the same position y,

Is it possible? the 1, 2 and 3 should have the same position.y
same with January and 1.1

Thanks in advance

@ZahidChandio
Copy link

I need the nodes to be positioned at the same level. Specifically, nodes 1, 2, and 3 should be aligned horizontally, and the 1.1 node should be adjusted upwards to maintain equal spacing between all the nodes.
@charlesnoqx, please let me know if you have found the solution. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants