GithubHelp home page GithubHelp logo

ydeshayes / react-d3-tree Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bkrem/react-d3-tree

0.0 2.0 0.0 3.51 MB

:twisted_rightwards_arrows: React component to create interactive D3 tree graphs

Home Page: https://bkrem.github.io/react-d3-tree/

License: MIT License

JavaScript 98.41% CSS 1.59%

react-d3-tree's Introduction

React D3 Tree

Build Status Coverage Status Codacy Badge npm version

React D3 Tree is a React component that lets you represent hierarchical data (e.g. ancestor trees, organisational structure, package dependencies) as an animated & interactive tree graph by leveraging D3's tree layout.

Contents

Demo

Installation

To install via NPM, run:

npm i --save react-d3-tree

Or if Yarn is more your thing:

yarn add react-d3-tree

Usage

import React from 'react';
import Tree from 'react-d3-tree';

const myTreeData = [
  {
    name: 'Top Level',
    attributes: {
      keyA: 'val A',
      keyB: 'val B',
      keyC: 'val C',
    },
    children: [
      {
        name: 'Level 2: A',
        attributes: {
          keyA: 'val A',
          keyB: 'val B',
          keyC: 'val C',
        },
      },
      {
        name: 'Level 2: B',
      },
    ],
  },
];

class MyComponent extends React.Component {
  render() {
    return (
      {/* <Tree /> will fill width/height of its container; in this case `#treeWrapper` */}
      <div id="treeWrapper" style={{width: '50em', height: '20em'}}> 
      
        <Tree data={myTreeData} />
        
      </div>
    );
  }
}

Props

Property Type Options Required? Default Description
data array required undefined Single-element array containing hierarchical object (see myTreeData above).
Contains (at least) name and parent keys.
orientation string (enum) horizontal vertical horizontal horizontal - Tree expands left-to-right.

vertical - Tree expands top-to-bottom.
translate object {x: 0, y: 0} Translates the graph along the x/y axis by the specified amount of pixels (avoids the graph being stuck in the top left canvas corner).
pathFunc string (enum) diagonal elbow diagonal diagonal - Renders smooth, curved edges between parent-child nodes.

elbow - Renders sharp edges at right angles between parent-child nodes.
collapsible bool true Toggles ability to collapse/expand the tree's nodes by clicking them.
initialDepth number 0..n undefined Sets the maximum node depth to which the tree is expanded on its initial render.
Tree renders to full depth if prop is omitted.
depthFactor number -n..0..n undefined Ensures the tree takes up a fixed amount of space (node.y = node.depth * depthFactor), regardless of tree depth.
TIP: Negative values invert the tree's direction.
zoomable bool true Toggles ability to zoom in/out on the Tree by scaling it according to props.scaleExtent.
scaleExtent object {min: 0.1, max: 1} Sets the minimum/maximum extent to which the tree can be scaled if props.zoomable is true.
transitionDuration number 0..n 500 Sets the animation duration (in ms) of each expansion/collapse of a tree node.

Set this to 0 to deactivate animations completely.
styles object see Styling Node/Link CSS files Overrides and/or enhances the tree's default styling.

Styling

The tree's styles prop may be used to override any of the tree's default styling. The following object shape is expected by styles:

{
  links: <svgStyleObject>,
  nodes: {
    node: {
      circle: <svgStyleObject>,
      name: <svgStyleObject>,
      attributes: <svgStyleObject>,
    },
    leafNode: {
      circle: <svgStyleObject>,
      name: <svgStyleObject>,
      attributes: <svgStyleObject>,
    },
  },
}

where <svgStyleObject> is any object containing CSS-like properties that are compatible with an <svg> element's style attribute, for example:

{
  stroke: 'blue',
  strokeWidth: 3,
}

For more information on the SVG style attribute, check this out.

External data sources

Statically hosted JSON or CSV files can be used as data sources via the additional treeUtil module.

Example

import React from 'react';
import { Tree, treeUtil } from 'react-d3-tree';

const csvSource = 'https://raw.githubusercontent.com/bkrem/react-d3-tree/master/docs/examples/data/csv-example.csv';

constructor() {
  super();

  this.state = {
    data: undefined,
  };
}

componentWillMount() {
  treeUtil.parseCSV(csvSource)
  .then((data) => {
    this.setState({ data })
  })
  .catch((err) => console.error(err));
}

class MyComponent extends React.Component {
  render() {
    return (
      {/* <Tree /> will fill width/height of its container; in this case `#treeWrapper` */}
      <div id="treeWrapper" style={{width: '50em', height: '20em'}}> 
      
        <Tree data={this.state.data} />
        
      </div>
    );
  }
}

For details regarding the treeUtil module, please check the module's API docs.
For examples of each data type that can be parsed with treeUtil, please check the data source examples.

react-d3-tree's People

Contributors

bkrem avatar

Watchers

James Cloos avatar Deshayes Yann avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.