Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-tree-graph

jpb1212.4kMIT8.0.3TypeScript support: definitely-typed

A react library for generating a graphical tree from data using d3

d3, graph, react, svg, tree, ui

readme

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 16.8+.

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

Property Type Mandatory Default Description
data object yes | The data to be rendered as a tree. Must be in a format accepted by d3.hierarchy.
margins object | { 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.
height number yes | The height of the rendered tree, including margins.
width number yes | The width of the rendered tree, including margins.
direction ltr,rtl | ltr The direction the tree will be rendered in. Either left-to-right or right-to-left.
children node | Will be rendered as children of the SVG, before the links and nodes.
getChildren function(node) | node => node.children A function that returns the children for a node, or null/undefined if no children exist.
keyProp string | "name" The property on each node to use as a key.
labelProp string | "name" The property on each node to render as a label.
nodeShape circle,image,polygon,rect | circle The shape of the node icons.
nodeProps object | {} 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.
gProps object | { className: 'node' } Props to be added to the <g> element. The default className will still be applied if a className property is not set.
pathProps object | { className: 'link' } Props to be added to the <path> element. The default className will still be applied if a className property is not set.
pathFunc function(x1,y1,x2,y2) | curved Function to calculate the co-ordinates of the path between nodes.
svgProps object | {} Props to be added to the <svg> element.
textProps object | {} Props to be added to the <text> element.

AnimatedTree has the following properties in addition to the above.

Property Type Mandatory Default Description
duration number | 500 The duration in milliseconds of animations.
easing function(interval) | d3-ease.easeQuadOut The easing function for animations. Takes in a number between 0 and 1, and returns a number between 0 and 1.
steps number | 20 The 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 Prop Node Prop
keyProp keyProp
labelProp labelProp
nodeShape shape

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.

Prop Description
nodeProps
gProps
pathProps Props for a path are taken from the target node.
textProps

TypeScript

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

changelog

Changelog

8.0.3 (13 Jan 2025)

  • Fixing defaultProps error in react 19

8.0.2 (30 Apr 2024)

  • Support for react 19
  • Dropped support for react < 16.8 (already broken in a pervious update)

8.0.1 (10 Jan 2023)

  • Fixing overriding of unrelated props

8.0.0 (25 Oct 2022)

  • Breaking change: Fixed x and y co-ordinates being flipped in pathfunc
  • Right-to-left support
  • Fixed margins not being applied properly

7.0.6 (21 Oct 2022)

  • Fixing cleanup of finished animations

7.0.5 (21 Oct 2022)

  • Fixing cleanup of unfinished animations

7.0.4 (21 Oct 2022)

  • Reducing bundle size
  • Rewriting to use functional components

7.0.3 (11 Feb 2022)

  • Moving RFDC from dependency to dev dependency

7.0.2 (9 Feb 2022)

  • Fixing preinstall script causes install failure

7.0.1 (9 Feb 2022)

  • Fixing non-string node labels

7.0.0 (10 Jan 2022)

  • Breaking change: Single default export replaced with two named exports
  • Support for tree shaking
  • Significantly reduces bundle size, reduced even further if not using animations

Migrating

  • If you were using animated=true, use the named AnimatedTree export
  • Otherwise, use the named Tree export
  • The animated prop is no longer used and can be removed

6.1.0 (29 Nov 2021)

  • Support for react 18

6.0.1 (17 Jun 2021)

  • Removing unneeded files from npm package

6.0.0 (9 Feb 2021)

  • Breaking change: Dropped support for IE (reduces bundle size by about 1/3)
  • Updated to d3 2.0.0

5.1.1 (8 Feb 2021)

  • Added support for react 17 as a peer dependency

5.1.0 (27 Jun 2020)

  • Adding support for image

5.0.0 (19 Jun 2020)

  • Breaking change: Adding support for rect and polygon
  • Breaking change: Allowing textProps to override default offsets
  • Breaking change: Fixing incorrect default offsets
  • Breaking change: Wrapping nodes and links in a <g> node for easier transformations

Migrating

  • If you were using circleProps, use nodeProps instead. The format is the same
  • If you were using nodeRadius, instead pass an r prop through nodeProps
  • If you were using nodeOffset, instead pass a dy prop through textProps
  • If you had css selectors relying on the path and g nodes being immediate children of svg, you will have to modify these due to the additional g node inbetween
  • If you weren't using nodeOffset, node text position will change slightly

4.1.1 (5 Jun 2020)

  • Fixed incorrect proptype (thanks @josh-stevens)

4.1.0 (16 Mar 2020)

  • Added pathFunc prop to configure custom paths

4.0.1 (12 Aug 2019)

  • Fixing default classname being removed when any props configured

4.0.0 (11 Feb 2019)

  • Breaking change: additional parameters are now passed in after the event parameter
  • Added support for additional parameters in arbitrary event handlers

3.3.0 (7 Feb 2019)

  • onContextMenu handlers for nodes and links now have the same additional parameters as onClick (thanks @Linton-Samuel-Dawson)

3.2.0 (24 Sep 2018)

  • Adding rendering of custom children

3.1.1 (21 Apr 2018)

  • Replaced webpack with rollup for smaller bundle size and better performance

3.1.0 (21 Dec 2017)

  • Changed babel transform settings to reduce minified bundle size

3.0.0 (16 Dec 2017)

  • New props for adding any prop to any DOM element
    • circleProps
    • gProps
    • pathProps
    • svgProps
    • textProps
  • Redundant props have been removed
    • linkClassName
    • linkClassHandler
    • nodeClassName
    • nodeClassHandler
    • treeClassName
    • treeClickHandler

2.0.0 (12 Jul 2017)

  • Animations
    • Significant performance improvements on large trees (tested with > 150 nodes)
    • Added nodes now animate from the position of the closest, previously visible, ancestor
    • Removed nodes now animate to the position of the closest, remaining ancestor
  • Renamed Class props to ClassName props
  • Added importing of polyfills for IE support

1.7.2 (7 Jul 2017)

  • Fixing initial position of added animated nodes when root moves

1.7.1 (4 Jul 2017)

  • Updating built files to include change in previous version

1.7.0 (26 Jun 2017)

  • Added treeClass and treeClickHandler props

1.6.0 (24 Jun 2017)

  • Adding animations

1.5.0 (13 May 2017)

  • Removed warnings in react 15.5+

1.4.0 (29 Apr 2017)

  • Added getChildren prop

1.3.0 (14 Apr 2017)

  • Node and click handlers now have event as a second parameter (thanks @ronaldborman)

1.2.3 (11 Apr 2017)

  • Updating built files to include change in previous version

1.2.2 (11 Apr 2017)

  • Fixed undefined being passed into Link's onClick handler

1.2.1 (29 Mar 2017)

  • Using d3-hierarchy instead of d3. This should significantly reduce bundle size

1.2.0 (5 Mar 2017)

  • Upgraded dependencies, including webpack to 2.x
  • Included a CSS file for basic styling

1.1.0 (14 Dec 2016)

  • Upgraded d3 dependency to 4.4.0
  • Included a minified file

1.0.1 (11 Dec 2016)

  • Removing an npm shrinkwrap file. Its presence caused duplicate dependencies to be installed when react-tree-graph was installed

1.0.0 (11 Dec 2016)

  • Initial release