Giter Club home page Giter Club logo

treeviz's Introduction

Treeviz

Known Vulnerabilities David license

This javascript module aims at providing an easy interface in order to represent tree diagrams on screen with the ability to handle dynamic data flows. The data format must be JSON.

๐Ÿ’… Come play with the Treeviz storybook! ๐Ÿ’…

Installation

With npm : npm install treeviz

and then you can use it with : import * as Treeviz from 'treeviz';

Or download this repository and link the dist/index.js file in your page directly : <script src="./dist/index.js><script> Also, but not recommended, you can use : <script src="https://rawgit.com/PierreCapo/treeviz/master/dist/index.js"></script>

Usage

React

Check the treeviz wrapper for react: treeviz-react

Vanilla JavaScript

// Define a tree element where dimensions are mandatory
<div id="tree" style="height:700px; width:900px"></div>

<script>
// Define a dataset
var data = [
  { id: 1, text_1: "Father", father: null },
  { id: 2, text_1: "Child A", father: 1 },
  { id: 3, text_1: "Child B", father: 1 },
  { id: 4, text_1: "Subchild C", father: 2 }
];

// Define and configure a tree object
var myTree = Treeviz.create({
  htmlId: "tree",
  idKey: "id",
  hasFlatData: true,
  nodeColor: (nodeData) => "grey",
  relationnalField: "father",
});

// Display the tree based on the data
myTree.refresh(data);
</script>

To update the tree visually you will just have to pass new data to the refresh method like this :

myTree.refresh(data);
myTree.refresh(data_update1);
myTree.refresh(data_update2);

The tree will be clever enough to updates only the part of the trees that have been added or removed in the dataset, and so it won't redraw the entire tree.

Treeviz Example

Hierarchical data case :

var hierarchical_data_example = {
  name: "Mom",
  qty: 10,
  children: [
    { name: "Son A", qty: 3 },
    { name: "Son B", qty: 7 },
  ],
};

var myTree = Treeviz.create({
  htmlId: "tree",
  idKey: "name",
  hasFlatData: false,
  relationnalField: "children",
});

myTree.refresh(hierarchical_data_example);

API

The big part of the API is configuring the tree before passing data to it :

Treeviz.create(config);

The table below lists all the avalaible key that the config object can have

Key Type Default Definition
htmlId string (Required) The HTML id tag on the page where the tree should be drawn. It must have a width and an height specified
idKey string "id" The key in a data item representing the unique identifier of a node
relationnalField string "father" In case of flat dataset, usually the relationnal field between each node is the field representing the father of the node, linking it to the id of the field. (See example below).
hasFlatData boolean true Specify whether the data passed to the tree is flat or already hierarchical
hasPan boolean false Toggle the ability to pan the tree
hasZoom boolean false Toggle the ability to zoom the tree
nodeWidth number 160 Width of a node in px
nodeHeight number 100 Height of a node in px
linkColor function (nodeData) => "#ffcc80" Color of the link
linkWidth function (nodeData) => 10 Width of the link
linkShape "quadraticBeziers" | "orthogonal" | "curve" "quadraticBeziers" Shape of the link
renderNode function (nodeData) => null HTML template for every node
isHorizontal boolean true Direction of the tree. If true, the tree expands from left to right. If false, it goes from top to bottom
onNodeClick function (nodeData) => null Function handling the event when someone click on it
onNodeMouseEnter function (nodeData) => null Function handling the event when someone hover a node
onNodeMouseLeave function (nodeData) => null Function handling the event when the mouse pointer leaves a node
mainAxisNodeSpacing number or "auto" 300 Set the distance in pixels between two depths in the tree. If the value is auto it will automatically display the tree to fit the size of the container.
secondaryAxisNodeSpacing number 1.25 Set the distance between nodes in the same level as a coefficient of node dimensions. Recommended to have the value superior to 1
marginTop number 1.25 Set the margin between the SVG element and the tree
marginBottom number 1.25 Set the margin between the SVG element and the tree
marginLeft number 1.25 Set the margin between the SVG element and the tree
marginRight number 1.25 Set the margin between the SVG element and the tree
duration number 600 The duration of the animation transition between layouts

Credits

This module is based on d3 library, credit to all the contributors of this project.

License

BSD

treeviz's People

Contributors

pierrecapo avatar andrew9994 avatar pronink avatar stevegichure avatar

Watchers

 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.