bumbeishvili / org-chart

Highly customizable org chart. Integrations available for Angular, React, Vue
https://stackblitz.com/edit/web-platform-o5t1ha
MIT License
919 stars 328 forks source link

change node position after chart render #435

Open mohcinsarrar opened 2 months ago

mohcinsarrar commented 2 months ago

Hi i want to change the x position of some nodes after chart render, i need to execute that after chart render because a calculate the nex position according to the old position of the node an another node

this is my function

function change_node_position(staticNodeId, changedNodeId, index) {

    // After rendering, manually adjust the position of the two specific nodes
    const staticNode = d3.select(`[data-nodeId="${staticNodeId}"]`); // Replace `node_id_1` with the actual node ID or class
    const changedNode = d3.select(`[data-nodeId="${changedNodeId}"]`); // Replace `node_id_2` with the actual node ID or classc

    if (staticNode.node() == null || changedNode.node() == null) {
        return false;
    }

    // Get current positions (x and y) of the two nodes

    const staticNodeCord = extract_cord(staticNode.attr('transform'))
    const changedNodeCord = extract_cord(changedNode.attr('transform'))

    // Adjust the margin by changing the x position of the second node
    const customMargin = (treeConfiguration.nodeWidthSpouse + 25) + ((treeConfiguration.nodeWidth + 25) * (index - 1)) ; // Set a custom margin between these two nodes
    changedNode.attr('transform', `translate(${staticNodeCord.x + customMargin}, ${changedNodeCord.y})`); // Adjust x position only

    const newnode1cord = extract_cord(changedNode.attr('transform'))
    /////////////////
    const path = d3.select(`[data-from="${staticNodeId}"][data-to="${changedNodeId}"]`);

    path.attr("d", update_path_position(path.attr("d"), newnode1cord.x));

}

i execute my function after chart.render(), but nothing happen, but when i execute the funtion when i click on a button its position nodes perfectly

bumbeishvili commented 2 months ago

I don't think that you can do that.

If you are willing to modify the source code, you can plug your code at the end of this function

https://github.com/bumbeishvili/org-chart/blob/cb1f376656bc83360a4fea6872597fd427fcd775/src/d3-org-chart.js#L798

Also your issue gave me an idea to further make it flexible, so in the future I will expose a way for the user to be able to modify coordinates