File Coverage

blib/lib/SVG/Graph.pm
Criterion Covered Total %
statement 57 57 100.0
branch 15 16 93.7
condition 7 10 70.0
subroutine 15 15 100.0
pod 11 11 100.0
total 105 109 96.3


line stmt bran cond sub pod time code
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 and L 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,
123             Chris To,
124              
125             =head1 CONTRIBUTORS
126              
127             James Chen,
128             Brian O'Connor,
129              
130             =head1 SEE ALSO
131              
132             L
133              
134             =cut
135              
136 7     7   123857 use SVG;
  7         146276  
  7         50  
137 7     7   13873 use SVG::Graph::Frame;
  7         24  
  7         600  
138              
139 7     7   65 use Data::Dumper;
  7         75  
  7         376  
140 7     7   36 use strict;
  7         10  
  7         5268  
141             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             sub new{
159 10     10 1 3448 my ($class,@args) = @_;
160              
161 10         34 my $self = bless {}, $class;
162 10         63 $self->init(@args);
163 10         151 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             sub init{
179 10     10 1 52 my($self, %args) = @_;
180              
181 10         39 foreach my $arg (keys %args){
182 42         64 my $meth = $arg;
183 42         130 $self->$meth($args{$arg});
184             }
185              
186             #allow passing of an existing SVG
187 10 100       49 if(!$self->svg){
188 6         22 $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             sub width{
205 26     26 1 39 my $self = shift;
206              
207 26 100       109 return $self->{'width'} = shift if @_;
208 16         74 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             sub height{
224 26     26 1 43 my $self = shift;
225              
226 26 100       92 return $self->{'height'} = shift if @_;
227 16         88 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             sub margin{
243 20     20 1 35 my $self = shift;
244              
245 20 100       107 return $self->{'margin'} = shift if @_;
246 10         40 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             sub svg{
262 40     40 1 3711 my $self = shift;
263              
264 40 100       122 return $self->{'svg'} = shift if @_;
265 30         225 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             sub add_frame{
284 10     10 1 929 my ($self,%args) = @_;
285              
286 10   50     36 my $margin = $self->margin || 0;
287 10   50     39 my $height = $self->height || 0;
288 10   50     30 my $width = $self->width || 0;
289 10   100     37 my $xoffset = $self->xoffset || 0;
290 10   100     32 my $yoffset = $self->yoffset || 0;
291              
292 10         120 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             );
299              
300             #print STDERR Dumper($frame);
301              
302 10         25 push @{$self->{frames}}, $frame;
  10         32  
303 10         32 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             sub frames{
319 10     10 1 24 my ($self,@args) = @_;
320              
321 10 50       46 return $self->{frames} ? @{$self->{frames}} : ();
  10         40  
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             sub xoffset{
337 14     14 1 21 my $self = shift;
338              
339 14 100       42 return $self->{'xoffset'} = shift if @_;
340 10         84 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             sub yoffset{
356 14     14 1 19 my $self = shift;
357              
358 14 100       45 return $self->{'yoffset'} = shift if @_;
359 10         73 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             sub draw{
374 10     10 1 20395 my ($self,@args) = @_;
375              
376 10         49 foreach my $frame ($self->frames){
377 10         57 $frame->draw;
378             }
379              
380 10         369 return $self->svg->xmlify;
381             }
382              
383              
384             1;
385             __END__