• Stars
    star
    516
  • Rank 85,726 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A library for visualizing data trees with multiple parents, such as family trees. Built on top of D3.

dTree

A library for visualizing data trees with multiple parents built on top of D3.

npm Bower

Using dTree? Send me a message with a link to your website to be listed below.

The Online Viewer

There exists an online viewer for dTree graphs called Treehouse, similar to https://bl.ocks.org/ for D3. Treehouse allows anybody to host a dTree graph without having to create a website or interact directly with the library. It fetches data from Github's gists and displays it in a nice format. All graphs are unlisted so without your Gist ID nobody else can view them. Checkout the demo graph for dTree:

https://treehouse.gartner.io/ErikGartner/58e58be650453b6d49d7

The same demo is also available on JSFiddle.

Installation

There are several ways to use dTree. One way is to simply include the compiled file dTree.js that then exposes a dTree variable. dTree is available on both NPM and Bower as d3-dtree.

npm install d3-dtree
bower install d3-dtree
yarn add d3-dtree

Lastly dTree is also available through several CDNs such as jsDelivr:

https://cdn.jsdelivr.net/npm/[email protected]/dist/dTree.min.js

Requirements

To use the library the follow dependencies must be loaded:

Usage

To create a graph from data use the following command:

tree = dTree.init(data, options);

The data object should have the following structure:

[{
  name: "Father",                         // The name of the node
  class: "node",                          // The CSS class of the node
  textClass: "nodeText",                  // The CSS class of the text in the node
  depthOffset: 1,                         // Generational height offset
  marriages: [{                           // Marriages is a list of nodes
    spouse: {                             // Each marriage has one spouse
      name: "Mother",
    },
    children: [{                          // List of children nodes
      name: "Child",
    }]
  }],
  extra: {}                               // Custom data passed to renderers
}]

The following CSS sets some good defaults:

.linage {
    fill: none;
    stroke: black;
}
.marriage {
    fill: none;
    stroke: black;
}
.node {
    background-color: lightblue;
    border-style: solid;
    border-width: 1px;
}
.nodeText{
    font: 10px sans-serif;
}
.marriageNode {
    background-color: black;
    border-radius: 50%;
}

The options object has the following default values:

{
  target: '#graph',
  debug: false,
  width: 600,
  height: 600,
  hideMarriageNodes: true,
  marriageNodeSize: 10,
  callbacks: {
    /*
      Callbacks should only be overwritten on a need to basis.
      See the section about callbacks below.
    */
  },
  margin: {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0
  },
  nodeWidth: 100,
  styles: {
    node: 'node',
    linage: 'linage',
    marriage: 'marriage',
    text: 'nodeText'
  }
}

Zooming

The returned object, tree = dTree.init(data, options), contains functions to control the viewport.

  • tree.resetZoom(duration = 500) - Reset zoom and position to initial state
  • zoomTo(x, y, zoom = 1, duration = 500) - Zoom to a specific position
  • zoomToNode(nodeId, zoom = 2, duration = 500) - Zoom to a specific node
  • zoomToFit(duration = 500) - Zoom to fit the entire tree into the viewport

Callbacks

Below follows a short descriptions of the available callback functions that may be passed to dTree. See dtree.js for the default implementations. Information about e.g. mouse cursor position can retrieved by interacting with the this object, i.e. d3.mouse(this).

nodeClick

function(name, extra, id)

The nodeClick function is called by dTree when the node or text is clicked by the user. It shouldn't return any value.

nodeRightClick

function(name, extra, id)

The nodeRightClick function is called by dTree when the node or text is right-clicked by the user. It shouldn't return any value.

nodeRenderer

function(name, x, y, height, width, extra, id, nodeClass, textClass, textRenderer)

The nodeRenderer is called once for each node and is expected to return a string containing the node. By default the node is rendered using a div containing the text returned from the default textRendeder. See the JSFiddle above for an example on how to set the callback.

nodeHeightSeperation

function(nodeWidth, nodeMaxHeight)

The nodeHeightSeperation is called during intial layout calculation. It shall return one number representing the distance between the levels in the graph.

nodeSize

function(nodes, width, textRenderer)

This nodeSize function takes all nodes and a preferred width set by the user. It is then expected to return an array containing the width and height for all nodes (they all share the same width and height during layout though nodes may be rendered as smaller by the nodeRenderer).

nodeSorter

function(aName, aExtra, bName, bExtra)

The nodeSorterer takes two nodes names and extra data, it then expected to return -1, 0 or 1 depending if A is less, equal or greater than B. This is used for sorting the nodes in the tree during layout.

textRenderer

function(name, extra, textClass)

The textRenderer function returns the formatted text to the nodeRenderer. This way the user may chose to overwrite only what text is shown but may opt to keep the default nodeRenderer.

marriageClick

function(extra, id)

Same as nodeClick but for the marriage nodes (connector).

marriageRightClick

function(extra, id)

Same as nodeRightClick but for the marriage nodes (connector).

marriageRenderer

function(x, y, height, width, extra, id, nodeClass)

Same as nodeRenderer but for the marriage nodes (connector).

marriageSize

function(nodes, size)

Same as nodeSize but for the marriage nodes (connector).

Development

dTree has the following development environment:

  • node v11.x (use Docker image node:11)
  • gulp 3.x
  • Yarn instead of npm.

To setup and build the library from scratch follow these steps:

  1. yarn install
  2. yarn run build

A demo is available by running:

yarn run demo

It hosts a demo on localhost:3000/ by serving test/demo and using the latest compiled local version of the library.

Contributing

Contributions are very welcomed! Checkout the CONTRIBUTING document for style information. A good place to start is to make a pull request to solve an open issue. Feel free to ask questions regarding the issue since most have a sparse description.

License

The MIT License (MIT)

Copyright (c) 2015-2020 Erik Gärtner

More Repositories

1

sentimental

Sentiment analysis made easy; built on top off solid libraries.
Python
24
star
2

actor

Official implementation of the NeurIPS 2019 paper Domes to Drones: Self-Supervised Active Triangulation for 3D Human Pose Reconstruction
MATLAB
11
star
3

scene-memory-transformer

My implementation of a scene memory transformer module for reinforcement learning
Python
10
star
4

hyperdock

A distributed hyperparameter optimizer for machine learning that lives in Docker
Python
8
star
5

wasp-klt-tracker

The KTL tracker implemented for the WASP course
Python
7
star
6

stock-collector

This small application downloads all available real time data for a set of instruments and stores it in a Mongo database
Python
5
star
7

plectron

Plex in an Electron shell
JavaScript
5
star
8

xeed

A program for organising roleplaying settings and their characters, groups and relationships.
Java
5
star
9

easy-query-dsl

A user-friendly string based query format for Mongo
JavaScript
5
star
10

stocky

Stock prediction using JavaML and WEKA.
Java
4
star
11

ghostdoc

A smart text viewer that semi-automatically identifies, highlights and summarizes based on artifacts.
JavaScript
4
star
12

crazyflie-trajectory

Trajectory generation for the Crazyflie
Python
3
star
13

custom-linux-alloc

Custom implementations of malloc, calloc, free and realloc.
C
3
star
14

relationship-extractor

A NLP proof-of-concept for doing automatic relationship extraction from texts.
Scala
3
star
15

treehouse

The beautiful open viewer of dTree graphs
HTML
2
star
16

wasp-cifar10

Cifar10 for the WASP course
Python
2
star
17

stock-data-visualizer

A graphical interface to the stock data collector
JavaScript
2
star
18

docker-deeplearning

Docker images for deep learning
2
star
19

hashcode

Google Hash Code repository
Python
1
star
20

dokku-backup

Script that backups my Dokku databases to Dropbox.
Shell
1
star
21

entropy-game-engine

This is an exploratory attempt at constructing an Interactive Story engine
Scala
1
star
22

franz-microsoft-todo

A plugin for Microsoft To-Do for Franz
JavaScript
1
star
23

ghostdoc-ritter

Ritter is the new data processing engine for Ghostdoc.
Python
1
star