react-d3-tree vs react-treebeard
Reactツリー表示ライブラリ
react-d3-treereact-treebeard類似パッケージ:
Reactツリー表示ライブラリ

Reactツリー表示ライブラリは、階層的なデータを視覚的に表現するためのツールです。これらのライブラリは、ユーザーがデータの構造を簡単に理解できるようにし、インタラクティブなツリーを作成するための機能を提供します。特に、データの親子関係を示すのに役立ち、複雑なデータセットを整理して表示することができます。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
react-d3-tree162,0231,182121 kB1479ヶ月前MIT
react-treebeard118,1911,690-726年前MIT
機能比較: react-d3-tree vs react-treebeard

カスタマイズ性

  • react-d3-tree:

    react-d3-treeは、D3.jsを基盤としているため、データの視覚化において非常に高いカスタマイズ性を提供します。ノードのスタイル、アニメーション、インタラクションを自由に設定でき、独自のデザイン要件に合わせたツリーを構築できます。

  • react-treebeard:

    react-treebeardは、基本的なスタイルのカスタマイズが可能ですが、react-d3-treeほどの柔軟性はありません。シンプルなデザインを求める場合には適していますが、複雑なビジュアル要件には制約があります。

パフォーマンス

  • react-d3-tree:

    react-d3-treeは、D3.jsの力を活用しており、大量のデータを効率的に処理できます。ただし、複雑なデータ構造や多くのノードを持つ場合、パフォーマンスに影響を与える可能性があるため、最適化が必要です。

  • react-treebeard:

    react-treebeardは、シンプルな実装により、軽量で高速なパフォーマンスを提供します。ノード数が少ない場合や、シンプルなツリー構造を表示する場合には非常に効果的です。

インタラクション

  • react-d3-tree:

    react-d3-treeは、ノードの展開や折りたたみ、ドラッグ&ドロップなどのインタラクションをサポートしています。ユーザーがデータを操作する際の体験を向上させるための多くの機能を提供しています。

  • react-treebeard:

    react-treebeardも基本的なインタラクションをサポートしていますが、react-d3-treeほどの豊富な機能はありません。ノードの展開や折りたたみは可能ですが、カスタマイズ性は限られています。

学習曲線

  • react-d3-tree:

    react-d3-treeは、D3.jsの知識が必要なため、学習曲線がやや急です。D3.jsに慣れていない開発者にとっては、初期の学習が必要ですが、その分、強力な機能を活用できます。

  • react-treebeard:

    react-treebeardは、シンプルなAPIを提供しているため、学習曲線が緩やかです。基本的な使い方が直感的で、迅速にプロジェクトに組み込むことができます。

ドキュメントとサポート

  • react-d3-tree:

    react-d3-treeは、詳細なドキュメントが提供されており、D3.jsのリソースも活用できるため、開発者が問題を解決しやすい環境が整っています。

  • react-treebeard:

    react-treebeardもドキュメントが整備されていますが、react-d3-treeに比べると情報量は少なめです。基本的な使用方法は明確ですが、複雑なカスタマイズに関しては情報が不足する場合があります。

選び方: react-d3-tree vs react-treebeard
  • react-d3-tree:

    react-d3-treeは、D3.jsを利用して高度なカスタマイズが可能なツリー構造を作成したい場合に選択してください。特に、視覚的な表現やアニメーションを重視するプロジェクトに適しています。

  • react-treebeard:

    react-treebeardは、シンプルで軽量なツリー表示を必要とする場合に選択してください。基本的な機能を持ち、簡単に使えるため、迅速な開発が求められるプロジェクトに向いています。

react-d3-tree のREADME

React D3 Tree

build status coverage status npm package npm package: downloads monthly npm package: minzipped size npm package: types code style: prettier

👾 Playground

📖 API Documentation (v3)

React D3 Tree is a React component that lets you represent hierarchical data (e.g. family trees, org charts, file directories) as an interactive tree graph with minimal setup, by leveraging D3's tree layout.

Upgrading from v1? Check out the v2 release notes.

Legacy v1 docs

Contents

Installation

npm i --save react-d3-tree

Usage

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

// This is a simplified example of an org chart with a depth of 2.
// Note how deeper levels are defined recursively via the `children` property.
const orgChart = {
  name: 'CEO',
  children: [
    {
      name: 'Manager',
      attributes: {
        department: 'Production',
      },
      children: [
        {
          name: 'Foreman',
          attributes: {
            department: 'Fabrication',
          },
          children: [
            {
              name: 'Worker',
            },
          ],
        },
        {
          name: 'Foreman',
          attributes: {
            department: 'Assembly',
          },
          children: [
            {
              name: 'Worker',
            },
          ],
        },
      ],
    },
  ],
};

export default function OrgChartTree() {
  return (
    // `<Tree />` will fill width/height of its container; in this case `#treeWrapper`.
    <div id="treeWrapper" style={{ width: '50em', height: '20em' }}>
      <Tree data={orgChart} />
    </div>
  );
}

Props

For details on all props accepted by Tree, check out the TreeProps reference docs.

The only required prop is data, all other props on Tree are optional/pre-defined (see "Default value" on each prop definition).

Working with the default Tree

react-d3-tree provides default implementations for Tree's nodes & links, which are intended to get you up & running with a working tree quickly.

This section is focused on explaining how to provide data, styles and event handlers for the default Tree implementation.

Need more fine-grained control over how nodes & links appear/behave? Check out the Customizing the Tree section below.

Providing data

By default, Tree expects each node object in data to implement the RawNodeDatum interface:

interface RawNodeDatum {
  name: string;
  attributes?: Record<string, string | number | boolean>;
  children?: RawNodeDatum[];
}

The orgChart example in the Usage section above is an example of this:

  • Every node has at least a name. This is rendered as the node's primary label.
  • Some nodes have attributes defined (the CEO node does not). The key-value pairs in attributes are rendered as a list of secondary labels.
  • Nodes can have further RawNodeDatum objects nested inside them via the children key, creating a hierarchy from which the tree graph can be generated.

Styling Nodes

Tree provides the following props to style different types of nodes, all of which use an SVG circle by default:

  • rootNodeClassName - applied to the root node.
  • branchNodeClassName - applied to any node with 1+ children.
  • leafNodeClassName - applied to any node without children.

To visually distinguish these three types of nodes from each other by color, we could provide each with their own class:

/* custom-tree.css */

.node__root > circle {
  fill: red;
}

.node__branch > circle {
  fill: yellow;
}

.node__leaf > circle {
  fill: green;
  /* Let's also make the radius of leaf nodes larger */
  r: 40;
}
import React from 'react';
import Tree from 'react-d3-tree';
import './custom-tree.css';

// ...

export default function StyledNodesTree() {
  return (
    <div id="treeWrapper" style={{ width: '50em', height: '20em' }}>
      <Tree
        data={data}
        rootNodeClassName="node__root"
        branchNodeClassName="node__branch"
        leafNodeClassName="node__leaf"
      />
    </div>
  );
}

For more details on the className props for nodes, see the TreeProps reference docs.

Styling Links

Tree provides the pathClassFunc property to pass additional classNames to every link to be rendered.

Each link calls pathClassFunc with its own TreeLinkDatum and the tree's current orientation. Tree expects pathClassFunc to return a className string.

function StyledLinksTree() {
  const getDynamicPathClass = ({ source, target }, orientation) => {
    if (!target.children) {
      // Target node has no children -> this link leads to a leaf node.
      return 'link__to-leaf';
    }

    // Style it as a link connecting two branch nodes by default.
    return 'link__to-branch';
  };

  return (
    <Tree
      data={data}
      // Statically apply same className(s) to all links
      pathClassFunc={() => 'custom-link'}
      // Want to apply multiple static classes? `Array.join` is your friend :)
      pathClassFunc={() => ['custom-link', 'extra-custom-link'].join(' ')}
      // Dynamically determine which `className` to pass based on the link's properties.
      pathClassFunc={getDynamicPathClass}
    />
  );
}

For more details, see the PathClassFunction reference docs.

Event Handlers

Tree exposes the following event handler callbacks by default:

Note: Nodes are expanded/collapsed whenever onNodeClick fires. To prevent this, set the collapsible prop to false.
onNodeClick will still fire, but it will not change the target node's expanded/collapsed state.

Customizing the Tree

renderCustomNodeElement

The renderCustomNodeElement prop accepts a custom render function that will be used for every node in the tree.

Cases where you may find rendering your own Node element useful include:

  • Using a different SVG tag for your nodes (instead of the default <circle>) - Example (codesandbox.io)
  • Gaining fine-grained control over event handling (e.g. to implement events not covered by the default API) - Example (codesandbox.io)
  • Building richer & more complex nodes/labels by leveraging the foreignObject tag to render HTML inside the SVG namespace - Example (codesandbox.io)

pathFunc

The pathFunc prop accepts a predefined PathFunctionOption enum or a user-defined PathFunction.

By changing or providing your own pathFunc, you are able to change how links between nodes of the tree (which are SVG path tags under the hood) are drawn.

The currently available enums are:

  • diagonal (default)
  • elbow
  • straight
  • step

Want to see how each option looks? Try them out on the playground.

Providing your own pathFunc

If none of the available path functions suit your needs, you're also able to provide a custom PathFunction:

function CustomPathFuncTree() {
  const straightPathFunc = (linkDatum, orientation) => {
    const { source, target } = linkDatum;
    return orientation === 'horizontal'
      ? `M${source.y},${source.x}L${target.y},${target.x}`
      : `M${source.x},${source.y}L${target.x},${target.y}`;
  };

  return (
    <Tree
      data={data}
      // Passing `straight` function as a custom `PathFunction`.
      pathFunc={straightPathFunc}
    />
  );
}

For more details, see the PathFunction reference docs.

Development

Setup

To set up react-d3-tree for local development, clone the repo and follow the steps below:

# 1. Set up the library, create a reference to it for symlinking.
cd react-d3-tree
npm i
npm link

# 2. Set up the demo/playground, symlink to the local copy of `react-d3-tree`.
cd demo
npm i
npm link react-d3-tree

Tip: If you'd prefer to use your own app for development instead of the demo, simply run npm link react-d3-tree in your app's root folder instead of the demo's :)

Hot reloading

npm run build:watch

If you're using react-d3-tree/demo for development, open up another terminal window in the demo directory and call:

npm start

Contributors

A huge thank you to all the contributors, as well as users who have opened issues with thoughtful suggestions and feedback.