# Ruby/GraphViz
[![All Contributors](https://img.shields.io/badge/all_contributors-31-orange.svg)](#contributors)
[![Travis CI build](https://secure.travis-ci.org/glejeune/Ruby-Graphviz.svg)](https://travis-ci.org/glejeune/Ruby-Graphviz)
[![Gem Version](https://badge.fury.io/rb/ruby-graphviz.svg)](https://rubygems.org/gems/ruby-graphviz)

Copyright (C) 2004-2018 Gregoire Lejeune

* Doc : http://rdoc.info/projects/glejeune/Ruby-Graphviz
* Sources : https://github.com/glejeune/Ruby-Graphviz
* On Rubygems: https://rubygems.org/gems/ruby-graphviz

## INSTALLATION

Add this line to your application's Gemfile:

	gem 'ruby-graphviz'

And then execute:

	$ bundle

Or install it yourself as:

	$ gem install ruby-graphviz

## DESCRIPTION

Interface to the GraphViz graphing tool

## TODO

* New FamilyTree


## SYNOPSIS

A basic example

```ruby
require 'ruby-graphviz'

# Create a new graph
g = GraphViz.new( :G, :type => :digraph )

# Create two nodes
hello = g.add_nodes( "Hello" )
world = g.add_nodes( "World" )

# Create an edge between the two nodes
g.add_edges( hello, world )

# Generate output image
g.output( :png => "hello_world.png" )
```

The same but with a block

```ruby
require 'ruby-graphviz'

GraphViz::new( :G, :type => :digraph ) { |g|
  g.world( :label => "World" ) << g.hello( :label => "Hello" )
}.output( :png => "hello_world.png" )
```

Or with the DSL

```ruby
    require 'ruby-graphviz/dsl'
    digraph :G do
      world[:label => "World"] << hello[:label => "Hello"]

      output :png => "hello_world.png"
    end
```

Create a graph from a file 

```ruby
    require 'ruby-graphviz'

    # In this example, hello.dot is :
    #   digraph G {Hello->World;}

    GraphViz.parse( "hello.dot", :path => "/usr/local/bin" ) { |g|
      g.get_node("Hello") { |n|
        n[:label] = "Bonjour"
      }
      g.get_node("World") { |n|
        n[:label] = "Le Monde"
      }
    }.output(:png => "sample.png")
```

[GraphML](http://graphml.graphdrawing.org/) support

```ruby
    require 'ruby-graphviz/graphml'

    g = GraphViz::GraphML.new( "graphml/cluster.graphml" )
    g.graph.output( :path => "/usr/local/bin", :png => "#{$0}.png" )
```

## TOOLS

Ruby/GraphViz also includes :

* `ruby2gv`, a simple tool that allows you to create a dependency graph from a ruby script. Example : http://drp.ly/dShaZ

```ruby
ruby2gv -Tpng -oruby2gv.png ruby2gv
```

* `gem2gv`, a tool that allows you to create a dependency graph between gems. Example : http://drp.ly/dSj9Y

```ruby
gem2gv -Tpng -oruby-graphviz.png ruby-graphviz
```

* `dot2ruby`, a tool that allows you to create a ruby script from a graphviz script

```ruby
$ cat hello.dot
digraph G {Hello->World;}

$ dot2ruby hello.dot
# This code was generated by dot2ruby.g

require 'rubygems'
require 'ruby-graphviz'
graph_g = GraphViz.digraph( "G" ) { |graph_g|
  graph_g[:bb] = '0,0,70,108'
  node_hello = graph_g.add_nodes( "Hello", :height => '0.5', :label => '\N', :pos => '35,90', :width => '0.88889' )
  graph_g.add_edges( "Hello", "World", :pos => 'e,35,36.413 35,71.831 35,64.131 35,54.974 35,46.417' )
  node_world = graph_g.add_nodes( "World", :height => '0.5', :label => '\N', :pos => '35,18', :width => '0.97222' )
}
puts graph_g.output( :canon => String )
```

* `git2gv`, a tool that allows you to show your git commits

* `xml2gv`, a tool that allows you to show a xml file as graph.


## GRAPH THEORY

```ruby
require 'ruby-graphviz'
require 'ruby-graphviz/theory'

g = GraphViz.new( :G ) { ... }

t = GraphViz::Theory.new( g )

puts "Adjancy matrix : "
puts t.adjancy_matrix

puts "Symmetric ? #{t.symmetric?}"

puts "Incidence matrix :"
puts t.incidence_matrix

g.each_node do |name, node|
  puts "Degree of node `#{name}' = #{t.degree(node)}"
end

puts "Laplacian matrix :"
puts t.laplacian_matrix

puts "Dijkstra between a and f"
r = t.moore_dijkstra(g.a, g.f)
if r.nil?
  puts "No way !"
else
  print "\tPath : "; p r[:path]
  puts "\tDistance : #{r[:distance]}"
end

print "Ranges : "
rr = t.range
p rr
puts "Your graph contains circuits" if rr.include?(nil)

puts "Critical path : "
rrr = t.critical_path
print "\tPath "; p rrr[:path]
puts "\tDistance : #{rrr[:distance]}"
```

## INSTALLATION

```
sudo gem install ruby-graphviz
```

You also need to install [GraphViz](http://www.graphviz.org) 

On **Windows** you also need to install win32-open3. This is not an absolute
requirement.

## LICENCES

### Ruby/GraphViz 

Ruby/GraphViz is freely distributable according to the terms of the GNU
General Public License (see the file 'COPYING').

This program is distributed without any warranty. See the file 'COPYING' for
details.

GNU General Public Licence: https://en.wikipedia.org/wiki/Gpl

### nothugly.xsl

By Vidar Hokstad and Ryan Shea; Contributions by Jonas Tingborn, Earl
Cummings, Michael Kennedy (Graphviz 2.20.2 compatibility, bug fixes, testing,
lots of gradients)

Copyright (c) 2009 Vidar Hokstad

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

MIT license: https://en.wikipedia.org/wiki/MIT_License

## Contributors

Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):

Dave Burt (https://github.com/dburt)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=dburt)
     
obruening (https://github.com/obruening)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=obruening)
     
Chip Malice (https://github.com/hipe)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=hipe)
     
Stefan Stüben (https://github.com/MSNexploder)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=MSNexploder)
     
oupo (https://github.com/oupo)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=oupo)
     
Gregoire Lejeune (http://lejeun.es)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=glejeune) 🎨 📖 (https://github.com/glejeune/Ruby-Graphviz/commits?author=glejeune)
     
Markus Hauck (https://github.com/markus1189)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=markus1189)

Khalil Fazal (https://github.com/khalilfazal)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=khalilfazal)
     
Kenichi Kamiya (https://github.com/kachick)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=kachick)
     
Neven Has (https://github.com/nevenh)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=nevenh)
     
Andrew (https://Andrew.Kvalhe.im)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=AndrewKvalheim)
     
Daniel Zollinger (https://github.com/dznz)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=dznz)
     
Guilherme Simoes (https://guilhermesimoes.github.io)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=guilhermesimoes)
     
Oleg Orlov (https://github.com/OrelSokolov)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=OrelSokolov)

Gabe Kopley (https://www.instagram.com/stewiecutedog/)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=gkop)
     
Jake Goulding (http://jakegoulding.com)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=shepmaster)
     
hirochachacha (https://github.com/hirochachacha)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=hirochachacha)
     
ronen barzel (http://www.ronenbarzel.org)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=ronen)
     
Jamison Dance (https://jamison.dance)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=jergason) 📖 (https://github.com/glejeune/Ruby-Graphviz/commits?author=jergason)
     
Joseph Anthony Pasquale Holsten (http://josephholsten.com)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=josephholsten)
     
Miguel Cabrera (http://mfcabrera.com)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=mfcabrera)

Mike Fiedler (http://www.miketheman.net)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=miketheman)
     
Nathan Long (http://nathanmlong.com)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=nathanl) 📖 (https://github.com/glejeune/Ruby-Graphviz/commits?author=nathanl)
     
Olle Jonsson (http://ollehost.dk/)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=olleolleolle) 📖 (https://github.com/glejeune/Ruby-Graphviz/commits?author=olleolleolle)
     
Postmodern (http://postmodern.github.com/)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=postmodern)
     
Robert Reiz (https://www.versioneye.com)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=reiz)
     
Göran Bodenschatz (http://46halbe.de)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=coding46)
     
SHIBATA Hiroshi (https://www.hsbt.org/)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=hsbt)

moracca (https://github.com/moracca)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=moracca)
     
TPei (https://github.com/TPei)
📖 (https://github.com/glejeune/Ruby-Graphviz/commits?author=TPei)
     
Villu Orav (https://github.com/villuorav)
📖 (https://github.com/glejeune/Ruby-Graphviz/commits?author=villuorav)
     
David Rodríguez (https://github.com/deivid-rodriguez)
💻 (https://github.com/glejeune/Ruby-Graphviz/commits?author=deivid-rodriguez) 📦

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
