Chart::Clicker::Axis::DateTime - An X or Y Axis using DateTime


Chart-Clicker documentation Contained in the Chart-Clicker distribution.

Index


Code Index:

NAME

Top

Chart::Clicker::Axis::DateTime - An X or Y Axis using DateTime

DESCRIPTION

Top

A temporal Axis. Requires DateTime and DateTime::Set. Inherits from Axis, so check the methods there as well. Expects that times will be in unix format.

SYNOPSIS

Top

  my $axis = Chart::Clicker::Axis::DateTime->new;

ATTRIBUTES

Top

time_zone

Set/Get the time zone to use when creating DateTime objects! Accepts an object or a string ('America/Chicago').

format

Set/Get the formatting string used to format the DateTime. See DateTime's strftime.

METHODS

Top

new

Creates a new DateTime Axis.

format_value

Formats the value using DateTime's strftime.

AUTHOR

Top

Cory G Watson <gphat@cpan.org>

SEE ALSO

Top

perl(1)

LICENSE

Top

You can redistribute and/or modify this code under the same terms as Perl itself.


Chart-Clicker documentation Contained in the Chart-Clicker distribution.

package Chart::Clicker::Axis::DateTime;
use Moose;

use Chart::Clicker::Data::Marker;

use DateTime;
use DateTime::Set;
use Graphics::Color::RGB;

extends 'Chart::Clicker::Axis';

has 'format' => (
    is => 'rw',
    isa => 'Str'
);

has 'time_zone' => (
    is => 'rw',
    isa => 'Str'
);

override 'prepare' => sub {
     my ($self, $driver) = @_;

    my ($dstart, $dend);
    eval {
        $dstart = DateTime->from_epoch(epoch => $self->range->lower);
        $dend = DateTime->from_epoch(epoch => $self->range->upper);
    };

    if(!defined($dstart) || !defined($dend)) {
        $dstart = DateTime->now();
        $dend = DateTime->now();
    }

    my $dur = $dend - $dstart;

    unless(defined($self->format) && length($self->format)) {
        if($dur->years) {
            $self->format('%b %Y');
        } elsif($dur->months) {
            $self->format('%d');
        } elsif($dur->weeks) {
            $self->format('%d');
        } elsif($dur->days) {
            $self->format('%m/%d %H:%M');
        } else {
            $self->format('%H:%M');
        }
    }

    super();

    my $clicker = shift();
    if(!defined($clicker)) {
        die('No clicker?')
    }

    # my @markers = @{ $clicker->markers() };

    my $set = DateTime::Span->from_datetimes(
        start => $dstart, end => $dend
    );

    my $linecolor = Graphics::Color::RGB->new({
        red => 0, green => 0, blue => 0, alpha => .35
    });
    my $fillcolor = Graphics::Color::RGB->new({
        red => 0, green => 0, blue => 0, alpha => .10
    });

    # my @dmarkers;
    # my $day = $set->start->truncate(to => 'day');
    # 
    # my $dayval;
    # while($day < $set->end()) {
    #     if($set->contains($day)) {
    #         if(defined($dayval)) {
    #             push(@dmarkers,
    #                 Chart::Clicker::Data::Marker->new({
    #                     key         => $dayval,
    #                     key2        => $day->epoch(),
    #                     color       => $linecolor,
    #                     inside_color=> $fillcolor,
    #                 })
    #             );
    #             $dayval = undef;
    #         } else {
    #             $dayval = $day->epoch();
    #         }
    #     }
    #     $day = $day->add(days => 1);
    # }
    # if($dayval) {
    #     push(@dmarkers,
    #         Chart::Clicker::Data::Marker->new({
    #             key         => $dayval,
    #             key2        => $day->epoch(),
    #             color       => $linecolor,
    #             inside_color=> $fillcolor,
    #         })
    #     );
    # }
    # 
    # push(@dmarkers, @markers);
    # $clicker->markers(\@dmarkers);

    return 1;
};

sub format_value {
    my $self = shift();
    my $value = shift();

    my %dtargs = (
        'epoch' => $value
    );
    if($self->time_zone()) {
        $dtargs{'time_zone'} = $self->time_zone();
    }
    my $dt = DateTime->from_epoch(%dtargs);

    return $dt->strftime($self->format());
}

__PACKAGE__->meta->make_immutable;

no Moose;

1;
__END__