src/components/graph/graph.helper.js
function initializeGraphState({ data, id, config }, state) {
    _validateGraphData(data);

    let graph;

    if (state && state.nodes) {
        graph = {
            nodes: data.nodes.map(n =>
                state.nodes[n.id]
                    ? Object.assign({}, n, utils.pick(state.nodes[n.id], NODE_PROPS_WHITELIST))
                    : Object.assign({}, n)
            ),
            links: data.links.map((l, index) => _mapDataLinkToD3Link(l, index, state && state.d3Links, config, state)),
        };
    } else {
        graph = {
            nodes: data.nodes.map(n => Object.assign({}, n)),
            links: data.links.map(l => Object.assign({}, l)),
        };
    }

    let newConfig = Object.assign({}, utils.merge(DEFAULT_CONFIG, config || {}));
    let links = _initializeLinks(graph.links, newConfig); // matrix of graph connections
    let nodes = _tagOrphanNodes(_initializeNodes(graph.nodes), links);
    const { nodes: d3Nodes, links: d3Links } = graph;
    const formatedId = id.replace(/ /g, "_");
    const simulation = _createForceSimulation(newConfig.width, newConfig.height, newConfig.d3 && newConfig.d3.gravity);

    const { minZoom, maxZoom, focusZoom } = newConfig;

    if (focusZoom > maxZoom) {
        newConfig.focusZoom = maxZoom;
    } else if (focusZoom < minZoom) {
        newConfig.focusZoom = minZoom;
    }

    return {
        id: formatedId,
        config: newConfig,
        links,
        d3Links,
        nodes,
        d3Nodes,
        highlightedNode: "",
        simulation,
        newGraphElements: false,
        configUpdated: false,
        transform: 1,
    };
}
function initializeGraphState({ data, id, config }, state) {
    _validateGraphData(data);

    let graph;

    if (state && state.nodes) {
        graph = {
            nodes: data.nodes.map(n =>
                state.nodes[n.id]
                    ? Object.assign({}, n, utils.pick(state.nodes[n.id], NODE_PROPS_WHITELIST))
                    : Object.assign({}, n)
            ),
            links: data.links.map((l, index) => _mapDataLinkToD3Link(l, index, state && state.d3Links, config, state)),
        };
    } else {
        graph = {
            nodes: data.nodes.map(n => Object.assign({}, n)),
            links: data.links.map(l => Object.assign({}, l)),
        };
    }

    let newConfig = Object.assign({}, utils.merge(DEFAULT_CONFIG, config || {}));
    let links = _initializeLinks(graph.links, newConfig); // matrix of graph connections
    let nodes = _tagOrphanNodes(_initializeNodes(graph.nodes), links);
    const { nodes: d3Nodes, links: d3Links } = graph;
    const formatedId = id.replace(/ /g, "_");
    const simulation = _createForceSimulation(newConfig.width, newConfig.height, newConfig.d3 && newConfig.d3.gravity);

    const { minZoom, maxZoom, focusZoom } = newConfig;

    if (focusZoom > maxZoom) {
        newConfig.focusZoom = maxZoom;
    } else if (focusZoom < minZoom) {
        newConfig.focusZoom = minZoom;
    }

    return {
        id: formatedId,
        config: newConfig,
        links,
        d3Links,
        nodes,
        d3Nodes,
        highlightedNode: "",
        simulation,
        newGraphElements: false,
        configUpdated: false,
        transform: 1,
    };
}
function initializeGraphState ( { data , id , config } , state ) { _validateGraphData ( data ) ; let graph ; if ( state && state . nodes ) { graph = { nodes : data . nodes . map ( n => state . nodes [ n . id ] ? Object . assign ( { } , n , utils . pick ( state . nodes [ n . id ] , NODE_PROPS_WHITELIST ) ) : Object . assign ( { } , n ) ) , links : data . links . map ( ( l , index ) => _mapDataLinkToD3Link ( l , index , state && state . d3Links , config , state ) ) , } ; } else { graph = { nodes : data . nodes . map ( n => Object . assign ( { } , n ) ) , links : data . links . map ( l => Object . assign ( { } , l ) ) , } ; } let newConfig = Object . assign ( { } , utils . merge ( DEFAULT_CONFIG , config || { } ) ) ; let links = _initializeLinks ( graph . links , newConfig ) ; // matrix of graph connections let nodes = _tagOrphanNodes ( _initializeNodes ( graph . nodes ) , links ) ; const { nodes : d3Nodes , links : d3Links } = graph ; const formatedId = id . replace ( / / g , "_" ) ; const simulation = _createForceSimulation ( newConfig . width , newConfig . height , newConfig . d3 && newConfig . d3 . gravity ) ; const { minZoom , maxZoom , focusZoom } = newConfig ; if ( focusZoom > maxZoom ) { newConfig . focusZoom = maxZoom ; } else if ( focusZoom < minZoom ) { newConfig . focusZoom = minZoom ; } return { id : formatedId , config : newConfig , links , d3Links , nodes , d3Nodes , highlightedNode : "" , simulation , newGraphElements : false , configUpdated : false , transform : 1 , } ; }
Encapsulates common procedures to initialize graph.
@param {Object} props - Graph component props, object that holds data, id and config.
@param {Object} props.data - Data object holds links (array of **Link**) and nodes (array of **Node**).
@param {string} props.id - the graph id.
@param {Object} props.config - same as {@link #graphrenderer|config in renderGraph}.
@param {Object} state - Graph component current state (same format as returned object on this function).
@returns {Object} a fully (re)initialized graph state object.
@memberof Graph/helper
Encapsulates common procedures to initialize graph .