Graph.pm

filter: Statementstmt 100% 59 / 59 Branchbran 93.7% 15 / 16 Conditioncond 70.0% 7 / 10 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 15 / 15 Podpod 100% 11 / 11 totaltotal 93.9% 109 / 116 CC 16 SCAR 10.2
CC
16
Cov
95%
add_frame
6.1
init
3.0
frames
2.0
CRAP 2.8
« Graph/Data.pm ↑ Summary Graph/Data/Tree.pm »
1 package SVG::Graph;
2  
3 =head1 NAME
4  
5 SVG::Graph - Visualize your data in Scalable Vector Graphics (SVG) format.
6  
7 =head1 SYNOPSIS
8  
9 use SVG::Graph;
10 use SVG::Graph::Data;
11 use SVG::Graph::Data::Datum;
12  
13 #create a new SVG document to plot in...
14 my $graph = SVG::Graph->new(width=>600,height=>600,margin=>30);
15  
16 #and create a frame to hold the data/glyphs
17 my $frame = $graph->add_frame;
18  
19 #let's plot y = x^2
20 my @data = map {SVG::Graph::Data::Datum->new(x=>$_,y=>$_^2)}
21 (1,2,3,4,5);
22 my $data = SVG::Graph::Data->new(data => \@data);
23  
24 #put the xy data into the frame
25 $frame->add_data($data);
26  
27 #add some glyphs to apply to the data in the frame
28 $frame->add_glyph('axis', #add an axis glyph
29 'x_absolute_ticks' => 1, #with ticks every one
30 #unit on the x axis
31 'y_absolute_ticks' => 1, #and ticks every one
32 #unit on the y axis
33  
34 'stroke' => 'black', #draw the axis black
35 'stroke-width' => 2, #and 2px thick
36 );
37  
38 $frame->add_glyph('scatter', #add a scatterplot glyph
39 'stroke' => 'red', #the dots will be outlined
40 #in red,
41 'fill' => 'red', #filled red,
42 'fill-opacity' => 0.5, #and 50% opaque
43 );
44  
45 #print the graphic
46 print $graph->draw;
47  
48 =head1 DESCRIPTION
49  
50 SVG::Graph is a suite of perl modules for plotting data. SVG::Graph
51 currently supports plots of one-, two- and three-dimensional data, as
52 well as N-ary rooted trees. Data may be represented as:
53  
54 Glyph Name Dimensionality supported
55 1d 2d 3d tree
56 --------------------------------------------------------
57 Axis x
58 Bar Graph x
59 Bubble Plot x
60 Heatmap Graph x
61 Line Graph x
62 Pie Graph x
63 Scatter Plot x
64 Spline Graph x
65 Tree x
66  
67 SVG::Graph 0.02 is a pre-alpha release. Keep in mind that many of the
68 glyphs are not very robust.
69  
70 =head1 PLOTTING
71  
72 You need to create a SVG::Graph::Frame instance from the parent
73 SVG::Graph instance for each set of data to be plotted. Datasets
74 can be hierarchical, and to represent this, SVG::Graph::Frame
75 instances can themselves contain subframes. SVG::Graph::Frame can
76 contain:
77  
78 - multiple subframes as instances of SVG::Graph::Frame
79 - a single SVG::Graph::Data instance
80 - multiple SVG::Graph::Glyph instances with which to render
81 the attached SVG::Graph::Data instance, and all SVG::Graph::Data
82 instances attached to SVG::Graph::Frame subinstances
83  
84 See L<SVG::Graph::Frame> and L<SVG::Graph::Glyph> for details.
85  
86 =head2 ONE DATA SET
87  
88 1. create an SVG::Graph instance
89 2. create an SVG::Graph::Frame instance by calling
90 SVG::Graph::add_frame();
91 3. create an SVG::Graph::Data instance, containing
92 an SVG::Graph::Data::Datum instance for each data point.
93 4. Attach the SVG::Graph::Data instance to your SVG::Graph::Frame
94 using SVG::Graph::Frame::add_data();
95 5. Attach glyphs to the SVG::Graph::Frame instance using
96 SVG::Graph::Frame::add_glyph();
97 6. Call SVG::Graph::draw();
98  
99 =head2 MULTIPLE DATA SETS
100  
101 1. create an SVG::Graph instance
102 2. create an SVG::Graph::Frame instance by calling
103 SVG::Graph::add_frame();
104 3. create an SVG::Graph::Data instance, containing
105 an SVG::Graph::Data::Datum instance for each data point.
106 4. Attach the SVG::Graph::Data instance to your SVG::Graph::Frame
107 using SVG::Graph::Frame::add_data();
108 5. Attach glyphs to the SVG::Graph::Frame instance using
109 SVG::Graph::Frame::add_glyph();
110 6. repeat [2-5] for each additional data set to be added.
111 add_frame() can be called on SVG::Graph to add top-level data
112 sets, or SVG::Graph::Frame to add hierarchical data sets.
113 7. Call SVG::Graph::draw();
114  
115 =head1 FEEDBACK
116  
117 Send an email to the svg-graph-developers list. For more info,
118 visit the project page at http://www.sf.net/projects/svg-graph
119  
120 =head1 AUTHORS
121  
122 Allen Day, <allenday@ucla.edu>
123 Chris To, <crsto@ucla.edu>
124  
125 =head1 CONTRIBUTORS
126  
127 James Chen, <chenj@seas.ucla.edu>
128 Brian O'Connor, <boconnor@ucla.edu>
129  
130 =head1 SEE ALSO
131  
132 L<SVG>
133  
134 =cut
135  
136 7 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 7 ▶use SVG::Graph::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 7 our $VERSION = '0.02';
142  
143 =head2 new
144  
145 Title : new
146 Usage : my $graph = SVG::Graph->new(width=>600,
147 height=>600,
148 margin=>20);
149 Function: creates a new SVG::Graph object
150 Returns : a SVG::Graph object
151 Args : width => the width of the SVG
152 height => the height of the SVG
153 margin => margin for the root frame
154  
155  
156 =cut
157  
158 10 ▶sub new{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
159 10 my ($class,@args) = @_;
160  
161 10 my $self = bless {}, $class;
162 10 $self->init(@args);
163 10 return $self;
164 }
165  
166 =head2 init
167  
168 Title : init
169 Usage :
170 Function:
171 Example :
172 Returns :
173 Args :
174  
175  
176 =cut
177  
178 10 ▶sub init{
Subroutine init: called CC=3 SCAR=11.0
Pod documented
179 10 my($self, %args) = @_;
180  
181 10 foreach my $arg (keys %args){
182 42 my $meth = $arg;
183 42 $self->$meth($args{$arg});
184 }
185  
186 #allow passing of an existing SVG
187 10 ▶ if(!$self->svg){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->svg) 6 4 10
188 6 $self->svg(SVG->new(xmlns=>"http://www.w3.org/2000/svg",width=>$self->width,height=>$self->height));
189 }
190 }
191  
192 =head2 width
193  
194 Title : width
195 Usage : $obj->width($newval)
196 Function:
197 Example :
198 Returns : value of width (a scalar)
199 Args : on set, new value (a scalar or undef, optional)
200  
201  
202 =cut
203  
204 26 ▶sub width{
Subroutine width: called CC=2 SCAR=6.9
Pod documented
205 26 my $self = shift;
206  
207 26 ▶ return $self->{'width'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 10 16 26
208 16 return $self->{'width'};
209 }
210  
211 =head2 height
212  
213 Title : height
214 Usage : $obj->height($newval)
215 Function:
216 Example :
217 Returns : value of height (a scalar)
218 Args : on set, new value (a scalar or undef, optional)
219  
220  
221 =cut
222  
223 26 ▶sub height{
Subroutine height: called CC=2 SCAR=6.9
Pod documented
224 26 my $self = shift;
225  
226 26 ▶ return $self->{'height'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 10 16 26
227 16 return $self->{'height'};
228 }
229  
230 =head2 margin
231  
232 Title : margin
233 Usage : $obj->margin($newval)
234 Function:
235 Example :
236 Returns : value of margin (a scalar)
237 Args : on set, new value (a scalar or undef, optional)
238  
239  
240 =cut
241  
242 20 ▶sub margin{
Subroutine margin: called CC=2 SCAR=6.9
Pod documented
243 20 my $self = shift;
244  
245 20 ▶ return $self->{'margin'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 10 10 20
246 10 return $self->{'margin'};
247 }
248  
249 =head2 svg
250  
251 Title : svg
252 Usage : $obj->svg($newval)
253 Function:
254 Example :
255 Returns : value of svg (a scalar)
256 Args : on set, new value (a scalar or undef, optional)
257  
258  
259 =cut
260  
261 40 ▶sub svg{
Subroutine svg: called CC=2 SCAR=6.9
Pod documented
262 40 my $self = shift;
263  
264 40 ▶ return $self->{'svg'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 10 30 40
265 30 return $self->{'svg'};
266 }
267  
268 =head2 add_frame
269  
270 Title : add_frame
271 Usage : my $frame = $graph->add_frame
272 Function: adds a Frame to the current Graph
273 Returns : a SVG::Graph::Frame object
274 Args : a hash. usable keys:
275 frame_transform (optional)
276 'top' default orientation
277 'bottom' rotates graph 180 deg (about the center)
278 'right' points top position towards right
279 'left' points top position towards left
280  
281 =cut
282  
283 10 ▶sub add_frame{
Subroutine add_frame: called CC=6 SCAR=18.1
Pod documented
284 10 my ($self,%args) = @_;
285  
286 10 ▶ my $margin = $self->margin || 0;
Condition50%1/2
$self->margin || 0
l!l
100
MC/DC0%0/1
$self->margin || 0
$self->margin
Truth table50%1/2
A
A= $self->margin
Aresult
11
00
287 10 ▶ my $height = $self->height || 0;
Condition50%1/2
$self->height || 0
l!l
100
MC/DC0%0/1
$self->height || 0
$self->height
Truth table50%1/2
A
A= $self->height
Aresult
11
00
288 10 ▶ my $width = $self->width || 0;
Condition50%1/2
$self->width || 0
l!l
100
MC/DC0%0/1
$self->width || 0
$self->width
Truth table50%1/2
A
A= $self->width
Aresult
11
00
289 10 ▶ my $xoffset = $self->xoffset || 0;
Condition100%2/2
$self->xoffset || 0
l!l
28
MC/DC100%1/1
$self->xoffset || 0
$self->xoffset
Truth table100%2/2
A
A= $self->xoffset
Aresult
11
00
290 10 ▶ my $yoffset = $self->yoffset || 0;
Condition100%2/2
$self->yoffset || 0
l!l
28
MC/DC100%1/1
$self->yoffset || 0
$self->yoffset
Truth table100%2/2
A
A= $self->yoffset
Aresult
11
00
291  
292 my $frame = SVG::Graph::Frame->new(svg=>$self,
293 xoffset=>$xoffset + $margin,
294 yoffset=>$yoffset + $margin,
295 xsize=>$width - (2 * $margin),
296 ysize=>$height - (2 * $margin),
297 frame_transform=>$args{frame_transform}
298 10 );
299  
300 #print STDERR Dumper($frame);
301  
302 10 push @{$self->{frames}}, $frame;
303 10 return $frame;
304 }
305  
306 =head2 frames
307  
308 Title : frames
309 Usage : get/set
310 Function:
311 Example :
312 Returns :
313 Args :
314  
315  
316 =cut
317  
318 10 ▶sub frames{
Subroutine frames: called CC=2 SCAR=7.1
Pod documented
319 10 my ($self,@args) = @_;
320  
321 10 ▶ return $self->{frames} ? @{$self->{frames}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'frames'} ? : 10 0 10
322 }
323  
324 =head2 xoffset
325  
326 Title : xoffset
327 Usage : $obj->xoffset($newval)
328 Function:
329 Example :
330 Returns : value of xoffset (a scalar)
331 Args : on set, new value (a scalar or undef, optional)
332  
333  
334 =cut
335  
336 14 ▶sub xoffset{
Subroutine xoffset: called CC=2 SCAR=6.9
Pod documented
337 14 my $self = shift;
338  
339 14 ▶ return $self->{'xoffset'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 10 14
340 10 return $self->{'xoffset'};
341 }
342  
343 =head2 yoffset
344  
345 Title : yoffset
346 Usage : $obj->yoffset($newval)
347 Function:
348 Example :
349 Returns : value of yoffset (a scalar)
350 Args : on set, new value (a scalar or undef, optional)
351  
352  
353 =cut
354  
355 14 ▶sub yoffset{
Subroutine yoffset: called CC=2 SCAR=6.9
Pod documented
356 14 my $self = shift;
357  
358 14 ▶ return $self->{'yoffset'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 10 14
359 10 return $self->{'yoffset'};
360 }
361  
362 =head2 draw
363  
364 Title : draw
365 Usage : $graph=>draw
366 Function: depends on child glyph implementations
367 Returns : xmlifyied SVG object
368 Args : none
369  
370  
371 =cut
372  
373 10 ▶sub draw{
Subroutine draw: called CC=2 SCAR=6.9
Pod documented
374 10 my ($self,@args) = @_;
375  
376 10 foreach my $frame ($self->frames){
377 10 $frame->draw;
378 }
379  
380 10 return $self->svg->xmlify;
381 }
382  
383  
384 7 1;
385 __END__
« Graph/Data.pm ↑ Summary Graph/Data/Tree.pm »