Giter Club home page Giter Club logo

elm-tree-diagram's Introduction

elm-tree-diagram

Note: Fork Differences

This is a fork of https://github.com/brenden/elm-tree-diagram

It does not include the original TreeDiagram.Canvas module as I wasn't using it and it relied on the outdated evancz/elm-graphics library.

This is an Elm package for drawing diagrams of trees. For positioning the trees it uses the approach described in Tidier Drawings of Trees.

Usage

Here's the tree data structure we want to draw:

coolTree =
    node
        1
        [ node 2 []
        , node 3 []
        , node
            4
            [ node 5 []
            , node 6 []
            , node 7 []
            ]
        ]

We first define a function for drawing the tree nodes, which in this case each contain an integer. We'll display each node as white text on a black circle.

drawNode n =
    group
        [ circle 16 |> filled black
        , toString n |> fromString |> Text.color white |> text
        ]

Then we define a function for drawing the edges between nodes.

drawEdge to =
    segment (0, 0) to |> traced (solid black)

Finally we call draw with our tree, node drawer, and edge drawer:

main =
    toHtml <|
        draw defaultTreeLayout drawNode drawEdge coolTree

This should produce the diagram below.

Output of usage example

The argument defaultTreeLayout contains some options for configuring the layout of the tree. See the API section below for more details.

Examples

API

TreeDiagram.Canvas

draw : TreeLayout -> NodeDrawer a Form -> EdgeDrawer Form -> Tree a -> Element

Draws the tree as a canvas image using the provided functions for drawings nodes and edges.

TreeDiagram.Svg

draw : TreeLayout -> NodeDrawer a (Svg msg) -> EdgeDrawer (Svg msg) -> Tree a -> Html msg

Draws the tree as an SVG using the provided functions for drawings nodes and edges.

TreeDiagram

node : a -> List (Tree a) -> Tree a

Creates a tree of type a from a node value and a list of subtrees.


type alias NodeDrawer a format = a -> format

Alias for functions that draw nodes of type a in the given format (e.g. Svg msg or Form).


type alias EdgeDrawer format = Coord -> format

Alias for functions that draw edges between nodes in the given format.


type alias TreeLayout =
    { orientation : TreeOrientation
    , levelHeight : Int
    , subtreeDistance : Int
    , siblingDistance : Int
    , padding : Int
    }

Options for laying out the tree:

  • orientation: direction of the tree from root to leaves.
  • levelHeight: vertical distance between parent and child nodes.
  • subtreeDistance: horizontal distance between subtrees.
  • siblingDistance: horizontal distance between siblings. This is usually set below subtreeDistance to produce a clearer distinction between sibling nodes and non-siblings on the same level of the tree.
  • padding: amount of space to leave around the edges of the diagram.

leftToRight : TreeOrientation
rightToLeft : TreeOrientation
topToBottom : TreeOrientation
bottomToTop : TreeOrientation

Possible orientations of the tree from root to leaves.


defaultTreeLayout =
    { orientation = topToBottom
    , levelHeight = 100
    , siblingDistance = 50
    , subtreeDistance = 80
    , padding = 40
    }

A set of default values that should be modified to create your TreeLayout.

Future work

  • Add support for drawing trees with labeled edges
  • Add support for trees with varied node sizes
  • Implement some more algorithms for laying out the tree

elm-tree-diagram's People

Contributors

adrianroe avatar brenden avatar jean-lopes avatar lorenzo 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.