Skip to content

Commit ca84242

Browse files
authored
Fixing graph reset (#377)
1 parent 46050fa commit ca84242

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/components/graph/Graph.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
checkForGraphElementsChanges,
1717
getCenterAndZoomTransformation,
1818
initializeGraphState,
19+
initializeNodes,
1920
} from "./graph.helper";
2021
import { renderGraph } from "./graph.renderer";
2122
import { merge, debounce, throwErr } from "../../utils";
@@ -478,13 +479,20 @@ export default class Graph extends React.Component {
478479
*/
479480
resetNodesPositions = () => {
480481
if (!this.state.config.staticGraph) {
482+
let initialNodesState = initializeNodes(this.props.data.nodes);
481483
for (let nodeId in this.state.nodes) {
482484
let node = this.state.nodes[nodeId];
483485

484486
if (node.fx && node.fy) {
485487
Reflect.deleteProperty(node, "fx");
486488
Reflect.deleteProperty(node, "fy");
487489
}
490+
491+
if (nodeId in initialNodesState) {
492+
let initialNode = initialNodesState[nodeId];
493+
node.x = initialNode.x;
494+
node.y = initialNode.y;
495+
}
488496
}
489497

490498
this.state.simulation.alphaTarget(this.state.config.d3.alphaTarget).restart();

src/components/graph/graph.helper.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function _initializeLinks(graphLinks, config) {
102102
* and highlighted values.
103103
* @memberof Graph/helper
104104
*/
105-
function _initializeNodes(graphNodes) {
105+
function initializeNodes(graphNodes) {
106106
let nodes = {};
107107
const n = graphNodes.length;
108108

@@ -394,7 +394,7 @@ function initializeGraphState({ data, id, config }, state) {
394394

395395
let newConfig = { ...merge(DEFAULT_CONFIG, config || {}) },
396396
links = _initializeLinks(graph.links, newConfig), // matrix of graph connections
397-
nodes = _tagOrphanNodes(_initializeNodes(graph.nodes), links);
397+
nodes = _tagOrphanNodes(initializeNodes(graph.nodes), links);
398398
const { nodes: d3Nodes, links: d3Links } = graph;
399399
const formatedId = id.replace(/ /g, "_");
400400
const simulation = _createForceSimulation(newConfig.width, newConfig.height, newConfig.d3 && newConfig.d3.gravity);
@@ -561,4 +561,5 @@ export {
561561
initializeGraphState,
562562
updateNodeHighlightedValue,
563563
getNormalizedNodeCoordinates,
564+
initializeNodes,
564565
};

0 commit comments

Comments
 (0)