blib/lib/SVG/Timeline.pm

filter: Statementstmt 97.0% 97 / 100 Branchbran 70.0% 7 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 91.6% 22 / 24 Podpod 100% 9 / 9 totaltotal 93.1% 136 / 146 CC 9 SCAR 5.5
CC
9
Cov
94%
_build_svg
3.1
draw
3.0
draw_grid
3.0
CRAP 1.7
« blib/lib/SVG/Timeline/Event.pm ↑ Summary bin/timeline »
1 =head1 NAME
2  
3 SVG::Timeline - Create SVG timeline charts
4  
5 =head1 SYNOPSIS
6  
7 use SVG::Timeline;
8  
9 my $tl = SVG::Timeline->new;
10  
11 $tl->add_event({
12 start => 1914,
13 end => 1918,
14 text => 'World War I',
15 });
16  
17 $tl->add_event({
18 start => '1939-09-01', # 1 Sep 1939
19 end => '1945-05-08', # 8 May 1945
20 text => 'World War II',
21 });
22  
23 print $tl->draw;
24  
25 =head1 DESCRIPTION
26  
27 This module allows you to easily create SVG documents that represent timelines.
28  
29 An SVG timeline is a diagram with a list of years across the top and a
30 number of bars below. Each bar represents a period of time.
31  
32 =head1 METHODS
33  
34 =head2 new(\%options)
35  
36 Creates and returns a new SVG::Timeline object.
37  
38 Takes an optional hash reference containing configuration options. You
39 probably don't need any of these, but the following options are supported:
40  
41 =over 4
42  
43 =cut
44  
45 package SVG::Timeline;
46  
47 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 7 our $VERSION = '0.2.1';
50  
51 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 7 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 7 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 7 ▶use List::Util qw[min max];
Subroutine BEGIN: called CC=1 SCAR=0.0
55 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 7 ▶use SVG::Timeline::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 7 subtype 'ArrayOfEvents', as 'ArrayRef[SVG::Timeline::Event]';
60  
61 coerce 'ArrayOfEvents',
62 from 'HashRef',
63 0 ▶ via { [ SVG::Timeline::Event->new($_) ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 from 'ArrayRef[HashRef]',
65 0 ▶ via { [ map { SVG::Timeline::Event->new($_) } @$_ ] };
Subroutine __ANON__: not called CC=1 SCAR=2.6
66  
67 =item * events - a reference to an array containing events. Events are hash
68 references. See L<add_event> below for the format of events.
69  
70 =cut
71  
72 has events => (
73 traits => ['Array'],
74 isa => 'ArrayOfEvents',
75 is => 'rw',
76 coerce => 1,
77 9 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 7 handles => {
79 all_events => 'elements',
80 add_event => 'push',
81 count_events => 'count',
82 has_events => 'count',
83 },
84 );
85  
86 15 ▶around 'add_event' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 15 my $orig = shift;
88 15 my $self = shift;
89  
90 15 $self->_clear_viewbox;
91 15 $self->_clear_svg;
92  
93 15 my $index = $self->count_events + 1;
94 15 $_[0]->{index} = $index;
95  
96 15 $self->$orig(@_);
97 7 };
98  
99 =item * width - the width of the output in any format used by SVG. The default
100 is 100%.
101  
102 =cut
103  
104 7 has width => (
105 is => 'ro',
106 isa => 'Str',
107 default => '100%',
108 );
109  
110 =item * height - the height of the output in any format used by SVG. The
111 default is 100%.
112  
113 =cut
114  
115 7 has height => (
116 is => 'ro',
117 isa => 'Str',
118 default => '100%',
119 );
120  
121 =item * aspect_ratio - the default is 16/9.
122  
123 =cut
124  
125 7 has aspect_ratio => (
126 is => 'ro',
127 isa => 'Num',
128 default => 16/9,
129 );
130  
131 =item * viewport - a viewport definition (which is a space separated list of
132 four integers. Unless you know what you're doing, it's probably best to leave
133 the class to work this out for you.
134  
135 =cut
136  
137 7 has viewbox => (
138 is => 'ro',
139 isa => 'Str',
140 lazy_build => 1,
141 clearer => '_clear_viewbox',
142 );
143  
144 9 ▶sub _build_viewbox {
Subroutine _build_viewbox: called CC=1 SCAR=0.0
145 9 my $self = shift;
146 9 return join ' ',
147 $self->min_year * $self->units_per_year,
148 0,
149 $self->years * $self->units_per_year,
150 ($self->bar_height * $self->events_in_timeline) + $self->bar_height
151 + (($self->count_events - 1) * $self->bar_height * $self->bar_spacing);
152 }
153  
154 =item * svg - an instance of the SVG class that is used to generate the final
155 SVG output. Unless you're using a subclass of this class for some reason,
156 there is no reason to set this manually.
157  
158 =cut
159  
160 7 has svg => (
161 is => 'ro',
162 isa => 'SVG',
163 lazy_build => 1,
164 clearer => '_clear_svg',
165 handles => [qw[xmlify line text rect cdata]],
166 );
167  
168 9 ▶sub _build_svg {
Subroutine _build_svg: called CC=3 SCAR=11.2
169 9 my $self = shift;
170  
171 9 ▶ $_->{end} //= (localtime)[5] + 1900 foreach $self->all_events;
Condition50%1/2
$_->{"end"} //= (localtime)[5] + 1900
l!l
170
MC/DC0%0/1
$_->{"end"} //= (localtime)[5] + 1900
$_->{"end"}
Truth table50%1/2
A
A= $_->{"end"}
Aresult
11
00
172  
173 9 return SVG->new(
174 width => $self->width,
175 height => $self->height,
176 viewBox => $self->viewbox,
177 );
178 }
179  
180 =item * default_colour - the colour that is used to fill the timeline
181 blocks. This should be defined in the RGB format used by SVG. For example,
182 red would be 'RGB(255,0,0)'.
183  
184 =cut
185  
186 7 has default_colour => (
187 is => 'ro',
188 isa => 'Str',
189 lazy_build => 1,
190 );
191  
192 6 ▶sub _build_default_colour {
Subroutine _build_default_colour: called CC=1 SCAR=0.0
193 6 return 'rgb(255,127,127)';
194 }
195  
196 =item * years_per_grid - the number of years between vertical grid lines
197 in the output. The default of 10 should be fine unless your timeline covers
198 a really long timespan.
199  
200 =cut
201  
202 # The number of years between vertical grid lines
203 7 has years_per_grid => (
204 is => 'ro',
205 isa => 'Int',
206 default => 10, # One decade by default
207 );
208  
209 =item * bar_height - the height of an individual timeline bar.
210  
211 =cut
212  
213 7 has bar_height => (
214 is => 'ro',
215 isa => 'Int',
216 default => 50,
217 );
218  
219 =item * bar_spacing - the height if the vertical space between bars (expresssed
220 as a decimal fraction of the bar height).
221  
222 =cut
223  
224 7 has bar_spacing => (
225 is => 'ro',
226 isa => 'Num',
227 default => 0.25,
228 );
229  
230 =item * decade_line_colour - the colour of the grid lines.
231  
232 =cut
233  
234 7 has decade_line_colour => (
235 is => 'ro',
236 isa => 'Str',
237 default => 'rgb(127,127,127)',
238 );
239  
240 =item * bar_outline_colour - the colour that is used for the outline of the
241 timeline bars.
242  
243 =cut
244  
245 7 has bar_outline_colour => (
246 is => 'ro',
247 isa => 'Str',
248 default => 'rgb(0,0,0)',
249 );
250  
251 =back
252  
253 =head2 events_in_timeline
254  
255 The number of events that we need to make space for in the timeline. This
256 is generally just the number of events that we have added to the timeline, but
257 this method is here in case subclasses want t odo something different.
258  
259 =cut
260  
261 217 ▶sub events_in_timeline {
Subroutine events_in_timeline: called CC=1 SCAR=0.0
Pod documented
262 217 return $_[0]->count_events;
263 }
264  
265 =head2 add_event
266  
267 Takes a hash reference with event details and adds an L<SVG::Timeline::Event>
268 to the timeline. The following details are supported:
269  
270 =over 4
271  
272 =item * text - the name of the event that is displayed on the bar. This is required.
273  
274 =item * start - the start year of the event. It is a string of format C<YYYY-MM-DD>.
275 For example, C<2017-07-02> is the 2nd of July 2017. The month and day can be omitted
276 (in which case they are replaced with '01').
277  
278 =item * end - the end year of the event, requirements are the same as that of C<start>.
279
280 =item * colour - the colour that is used to fill the timeline block. This should be
281 defined in the RGB format used by SVG. For example, red would be 'RGB(255,0,0)'.
282 This is optional. If not provided, the C<default_color> is used.
283  
284 =back
285  
286 =head2 calculated_height
287  
288 The height of the timeline in "calculated units".
289  
290 =cut
291  
292 96 ▶sub calculated_height {
Subroutine calculated_height: called CC=1 SCAR=0.0
Pod documented
293 96 my $self = shift;
294  
295 # Number of events ...
296 96 my $calulated_height = $self->events_in_timeline;
297 # ... plus one for the header ...
298 96 $calulated_height++;
299 # ... multiplied by the bar height...
300 96 $calulated_height *= $self->bar_height;
301 # .. add spacing.
302 96 $calulated_height += $self->bar_height * $self->bar_spacing *
303 ($self->events_in_timeline - 1);
304  
305 96 return $calulated_height;
306 }
307  
308 =head2 calculated_width
309  
310 The width in "calulated units".
311  
312 =cut
313  
314 74 ▶sub calculated_width {
Subroutine calculated_width: called CC=1 SCAR=0.0
Pod documented
315 74 my $self = shift;
316  
317 74 return $self->calculated_height * $self->aspect_ratio;
318 }
319  
320 =head2 units_per_year
321  
322 The number of horizontal units that each year should take up.
323  
324 =cut
325  
326 74 ▶sub units_per_year {
Subroutine units_per_year: called CC=1 SCAR=0.0
Pod documented
327 74 my $self = shift;
328  
329 74 return $self->calculated_width / $self->years;
330 }
331  
332 =head2 draw_grid
333  
334 Method to draw the underlying grid.
335  
336 =cut
337  
338 8 ▶sub draw_grid{
Subroutine draw_grid: called CC=3 SCAR=11.0
Pod documented
339 8 my $self = shift;
340  
341 8 my $curr_year = $self->min_year;
342 8 my $units_per_year = $self->units_per_year;
343  
344 # Draw the grid lines
345 8 ▶ while ( $curr_year <= $self->max_year ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_year <= $self->max_year) 214 8 222
346 214 ▶ unless ( $curr_year % $self->years_per_grid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($curr_year % $self->years_per_grid) 22 192 214
347 22 $self->line(
348 x1 => $curr_year * $units_per_year,
349 y1 => 0,
350 x2 => $curr_year * $units_per_year,
351 y2 => $self->calculated_height,
352 stroke => $self->decade_line_colour,
353 stroke_width => 1
354 );
355 22 $self->text(
356 x => ($curr_year + 1) * $units_per_year,
357 y => 20,
358 'font-size' => $self->bar_height / 2
359 )->cdata($curr_year);
360 }
361 214 $curr_year++;
362 }
363  
364 $self->rect(
365 8 x => $self->min_year * $units_per_year,
366 y => 0,
367 width => $self->years * $units_per_year,
368 height => ($self->bar_height * ($self->events_in_timeline + 1))
369 + ($self->bar_height * $self->bar_spacing
370 * ($self->events_in_timeline - 1)),
371 stroke => $self->bar_outline_colour,
372 'stroke-width' => 1,
373 fill => 'none',
374 );
375  
376 8 return $self;
377 }
378  
379 =head2 draw
380  
381 Method to draw the timeline.
382  
383 =cut
384  
385 8 ▶sub draw {
Subroutine draw: called CC=3 SCAR=11.0
Pod documented
386 8 my $self = shift;
387 8 my %args = @_;
388  
389 8 ▶ croak "Can't draw a timeline with no events"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_events 0 8 8
390 unless $self->has_events;
391  
392 8 $self->draw_grid;
393  
394 8 my $curr_event_idx = 1;
395 8 foreach ($self->all_events) {
396  
397 15 $_->draw_on($self);
398  
399 15 $curr_event_idx++;
400 }
401  
402 8 return $self->xmlify;
403 }
404  
405 =head2 min_year
406  
407 Returns the minimum year from all the events in the timeline.
408  
409 =cut
410  
411 116 ▶sub min_year {
Subroutine min_year: called CC=2 SCAR=7.0
Pod documented
412 116 my $self = shift;
413 116 ▶ return unless $self->has_events;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_events 0 116 116
414 116 my @years = map { $_->start_year } $self->all_events;
415 116 return min(@years);
416 }
417  
418 =head2 max_year
419  
420 Returns the maximum year from all the events in the timeline.
421  
422 =cut
423  
424 328 ▶sub max_year {
Subroutine max_year: called CC=2 SCAR=7.0
Pod documented
425 328 my $self = shift;
426 328 ▶ return unless $self->has_events;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_events 0 328 328
427 328 my @years = map { $_->end_year } $self->all_events;
428 328 return max(@years);
429 }
430  
431 =head2 years
432  
433 The number of years that all the events in the timeline span.
434  
435 =cut
436  
437 91 ▶sub years {
Subroutine years: called CC=1 SCAR=0.0
Pod documented
438 91 my $self = shift;
439 91 return $self->max_year - $self->min_year;
440 }
441  
442 7 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
443 7 __PACKAGE__->meta->make_immutable;
444  
445 =head1 AUTHOR
446  
447 Dave Cross <dave@perlhacks.com>
448  
449 =head1 COPYRIGHT AND LICENCE
450  
451 Copyright (c) 2017, Magnum Solutions Ltd. All Rights Reserved.
452  
453 This library is free software; you can redistribute it and/or modify it
454 under the same terms as Perl itself.
455  
456 =cut
457  
458 7 1;
« blib/lib/SVG/Timeline/Event.pm ↑ Summary bin/timeline »