layout.hpp

Tools for laying out nodes in D3.

namespace D3
class Layout : public D3::D3_Base
#include <layout.hpp>

Subclassed by D3::TreeLayout< TreeNode >, D3::TreeLayout< NODE_TYPE >

Protected Functions

inline Layout(int id)
inline Layout()
struct JSONTreeNode
#include <layout.hpp>
template<typename NODE_TYPE = JSONTreeNode>
class TreeLayout : public D3::Layout
#include <layout.hpp>

A TreeLayout can be used to visualize hierarchical data as a tree (a series of edges connecting parent and child nodes).

Since hierarchical data is much more pleasant to store in JSON format than anything C++ can offer, the TreeLayout expects your data to be stored in a D3::JSONDataset

. Each node is expected to have, at a minimum, the following values:

Calculating the tree layout will automatically create three additional values for each node:

You can include any additional parameters that you want to use to store data. The dataset is expected to be an array containing one element: the root node object, which in turn has the other nodes nested inside it. You must provide a dataset to the TreeLayout constructor.

A TreeLayout must be templated off of a type that describes all of the values that a node contains (or at least the ones you care about using from C++, as well as x and y). This allows nodes to be passed back up to C++ without C++ throwing a fit about types. If you don’t need access to any data other than name, parent, x, y, and depth from C++, you can use the default, JSONTreeNode.

If you need access to additional data, you can build structs to template TreeLayouts off with Empirical Introspective Tuple Structs (see JSONTreeNode as an example). Don’t be scared by the complicated sounding name - you just need to list the names and types of values you care about.

Param name:

- a name that uniquely identifies a single node

Param parent:

- the name of this node’s parent (each node is expected to have exactly one parent, unless it is the root, in which case the parent should be “null”)

Param children:

- an array containing all of the node’s children (yes, the nesting gets intense).

Param x:

- the x coordinate of the node

Param y:

- the y coordinate

Param depth:

- the depth of the node in the tree

Public Functions

inline TreeLayout(JSONDataset *dataset)

Constructor - handles creating a default DiagonalGenerator and links the specified dataset up to this object’s data pointer.

inline TreeLayout()

Default constructor - if you use this you need connect a dataset with SetDataset.

inline void SetDataset(JSONDataset *dataset)

Change this TreeLayout’s data to [dataset].

inline array<Selection, 4> GenerateNodesAndLinks(Selection svg)

This function does the heavy lifting of visualizing your data. It generates nodes and links between them based on this object’s dataset. [svg] must be a selection containing a single svg element on which to draw the the visualization.

In case you want to further customize the tree, this method returns an array of selections, containing: the enter selection for nodes (i.e. a selection containing all nodes that were just added to the tree), the exit selection for nodes (i.e. a selection containing any nodes that are currently drawn but are no longer in the dataset), the enter selection for links, and the exit selection for links.

inline void SetSize(int w, int h)

Set the width of the tree area to [w] and the height to [h].

Public Members

JSONDataset *data

Pointer to the data - must be in hierarchical JSON format.

D3::LinkGenerator *make_line

Function used to make the lines for the edges in the tree.