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

Tk::GraphViz - Render an interactive GraphViz graph

SYNOPSIS

    use Tk::Graphviz;
    my $gv = $mw->GraphViz ( qw/-width 300 -height 300/ )
      ->pack ( qw/-expand yes -fill both/ );
    $gv->show ( $dotfile );

DESCRIPTION

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.

METHODS

$gv->show ( graph )

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:

- An instance of the GraphViz class (or subclass thereof)
- A scalar containing a graph in DOT format. The scalar must match /^\s*(?:di)?graph /.
- An instance of the IO::Handle class (or subclass thereof), from which to read a graph in DOT format.
- The name / path of a file that contains a graph in DOT format.

$gv->createBindings ( ?option => value? )

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 => true

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.

-zoom => spec

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
-scroll => true

Creates the default bindings for scrolling / panning. Scrolling the canvas will be bound to <2> (Mouse button 2).

-scroll => spec

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

$gv->fit()

Scales all of the elements in the canvas to fit the canvas' width and height.

TAGS

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.

Nodes

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)

Edges

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 array reference containing two items, which are the names of the two endpoint nodes for the edge (in [ source, destination ] order).

Subgraphs

Subgraph elements are identified with a 'subgraph' tag. The value of the 'subgraph' is the name of the subgraph / cluster.

EXAMPLES

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;

BUGS AND LIMITATIONS

Currently only uses dot for layout. neato is not supported.

Lots of DOT language features not yet implemented

Various node shapes and attributes: polygon, skew, ...
Edge arrow head types

ACKNOWLEDGEMENTS

See http://www.graphviz.org/ for more info on the graphviz tools.

AUTHOR

Jeremy Slade <jeremy@jkslade.net>

COPYRIGHT AND LICENSE

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.