Giter Club home page Giter Club logo

graphvix's Introduction

Graphvix

Build Status

Graphviz in Elixir

Installation

If available in Hex, the package can be installed as:

  1. Add graphvix to your list of dependencies in mix.exs:
def deps do
  [{:graphvix, "~> 1.0.0"}]
end

Usage

See the wiki for examples.

API Overview

  • Create a new graph

    Graphvix.Graph.new/0

  • Add a vertex to a graph

    Graphvix.Graph.add_vertex/2 Graphvix.Graph.add_vertex/3

  • Add an edge between two vertices

    Graphvix.Graph.add_edge/3 Graphvix.Graph.add_edge/4

  • Create a vertex with type record

    Graphvix.Record.new/1 Graphvix.Record.new/2

  • Add a record vertex to a graph

    Graphvix.Graph.add_record/2

  • Create a vertex using HTML table markup

    Graphvix.HTMLRecord.new/1 Graphvix.HTMLRecord.new/2

  • Add an HTML table vertex to a graph

    Graphvix.Graph.add_html_record/2

  • Save a graph to disk in .dot format

    Graphvix.Graph.write/2

  • Save and compile a graph (defaults to .png)

    Graphvix.Graph.compile/2 Graphvix.Graph.compile/3

  • Save, compile and open a graph (defaults to .png and your OS's default image viewer)

    Graphvix.Graph.graph/2 Graphvix.Graph.graph/3

Basic Usage

  1. Alias the necessary module for ease of use

    alias Graphvix.Graph
  2. Create a new graph.

    graph = Graph.new()
  3. Add a simple vertex with a label

    {graph, vertex_id} = Graph.add_vertex(graph, "vertex label")
  4. Add a vertex with a label and attributes

    {graph, vertex2_id} = Graph.add_vertex(
      graph,
      "my other vertex",
      color: "blue", shape: "diamond"
    )
  5. Add an edge between two existing vertices

    {graph, edge_id} = Graph.add_edge(
      graph,
      vertex_id, vertex2_id,
      label: "Edge", color: "green"
    )
  6. Add a cluster containing one or more nodes

    {graph, cluster_id} = Graph.add_cluster(graph, [vertex_id, vertex2_id])

Records

  1. Alias the necessary module for ease of use

    alias Graphvix.Record
  2. Create a simple record that contains only a row of cells

    record = Record.new(Record.row(["a", "b", "c"]))
    • A record with a top-level row can also be created by just passing a list

      record = Record.new(["a", "b", "c"])
  3. Create a record with a single column of cells

    record = Record.new(Record.column(["a", "b", "c"]))
  4. Create a record with nested rows and columns

    import Graphvix.Record, only: [column: 1, row: 1]
    
    record = Record.new(row([
      "a",
      column([
        "b",
        row(["c", "d", "e"]),
        "f"
      ]),
      "g"
    ])
    

Ports

  1. Ports can be attached to record cells by passing a tuple of {port_name, label}

    import Graphvix.Record, only: [column: 1, row: 1]
    
    record = Record.new(row([
      {"port_a", "a"},
      column([
        "b",
        row(["c", {"port_d", "d"}, "e"]),
        "f"
      ]),
      "g"
    ])
    
  2. Edges can be drawn from specific ports on a record

    {graph, record_id} = Graph.add_record(graph, record)
    
    {graph, _edge_id} = Graph.add_edge({record_id, "port_a"}, vertex_id)

HTML Table Records

  1. Alias the necessary modules for ease of use

    alias Graphvix.HTMLRecord
  2. Create a simple table

    record = HTMLRecord.new([
      tr([
        td("a"),
        td("b"),
        td("c")
      ]),
      tr([
        td("d", port: "port_d"),
        td("e"),
        td("f")
      ])
    ])
  3. Or a more complex table

    record = HTMLRecord.new([
      tr([
        td("a", rowspan: 3),
        td("b", colspan: 2),
        td("f", rowspan: 3)
      ]),
      tr([
        td("c"),
        td("d")
      ])
      tr([
        td("e", colspan: 2)
      ])
    ])

Cells can also use the font/2 and br/0 helper methods to add font styling and forced line breaks. See the documentation for Graphvix.HTMLRecord for examples.

Output

  1. Convert the graph to DOT format

    Graph.to_dot(graph)
    """
    digraph G {
      cluster c0 {
        v0 [label="vertex label"]
        v1 [label="my other vertex",color="blue",shape="diamond"]
    
        v0 -> v1 [label="Edge",color="green"]
      }
    }
    """
  2. Save the graph to a .dot file, with an optional filename

    Graph.write(graph, "first_graph") #=> creates "first_graph.dot"
  3. Compile the graph to a .png or .pdf using the dot command

    ## creates first_graph.dot and first_graph.png
    Graph.compile(graph, "first_graph")
    
    ## creates first_graph.dot and first_graph.pdf
    Graph.compile(graph, "first_graph", :pdf)
  4. Compile the graph using the dot command and open the resulting file

    ## creates first_graph.dot and first_graph.pdf; opens first_graph.png
    Graph.graph(graph, "first_graph")
    
    ## creates first_graph.dot and first_graph.pdf; opens first_graph.pdf
    Graph.graph(graph, "first_graph", :pdf)

graphvix's People

Contributors

mikowitz avatar ivan-kolmychek avatar brewerx avatar

Stargazers

Ricardo Carvalho Santos avatar  avatar Sam Brotherton avatar  avatar Oleg Sovetnik avatar Petrus Janse van Rensburg avatar laserx avatar Humberto Aquino avatar Austin Ziegler avatar Noah Betzen avatar Michael Geiger avatar Mark Holmberg avatar Robin Hilliard avatar Aslak Johansen avatar Andrejs Agejevs avatar Patrick Smith avatar Yusuke Saito avatar Will Ricketts avatar Sam Gaw avatar hazn avatar Nikita Tsiganov avatar Carlo Gilmar avatar Leandro Pereira avatar Jason Axelson avatar alvinary avatar Cody avatar Zach Norris avatar Craig Tataryn avatar Michael Arciola avatar Benjamin Scholtz avatar Anderson Bravalheri avatar  avatar Rafael T. Ballestiero avatar Miroslav Malkin avatar Atul Bhosale avatar 李大狗 avatar Dawei Ma avatar Phillipp Ohlandt avatar Безбородов, Д.Р. avatar  avatar Andrey Pavlov avatar merik avatar Lorenzo Sinisi avatar Sean Hagstrom avatar  avatar Valentin Mihov avatar  avatar Leonel Mendez Jimenez avatar Dmitry Ledentsov avatar Adam Skołuda avatar Adam Oswalt avatar Everton Ribeiro avatar Eugene Oskin avatar Dave Lucia avatar Eduardo Aguilera Olascoaga avatar Florian Odronitz avatar David Enochs avatar Jesse J. Anderson avatar Josh Austin avatar Richard Palethorpe avatar

Watchers

 avatar  avatar James Cloos avatar  avatar

graphvix's Issues

Importing an existing DOT graph

I noticed a function to export a graph to a DOT file, but I was wondering if there was a way to import a DOT graph/file.

Appreciate any advice on this!

Throws error if quotes exist in labels

To recreate:

alias Graphvix.{Graph, Node}
Graph.new(:test)
Node.new(label: "\"test\"")
Graph.save

Will generate a dot file like this:

digraph G {
  node_1 [label=""test""];
}

The problem is that first it writes it like:
"digraph G {\n node_1 [label=\"\"test\"\"];\n}"
but then it escapes the quotes, when they should be kept unescaped (I hope that makes sense)

Graph.clear doesn't resets ids

After creating 1..n nodes, clearing a graph and making a new one with new nodes, the node ids aren't reseted.

Replicate

alias Graphvix.{Graph, Node, Edge}

Graph.new(:test)
Node.new(label: "1")
Node.new(label: "2")
Node.new(label: "3")
Graph.get

iex> ...nodes: %{
    1 => %{attrs: [label: "1"]},
    2 => %{attrs: [label: "2"]},
    3 => %{attrs: [label: "3"]}
  }...

Graph.clear
Graph.new(:test2)
Node.new(label: "1")
iex> {4, %{attrs: [label: "2"]}}

Choose file path to save graphs

I have a use case where I need to save the graph files (dot, png, svg, etc...) inside a specific folder.

I suggest using Application.get_env so the user can add the path through Mix.Config. Maybe place this inside the Writer.save function.

I thought inside each GenServer call, but I like how the name is handled there.

I can work on this if you have no problem, just let me know and I can get it in a couple of days.

Cheers and awesome library, you just made my life easier 👍

stream_data dependency is missing

Right now if you try to use graphvix, you have to also include the stream_data dependency in your project.

Steps to reproduce:

Create a new mix project

mix new test_graphvix
cd test_graphvix

Add graphvix as a dependency

defp deps do
  [{:graphvix, "~> 1.0"}]
end

Fetch deps

mix deps.get

Try to start application

iex -S mix

Expected: The application would start and I could play with graphvix
Actual:

** (Mix) Could not start application stream_data: could not find application file: stream_data.app

It looks like stream_data is trying to be started in the list of applications. If stream_data isn't needed, it should be removed from there.

If stream_data is required then it shouldn't be set to only: [:dev, :test] in the mix.exs

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.