Tk::GraphViz - Render an interactive GraphViz graph
use Tk::GraphViz; my $gv = $mw->GraphViz ( qw/-width 300 -height 300/ ) ->pack ( qw/-expand yes -fill both/ ); $gv->show ( $dotfile );
The GraphViz widget is derived from Tk::Canvas. It adds the ability to render graphs in the canvas. The graphs can be specified either using the DOT graph-description language, or using via a GraphViz object.
When show() is called, the graph is passed to the dot command to generate the layout info. That info is then used to create rectangles, lines, etc in the canvas that reflect the generated layout.
Once the items have been created in the graph, they can be used like any normal canvas items: events can be bound, etc. In this way, interactive graphing applications can be created very easily.
Renders the given graph in the canvas. The graph itself can be specified in a number of formats. 'graph' can be one of the following:
show() will recognize some options that control how the graph is rendered, etc. The recognized options:
Specifies an alternate command to invoke to generate the layout of the graph. If not given, then default is 'dot'. This can be used, for example, to use 'neato' instead of 'dot'.
Allows additional default graph attributes to be specified. Each name => value pair will be passed to dot as '-Gname=value' on the command-line.
Allows additional default node attributes to be specified. Each name => value pair will be passed to dot as '-Nname=value' on the command-line.
Allows additional default edge attributes to be specified. Each name => value pair will be passed to dot as '-Ename=value' on the command-line.
For example, to use neato to generate a layout with non-overlapping nodes and spline edges:
$gv->show ( $file, layout => 'neato', graphattrs => [qw( overlap false spline true )] );
The Tk::GraphViz canvas can be configured with some bindings for standard operations. If no options are given, the default bindings for zooming and scrolling will be enabled. Alternative bindings can be specified via these options:
Creates the default bindings for zooming. Zooming in or out in the canvas will be bound to <Shift-2> (Shift + mouse button 2). To zoom in, click and drag out a zoom rectangle from top left to bottom right. To zoom out, click and drag out a zoom rectangle from bottom left to top right.
This will bind zooming to an alternative event sequence. Examples:
-zoom => '<1>' # Zoom on mouse button 1 -zoom => '<Ctrl-3>' # Zoom on Ctrl + mouse button 3
Creates the default bindings for scrolling / panning. Scrolling the canvas will be bound to <2> (Mouse button 2).
This will bind scrolling to an alternative event sequence. Examples:
-scroll => '<1>' # Scroll on mouse button 1 -scroll => '<Ctrl-3>' # Scroll on Ctrl + mouse button 3
Binds the keypad arrow / number keys to scroll the canvas, and the keypad +/- keys to zoom in and out. Note that the canvas must have the keyboard focus for these bindings to be activated. This is done by default when createBindings() is called without any options.
Scales all of the elements in the canvas to fit the canvas' width and height.
Zoom in by scaling everything up by the given scale factor. The factor should be > 1.0 in order to get reasonable behavior.
Zoom out by scaling everything down by the given scale factor. This is equivalent to
$gv->zoom ( -in => 1/factor )
The factor show be > 1.0 in order to get reasonable behavior.
In order to facilitate binding, etc, all of the graph elements (nodes, edges, subgraphs) that a created in the cavas. Specific tags are given to each class of element. Additionally, all attributes attached to an element in the graph description (e.g. 'color', 'style') will be included as tags.
Node elements are identified with a 'node' tag. For example, to bind something to all nodes in a graph:
$gv->bind ( 'node', '<Any-Enter>', sub { ... } );
The value of the 'node' tag is the name of the node in the graph (which is not equivalent to the node label -- that is the 'label' tag)
Edge elements are identified with a 'edge' tag. For example, to bind something to all edges in a graph:
$gv->bind ( 'edge', '<Any-Enter>', sub { ... } );
The value of the 'edge' tag is an a string of the form "node1 node2", where node1 and node2 are the names of the respective nodes. To make it convenient to get the individual node names, the edge also has tags 'node1' and 'node2', which give the node names separately.
Subgraph elements are identified with a 'subgraph' tag. The value of the 'subgraph' is the name of the subgraph / cluster.
The following example creates a GraphViz widgets to display a graph from a file specified on the command line. Whenever a node is clicked, the node name and label are printed to stdout:
use GraphViz; use Tk; my $mw = new MainWindow (); my $gv = $mw->Scrolled ( 'GraphViz', -background => 'white', -scrollbars => 'sw' ) ->pack ( -expand => '1', -fill => 'both' ); $gv->bind ( 'node', '<Button-1>', sub { my @tags = $gv->gettags('current'); push @tags, undef unless (@tags % 2) == 0; my %tags = @tags; printf ( "Clicked node: '%s' => %s\n", $tags{node}, $tags{label} ); } ); $gv->show ( shift ); MainLoop;
Lots of DOT language features not yet implemented
See http://www.graphviz.org/ for more info on the graphviz tools.
Jeremy Slade <jeremy@jkslade.net>
Other contributors: Mike Castle, John Cerney, Phi Kasten, Jogi Kuenstner Tobias Lorenz, Charles Minc, Reinier Post, Slaven Rezic
Copyright 2003-2008 by Jeremy Slade
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
To install Tk::GraphViz, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tk::GraphViz
CPAN shell
perl -MCPAN -e shell install Tk::GraphViz
For more information on module installation, please visit the detailed CPAN module installation guide.