File Coverage

blib/lib/SVG/Timeline/Event.pm
Criterion Covered Total %
statement 23 23 100.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 1 2 50.0
total 33 37 89.1


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             SVG::Timelime::Event - A single event in an SVG timeline.
4              
5             =head1 SYNOPSIS
6              
7             See L<SVG::Timeline>.
8              
9             =cut
10              
11              
12             use 5.010;
13 2     2   40  
  2         6  
14             use Moose;
15 2     2   12 use Moose::Util::TypeConstraints;
  2         4  
  2         17  
16 2     2   13761 use Time::Piece;
  2         4  
  2         15  
17 2     2   4377  
  2         4  
  2         12  
18             coerce __PACKAGE__,
19             from 'HashRef',
20             via { __PACKAGE__->new($_) };
21              
22             # Chosen format: yyyy-mm-dd
23             subtype 'SVG::Timeline::DateStr',
24             as 'Str',
25             where { m/ \d{4}-\d{2}-\d{2} /ax };
26              
27             subtype 'SVG::Timeline::YearMonthStr',
28             as 'Str',
29             where { m/ \d{4}-\d{2} /ax };
30              
31             subtype 'SVG::Timeline::YearStr',
32             as 'Str',
33             where { m/ \d{4} /as };
34              
35             subtype 'SVG::Timeline::Num',
36             as 'Num';
37              
38             coerce 'SVG::Timeline::DateStr',
39             from 'SVG::Timeline::YearStr',
40             via { $_ . '-01-01' };
41              
42             coerce 'SVG::Timeline::DateStr',
43             from 'SVG::Timeline::YearMonthStr',
44             via { $_ . '-01' };
45              
46             coerce 'SVG::Timeline::Num',
47             from 'SVG::Timeline::DateStr',
48             via \&str2num;
49              
50             has index => (
51             is => 'ro',
52             isa => 'Int',
53             required => 1,
54             );
55              
56             has text => (
57             is => 'ro',
58             isa => 'Str',
59             required => 1,
60             );
61              
62             my ($datestr) = @_;
63              
64 2     2 0 26 my $date = Time::Piece->strptime($datestr, '%Y-%m-%d');
65              
66 2         11 return $date->year + ( $date->yday / ($date->is_leap_year ? 366 : 365) );
67             }
68 2 50       172  
69             has start => (
70             is => 'ro',
71             isa => 'SVG::Timeline::Num',
72             required => 1,
73             coerce => 1,
74             );
75              
76             has end => (
77             is => 'ro',
78             isa => 'SVG::Timeline::Num',
79             required => 1,
80             coerce => 1,
81             );
82              
83             has colour => (
84             is => 'ro',
85             isa => 'Maybe[Str]',
86             required => 0,
87             );
88              
89             =head1 METHODS
90              
91             =head2 draw_on($tl)
92              
93             Draw the event inside the given timeline object.
94              
95             =cut
96              
97             my $self = shift;
98             my ($tl) = @_;
99              
100 3     3 1 5 my $x = $self->start * $tl->units_per_year;
101 3         45 my $y = ($tl->bar_height * $self->index)
102             + ($tl->bar_height * $tl->bar_spacing
103 3         96 * ($self->index - 1));
104 3         76  
105             $tl->rect(
106             x => $x,
107             y => $y,
108 3   33     75 width => ($self->end - $self->start) * $tl->units_per_year,
109             height => $tl->bar_height,
110             fill => $self->colour // $tl->default_colour,
111             stroke => $tl->bar_outline_colour,
112             'stroke-width' => 1
113             );
114              
115             $tl->text(
116             x => ($x + $tl->bar_height * 0.2),
117             y => $y + $tl->bar_height * 0.8,
118 3         318 'font-size' => $tl->bar_height * 0.8,
119             )->cdata($self->text);
120             }
121              
122             no Moose;
123             __PACKAGE__->meta->make_immutable;
124              
125 2     2   1273 =head1 AUTHOR
  2         4  
  2         10  
126              
127             Dave Cross <dave@perlhacks.com>
128              
129             =head1 COPYRIGHT AND LICENCE
130              
131             Copyright (c) 2017, Magnum Solutions Ltd. All Rights Reserved.
132              
133             This library is free software; you can redistribute it and/or modify it
134             under the same terms as Perl itself.
135              
136             =cut
137              
138             1;