View/GraphViz.pm

filter: Statementstmt 100% 6 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 8 / 8 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Helper/View/GraphViz.pm ↑ Summary
1 package Catalyst::View::GraphViz;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base qw/Catalyst::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use GraphViz;
6 use NEXT;
7  
8 our $VERSION = '0.05';
9  
10 =head1 NAME
11  
12 Catalyst::View::GraphViz - GraphViz View Class
13  
14 =head1 SYNOPSIS
15  
16 =head2 Use the helper to create a View class
17  
18 script/myapp_create.pl view GraphViz GraphViz
19  
20 This creates the MyApp::View::GraphViz class.
21  
22  
23 =head2 Build the GraphViz object
24  
25 #In some method (some View method, since
26 #that's where the View code should go. See below.)
27 use GraphViz;
28 $graph = GraphViz->new();
29 $graph->add_node("Hello", shape => 'box');
30 $graph->add_node("world", shape => 'box');
31 $graph->add_edge("Hello", "world");
32
33 $c->stash->{graphviz}->{graph} = $graph;
34 $c->stash->{graphviz}->{format} = "cmapx"; #HTML image map (default: png)
35
36  
37  
38 =head2 Forward to the View
39  
40 #Meanwhile, maybe in a private end action
41 if(!$c->res->body) {
42 if($c->stash->{template}) {
43 $c->forward('MyApp::View::TT');
44 } elsif($c->stash->{graphviz}->{graph}) {
45 $c->forward('MyApp::View::GraphViz');
46 } else {
47 die("No output method!\n");
48 }
49 }
50  
51  
52 =head1 DESCRIPTION
53  
54 This is the Catalyst view class for L<GraphViz>. Your application
55 subclass should inherit from this class.
56  
57 This plugin renders the GraphViz object specified in
58 C<$c-E<gt>stash-E<gt>{graphviz}-E<gt>{graph}> into the
59 C<$c-E<gt>stash-E<gt>{graphviz}-E<gt>{format}> (one of e.g. png
60 gif, or one of the other as_* methods described in the
61 L<GraphViz> module. PNG is the default format.
62  
63 The output is stored in C<$c-E<gt>response-E<gt>output>.
64  
65 The normal way of using this is to render a PNG image for a request
66 and let Catalyst serve it.
67  
68 Another use of this View is to let it generate the text of a client
69 side imagemap (using a SubRequest) which you then put into the web
70 page currently being rendered. See below for an example.
71  
72  
73 =head1 BUILD THE GRAPHVIZ OBJECT IN A VIEW
74  
75 The Catalyst::View::GraphViz takes a pre-built GraphViz object to
76 render.
77  
78 But where should this GraphViz object be constructed? Preferrably in
79 a View class, since the GraphViz graph contains nodes with different
80 colors, shapes, etc.
81  
82 Consider how the GraphViz View relates to templating systems:
83  
84 Templating System GraphViz
85 ----------------- --------
86 Model | Model object(s) Model object(s) (a graph)
87 Output | Rendered HTML Rendered graph image/imagemap
88 View | TT/Mason/? View::GraphViz
89 View code | Custom template file Custom View class
90 Set "look" | $c->stash->{template} $c->stash->{graphview}->{view}
91 Set model object | varies $c->stash->{graphview}->{object}
92 E.g. "look" | update.thtml MyApp::View::OddEvenGraph
93  
94 So when using TT as a rendering engine, the template contains the
95 instructions for how to display the Model object. You have many
96 templates for displaying the same model object in different ways.
97  
98 And when using GraphViz as a rendering engine, the View class contains
99 the instructions for how to display the Model object. You have many
100 View classes for displaying the same model object in different ways.
101  
102 Here's how to create a specific View class for each type of graph.
103  
104  
105 =head2 MyApp::View::OddEvenGraph
106  
107 As an example, let's create a view to render a graph of numbers, where
108 the odd number nodes are boxes, and the even are ellipses.
109  
110 Our model object is set like this somewhere (for a quick demo, just
111 put it in the MyApp::default sub):
112  
113 $c->stash->{graphview}->{object} = {
114 3 => 2,
115 4 => 3,
116 1 => 3,
117 2 => 1,
118 };
119  
120 This can of course be anything that you can interpret as a graph and
121 would like to visualize using GraphViz, not necessarily a single deep
122 data structure like this. It could equally well be an array ref with
123 model objects which together form a graph.
124  
125 After setting up the model object, we assign the correct View class to
126 render it.
127  
128 $c->stash->{graphview}->{view} = "MyApp::View::OddEvenGraph";
129 $c->stash->{graphviz}->{format} = "cmapx"; #Optionally override the format
130  
131  
132 And, maybe in a private C<end> action, we forward to the view if it's
133 set, like this:
134  
135 sub end : Private {
136 my ( $self, $c ) = @_;
137  
138 if(!$c->res->body) {
139 if($c->stash->{template}) {
140 $c->forward('MyApp::View::TT');
141 } elsif(my $view = $c->stash->{graphview}->{view}) {
142 $c->forward($view);
143 } else {
144 die("No output method!\n");
145 }
146 }
147 }
148  
149  
150 That's what your application looks like. Now we need to create the
151 actual View class MyApp::View::OddEvenGraph. Use the helper GraphView:
152  
153 script/myapp_create.pl view OddEvenGraph GraphView
154  
155  
156 In the class MyApp::View::OddEvenGraph you can modify the process sub
157 to suit your needs.
158  
159 sub process {
160 my ($self, $c) = @_;
161  
162 my $graph = $c->stash->{graphview}->{object} or
163 die('No object specified in $c->stash->{graphview}->{object} for rendering');
164  
165 my $graphviz = GraphViz->new(node => {
166 name => "oddeven",
167 });
168  
169 $graphviz->add_node($_, shape => ($_ % 2) ? "box" : "ellipse") for(keys %$graph);
170 while(my ($from, $to) = each %$graph) {
171 $graphviz->add_edge($from, $to);
172 }
173  
174  
175 $c->stash->{graphviz}->{graph} = $graphviz;
176 $c->forward('MyApp::View::GraphViz');
177  
178 return 1;
179 }
180  
181 As you can see, the purpose of this method is to transform the model
182 graph object into a GraphViz object, which is then forwarded to the
183 GraphViz View.
184  
185  
186  
187 =head1 MAKE A SUBREQUEST TO GENERATE AN IMAGEMAP
188  
189 Together with the ability to render a GraphViz image soon comes the
190 need to generate a client-side imagemap which can be inserted in the
191 web page showing the image.
192  
193 You need a) an action that renders the GraphViz object in the cmapx
194 format, and b) to call that action from within another action so you
195 can assign the resulting HTML text to your stash and then put it in
196 the template.
197  
198  
199 =head2 Render as an Imagemap
200  
201 If your ordinary png action looks like this:
202  
203 sub png : Local {
204 my ( $self, $c ) = @_;
205 $c->stash->{graphview}->{view} = "MyApp::View::OddEvenGraph";
206 $c->stash->{graphview}->{object} = ...;
207 }
208  
209 then your imap action should look like this:
210  
211 sub imap : Local {
212 my ( $self, $c ) = @_;
213 $c->stash->{graphview}->{view} = "MyApp::View::OddEvenGraph";
214 $c->stash->{graphview}->{object} = ...;
215 $c->stash->{graphviz}->{format} = "cmapx";
216 }
217  
218  
219 =head2 Call the Imagemap Action
220  
221 First make sure you have the SubRequest plugin loaded:
222  
223 use Catalyst qw/SubRequest/;
224  
225 This is how to perform the SubRequest. Let's assume these actions are
226 in the Controller "Graph":
227  
228 $c->stash->{html_imagemap} = $c->subreq("/graph/imap");
229  
230 #Reset after subreq (until this bug is fixed: http://rt.cpan.org/NoAuth/Bug.html?id=15790 )
231 $c->response->content_type("text/html");
232  
233 Now you can simply output the imagemap text in the template
234  
235 [% html_imagemap %]
236 <img name="graph" src="/graph/png" USEMAP="#oddeven" border=0>
237  
238 Note 1: The name "oddeven" is the same as the one set in the
239  
240 GraphViz->new(name => "oddeven");
241  
242 Note 1a: Unfortunately this isn't quite true. The name of the GraphViz
243 image is always hardcoded to "test". Bug that needs to be fixed:
244 http://rt.cpan.org/NoAuth/Bug.html?id=14882
245  
246 Note 2: The nodes will not be clickable unless they have a URL property,
247 so you need to specify that for each
248  
249 $graphviz->add_node(URL => "/graph/node/select?name=$name");
250  
251 in the View class. Actually, there is a clever shortcut in GraphViz
252 for this, so instead of specifying it for each node, you can set a
253 default when calling
254  
255 my $graphviz = GraphViz->new(node => { URL => '/graph/node/select?name=\N' });
256  
257 The \N is a placeholder for the name of each node.
258  
259
260  
261  
262 =head1 METHODS
263  
264 =head2 new($c)
265  
266 The constructor for the GraphViz view. Sets up the template provider,
267 and reads the application config.
268  
269 =cut
270 sub new {
271 my $self = shift;
272 my $c = shift;
273  
274 $self = $self->NEXT::new(@_);
275  
276 return $self;
277 }
278  
279 =head2 process($c)
280  
281 Render the GraphViz object specified in
282 C<$c-E<gt>stash-E<gt>{graphviz}>.
283  
284 Output is stored in C<$c-E<gt>response-E<gt>output>.
285  
286 =cut
287 my $plain = 'text/plain; charset=utf-8';
288 my $html = 'text/html; charset=utf-8';
289 my %hExtType = (
290 ps => 'application/postscript',
291 hpgl => $plain,
292 pcl => $plain,
293 mif => 'application/x-mif',
294 pic => 'image/x-pict',
295 gd => $plain,
296 gd2 => $plain,
297 gif => 'image/gif',
298 jpeg => 'image/jpeg',
299 png => 'image/x-png',
300 wbmp => 'image/x-ms-bmp',
301 cmap => $plain,
302 cmapx => $plain,
303 ismap => $plain,
304 imap => $plain,
305 vrml => 'x-world/x-vrml',
306 vtx => $plain, #?
307 mp => $plain, #?
308 fig => $plain, #?
309 svg => 'image/svg+xml',
310 svgz => 'image/svg+xml',
311 dot => $plain,
312 canon => $plain,
313 plain => $plain,
314 );
315 sub process {
316 my ($self, $c) = @_;
317  
318 my $oGv = $c->stash->{graphviz}->{graph};
319 if(!$oGv) {
320 $c->log->debug('No GraphViz object specified in $c->stash->{graphviz}->{graph} for rendering') if $c->debug;
321 return 0;
322 }
323  
324 my $format = $c->stash->{graphviz}->{format} || $c->config->{graphviz}->{format} || $self->config->{format} || "png";
325 my $contentType;
326 my $output;
327 eval {
328 $c->log->debug(qq/Rendering GraphViz object as ($format)/) if $c->debug;
329 $contentType = $hExtType{$format} or die("Unknown format ($format). Known formats are (" . join("|", sort keys %hExtType) . ")\n");
330  
331 my $methodRender = "as_$format";
332 $output = $oGv->$methodRender() or die("Could not render GraphViz obejct as ($format)\n");
333 };
334 if($@) {
335 my $error = $@;
336 $c->log->error($error);
337 $c->error($error);
338 return 0;
339 }
340
341 $c->response->content_type or $c->response->content_type($contentType);
342  
343 $c->response->body($output);
344  
345 return 1;
346 }
347  
348  
349  
350  
351  
352 =head1 SEE ALSO
353  
354 L<Catalyst>, L<GraphViz>
355  
356  
357 =head1 CHANGES
358  
359  
360  
361  
362 =head2 0.05
363  
364 Docs
365  
366  
367 =head2 0.01 - 0.04
368  
369 Makefile and Windows/Unix stuff
370  
371  
372 =head1 AUTHOR
373  
374 Johan Lindstrom, C<johanl@cpan.org>
375  
376  
377  
378 =head1 CREDITS
379  
380 Largely based on the TT view.
381  
382 Obviosly uses Acme's L<GraphViz> module, which in turn uses the
383 brilliant GraphViz package (http://www.graphviz.org/).
384  
385 Quick link to a useful, but obscure, doc page:
386 http://www.graphviz.org/pub/scm/graphviz2/doc/info/shapes.html
387  
388  
389  
390 =head1 COPYRIGHT
391  
392 This program is free software, you can redistribute it and/or modify it
393 under the same terms as Perl itself.
394  
395 =cut
396  
397 1;
« Helper/View/GraphViz.pm ↑ Summary