Positioning nodes with x and y
See original GitHub issueI 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:
- Created 4 years ago
- Comments:13 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @ASchwad, you would have to compute yourself the
x,y
coordinates to fulfill your distancing requirements.Sure I will do a PR for this one !