blib/lib/SVG/Plot.pm

filter: Statementstmt 84.1% 101 / 120 Branchbran 60.0% 30 / 50 Conditioncond 66.6% 22 / 33 MC/DCmcdc 42.1% 8 / 19 Subroutinesub 100% 9 / 9 Podpod 33.3% 1 / 3 totaltotal 73.0% 171 / 234 CC 47 SCAR 31.7
CC
47
Cov
75%
_plot_pointset
31.6
plot
29.0
work_out_grid
9.4
CRAP 23.8
↑ Summary
1 =head1 NAME
2  
3 SVG::Plot - a simple module to take one or more sets of x,y points and plot them on a plane
4  
5 =head1 SYNOPSIS
6  
7 use SVG::Plot;
8 my $points = [ [0, 1, 'http://uri/'], [2, 3, '/uri/foo.png'] ];
9 my $plot = SVG::Plot->new(
10 points => $points,
11 debug => 0,
12 scale => 0.025,
13 max_width => 800,
14 max_height => 400,
15 point_size => 3,
16 point_style => {
17 fill => 'blue',
18 stroke => 'yellow',
19 },
20 line => 'follow',
21 margin => 6,
22 );
23  
24 # -- or --
25 $plot->points($points);
26 $plot->scale(4);
27  
28 print $plot->plot;
29  
30 =head1 DESCRIPTION
31  
32 a very simple module which allows you to give one or more sets of points [x co-ord, y co-ord and optional http uri]) and plot them in SVG.
33  
34 $plot->points($points) where $points is a reference to an array of array references.
35  
36 see B<new> for a list of parameters you can give to the plot. (overriding the styles on the ponts; sizing a margin; setting a scale; optionally drawing a line ( line => 'follow' ) between the points in the order they are specified.
37  
38 =cut
39  
40 package SVG::Plot;
41  
42 3 our $VERSION = '0.06';
43 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use Algorithm::Points::MinimumDistance;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 3 ▶use Class::MethodMaker new_hash_init => 'new', get_set => [ qw( debug grid scale points pointsets image point_style point_size min_point_size max_point_size margin line line_style max_width max_height svg_options) ];
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 =head1 METHODS
51  
52 =over 4
53  
54 =item B<new>
55  
56 use SVG::Plot;
57  
58 # Simple use - single set of points, all in same style.
59 my $points = [ [0, 1, 'http://uri/'], [2, 3, '/uri/foo.png'] ];
60 my $plot = SVG::Plot->new(
61 points => \@points,
62 point_size => 3,
63 point_style => {
64 fill => 'blue',
65 stroke => 'yellow',
66 },
67 line => 'follow',
68 debug => 0,
69 scale => 0.025,
70 max_width => 800,
71 max_height => 400,
72 margin => 6,
73 );
74  
75 # Prepare to plot two sets of points, in distinct styles.
76 my $pubs = [
77 [ 522770, 179023, "http://example.com/?Andover_Arms" ],
78 [ 522909, 178232, "http://example.com/?Blue Anchor" ] ];
79 my $stations = [
80 [ 523474, 178483, "http://example.com/?Hammersmith" ] ];
81 my $pointsets = [ { points => $pubs,
82 point_size => 3,
83 point_style => { fill => "blue" }
84 },
85 { points => $stations,
86 point_size => 5,
87 point_style => { fill => "red" }
88 } ];
89 my $plot = SVG::Plot->new(
90 pointsets => $pointsets,
91 scale => 0.025,
92 max_width => 800,
93 max_height => 400,
94 );
95  
96 To pass options through to L<SVG>, use the C<svg_options> parameter:
97  
98 SVG::Plot->new( points => $points,
99 svg_options => { -nocredits => 1 }
100 );
101  
102 You can define the boundaries of the plot:
103  
104 SVG::Plot->new(
105 grid => { min_x => 1,
106 min_y => 2,
107 max_x => 15,
108 max_y => 16 }
109 );
110  
111 or
112  
113 $plot->grid($grid)
114  
115 This is like a viewbox onto the plane of the plot. If it's not
116 specified, the module works out the viewbox from the highest and
117 lowest X and Y co-ordinates in the list(s) of points.
118  
119 Note that the actual margin will be half of the value set
120 in C<margin>, since half of it goes to each side.
121  
122 If C<max_width> and/or C<max_height> is set then C<scale> will be
123 reduced if necessary in order to keep the width down.
124  
125 If C<debug> is set to true then debugging information is emitted as
126 warnings.
127  
128 If C<point_size> is set to C<AUTO> then
129 L<Algorithm::Points::MinimumDistance> will be used to make the point
130 circles as large as possible without overlapping, within the
131 constraints of C<min_point_size> (which defaults to 1) and
132 C<max_point_size> (which defaults to 10). Note that if you have multiple
133 pointsets then the point circle sizes will be worked out I<per set>.
134  
135 All arguments have get_set accessors like so:
136  
137 $plot->point_size(3);
138  
139 The C<point_size>, C<point_style> attributes of the SVG::Plot object
140 will be used as defaults for any pointsets that don't have their own
141 style set.
142  
143 =cut
144  
145 =item B<plot>
146  
147 print $plot->plot;
148  
149 C<plot> will croak if the object has a C<max_width> or C<max_height>
150 attribute that is smaller than its C<margin> attribute, since this is
151 impossible.
152  
153 =cut
154  
155 5 ▶sub plot {
Subroutine plot: called CC=18 SCAR=33.7
Pod documented
156 5 my $self = shift;
157 5 my $points = $self->points;
158 5 my $pointsets = $self->pointsets;
159 5 ▶ croak "no points to plot!" unless ( $points or $pointsets );
Branch: 1 branch
BranchTrueFalseTotal
unless $points or $pointsets 0 5 5
Condition33%1/3
$points or $pointsets
l!l&&r!l&&!r
500
MC/DC0%0/2
$points or $pointsets
$points $pointsets
Truth table33%1/3
A or B
A= $pointsB= $pointsets
ABresult
1X1
011
000
160 5 my $grid = $self->grid;
161  
162 5 ▶ if (not $grid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($grid) 5 0 5
163 5 $grid = $self->work_out_grid;
164 }
165  
166 5 ▶ my $scale = $self->scale || 10;
Condition100%2/2
$self->scale || 10
l!l
23
MC/DC100%1/1
$self->scale || 10
$self->scale
Truth table100%2/2
A
A= $self->scale
Aresult
11
00
167 5 ▶ my $m = $self->margin || 10;
Condition50%1/2
$self->margin || 10
l!l
05
MC/DC0%0/1
$self->margin || 10
$self->margin
Truth table50%1/2
A
A= $self->margin
Aresult
11
00
168
169 # Reduce scale if necessary to fit to width constraint.
170 5 ▶ if ( $self->max_width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_width) 1 4 5
171 1 my $max_plot_width = $self->max_width - $m; # Account for margin
172 1 ▶ croak "max_width must be larger than margin"
Branch: 1 branch
BranchTrueFalseTotal
if $max_plot_width <= 0 0 1 1
173 if $max_plot_width <= 0;
174 1 my $x_extent = $grid->{max_x} - $grid->{min_x};
175 1 my $max_width_scale = $max_plot_width / $x_extent;
176 1 ▶ $scale = $max_width_scale if $scale > $max_width_scale;
Branch: 1 branch
BranchTrueFalseTotal
if $scale > $max_width_scale 1 0 1
177 }
178  
179 # Reduce scale further if necessary to fit to height constraint.
180 5 ▶ if ( $self->max_height ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_height) 0 5 5
181 0 my $max_plot_height = $self->max_height - $m; # Account for margin
182 0 ▶ croak "max_height must be larger than margin"
Branch: 1 branch
BranchTrueFalseTotal
if $max_plot_height <= 0 0 0 0
183 if $max_plot_height <= 0;
184 0 my $y_extent = $grid->{max_y} - $grid->{min_y};
185 0 my $max_height_scale = $max_plot_height / $y_extent;
186 0 ▶ $scale = $max_height_scale if $scale > $max_height_scale;
Branch: 1 branch
BranchTrueFalseTotal
if $scale > $max_height_scale 0 0 0
187 }
188  
189 5 my $h = int(($grid->{max_y} - $grid->{min_y})*$scale);
190 5 my $w = int(($grid->{max_x} - $grid->{min_x})*$scale);
191
192 my $svg = SVG->new(
193 width => $w + $m,
194 height => $h + $m,
195 5 ▶ %{ $self->svg_options || {} },
Condition100%2/2
$self->svg_options || {}
l!l
14
MC/DC100%1/1
$self->svg_options || {}
$self->svg_options
Truth table100%2/2
A
A= $self->svg_options
Aresult
11
00
196 );
197  
198 5 ▶ if (my $map = $self->image) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $map = $self->image) 0 5 5
199 0 my $img = $svg->image(
200 x=>0, y=>0,
201 '-href'=>$map, #may also embed SVG, e.g. "image.svg"
202 id=>'image_1'
203 );
204 }
205  
206 # Process each pointset.
207 5 my @pointset_data;
208  
209 5 ▶ if ( $self->points ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->points) 5 0 5
210 5 push @pointset_data, { points => $self->points,
211 point_size => $self->point_size,
212 min_point_size => $self->min_point_size,
213 max_point_size => $self->max_point_size,
214 point_style => $self->point_style,
215 line => $self->line,
216 line_style => $self->line_style };
217 }
218  
219 5 ▶ foreach my $pointset ( @{$self->pointsets || []} ) {
Condition50%1/2
$self->pointsets || []
l!l
05
MC/DC0%0/1
$self->pointsets || []
$self->pointsets
Truth table50%1/2
A
A= $self->pointsets
Aresult
11
00
220 0 push @pointset_data, $pointset;
221 }
222  
223 5 my %defaults = ( point_size => $self->point_size,
224 min_point_size => $self->min_point_size,
225 max_point_size => $self->max_point_size,
226 point_style => $self->point_style );
227  
228 5 foreach my $dataset ( @pointset_data ) {
229 5 $self->_plot_pointset( svg => $svg,
230 margin => $m,
231 grid => $grid,
232 scale => $scale,
233 %defaults, # can be overridden by %$dataset
234 %$dataset );
235 }
236  
237 5 return $svg->xmlify;
238 }
239  
240 # Adds a pointset to the SVG plot - pass in args svg, margin, grid, scale,
241 # points, point_size, min_point_size, max_point_size, point_style,
242 # line, line_style.
243 5 ▶sub _plot_pointset {
Subroutine _plot_pointset: called CC=22 SCAR=34.5
244 5 my ($self, %args) = @_;
245 5 ▶ my $points = $args{points} or croak "no points in pointset!";
Branch: 1 branch
BranchTrueFalseTotal
my $points = $args{"points"} or croak("no points in pointset!") 5 0 5
246 5 ▶ scalar @$points or croak "no points in pointset!";
Branch: 1 branch
BranchTrueFalseTotal
scalar @$points or croak("no points in pointset!") 5 0 5
247 5 ▶ my $svg = $args{svg} or croak "no SVG object passed";
Branch: 1 branch
BranchTrueFalseTotal
my $svg = $args{"svg"} or croak("no SVG object passed") 5 0 5
248 5 ▶ my $scale = $args{scale} or croak "no scale passed";
Branch: 1 branch
BranchTrueFalseTotal
my $scale = $args{"scale"} or croak("no scale passed") 5 0 5
249 5 ▶ my $point_style = $args{point_style} || { stroke => 'red',
Condition50%1/2
$args{'point_style'} || {'stroke', 'red', 'fill', 'white'}
l!l
05
MC/DC0%0/1
$args{'point_style'} || {'stroke', 'red', 'fill', 'white'}
$args{'point_style'}
Truth table50%1/2
A
A= $args{'point_style'}
Aresult
11
00
250 fill => 'white' };
251  
252 5 my $z = $svg->tag( 'g',
253 id => 'group_'.$self->random_id,
254 style => $point_style
255 );
256  
257 5 my $point_size = $args{point_size};
258 5 ▶ if ( $point_size && $point_size eq "AUTO" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($point_size and $point_size eq "AUTO") 4 1 5
Condition66%2/3
$point_size and $point_size eq "AUTO"
!ll&&!rl&&r
104
MC/DC50%1/2
$point_size and $point_size eq "AUTO"
$point_size $point_size eq "AUTO"
Truth table66%2/3
A and B
A= $point_sizeB= $point_size eq "AUTO"
ABresult
0X0
100
111
259 4 ▶ my $min_size = $args{min_point_size} || 1;
Condition100%2/2
$args{'min_point_size'} || 1
l!l
13
MC/DC100%1/1
$args{'min_point_size'} || 1
$args{'min_point_size'}
Truth table100%2/2
A
A= $args{'min_point_size'}
Aresult
11
00
260 4 ▶ my $max_size = $args{max_point_size} || 10;
Condition100%2/2
$args{'max_point_size'} || 10
l!l
22
MC/DC100%1/1
$args{'max_point_size'} || 10
$args{'max_point_size'}
Truth table100%2/2
A
A= $args{'max_point_size'}
Aresult
11
00
261 # Make sure we don't send URIs to A::P::MD
262 4 my @coords = map { [ $_->[0], $_->[1] ] } @$points;
263 4 my $boxsize = 1 + sprintf("%d", $max_size/$scale);
264 4 my $dists = Algorithm::Points::MinimumDistance->new(
265 points => \@coords,
266 boxsize => $boxsize );
267 4 my $min_dist = $dists->min_distance;
268 4 my $auto_size = sprintf("%d", $scale*$min_dist/2);
269 4 $point_size = $auto_size;
270 4 ▶ if ( $min_size and $point_size < $min_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_size and $point_size < $min_size) 1 3 4
Condition66%2/3
$min_size and $point_size < $min_size
!ll&&!rl&&r
031
MC/DC50%1/2
$min_size and $point_size < $min_size
$min_size $point_size < $min_size
Truth table66%2/3
A and B
A= $min_sizeB= $point_size < $min_size
ABresult
0X0
100
111
271 1 $point_size = $min_size;
272 }
273 4 ▶ if ( $max_size and $point_size > $max_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size and $point_size > $max_size) 1 3 4
Condition66%2/3
$max_size and $point_size > $max_size
!ll&&!rl&&r
031
MC/DC50%1/2
$max_size and $point_size > $max_size
$max_size $point_size > $max_size
Truth table66%2/3
A and B
A= $max_sizeB= $point_size > $max_size
ABresult
0X0
100
111
274 1 $point_size = $max_size;
275 }
276 }
277  
278 5 ▶ $point_size ||= 3;
Condition100%2/2
$point_size ||= 3
l!l
41
MC/DC100%1/1
$point_size ||= 3
$point_size
Truth table100%2/2
A
A= $point_size
Aresult
11
00
279 5 my $plotted;
280  
281 5 foreach (@$points) {
282 # adding a margin ...
283 17 my $halfm = $args{margin} / 2;
284  
285 17 my ($x,$y) = ($_->[0],$_->[1]);
286 17 ▶ my $href = $_->[2] || $self->random_id;
Condition66%2/3
$_->[2] || $self->random_id
l!l&&r!l&&!r
2150
MC/DC0%0/2
$_->[2] || $self->random_id
$_->[2] $self->random_id
Truth table66%2/3
A || B
A= $_->[2]B= $self->random_id
ABresult
1X1
011
000
287
288 # svg is upside-down
289 17 $x = int(($x - $args{grid}->{min_x})*$scale) + $halfm;
290 17 $y = int(($args{grid}->{max_y} - $y)*$scale) + $halfm;
291  
292 17 push @$plotted, [$x,$y,$href];
293 17 my $id = $self->random_id;
294 17 ▶ warn("anchor_$id") if $self->debug;;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 17 17
295  
296 17 $z->anchor(id => "anchor_".$id,
297 -href => $href,
298 -target => 'new_window_0')->circle(
299 cx => $x, cy => $y,
300 r => $point_size,
301 id => 'dot_'.$id,
302 );
303 }
304  
305 5 ▶ if (my $line = $args{line}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $args{"line"}) 0 5 5
306 0 my $style = $args{line_style};
307 0 ▶ $style ||= { 'stroke-width' => 2, stroke => 'blue' };
Condition0%0/2
$style ||= {"stroke-width", 2, "stroke", "blue"}
l!l
00
MC/DC0%0/1
$style ||= {"stroke-width", 2, "stroke", "blue"}
$style
Truth table0%0/2
A
A= $style
Aresult
11
00
308  
309 0 ▶ if ($line eq 'follow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "follow") 0 0 0
310 0 for my $n (0..($#{$plotted}-1)) {
311 0 my $p1 = $plotted->[$n];
312 0 my $p2 = $plotted->[$n+1];
313 0 my $tag = $svg->line(
314 id => $self->random_id,
315 x1 => $p1->[0], y1 => $p1->[1],
316 x2 => $p2->[0], y2 => $p2->[1],
317 style => $style
318 );
319 }
320 }
321 }
322 }
323  
324 5 ▶sub work_out_grid {
Subroutine work_out_grid: called CC=9 SCAR=22.4
Pod undocumented
325 5 my $self = shift;
326 5 my $all_points = $self->points;
327 5 my $pointsets = $self->pointsets;
328  
329 5 ▶ if ( $pointsets ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsets) 0 5 5
330 0 foreach my $pointset ( map { $_->{points} } @$pointsets ) {
331 0 foreach my $point ( @$pointset ) {
332 0 push @$all_points, $point;
333 }
334 }
335 }
336  
337 5 my $start = $all_points->[0];
338 5 my ($lx,$ly,$hx,$hy);
339 5 $lx = $start->[0];
340 5 $hx = $lx;
341 5 $ly = $start->[1];
342 5 $hy = $ly;
343  
344 5 foreach (@$all_points) {
345  
346 17 ▶ $lx = $_->[0] if ($_->[0] <= $lx);
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] <= $lx 5 12 17
347 17 ▶ $ly = $_->[1] if ($_->[1] <= $ly);
Branch: 1 branch
BranchTrueFalseTotal
if $_->[1] <= $ly 10 7 17
348 17 ▶ $hx = $_->[0] if ($_->[0] >= $hx);
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] >= $hx 15 2 17
349 17 ▶ $hy = $_->[1] if ($_->[1] >= $hy);
Branch: 1 branch
BranchTrueFalseTotal
if $_->[1] >= $hy 15 2 17
350 }
351 return {
352 5 min_x => $lx,
353 max_x => $hx,
354 min_y => $ly,
355 max_y => $hy
356 };
357 }
358  
359 37 ▶sub random_id {
Subroutine random_id: called CC=1 SCAR=0.0
Pod undocumented
360 37 my @t = (0..9);
361 37 return '_:id'.join '', (map { @t[rand @t] } 0..6);
362 }
363  
364 =back
365  
366 =cut
367
368 3 1;
369  
370 =head1 NOTES
371  
372 this is an early draft, released mostly so Kake can use it in OpenGuides without having non-CPAN dependencies.
373  
374 for an example of what one should be able to do with this, see http://space.frot.org/rdf/tubemap.svg ... a better way of making meta-information between the lines, some kind of matrix drawing; cf the grubstreet link below, different styles according to locales, sets, conceptual contexts...
375  
376 it would be fun to supply access to different plotting algorithms, not just for the cartesian plane; particularly the buckminster fuller dymaxion map; cf Geo::Dymaxion, when that gets released (http://iconocla.st/hacks/dymax/ )
377  
378 to see work in progress, http://un.earth.li/~kake/cgi-bin/plot2.cgi?cat=Pubs&cat=Restaurants&cat=Tube&colour_diff_this=loc&action=display
379  
380 =head1 BUGS
381  
382 possibly. this is alpha in terms of functionality, beta in terms of code; the API won't break backwards, though.
383  
384  
385 =head1 AUTHOR
386  
387 Jo Walsh ( jo@london.pm.org )
388 Kate L Pugh ( kake@earth.li )
389  
390 =cut
391  
392  
393  
↑ Summary