- Notifications
You must be signed in to change notification settings - Fork23
xcomponent/react-gojs
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
react-gojs is aGoJS React integration.
Install it from npm. It has peer dependencies of react and react-dom, which will have to be installed as well.
npm install --save react-gojs
or:
yarn add react-gojs
ImportGojsDiagram
in your React component:
importGojsDiagramfrom'react-gojs';
To create a GoJS diagram, just use theGojsDiagram React component:
<GojsDiagramdiagramId="myDiagramDiv"model={this.props.model}createDiagram={this.createDiagram}className="myDiagram"onModelChange={this.modelChangedhandler}updateDiagramProps={this.updateDiagramProps}/>
GojsDiagram is a generic React component which is responsible for rendering and updating (when the model changes) the diagram. The render step is based on the model and the go.Diagram object provided as props. It acts as a go.Diagram wrapper.
GojsDiagram props:
- diagramId:id required by GoJS.
- model: generic model containing nodes and links.
Model type:DiagramModel<N extends BaseNodeModel, L extends LinkModel>
Example (Typescript / Javascript):
constmodel={nodeDataArray:[{key:'Alpha',color:'lightblue'},{key:'Beta',color:'orange'},{key:'Gamma',color:'lightgreen'},{key:'Delta',color:'pink'},{key:'Omega',color:'grey'}],linkDataArray:[{from:'Alpha',to:'Beta'},{from:'Alpha',to:'Gamma'},{from:'Beta',to:'Delta'},{from:'Gamma',to:'Omega'}]};
- createDiagram: method called by the React component to create the customized GoJS diagram object. It enables you to customize the look and feel.
Typescript example:
constcreateDiagram=(diagramId:string):Diagram=>{const$=go.GraphObject.make;constmyDiagram:Diagram=$(go.Diagram,diagramId,{initialContentAlignment:go.Spot.LeftCenter});myDiagram.nodeTemplate=$(go.Node,'Auto',$(go.Shape,'RoundedRectangle',{strokeWidth:0},newgo.Binding('fill','color')),$(go.TextBlock,{margin:8},newgo.Binding('text','key')));returnmyDiagram;};
Javascript (ES6) example:
constcreateDiagram=diagramId=>{const$=go.GraphObject.make;constmyDiagram=$(go.Diagram,diagramId,{initialContentAlignment:go.Spot.LeftCenter});myDiagram.nodeTemplate=$(go.Node,'Auto',$(go.Shape,'RoundedRectangle',{strokeWidth:0},newgo.Binding('fill','color')),$(go.TextBlock,{margin:8},newgo.Binding('text','key')));returnmyDiagram;};
- className: css applied to thediv containing our diagram. You should define at least width and height.
Example:
.myDiagram {width:70%;height:400px;}
- onModelChange: theonModelChange event occurs when the diagram model has changed (add/remove nodes/links from the UI). This event is very useful to keep your model (provided as prop) in sync with the diagram.
For example, in a Redux environment, the diagram model should be immutable (and stored in the redux store). TheonModelChange handler can dispatch actions to update the model.
- updateDiagramProps (optional parameter): Method allows to update/modify Diagram properties dynamically once the diagram has been rendered. It gives more control to the user, as it is a user-defined.Basic implementation of the method.
Example 1:
const updateDiagramProps = (myDiagram: Diagram): void => {myDiagram.layout = go.GraphObject.make(go.LayeredDigraphLayout, { direction: 90 });// User can add more properties here.};
Example 2:
const updateDiagramProps = (myDiagram: Diagram): void => {// Empty method.};
Typescript: You can find a react / redux / react-gojs example + live demohere.
Javascript (ES6): You can find a react / react-gojs example + live demohere.
yarn install
yarn build
yarn test
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Fix lint errors:
yarn lint
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request