SVG-Timeline

 view release on metacpan or  search on metacpan

lib/SVG/Timeline/Event.pm  view on Meta::CPAN


=cut

sub end_year {
  my $self = shift;

  if ($self->end) {
    return int $self->end;
  } else {
    return DateTime->now->year;
  }
}

=head2 draw_on($tl)

Draw the event inside the given timeline object.

=cut

sub draw_on {
  my $self = shift;
  my ($tl) = @_;

  my $x = $self->start * $tl->units_per_year;
  my $y = ($tl->bar_height * $self->index)
        + ($tl->bar_height * $tl->bar_spacing
           * ($self->index - 1));

  my $width = ($self->end - $self->start) * $tl->units_per_year;

  $tl->rect(
    x              => $x,
    y              => $y,
    width          => $width,
    height         => $tl->bar_height,
    fill           => $self->colour // $tl->default_colour,
    stroke         => $tl->bar_outline_colour,
    'stroke-width' => 1
  );

  my $font_size   = $tl->bar_height * 0.8;
  my $padding     = $tl->bar_height * 0.2;
  my $text_x      = $x + $padding;
  my $text_anchor = 'start';

  # Estimate text width: approximate average character width is 0.6 × font-size
  my $approx_text_width = length($self->text) * $font_size * 0.6;
  my $right_edge        = $x + $width;
  my $image_right_edge  = $tl->max_year * $tl->units_per_year;

  if ($text_x + $approx_text_width > $image_right_edge) {
    $text_x      = ($right_edge < $image_right_edge ? $right_edge : $image_right_edge) - $padding;
    $text_anchor = 'end';
  }

  $tl->text(
    x             => $text_x,
    y             => $y + $font_size,
    'font-size'   => $font_size,
    'text-anchor' => $text_anchor,
  )->cdata($self->text);
}

no Moose;
__PACKAGE__->meta->make_immutable;

=head1 AUTHOR

Dave Cross <dave@perlhacks.com>

=head1 COPYRIGHT AND LICENCE

Copyright (c) 2017, Magnum Solutions Ltd. All Rights Reserved.

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1;



( run in 2.338 seconds using v1.01-cache-2.11-cpan-d06a3f9ecfd )