1# NAME
2
3[GraphViz2::Data::Grapher](https://metacpan.org/pod/GraphViz2::Data::Grapher) - Visualize a data structure as a graph
4
5# Synopsis
6
7        #!/usr/bin/env perl
8
9        use strict;
10        use warnings;
11
12        use File::Spec;
13
14        use GraphViz2;
15        use GraphViz2::Data::Grapher;
16
17        my($sub) = sub{};
18        my($s)   =
19        {
20                A =>
21                {
22                        a =>
23                        {
24                        },
25                        bbbbbb => $sub,
26                        c123   => $sub,
27                        d      => \$sub,
28                },
29                C =>
30                {
31                        b =>
32                        {
33                                a =>
34                                {
35                                        a =>
36                                        {
37                                        },
38                                        b => sub{},
39                                        c => 42,
40                                },
41                        },
42                },
43                els => [qw(element_1 element_2 element_3)],
44        };
45
46        my($graph) = GraphViz2 -> new
47                (
48                 edge   => {color => 'grey'},
49                 global => {directed => 1},
50                 graph  => {rankdir => 'TB'},
51                 node   => {color => 'blue', shape => 'oval'},
52                );
53
54        my($g)           = GraphViz2::Data::Grapher->new(graph => $graph);
55        my($format)      = shift || 'svg';
56        my($output_file) = shift || File::Spec -> catfile('html', "parse.data.$format");
57
58        $g -> create(name => 's', thing => $s);
59        $graph -> run(format => $format, output_file => $output_file);
60
61        # If you did not provide a GraphViz2 object, do this
62        # to get access to the auto-created GraphViz2 object.
63
64        #$g -> create(name => 's', thing => $s);
65        #$g -> graph -> run(format => $format, output_file => $output_file);
66
67        # Or even
68
69        #$g -> create(name => 's', thing => $s)
70        #-> graph
71        #-> run(format => $format, output_file => $output_file);
72
73See scripts/parse.data.pl (["Scripts Shipped with this Module" in GraphViz2](https://metacpan.org/pod/GraphViz2#Scripts-Shipped-with-this-Module)).
74
75# Description
76
77Takes a Perl data structure and recursively converts it into [Tree::DAG\_Node](https://metacpan.org/pod/Tree::DAG_Node) object, and then graphs it.
78
79You can write the result in any format supported by [Graphviz](http://www.graphviz.org/).
80
81Here is the list of [output formats](http://www.graphviz.org/content/output-formats).
82
83Within the graph:
84
85- o Array names are preceeded by '@'
86- o Code references are preceeded by '&'
87- o Hash names are preceeded by '%'
88- o Scalar names are preceeded by '$'
89
90Hence, a hash ref will look like '%$h'.
91
92Further, objects of different type have different shapes.
93
94# Constructor and Initialization
95
96## Calling new()
97
98`new()` is called as `my($obj) = GraphViz2::Data::Grapher -> new(k1 => v1, k2 => v2, ...)`.
99
100It returns a new object of type `GraphViz2::Data::Grapher`.
101
102Key-value pairs accepted in the parameter list:
103
104- o graph => $graphviz\_object
105
106    This option specifies the GraphViz2 object to use. This allows you to configure it as desired.
107
108    The default is GraphViz2 -> new. The default attributes are the same as in the synopsis, above,
109    except for the graph label of course.
110
111    This key is optional.
112
113# Methods
114
115## create(name => $name, thing => $thing)
116
117Creates the graph, which is accessible via the graph() method, or via the graph object you passed to new().
118
119Returns $self to allow method chaining.
120
121$name is the string which will be placed in the root node of the tree.
122
123If $s = {...}, say, use 's', not '$s', because '%$' will be prefixed automatically to the name,
124because $s is a hashref.
125
126$thing is the data stucture to graph.
127
128## graph()
129
130Returns the graph object, either the one supplied to new() or the one created during the call to new().
131
132## tree()
133
134Returns the tree object (of type [Tree::DAG\_Node](https://metacpan.org/pod/Tree::DAG_Node)) built before it is traversed to generate the nodes and edges.
135
136# Scripts Shipped with this Module
137
138## scripts/parse.data.pl
139
140Demonstrates graphing a Perl data structure.
141
142Outputs to ./html/parse.data.svg by default.
143
144## scripts/parse.html.pl
145
146Demonstrates using [XML::Bare](https://metacpan.org/pod/XML::Bare) to parse HTML.
147
148Inputs from ./t/sample.html, and outputs to ./html/parse.html.svg by default.
149
150## scripts/parse.xml.bare.pl
151
152Demonstrates using [XML::Bare](https://metacpan.org/pod/XML::Bare) to parse XML.
153
154Inputs from ./t/sample.xml, and outputs to ./html/parse.xml.bare.svg by default.
155
156# Thanks
157
158Many thanks are due to the people who chose to make [Graphviz](http://www.graphviz.org/) Open Source.
159
160And thanks to [Leon Brocard](http://search.cpan.org/~lbrocard/), who wrote [GraphViz](https://metacpan.org/pod/GraphViz), and kindly gave me co-maint of the module.
161
162# Author
163
164[GraphViz2](https://metacpan.org/pod/GraphViz2) was written by Ron Savage _<ron@savage.net.au>_ in 2011.
165
166Home page: [http://savage.net.au/index.html](http://savage.net.au/index.html).
167
168# Copyright
169
170Australian copyright (c) 2011, Ron Savage.
171
172        All Programs of mine are 'OSI Certified Open Source Software';
173        you can redistribute them and/or modify them under the terms of
174        The Perl License, a copy of which is available at:
175        http://dev.perl.org/licenses/
176