The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

GraphViz - Interface to the GraphViz graphing tool

SYNOPSIS

  use GraphViz;

  my $g = GraphViz->new();

  $g->add_node({ name => 'London'});
  $g->add_node({ name => 'Paris', label => 'City of\nlurve'});
  $g->add_node({ name => 'New York'});

  $g->add_edge({ from => 'London',
                   to => 'Paris',});

  $g->add_edge({ from => 'London',
                   to => 'New York',
                label => 'Far'});

  $g->add_edge({ from => 'Paris',
                   to => 'London',});


  print $g->as_png;

DESCRIPTION

This modules provides an interface to layout and generate images of directed graphs in a variety of formats (PostScript, PNG, etc.) using the "dotneato" program from the GraphViz project (http://www.graphviz.org/).

At the moment this is a fairly simple library. Some features of dotneato are not currently implemented, such as graph attributes. Feature requests are welcome!

METHODS

new

This is the constructor. It currently takes no arguments:

  my $g = GraphViz->new();

add_node

A graph consists of at least one node. This method creates a new node and assigns it attributes. Various attributes are possible: "name" suggests a name for the node (if you do not supply one, one is generated for you and returned), "label" provides a label for the node (the label defaults to the name if none is specified). See the "dot" manpage under "NODE ATTRIBUTES" for others.

  $g->add_node({ name => 'Paris', label => 'City of\nlurve'});
  $g->add_node({ name => 'Paris', label => ['A', 'B', 'C']});

Nodes can be clustered together with the "cluster" attribute, which is drawn by having a labelled rectangle around all the nodes in a cluster.

  $g->add_node({ name => 'London', cluster => 'Europe'});
  $g->add_node({ name => 'Amsterdam', cluster => 'Europe'});

Also, nodes can consist of multiple parts (known as ports). This is implemented by passing an array reference as the label, and the parts are displayed as a label. GraphViz has a much more complete port system, this is just a simple interface to it.

  $g->add_node({ name => 'London', label => ['Heathrow', 'Gatwick']});

add_edge

Edges are directed links between nodes. This method creates a new edge between two nodes and optionally assigns it attributes. Two mandatory parameters are 'from' and 'to', which indicate the node names that the edge connects. Optional attributes such as 'label' are also available (see the "dot" manpage under the "EDGE ATTRIBUTES" for others).

  $g->add_edge({ from => 'London',
                   to => 'New York',
                label => 'Far'});

Adding edges between ports of a node is done via the 'from_port' and 'to_port' parameters, which currently takes in the offset of the port (ie 0, 1, 2...).

  $g->add_edge({      from => 'London',
                 from_port => 0,
                        to => 'Paris',
  });

as_canon, as_text, as_gif etc. methods

There are a number of methods which generate input for dotneato or output the graph in a variety of formats.

as_canon

The as_canon method returns the canonical dotneato file which corresponds to the graph. It does not layout the graph - every other as_* method does.

  print $g->as_canon;


  # prints out something like:
  digraph test {
      node [    label = "\N" ];
      London [label=London];
      Paris [label="City of\nlurve"];
      New_York [label="New York"];
      London -> Paris;
      London -> New_York [label=Far];
      Paris -> London;
  }
as_text

The as_text method returns text which is a layed-out dotneato-format file.

  print $g->as_text;

  # prints out something like:
  digraph test {
      node [    label = "\N" ];
      graph [bb= "0,0,162,134"];
      London [label=London, pos="33,116", width="0.89", height="0.50"];
      Paris [label="City of\nlurve", pos="33,23", width="0.92", height="0.62"];
      New_York [label="New York", pos="123,23", width="1.08", height="0.50"];
      London -> Paris [pos="e,27,45 28,98 26,86 26,70 27,55"];
      London -> New_York [label=Far, pos="e,107,40 49,100 63,85 84,63 101,46", lp="99,72"];
      Paris -> London [pos="s,38,98 39,92 40,78 40,60 39,45"];
  }
as_ps

Returns a string which contains a layed-out PostScript-format file.

  print $g->as_ps;
as_hpgl

Returns a string which contains a layed-out HP pen plotter-format file.

  print $g->as_hpgl;
as_pcl

Returns a string which contains a layed-out Laserjet printer-format file.

  print $g->as_pcl;
as_mif

Returns a string which contains a layed-out FrameMaker graphics-format file.

  print $g->as_mif;
as_pic

Returns a string which contains a layed-out PIC-format file.

  print $g->as_pic;
as_gd

Returns a string which contains a layed-out GD-format file.

  print $g->as_gd;
as_gd2

Returns a string which contains a layed-out GD2-format file.

  print $g->as_gd2;
as_gif

Returns a string which contains a layed-out GIF-format file.

  print $g->as_gif;
as_jpeg

Returns a string which contains a layed-out JPEG-format file.

  print $g->as_jpeg;
as_png

Returns a string which contains a layed-out PNG-format file.

  print $g->as_png;
as_wbmp

Returns a string which contains a layed-out Windows BMP-format file.

  print $g->as_wbmp;
as_ismap

Returns a string which contains a layed-out HTML client-side image map format file.

  print $g->as_ismap;
as_imap

Returns a string which contains a layed-out HTML server-side image map format file.

  print $g->as_imap;
as_vrml

Returns a string which contains a layed-out VRML-format file.

  print $g->as_vrml;
as_vtx

Returns a string which contains a layed-out VTX (Visual Thought) format file.

  print $g->as_vtx;
as_mp

Returns a string which contains a layed-out MetaPost-format file.

  print $g->as_mp;
as_fig

Returns a string which contains a layed-out FIG-format file.

  print $g->as_fig;
as_svg

Returns a string which contains a layed-out SVG-format file.

  print $g->as_svg;
as_plain

Returns a string which contains a layed-out simple-format file.

  print $g->as_plain;

AUTHOR

Leon Brocard <acme@astray.com>

COPYRIGHT

Copyright (C) 2000, Leon Brocard

This module is free software; you can redistribute it or modify it under the same terms as Perl itself.