File Coverage

blib/lib/Chart/Clicker/Axis/DateTime.pm
Criterion Covered Total %
statement 25 25 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 35 35 100.0


line stmt bran cond sub pod time code
1             package Chart::Clicker::Axis::DateTime;
2             $Chart::Clicker::Axis::DateTime::VERSION = '2.88';
3 2     2   52563 use Moose;
  2         675452  
  2         20  
4              
5             # ABSTRACT: An X or Y Axis using DateTime
6              
7 2     2   16627 use Chart::Clicker::Data::Marker;
  2         8  
  2         78  
8              
9 2     2   3239 use DateTime;
  2         359846  
  2         91  
10 2     2   2229 use DateTime::Set;
  2         136670  
  2         71  
11 2     2   26 use Graphics::Color::RGB;
  2         3  
  2         1024  
12              
13             extends 'Chart::Clicker::Axis';
14              
15              
16             has 'format' => (
17             is => 'rw',
18             isa => 'Str'
19             );
20              
21              
22             has 'time_zone' => (
23             is => 'rw',
24             isa => 'Str'
25             );
26              
27             override 'prepare' => sub {
28             my ($self, $driver) = @_;
29              
30             my ($dstart, $dend);
31             eval {
32             $dstart = DateTime->from_epoch(epoch => $self->range->lower);
33             $dend = DateTime->from_epoch(epoch => $self->range->upper);
34             };
35              
36             if(!defined($dstart) || !defined($dend)) {
37             $dstart = DateTime->now;
38             $dend = DateTime->now;
39             }
40              
41             my $dur = $dend - $dstart;
42              
43             unless(defined($self->format) && length($self->format)) {
44             if($dur->years) {
45             $self->format('%b %Y');
46             } elsif($dur->months) {
47             $self->format('%d');
48             } elsif($dur->weeks) {
49             $self->format('%d');
50             } elsif($dur->days) {
51             $self->format('%m/%d %H:%M');
52             } else {
53             $self->format('%H:%M');
54             }
55             }
56              
57             super;
58              
59             my $clicker = shift;
60             if(!defined($clicker)) {
61             die('No clicker?')
62             }
63              
64             # my @markers = @{ $clicker->markers };
65              
66             my $set = DateTime::Span->from_datetimes(
67             start => $dstart, end => $dend
68             );
69              
70             my $linecolor = Graphics::Color::RGB->new({
71             red => 0, green => 0, blue => 0, alpha => .35
72             });
73             my $fillcolor = Graphics::Color::RGB->new({
74             red => 0, green => 0, blue => 0, alpha => .10
75             });
76              
77             # my @dmarkers;
78             # my $day = $set->start->truncate(to => 'day');
79             #
80             # my $dayval;
81             # while($day < $set->end) {
82             # if($set->contains($day)) {
83             # if(defined($dayval)) {
84             # push(@dmarkers,
85             # Chart::Clicker::Data::Marker->new({
86             # key => $dayval,
87             # key2 => $day->epoch,
88             # color => $linecolor,
89             # inside_color=> $fillcolor,
90             # })
91             # );
92             # $dayval = undef;
93             # } else {
94             # $dayval = $day->epoch;
95             # }
96             # }
97             # $day = $day->add(days => 1);
98             # }
99             # if($dayval) {
100             # push(@dmarkers,
101             # Chart::Clicker::Data::Marker->new({
102             # key => $dayval,
103             # key2 => $day->epoch,
104             # color => $linecolor,
105             # inside_color=> $fillcolor,
106             # })
107             # );
108             # }
109             #
110             # push(@dmarkers, @markers);
111             # $clicker->markers(\@dmarkers);
112              
113             return 1;
114             };
115              
116              
117             sub format_value {
118 2     2 1 444 my $self = shift;
119 2         4 my $value = shift;
120              
121 2         7 my %dtargs = (
122             'epoch' => $value
123             );
124 2 100       90 if($self->time_zone) {
125 1         37 $dtargs{'time_zone'} = $self->time_zone;
126             }
127 2         20 my $dt = DateTime->from_epoch(%dtargs);
128              
129 2         809 return $dt->strftime($self->format);
130             }
131              
132             __PACKAGE__->meta->make_immutable;
133              
134 2     2   14 no Moose;
  2         4  
  2         25  
135              
136             1;
137              
138             __END__
139              
140             =pod
141              
142             =head1 NAME
143              
144             Chart::Clicker::Axis::DateTime - An X or Y Axis using DateTime
145              
146             =head1 VERSION
147              
148             version 2.88
149              
150             =head1 SYNOPSIS
151              
152             my $axis = Chart::Clicker::Axis::DateTime->new;
153              
154             =head1 DESCRIPTION
155              
156             A temporal Axis. Requires L<DateTime> and L<DateTime::Set>. Inherits from
157             Axis, so check the methods there as well. Expects that times will be in
158             unix format.
159              
160             =head1 ATTRIBUTES
161              
162             =head2 format
163              
164             Set/Get the formatting string used to format the DateTime. See DateTime's
165             strftime.
166              
167             =head2 time_zone
168              
169             Set/Get the time zone to use when creating DateTime objects! Accepts an
170             object or a string ('America/Chicago').
171              
172             =head1 METHODS
173              
174             =head2 format_value
175              
176             Formats the value using L<DateTime>'s strftime.
177              
178             =head1 AUTHOR
179              
180             Cory G Watson <gphat@cpan.org>
181              
182             =head1 COPYRIGHT AND LICENSE
183              
184             This software is copyright (c) 2014 by Cold Hard Code, LLC.
185              
186             This is free software; you can redistribute it and/or modify it under
187             the same terms as the Perl 5 programming language system itself.
188              
189             =cut