Visualize Hierarchical Tree Structures Using D3.js – d3-mitch-tree

Category: Chart & Graph , Javascript | February 4, 2019
Author:deltoss
Views Total:21,760 views
Official Page:Go to website
Last Update:February 4, 2019
License:MIT

Preview:

Visualize Hierarchical Tree Structures Using D3.js – d3-mitch-tree

Description:

The d3-mitch-tree JavaScript library lets you render an interactive diagram from JS objects/arrays to visualize the hierarchical tree structures in an elegant way.

Built with the d3.js library and supports zoom, panning, centering, and AJAX loading.

How to use it:

Load the d3-mitch-tree’s core JavaScript and Stylesheet in the document.

<script src="dist/js/d3-mitch-tree.min.js"></script>
<link rel="stylesheet" href="dist/css/d3-mitch-tree.min.css">

Load the default theme in the document. You can also create your own themes just like the d3-mitch-tree-theme-default.css.

<link rel="stylesheet" href="dist/css/d3-mitch-tree-theme-default.min.css">

Define your tree data in the JavaScript objects/arrays as follows:

// flat data
var data = [
    {
      "id": 1,
      "name": "Animals",
      "type": "Root",
      "description": "A living organism that feeds on organic matter"
    },
    {
      "id": 2,
      "parentId": 1,
      "name": "Carnivores",
      "type": "Type",
      "description": "Diet consists solely of animal materials"
    },
    // more data here
]
// nested data
var data = {
    "id": 1,
    "name": "Animals",
    "type": "Root",
    "description": "A living organism that feeds on organic matter",
    "children": [{
        "id": 2,
        "name": "Carnivores",
        "type": "Type",
        "description": "Diet consists solely of animal materials",
        "children": [
          {
            "id": 3,
            "name": "Felidae",
            "type": "Family",
            "description": "Also known as cats",
          }]
    }]
};

Create a new d3.mitchTree object and set the data source.

var treePlugin = new d3.mitchTree.boxedTree()
    .setData(data)

Specify the container element to hold the tree structures.

var treePlugin = new d3.mitchTree.boxedTree()
    .setData(data)
    .setElement(document.getElementById("container-element"))

Initialize the d3.mitchTree.

var treePlugin = new d3.mitchTree.boxedTree()
    .setData(data)
    .setElement(document.getElementById("container-element"))
    .setIdAccessor(function(data) {
      return data.id;
    })
    .setChildrenAccessor(function(data) {
      return data.children;
    })
    .setBodyDisplayTextAccessor(function(data) {
      return data.description;
    })
    .setTitleDisplayTextAccessor(function(data) {
      return data.name;
    })
    .initialize();

Alternative Options Object Syntax, opposed to the Fluent Interface Above.

var options = {
    data: data,
    element: document.getElementById("example"),
    getId: function (data) {
      return data.id;
    },
    getChildren: function (data) {
      return data.children;
    },
    getBodyDisplayText: function (data) {
      return data.description;
    },
    getTitleDisplayText: function (data) {
      return data.name;
    }
};
var treePlugin = new d3.MitchTree.BoxedTree(options).initialize();

You Might Be Interested In:


4 thoughts on “Visualize Hierarchical Tree Structures Using D3.js – d3-mitch-tree

    1. Mariano Javier Scazzino

      Hello!
      You can assemble the dist folder from the source code of the examples page. Follow these steps:

      1 – Open the source code of the page.
      2 – Search for links that exist to the “dist” folder. It’s these 3:

      http://dist%20/%20js%20/%20d3-mitch-tree.min.js

      3 – Assemble the URL with the name of the server and the part of the link you obtained from the previous step. They would look like this:

      https://cssscript.com/demo/visualize-hierarchical-tree-d3-mitch/dist/js/d3-mitch-tree.min.js
      https://cssscript.com/demo/visualize-hierarchical-tree-d3-mitch/dist/css/d3-mitch-tree.min.css
      https://cssscript.com/demo/visualize-hierarchical-tree-d3-mitch/dist/css/d3-mitch-tree-theme-default.min.css

      4 – Access each URL from a browser.
      5 – The code obtained, save it with the name that corresponds to each one maintaining the corresponding directory structure (dist/js for the .js files and dist/css for the .css files)
      6 – Ready. You already have the “dist” folder armed with the necessary files.

      I have tested this with the download of the compressed file from GitHub to which I “added” this “dist” directory and that way I got the included examples to work again.

      Luck!

      Reply
  1. Mikael

    Hello,

    Is it possible to target a particular tree field with a CSS class in order to apply a specific color?
    If so, how should it be done?

    Thanks in advance.

    Reply

Leave a Reply