A simple Ruby-graphviz example

Installation

observed:

Fetching: ruby-graphviz-1.0.9.gem (100%)

Since version 0.9.2, Ruby/GraphViz can use Open3.popen3 (or not)
On Windows, you can install 'win32-open3'

You need to install GraphViz (http://graphviz.org/) to use this Gem.

For more information about Ruby-Graphviz :
* Doc : http://rdoc.info/projects/glejeune/Ruby-Graphviz
* Sources : http://github.com/glejeune/Ruby-Graphviz
* NEW - Mailing List : http://groups.google.com/group/ruby-graphviz

Last (important) changes :
* GraphViz#add_edge is deprecated, use GraphViz#add_edges
* GraphViz#add_node is deprecated, use GraphViz#add_nodes
* GraphViz::Edge#each_attribut is deprecated, use GraphViz::Edge#each_attribute
* GraphViz::GraphML#attributs is deprecated, use GraphViz::GraphML#attributes
* GraphViz::Node#each_attribut is deprecated, use GraphViz::Node#each_attribute
  
Successfully installed ruby-graphviz-1.0.9
Parsing documentation for ruby-graphviz-1.0.9
Installing ri documentation for ruby-graphviz-1.0.9
Done installing documentation for ruby-graphviz after 2 seconds
1 gem installed

require '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" )

output:

A hello world GraphViz diagram

Outputting to SVG

Here's an example of outputting to SVG:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.26.3 (20100126.1600)
 -->
<!-- Title: G Pages: 1 -->
<svg width="88pt" height="120pt"
 viewBox="0.00 0.00 88.00 120.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph1" class="graph" transform="scale(1 1) rotate(0) translate(4 116)">
<title>G</title>
<polygon fill="white" stroke="white" points="-4,5 -4,-116 85,-116 85,5 -4,5"/>
<!-- Hello -->
<g id="node1" class="node"><title>Hello</title>
<ellipse fill="none" stroke="black" cx="40" cy="-93" rx="36.0624" ry="18.3848"/>
<text text-anchor="middle" x="40" y="-89.4" font-family="Times Roman,serif" font-size="14.00">Hello</text>
</g>
<!-- World -->
<g id="node2" class="node"><title>World</title>
<ellipse fill="none" stroke="black" cx="40" cy="-19" rx="39.8075" ry="18.3848"/>
<text text-anchor="middle" x="40" y="-15.4" font-family="Times Roman,serif" font-size="14.00">World</text>
</g>
<!-- Hello->World -->
<g id="edge2" class="edge"><title>Hello->World</title>
<path fill="none" stroke="black" d="M40,-74.3267C40,-66.2639 40,-56.6477 40,-47.7115"/>
<polygon fill="black" stroke="black" points="43.5001,-47.6673 40,-37.6674 36.5001,-47.6674 43.5001,-47.6673"/>
</g>
</g>
</svg>

Resources

Tags:
Source:
0020hrs.txt
Published:
04-04-2014 00:20