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