GithubHelp home page GithubHelp logo

schnurber / treevis Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 2.0 913 KB

A tree visualisation library for p5.js

License: Apache License 2.0

JavaScript 92.23% HTML 5.15% Python 2.61%
treemap sunburst sunburst-chart sunburst-visualisation information-visualization treeview tree-structure visualization visual-analytics visual-analysis

treevis's Introduction

p5.treevis

p5.treevis

With this library, one can create and customize a treemap or a sunburst visualisation from any json object tree.

The json object tree should be an object with the following structure:

<Node> ::= 
{
   <Name> : string,
   <Value> : number,
   <Children> : [ <Node>, ... ]
}

For leaf nodes, <Children> is optional. For parent nodes, <Name> and <Value> are optional.

Example:

{
   "children":[
      {
         "name":"a",
         "size":1
      },
      {
         "children":[
            {
               "name":"b",
               "size":2
            },
            {
               "name":"c",
               "size":3
            }
         ]
      }
   ]
}

The property names <Name>, <Value> and <Children> must be specified in a parameter-object (see example code below).

If you want, you can generate a json file containing a json tree out from any folder at your disk. Just scan it with my python script (use scandir.py in the scanner folder)

Following command scans project directory ../ and write into file

python scandir.py ../ > ../examples/data/test.json

Live examples

Installation

Just include the scripts.

<script src="addons/p5.dom.min.js"></script>
<script src="addons/p5.treevis.js"></script>

Basic usage

This is a complete example for displaying and drawing with interaction Please look in the source code of examples to see how to customize. Or look in the well documented source files in the src folder.

var treevis, data;

function preload() {
  // Loads json tree
  data = loadJSON("data/example_scan.json");
}

function setup() {
  createCanvas(800, 600);
  // Description of json properties
  // 'children' is an array with child objects
  // 'name' is identifier
  // 'size' is content value
  var properties = {
      children: "children",
      label: "name",
      value: "size"
  };
  // creates a new Sunburst object
  treevis = createSunburst(data, properties);
  //callback function
  treevis.onSelected((v, name) => console.log("Selected: "+name));
}

function draw() {
  background(255);
  // draws sunburst
  treevis.draw();
}

function mouseClicked() {
  if (mouseButton == RIGHT) {
    // navigate out
    treevis.up();
  } else {
    // navigate in
    treevis.select(mouseX, mouseY);
  }
}

p5.treevis documentation

createTreemap()

treemap = createTreemap(json_data, props)

Creates a new treemap visualisation

Rounds corners

treemap.setCorner(5);

Makes inset

treemap.setInset(5);

createSunburst()

sunburst = createSunburst(json_data, props)

Creates a new sunburst visualisation

Sets a different angle and draws a full circle

sunburst.setAngle(90, 360);

Sets text invisible

sunburst.setTextVisible(false);

This works on both treemaps and sundbursts:

Callback function

treevis.onSelected((v, name) => console.log("Selected: "+name));

Sets size and position

treevis.setBounds(100,100,400,400);

Customize fill

treevis.onFill((level, maxLevel) => fill(color(237, (255 - level / maxLevel * 255) * 2 / 3, 255)));

Navigates in when mouse is pressed

function mousePressed() {
    treevis.select(mouseX, mouseY);
}

Navigates out when mouse is pressed

function mouseClicked() {
  treevis.up(mouseX, mouseY);
}

Navigates up without test if pressed mouse position is inside visualisation

treevis.up();

Custom function for building label out of name property

treevis.onGetLabel(name => name.substring(name.lastIndexOf("-") + 1));

Interaction off, no navigation in or out

treevis.setInteraction(false);

Custom font size

treevis.setTextStyle(14);

Custom font size and type

treevis.setTextStyle(14, 'Times');

treevis's People

Contributors

schnurber avatar

Stargazers

 avatar  avatar  avatar  avatar  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.