npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

trees.js

v1.1.9

Published

A tree diagramming library. This library uses SVG and the built in events system.

Downloads

40

Readme

trees.js

A somewhat optimised interactive tree diagramming library. This library uses SVG and the built in events system at this stage.

Installing

To install the package, simply run the following command:

npm install trees.js

Version

1.1.9

  • Added setText function
  • Fixed deselect function and added node parameter.
  • Added removeEventListeners (Only removes global listeners for now, such as on window)
  • Added setDeselected
  • Reduced number of event listeners.
  • Added new setColor function.
  • Fixed zooming for an offset element.
  • Zooming no longer allows document scrolling.
  • SVG width and height values now accept all units, not just pixels. (This includes percentage based width/height).
  • Window scaling issue fixed.
  • require now exports the trees object.
  • More options added to drawTree function.
  • Scaling in desktop and mobile added.
  • removeNode function added.
  • Renamed SVG pointers that are attached to nodes.

Using the library

Once you have imported the .js file from the dist folder, a tree object will be brought into the global space. It can also be accessed using a require.

Once you have this, create an svg object by doing the following:

var svg = new trees.SVG('mySVG', { width: 1920, height: 980 });

Where 'mySVG' is the id of the svg element on the dom.

Now once you have a tree of the following form...

var root = { contents: "Hello", children: [
				{ contents: "Whoa", children: [
					{ contents: "Um", children: [] },
					{ contents: "Dude", children: [] },
					{ contents: "Breadth-First", children: [] }
				] },
				{ contents: "Another", children: [
					{ contents: "Um", children: [] }
				] }
			] };

... you can draw the tree to the svg by using the following function:

svg.drawTree(root, { lineType: 'bezier' });

Options

These are the options and their defaults.

SVG Instantiation:

var svg = new trees.SVG(id, options)

//options:
clear: false         // Clears the previous tree if true.
height: '200px'      // Sets the height of the svg.
width: '200px'       // Sets the width of the svg.

SVG Draw:

svg.drawTree(root, options)

//options:
anchor: 'none'            // Sets which nodes move with the current node. Options are 'children', 'descendents', 'none'.
cornerRadius: 2           // Sets the corner radius of the nodes in the given tree.
fill: '#BBDDFF'           // The fill color of a regular node.
lineStroke: stroke        // The edge/line stroke colour.
lineType: 'line'          // The type of edge/line. Options are 'bezier', 'line'.
rootFill: '#FF6666'       // The fill color of the root node.
rootStroke: '#DD2222'     // The stroke color of the root node.
selectedFill: '#33DD33'   // The fill color of the selected node.
selectedStroke: '#11BB11' // The stroke color of the selected node.
stroke: '#6688BB'         // The stroke color of a regular node.

Functions

Set anchor:

svg.setAnchor(anchor);

Set the selected node's action, where the signature of func is func(node):

svg.setSelectedAction(func);

Set the deselection action, when a node is deselected, where the signature of func is func(node):

svg.setDeselectedAction(func);

Remove global event listeners

svg.removeEventListeners();

Remove a node from the SVG, and the Tree data structure. The default of maintainChildren is true, if false, it will delete a node and all of its children.

svg.removeNode(node, maintainChildren); // Returns the node if deleted.

Set color of a given node:

svg.setColor(node, options);

//options
fill: tree default   // fill color as a string.
stroke: tree default // stroke color as a string.

Set text of a given node, changes the contents of the node:

svg.setText(node, text);

Variables

Get the currently selected node:

svg.selectedNode