Tree diagrams are special kinds of graphs comprised of nodes and edges which connect those nodes.
KGJS creates trees using an axisless graph whose dimensions are fixed at $24 \times 24$ since that’s easily divisible by many numbers, and can therefore accommodate a range of layouts. For convenience, while you’re authoring, a grid showing the coordinate system is available to you; just put showGrid: true
under tree
and it will display the following grid:
In the examples below, we’ve included line showGrid: false
; if you want to see the grid, just change that to showGrid: true
.
A tree
object is just like a graph
object, and in fact they are identical. However, a tree
must have a child called nodes
, and may have a child called edges
.
A node is a special kind of Point. Because it generates a Point
object, you can do everything you can with a Point
: for example, give it a label, a color, a radius, etc:
So far, there’s nothing that a node can do that a Point
can’t. However, we can name nodes and connect them with edges:
Now in fact, an edge is just a Segment that takes its coordinates from the referenced nodes. So you can use any formatting you would for a segment, including adding an arrow or trimming it:
A directed graph is a kind of tree which begins with an initial node, which has various children. Game trees in economics are directed graphs.
Because they are so useful, there is built-in support for directed graphs: for any node, you may specify its chidren
, which is a list of nodes. The code will then connect each node with its children by auto-generating edges. You can label the edges using the attribute edgeLabel
: