8.0.2 • Published 2 days ago

react-tree-graph v8.0.2

Weekly downloads
1,535
License
MIT
Repository
github
Last release
2 days ago

react-tree-graph Github

Build Status Coverage Status npm version npm bundle size license Storybook

A simple react component which renders data as a tree using svg.

Supports react 15.3+.

Check out the examples and the demo.

Older Versions

7.X 6.X 5.X 4.X 3.X 2.X 1.X

Installation

npm install react-tree-graph --save

Usage

import { Tree } from 'react-tree-graph';

const data = {
	name: 'Parent',
	children: [{
		name: 'Child One'
	}, {
		name: 'Child Two'
	}]
};

<Tree
	data={data}
	height={400}
	width={400}/>);

import { AnimatedTree } from 'react-tree-graph';

<AnimatedTree
	data={data}
	height={400}
	width={400}/>);

If you are using webpack, and have css-loader, you can include some default styles with:

import 'react-tree-graph/dist/style.css'

Alternatively, both the JavaScript and CSS can be included directly from the dist folder with script tags.

Configuration

Tree

PropertyTypeMandatoryDefaultDescription
dataobjectyesThe data to be rendered as a tree. Must be in a format accepted by d3.hierarchy.
marginsobject{ bottom : 10, left : 20, right : 150, top : 10}The margins around the content. The right margin should be larger to include the rendered label text.
heightnumberyesThe height of the rendered tree, including margins.
widthnumberyesThe width of the rendered tree, including margins.
directionltr,rtlltrThe direction the tree will be rendered in. Either left-to-right or right-to-left.
childrennodeWill be rendered as children of the SVG, before the links and nodes.
getChildrenfunction(node)node => node.childrenA function that returns the children for a node, or null/undefined if no children exist.
keyPropstring"name"The property on each node to use as a key.
labelPropstring"name"The property on each node to render as a label.
nodeShapecircle,image,polygon,rectcircleThe shape of the node icons.
nodePropsobject{}Props to be added to the <circle>, <image>, <polygon> or <rect> element. These will take priority over the default r added to circle and height, width, x and y added to image and rect.
gPropsobject{ className: 'node' }Props to be added to the <g> element. The default className will still be applied if a className property is not set.
pathPropsobject{ className: 'link' }Props to be added to the <path> element. The default className will still be applied if a className property is not set.
pathFuncfunction(x1,y1,x2,y2)curvedFunction to calculate the co-ordinates of the path between nodes.
svgPropsobject{}Props to be added to the <svg> element.
textPropsobject{}Props to be added to the <text> element.

AnimatedTree has the following properties in addition to the above.

PropertyTypeMandatoryDefaultDescription
durationnumber500The duration in milliseconds of animations.
easingfunction(interval)d3-ease.easeQuadOutThe easing function for animations. Takes in a number between 0 and 1, and returns a number between 0 and 1.
stepsnumber20The number of steps in animations. A higher number will result in a smoother animation, but too high will cause performance issues.

Events

Event handlers in nodeProps, gProps and textProps will be called with the node ID as an additional parameter.

function(event, nodeId) { ... }

Event handlers in pathProps will be called with the source and target node IDs as additional parameters.

function(event, sourceNodeId, targetNodeId) { ... }

Overriding props

The following properties can also be overridden by setting then for individual nodes.

Global PropNode Prop
keyPropkeyProp
labelProplabelProp
nodeShapeshape

The following object properties, if set on individual nodes, will be combined with the object properties set on the tree. If a property exists in both objects, the value from the node will be taken.

PropDescription
nodeProps
gProps
pathPropsProps for a path are taken from the target node.
textProps

TypeScript

Type definitions are available as a separate package. (thanks @PCOffline)

8.0.2

2 days ago

7.0.4

2 years ago

7.0.6

2 years ago

7.0.5

2 years ago

8.0.1

1 year ago

8.0.0

2 years ago

7.0.0

2 years ago

7.0.3

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

6.1.0

2 years ago

6.0.1

3 years ago

6.0.0

3 years ago

5.1.1

3 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.3.0

5 years ago

3.2.0

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.0.0

7 years ago

1.7.2

7 years ago

1.7.1

7 years ago

1.7.0

7 years ago

1.6.0

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago