question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Positioning nodes with x and y

See original GitHub issue

I have been using the default example given and was simply playing around with the config.

import React from "react";
import { Graph } from "react-d3-graph";

// graph payload (with minimalist structure)
const data = {
    // nodes: [{ id: "Harry" }, { id: "Sally" }, { id: "Alice" }],
    // links: [{ source: "Harry", target: "Sally" }, { source: "Harry", target: "Alice" }],
    nodes : [
      {
          id: 1,
          name: "A",
          x: 50,
          y: 300,
      },
      {
          id: 2,
          name: "B",
          x: 40,
          y: 310,
      },
      {
          id: 3,
          name: "C",
          x: 40,
          y: 310,
      },
      {
          id: 4,
          name: "D",
          x: 400,
          y: 200,
      },
  ],
    links: [
      {
          source: 1,
          target: 2,
          //label: "A-B",
      },
      {
          source: 1,
          target: 3,
          //label: "A-C",
      },
      {
          source: 2,
          target: 4,
          //label: "A-D",
      },
  ],
};

// the graph configuration, you only need to pass down properties
// that you want to override, otherwise default ones will be used
const myConfig = {
    // nodeHighlightBehavior: true,
    // node: {
    //     color: "lightgreen",
    //     size: 120,
    //     highlightStrokeColor: "blue",
    // },
    // link: {
    //     highlightColor: "lightblue",
    // },
    height: 1200,
    width: 1200,
    nodeHighlightBehavior: true,
    linkHighlightBehavior: true,
    staticGraphWithDragAndDrop: true,
    node: {
        fontSize: 12,
        highlightFontSize: 12,
        highlightFontWeight: "bold",
        highlightStrokeColor: "blue",
        labelProperty: "name",
        size: 500,
        strokeWidth: 2,
    },
    link: {
        highlightColor: "blue",
        renderLabel: true,
        highlightFontWeight: "bold",
        semanticStrokeWidth: true,
        fontSize: 12,
    },
    d3: {
        gravity: -400,
        linkLength: 180,
    },
};

// graph event callbacks
const onClickGraph = function() {
    window.alert(`Clicked the graph background`);
};

const onClickNode = function(nodeId) {
    window.alert(`Clicked node ${nodeId}`);
};

// const onDoubleClickNode = function(nodeId) {
//     window.alert(`Double clicked node ${nodeId}`);
// };

const onRightClickNode = function(event, nodeId) {
    window.alert(`Right clicked node ${nodeId}`);
};

const onMouseOverNode = function(nodeId) {
    // window.alert(`Mouse over node ${nodeId}`);
};

const onMouseOutNode = function(nodeId) {
    // window.alert(`Mouse out node ${nodeId}`);
};

const onClickLink = function(source, target) {
    window.alert(`Clicked link between ${source} and ${target}`);
};

const onRightClickLink = function(event, source, target) {
    window.alert(`Right clicked link between ${source} and ${target}`);
};

const onMouseOverLink = function(source, target) {
    // window.alert(`Mouse over in link between ${source} and ${target}`);
};

const onMouseOutLink = function(source, target) {
    // window.alert(`Mouse out link between ${source} and ${target}`);
};

const onNodePositionChange = function(nodeId, x, y) {
    window.alert(`Node ${nodeId} is moved to new position. New position is x= ${x} y= ${y}`);
};

function App() {
  return (<Graph
    id="graph-id" // id is mandatory, if no id is defined rd3g will throw an error
    data={data}
    config={myConfig}
    onClickNode={onClickNode}
    onRightClickNode={onRightClickNode}
    onClickGraph={onClickGraph}
    onClickLink={onClickLink}
    onRightClickLink={onRightClickLink}
    onMouseOverNode={onMouseOverNode}
    onMouseOutNode={onMouseOutNode}
    onMouseOverLink={onMouseOverLink}
    onMouseOutLink={onMouseOutLink}
    onNodePositionChange={onNodePositionChange}
/>)
}

export default App;

Each time I run this code, even when the coordinates have been hardcoded, I get new positions. Why is that so since they should now be fixed. I would like to simply create a tree structure with siblings getting equally spaced, how do I calculate these coordinates. Could you please explain?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
danielcaldascommented, Nov 18, 2020

Hi @ASchwad, you would have to compute yourself the x,y coordinates to fulfill your distancing requirements.

1reaction
antoninkloppcommented, Dec 13, 2019

Sure I will do a PR for this one !

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to force node position (x and y) in graphviz - Stack Overflow
I am trying to force position of nodes. I have x and y coordinates of my nodes and its also directed graph. I...
Read more >
A Node-Positioning Algorithm for General Trees
This task is accomplished by a node-positioning algorithm that calculates the x and y coordinates for every node of the tree. A rendering....
Read more >
How to get the position of nodes (x and y coordinates) stored ...
How to get the position of nodes (x and y... Learn more about binary, indices.
Read more >
TikZ: Node at same x-coordinate as another node, but ...
I guess you should write an answer using the positioning library, it is a worthwhile alternative indeed. – Jake. May 16, 2011 at...
Read more >
Node position on xy axis - Neo4j Community
If you want to delve deep into layout (and I seem to recall there was a way to export the x,y info from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found