| 1 | package Lab::Moose::Plot; | ||
| 2 | $Lab::Moose::Plot::VERSION = '3.881'; | ||
| 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__ | ||
| 127 | |||
| 128 | =pod | ||
| 129 | |||
| 130 | =encoding UTF-8 | ||
| 131 | |||
| 132 | =head1 NAME | ||
| 133 | |||
| 134 | Lab::Moose::Plot - Frontend to L<PDL::Graphics::Gnuplot> | ||
| 135 | |||
| 136 | =head1 VERSION | ||
| 137 | |||
| 138 | version 3.881 | ||
| 139 | |||
| 140 | =head1 SYNOPSIS | ||
| 141 | |||
| 142 | use PDL; | ||
| 143 | use Lab::Moose::Plot; | ||
| 144 | |||
| 145 | # use default terminal ('qt' on Linux) | ||
| 146 | my $plot = Lab::Moose::Plot->new(); | ||
| 147 | |||
| 148 | # simple 2-D plot | ||
| 149 | my $x = sequence(10); | ||
| 150 | my $y = 2 * $x; | ||
| 151 | |||
| 152 | $plot->plot( | ||
| 153 | plot_options => {title => 'linear function'}, | ||
| 154 | curve_options => {legend => '2 * x'}, | ||
| 155 | data => [$x, $y] | ||
| 156 | ); | ||
| 157 | |||
| 158 | # pm3d plot | ||
| 159 | my $z = sin(rvals(300, 300) / 10); | ||
| 160 | my $x = xvals($z); | ||
| 161 | my $y = yvals($z); | ||
| 162 | |||
| 163 | $plot->splot( | ||
| 164 | plot_options => { | ||
| 165 | title => 'a pm3d plot', | ||
| 166 | pm3d => 1, | ||
| 167 | view => 'map', | ||
| 168 | surface => 0, | ||
| 169 | palette => "model RGB defined ( 0 'red', 1 'yellow', 2 'white' )", | ||
| 170 | }, | ||
| 171 | data => [$x, $y, $z], | ||
| 172 | ); | ||
| 173 | |||
| 174 | |||
| 175 | # use a different terminal with default plot options | ||
| 176 | |||
| 177 | my $plot = Lab::Moose::Plot( | ||
| 178 | terminal => 'svg', | ||
| 179 | terminal_options => {output => 'file.svg', enhanced => 0}, | ||
| 180 | plot_options => {pm3d => 1, view => 'map', surface => 0} | ||
| 181 | ); | ||
| 182 | |||
| 183 | =head1 DESCRIPTION | ||
| 184 | |||
| 185 | This is a small wrapper around L<PDL::Graphics::Gnuplot> with the aim to make | ||
| 186 | it accessible with our hash-based calling convention. | ||
| 187 | |||
| 188 | See the documentation of L<PDL::Graphics::Gnuplot> for the allowed values of | ||
| 189 | terminal-, plot- and curve-options. | ||
| 190 | |||
| 191 | =head1 METHODS | ||
| 192 | |||
| 193 | =head2 new | ||
| 194 | |||
| 195 | my $plot = Lab::Moose::Plot->new( | ||
| 196 | terminal => $terminal, | ||
| 197 | terminal_options => \%terminal_options, | ||
| 198 | plot_options => \%plot_options, | ||
| 199 | curve_options => \%curve_options, | ||
| 200 | ); | ||
| 201 | |||
| 202 | Construct a new plotting backend. All arguments are optional. The default for | ||
| 203 | C<plot_options> and C<curve_options> is the empty hash. | ||
| 204 | |||
| 205 | =head2 plot | ||
| 206 | |||
| 207 | $plot->plot( | ||
| 208 | plot_options => \%plot_options, | ||
| 209 | curve_options => \%curve_options, | ||
| 210 | data => [$x, $y, $z], | ||
| 211 | ); | ||
| 212 | |||
| 213 | Call L<PDL::Graphics::Gnuplot>'s plot function. | ||
| 214 | The data array can contain either PDLs ore 1D arrad refs. The required number | ||
| 215 | of elements in the array depends on the used plotting style. | ||
| 216 | |||
| 217 | =head2 splot | ||
| 218 | |||
| 219 | =head2 replot | ||
| 220 | |||
| 221 | splot and replot call L<PDL::Graphics::Gnuplot>'s splot and replot functions. | ||
| 222 | Otherwise they behave like plot. | ||
| 223 | |||
| 224 | =head1 COPYRIGHT AND LICENSE | ||
| 225 | |||
| 226 | This software is copyright (c) 2023 by the Lab::Measurement team; in detail: | ||
| 227 | |||
| 228 | Copyright 2016 Simon Reinhardt | ||
| 229 | 2017 Andreas K. Huettel, Simon Reinhardt | ||
| 230 | 2018-2019 Simon Reinhardt | ||
| 231 | 2020 Andreas K. Huettel | ||
| 232 | |||
| 233 | |||
| 234 | This is free software; you can redistribute it and/or modify it under | ||
| 235 | the same terms as the Perl 5 programming language system itself. | ||
| 236 | |||
| 237 | =cut |