UNPKG

react-sigma-conglei

Version:

Lightweight but powerful library for drawing network graphs built on top of dunnock/react-sigma

51 lines (42 loc) 1.26 kB
import React from 'react'; import { embedProps } from './tools'; /** RandomizeNodePositions component, sets random positions to all nodes. Can be used within Sigma component with predefined graph or within graph loader component. **/ class RandomizeNodePositions extends React.PureComponent { constructor(props) { super(props); if (this.props.sigma) { this.props.sigma.graph.nodes().forEach(n => { n.x = Math.random(); n.y = Math.random(); }); } if (this.props.sigma) this.props.sigma.refresh(); } componentDidMount() { if (this.props.sigma) this.props.sigma.refresh(); } componentWillReceiveProps(nextProps) { if (this.props.sigma !== nextProps.sigma) { nextProps.sigma.graph.nodes().forEach(n => { n.x = Math.random(); n.y = Math.random(); }); } nextProps.sigma.refresh(); } render() { return React.createElement( 'div', null, embedProps(this.props.children, { sigma: this.props.sigma }) ); } } RandomizeNodePositions.propTypes = { children: require('prop-types').any, sigma: typeof sigma === 'function' ? require('prop-types').instanceOf(sigma) : require('prop-types').any }; export default RandomizeNodePositions;