3.0.1 • Published 5 months ago

treeviz v3.0.1

Weekly downloads
74
License
MIT
Repository
github
Last release
5 months ago

Treeviz

Known Vulnerabilities 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.

npm.io

Installation

With npm :

npm install treeviz

and then you can use it with :

import {Treeviz} from 'treeviz';

Or download this zip repository in the Github Release section and link the dist/treeviz.js file in your page directly : <script src="./dist/index.js><script>

Usage

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

KeyTypeDefaultDefinition
htmlIdstring (Required)The HTML id tag on the page where the tree should be drawn. It must have a width and an height specified
idKeystring"id"The key in a data item representing the unique identifier of a node
relationnalFieldstring"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).
hasFlatDatabooleantrueSpecify whether the data passed to the tree is flat or already hierarchical
hasPanbooleanfalseToggle the ability to pan the tree
hasZoombooleanfalseToggle the ability to zoom the tree
nodeWidthnumber160Width of a node in px
nodeHeightnumber100Height of a node in px
linkColorfunction(node: NodeData) => "#ffcc80"Color of the link
linkWidthfunction(node: NodeData) => 10Width of the link
linkShape"quadraticBeziers" | "orthogonal" | "curve""quadraticBeziers"Shape of the link
renderNodefunction(node: NodeData) => nullHTML template for every node
isHorizontalbooleantrueDirection of the tree. If true, the tree expands from left to right. If false, it goes from top to bottom
onNodeClickfunction(node: NodeData) => nullFunction handling the event when someone click on it
onNodeMouseEnterfunction(node: NodeData) => nullFunction handling the event when someone hover a node
onNodeMouseLeavefunction(node: NodeData) => nullFunction handling the event when the mouse pointer leaves a node
mainAxisNodeSpacingnumber or "auto"300Set 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.
secondaryAxisNodeSpacingnumber1.25Set the distance between nodes in the same level as a coefficient of node dimensions. Recommended to have the value superior to 1
marginTopnumber1.25Set the margin between the SVG element and the tree
marginBottomnumber1.25Set the margin between the SVG element and the tree
marginLeftnumber1.25Set the margin between the SVG element and the tree
marginRightnumber1.25Set the margin between the SVG element and the tree
durationnumber600The duration of the animation transition between layouts
dataanyNeeded for Typescript projects only to type the NodeData argument

And then, we have the NodeData type that is passed as callback of some functions: type NodeData { data: // the data of each item settings: // the settings object }

Contributing

  • Clone the repo.
  • Run npm install.
  • Run npm run dev, then you can edit the files in the ./src folder and the ./example/index.html file.
  • To publish (admin rights), run npm run build && npm publish.

Credits

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

License

MIT

3.0.0-alpha.1

5 months ago

3.0.0-alpha.0

5 months ago

3.0.0-alpha.3

5 months ago

3.0.0-alpha.2

5 months ago

3.0.0-alpha.4

5 months ago

3.0.1

5 months ago

3.0.0

5 months ago

2.3.0

3 years ago

2.3.0-beta.2

3 years ago

2.3.0-beta.1

3 years ago

2.3.0-beta.0

3 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago