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

Configurable graph force strength and link length #104

Merged
merged 6 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/graph/Graph.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class Graph extends React.Component {

const forceLink = d3ForceLink(this.state.d3Links)
.id(l => l.id)
.distance(D3_CONST.LINK_IDEAL_DISTANCE)
.distance(this.state.config.link.size || D3_CONST.LINK_IDEAL_DISTANCE)
.strength(D3_CONST.FORCE_LINK_STRENGTH);
Copy link
Owner

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:

// Some d3 constant values
const D3_CONST = {
    FORCE_LINK_STRENGTH: 1,
    LINK_IDEAL_DISTANCE: 100,
    SIMULATION_ALPHA_TARGET: 0.05
};

They are on the top of this file


this.state.simulation.force(CONST.LINK_CLASS_NAME, forceLink);
Expand Down
10 changes: 8 additions & 2 deletions src/components/graph/graph.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Owner

Choose a reason for hiding this comment

The 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 object property, can you please move this to:

...
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
Expand All @@ -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"/>
*
Expand Down Expand Up @@ -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
}
};
12 changes: 9 additions & 3 deletions src/components/graph/graph.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for node.gravity check this prop will always exist

);

return {
id: formatedId,
Expand Down