-
Notifications
You must be signed in to change notification settings - Fork 232
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
Configurable graph force strength and link length #104
Changes from 3 commits
29029f4
220e379
90179b3
187f9f1
a1be545
34a4c21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,9 @@ | |
* | ||
* **[note]** react-d3-graph will map this values to [d3 symbols](https://github.com/d3/d3-shape#symbols) | ||
* <br/> | ||
* @param {number} [node.gravity=-100] - this will define how close nodes are to each other. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to see this values isolated in the scope of a ...
d3: {
gravity: -100,
linkSize: 100
},
... |
||
* - If value is positive, nodes will attract each other. | ||
* - If value is negative, nodes will repel each other. Most of the times this is what we want, so nodes don't overlap. | ||
* @param {Object} link link object is explained in the next section. ⬇️ | ||
* <h2>Link level configurations</h2> | ||
* @param {string} [link.color='#d3d3d3'] - 🚅🚅🚅 the color for links | ||
|
@@ -118,6 +121,7 @@ | |
* - "STRAIGHT" <small>(default)</small> - a straight line. | ||
* - "CURVE_SMOOTH" - a slight curve between two nodes | ||
* - "CURVE_FULL" - a semicircumference trajectory unites source and target nodes. | ||
* @param {number} [link.size=100] - the length of the link from the center of the nodes it joins. | ||
* <br/> | ||
* <img src="https://github.com/danielcaldas/react-d3-graph/blob/master/docs/rd3g-bend.gif?raw=true" width="820" height="480"/> | ||
* | ||
|
@@ -168,14 +172,16 @@ export default { | |
strokeColor: 'none', | ||
strokeWidth: 1.5, | ||
svg: '', | ||
symbolType: 'circle' | ||
symbolType: 'circle', | ||
gravity: -100 | ||
}, | ||
link: { | ||
color: '#d3d3d3', | ||
highlightColor: '#d3d3d3', | ||
opacity: 1, | ||
semanticStrokeWidth: false, | ||
strokeWidth: 1.5, | ||
type: 'STRAIGHT' | ||
type: 'STRAIGHT', | ||
size: 100 | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,15 +42,17 @@ const NODE_PROPS_WHITELIST = ['id', 'highlighted', 'x', 'y', 'index', 'vy', 'vx' | |
* Wtf is a force? {@link https://github.com/d3/d3-force#forces| here} | ||
* @param {number} width - the width of the container area of the graph. | ||
* @param {number} height - the height of the container area of the graph. | ||
* @param {number} gravity - the force strength applied to the graph. | ||
* @returns {Object} returns the simulation instance to be consumed. | ||
* @memberof Graph/helper | ||
*/ | ||
function _createForceSimulation(width, height) { | ||
function _createForceSimulation(width, height, gravity) { | ||
const frx = d3ForceX(width / 2).strength(CONST.FORCE_X); | ||
const fry = d3ForceY(height / 2).strength(CONST.FORCE_Y); | ||
const forceStrength = gravity || CONST.FORCE_IDEAL_STRENGTH; | ||
LonelyPrincess marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return d3ForceSimulation() | ||
.force('charge', d3ForceManyBody().strength(CONST.FORCE_IDEAL_STRENGTH)) | ||
.force('charge', d3ForceManyBody().strength(forceStrength)) | ||
.force('x', frx) | ||
.force('y', fry); | ||
} | ||
|
@@ -360,7 +362,11 @@ function initializeGraphState({ data, id, config }, state) { | |
let links = _initializeLinks(graph.links); // matrix of graph connections | ||
const { nodes: d3Nodes, links: d3Links } = graph; | ||
const formatedId = id.replace(/ /g, '_'); | ||
const simulation = _createForceSimulation(newConfig.width, newConfig.height); | ||
const simulation = _createForceSimulation( | ||
newConfig.width, | ||
newConfig.height, | ||
newConfig.node && newConfig.node.gravity | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for |
||
); | ||
|
||
return { | ||
id: formatedId, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of also having this constants configurable:
They are on the top of this file