Tk::GraphViz - Render an interactive GraphViz graph |
fit()
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:
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:
-zoom => '<1>' # Zoom on mouse button 1 -zoom => '<Ctrl-3>' # Zoom on Ctrl + mouse button 3
-scroll => '<1>' # Scroll on mouse button 1 -scroll => '<Ctrl-3>' # Scroll on Ctrl + mouse button 3
createBindings()
is called without any options.
fit()
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: John Cerney, Slaven Rezic, Mike Castle
Copyright 2003 by Jeremy Slade
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Tk::GraphViz - Render an interactive GraphViz graph |