| 1 | package Lab::Moose::Plot; | ||
| 2 | $Lab::Moose::Plot::VERSION = '3.931'; | ||
| 3 | #ABSTRACT: Frontend to L<PDL::Graphics::Gnuplot> | ||
| 4 | |||
| 5 | use v5.20; | ||
| 6 | |||
| 7 | |||
| 8 | use warnings; | ||
| 9 | use strict; | ||
| 10 | |||
| 11 | use Moose; | ||
| 12 | use MooseX::StrictConstructor; | ||
| 13 | use Moose::Util::TypeConstraints qw/union class_type/; | ||
| 14 | use MooseX::Params::Validate; | ||
| 15 | use Carp; | ||
| 16 | |||
| 17 | use PDL::Graphics::Gnuplot (); | ||
| 18 | |||
| 19 | # need to load for ClassName type constraint. | ||
| 20 | use PDL (); | ||
| 21 | |||
| 22 | has terminal => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => 'Str', | ||
| 25 | ); | ||
| 26 | |||
| 27 | has terminal_options => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => 'HashRef', | ||
| 30 | default => sub { {} } | ||
| 31 | ); | ||
| 32 | |||
| 33 | has plot_options => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => 'HashRef', | ||
| 36 | builder => 'build_plot_options', | ||
| 37 | lazy => 1, | ||
| 38 | ); | ||
| 39 | |||
| 40 | has curve_options => ( | ||
| 41 | is => 'ro', | ||
| 42 | isa => 'HashRef', | ||
| 43 | builder => 'build_curve_options', | ||
| 44 | lazy => 1, | ||
| 45 | ); | ||
| 46 | |||
| 47 | has gpwin => ( | ||
| 48 | is => 'ro', | ||
| 49 | isa => 'PDL::Graphics::Gnuplot', | ||
| 50 | init_arg => undef, | ||
| 51 | writer => '_gpwin', | ||
| 52 | ); | ||
| 53 | |||
| 54 | sub build_plot_options { | ||
| 55 | return {}; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub build_curve_options { | ||
| 59 | return {}; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub BUILD { | ||
| 64 | my $self = shift; | ||
| 65 | my $terminal = $self->terminal; | ||
| 66 | my $gpwin = PDL::Graphics::Gnuplot->new( | ||
| 67 | ( defined($terminal) ? $terminal : () ), | ||
| 68 | %{ $self->terminal_options() }, | ||
| 69 | $self->plot_options() | ||
| 70 | ); | ||
| 71 | $self->_gpwin($gpwin); | ||
| 72 | } | ||
| 73 | |||
| 74 | # Cannot simply put 'isa => 'PDL|ArrayRef[Num]'. See | ||
| 75 | # http://stackoverflow.com/questions/5196294/why-can-i-use-a-class-name-as-a-moose-type-but-not-when-part-of-a-type-union | ||
| 76 | |||
| 77 | union 'Lab::Moose::Plot::DataArg', | ||
| 78 | [ class_type('PDL'), 'ArrayRef[Num]', 'HashRef' ]; | ||
| 79 | |||
| 80 | sub _parse_options { | ||
| 81 | my $self = shift; | ||
| 82 | my ( $plot_options, $curve_options, $data ) = validated_list( | ||
| 83 | \@_, | ||
| 84 | plot_options => { isa => 'HashRef', default => {} }, | ||
| 85 | curve_options => { isa => 'HashRef', optional => 1 }, | ||
| 86 | data => { isa => 'ArrayRef[Lab::Moose::Plot::DataArg]' }, | ||
| 87 | ); | ||
| 88 | |||
| 89 | if ( @{$data} == 0 ) { | ||
| 90 | croak "missing data columns"; | ||
| 91 | } | ||
| 92 | |||
| 93 | if ( not defined $curve_options ) { | ||
| 94 | $curve_options = $self->curve_options(); | ||
| 95 | } | ||
| 96 | |||
| 97 | return ( $plot_options, $curve_options, $data ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _plot { | ||
| 101 | my $self = shift; | ||
| 102 | my ( $plot_options, $curve_options, $data, $plot_function ) = @_; | ||
| 103 | |||
| 104 | my $gpwin = $self->gpwin(); | ||
| 105 | $gpwin->$plot_function( $plot_options, %{$curve_options}, @{$data} ); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | my $meta = __PACKAGE__->meta(); | ||
| 110 | |||
| 111 | for my $func (qw/plot splot replot/) { | ||
| 112 | $meta->add_method( | ||
| 113 | $func => sub { | ||
| 114 | my $self = shift; | ||
| 115 | my ( $plot_options, $curve_options, $data ) | ||
| 116 | = $self->_parse_options(@_); | ||
| 117 | $self->_plot( $plot_options, $curve_options, $data, $func ); | ||
| 118 | } | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | |||
| 122 | __PACKAGE__->meta->make_immutable(); | ||
| 123 | |||
| 124 | 1; | ||
| 125 | |||
| 126 | __END__ |