| 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 | 2 | ▶ | use 5.014; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 2 | our $VERSION = '0.1.3'; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 2 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 52 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 53 | 2 | ▶ | use SVG; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | 2 | ▶ | use List::Util qw[min max]; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 55 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 2 | ▶ | use SVG::Timeline::Event; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 2 | 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 | 2 | ▶ | default => sub { [] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 78 | 2 | handles => { | |||||||||||||||||||
| 79 | all_events => 'elements', | ||||||||||||||||||||
| 80 | add_event => 'push', | ||||||||||||||||||||
| 81 | count_events => 'count', | ||||||||||||||||||||
| 82 | has_events => 'count', | ||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | around 'add_event' => sub { | ||||||||||||||||||||
| 87 | 3 | ▶ | my $orig = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 88 | 3 | my $self = shift; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 3 | $self->_clear_viewbox; | |||||||||||||||||||
| 91 | 3 | $self->_clear_svg; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 3 | my $index = $self->count_events + 1; | |||||||||||||||||||
| 94 | 3 | $_[0]->{index} = $index; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 3 | $self->$orig(@_); | |||||||||||||||||||
| 97 | 2 | }; | |||||||||||||||||||
| 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 | 2 | 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 | 2 | 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 | 2 | 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 | 2 | has viewbox => ( | |||||||||||||||||||
| 138 | is => 'ro', | ||||||||||||||||||||
| 139 | isa => 'Str', | ||||||||||||||||||||
| 140 | lazy_build => 1, | ||||||||||||||||||||
| 141 | clearer => '_clear_viewbox', | ||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | sub _build_viewbox { | ||||||||||||||||||||
| 145 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_build_viewbox: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 146 | 2 | 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 | 2 | 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 | sub _build_svg { | ||||||||||||||||||||
| 169 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_build_svg: called CC=3 SCAR=12.0
| |||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 2 | ▶ | $_->{end} //= (localtime)[5] + 1900 foreach $self->all_events; | ||||||||||||||||||
|
Condition33%1/3
$_->{"end"} //= (localtime)[5] + 1900
MC/DC0%0/2
$_->{"end"} //= (localtime)[5] + 1900
$_->{"end"}
(localtime)[5] + 1900
Truth table33%1/3
A //= B
A= $_->{"end"}B= (localtime)[5] + 1900
| |||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 2 | 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 | 2 | has default_colour => ( | |||||||||||||||||||
| 187 | is => 'ro', | ||||||||||||||||||||
| 188 | isa => 'Str', | ||||||||||||||||||||
| 189 | lazy_build => 1, | ||||||||||||||||||||
| 190 | ); | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | sub _build_default_colour { | ||||||||||||||||||||
| 193 | 1 | ▶ | return 'rgb(255,127,127)'; | ||||||||||||||||||
|
Subroutine
_build_default_colour: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | 2 | 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 | 2 | 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 | 2 | 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 | 2 | 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 | 2 | 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 | sub events_in_timeline { | ||||||||||||||||||||
| 262 | 34 | ▶ | return $_[0]->count_events; | ||||||||||||||||||
|
Subroutine
events_in_timeline: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 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 | sub calculated_height { | ||||||||||||||||||||
| 293 | 15 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
calculated_height: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | # Number of events ... | ||||||||||||||||||||
| 296 | 15 | my $calulated_height = $self->events_in_timeline; | |||||||||||||||||||
| 297 | # ... plus one for the header ... | ||||||||||||||||||||
| 298 | 15 | $calulated_height++; | |||||||||||||||||||
| 299 | # ... multiplied by the bar height... | ||||||||||||||||||||
| 300 | 15 | $calulated_height *= $self->bar_height; | |||||||||||||||||||
| 301 | # .. add spacing. | ||||||||||||||||||||
| 302 | 15 | $calulated_height += $self->bar_height * $self->bar_spacing * | |||||||||||||||||||
| 303 | ($self->events_in_timeline - 1); | ||||||||||||||||||||
| 304 | |||||||||||||||||||||
| 305 | 15 | return $calulated_height; | |||||||||||||||||||
| 306 | } | ||||||||||||||||||||
| 307 | |||||||||||||||||||||
| 308 | =head2 calculated_width | ||||||||||||||||||||
| 309 | |||||||||||||||||||||
| 310 | The width in "calulated units". | ||||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | =cut | ||||||||||||||||||||
| 313 | |||||||||||||||||||||
| 314 | sub calculated_width { | ||||||||||||||||||||
| 315 | 11 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
calculated_width: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 316 | |||||||||||||||||||||
| 317 | 11 | 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 | sub units_per_year { | ||||||||||||||||||||
| 327 | 11 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
units_per_year: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 328 | |||||||||||||||||||||
| 329 | 11 | 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 | sub draw_grid{ | ||||||||||||||||||||
| 339 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
draw_grid: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 340 | |||||||||||||||||||||
| 341 | 1 | my $curr_year = $self->min_year; | |||||||||||||||||||
| 342 | 1 | my $units_per_year = $self->units_per_year; | |||||||||||||||||||
| 343 | |||||||||||||||||||||
| 344 | # Draw the grid lines | ||||||||||||||||||||
| 345 | 1 | ▶ | while ( $curr_year <= $self->max_year ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 346 | 40 | ▶ | unless ( $curr_year % $self->years_per_grid ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 347 | 4 | $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 | 4 | $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 | 40 | $curr_year++; | |||||||||||||||||||
| 362 | } | ||||||||||||||||||||
| 363 | |||||||||||||||||||||
| 364 | $self->rect( | ||||||||||||||||||||
| 365 | 1 | 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 | 1 | return $self; | |||||||||||||||||||
| 377 | } | ||||||||||||||||||||
| 378 | |||||||||||||||||||||
| 379 | =head2 draw | ||||||||||||||||||||
| 380 | |||||||||||||||||||||
| 381 | Method to draw the timeline. | ||||||||||||||||||||
| 382 | |||||||||||||||||||||
| 383 | =cut | ||||||||||||||||||||
| 384 | |||||||||||||||||||||
| 385 | sub draw { | ||||||||||||||||||||
| 386 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
draw: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 387 | 1 | my %args = @_; | |||||||||||||||||||
| 388 | |||||||||||||||||||||
| 389 | 1 | ▶ | croak "Can't draw a timeline with no events" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 390 | unless $self->has_events; | ||||||||||||||||||||
| 391 | |||||||||||||||||||||
| 392 | 1 | $self->draw_grid; | |||||||||||||||||||
| 393 | |||||||||||||||||||||
| 394 | 1 | my $curr_event_idx = 1; | |||||||||||||||||||
| 395 | 1 | foreach ($self->all_events) { | |||||||||||||||||||
| 396 | |||||||||||||||||||||
| 397 | 3 | $_->draw_on($self); | |||||||||||||||||||
| 398 | |||||||||||||||||||||
| 399 | 3 | $curr_event_idx++; | |||||||||||||||||||
| 400 | } | ||||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | 1 | 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 | sub min_year { | ||||||||||||||||||||
| 412 | 18 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
min_year: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 413 | 18 | ▶ | return unless $self->has_events; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 414 | 18 | my @years = map { int ($_->start) } $self->all_events; | |||||||||||||||||||
| 415 | 18 | 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 | sub max_year { | ||||||||||||||||||||
| 425 | 55 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
max_year: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||
| 426 | 55 | ▶ | return unless $self->has_events; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 427 | 55 | ▶ | my @years = map { int($_->end) // localtime->year } $self->all_events; | ||||||||||||||||||
|
Condition33%1/3
int $_->end // (localtime)->year
MC/DC0%0/2
int $_->end // (localtime)->year
int $_->end
(localtime)->year
Truth table33%1/3
A // B
A= int $_->endB= (localtime)->year
| |||||||||||||||||||||
| 428 | 55 | 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 | sub years { | ||||||||||||||||||||
| 438 | 14 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
years: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 439 | 14 | return $self->max_year - $self->min_year; | |||||||||||||||||||
| 440 | } | ||||||||||||||||||||
| 441 | |||||||||||||||||||||
| 442 | 2 | ▶ | no Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 443 | 2 | __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 | 2 | 1; | |||||||||||||||||||