From 75a913f652915cca4c8acbad09611cc7af1a0029 Mon Sep 17 00:00:00 2001 From: Ed J Date: Fri, 12 Apr 2024 22:31:58 +0100 Subject: [PATCH] PGPLOT zap last string-eval --- lib/PDL/Graphics/Simple/PGPLOT.pm | 76 ++++++++----------------------- 1 file changed, 19 insertions(+), 57 deletions(-) diff --git a/lib/PDL/Graphics/Simple/PGPLOT.pm b/lib/PDL/Graphics/Simple/PGPLOT.pm index 7c6507b..b3a52e8 100644 --- a/lib/PDL/Graphics/Simple/PGPLOT.pm +++ b/lib/PDL/Graphics/Simple/PGPLOT.pm @@ -187,9 +187,6 @@ our $pgplot_methods = { }; -############################## -# PDL::Graphics::Simple::PGPLOT::plot - sub plot { my $me = shift; my $ipo = shift; @@ -198,13 +195,13 @@ sub plot { $po->{xtitle}= $ipo->{xtitle} if(defined($ipo->{xtitle})); $po->{ytitle}= $ipo->{ytitle} if(defined($ipo->{ytitle})); $po->{justify}= $ipo->{justify} if(defined($ipo->{justify})); - - my %color_opts = (); - if(defined($ipo->{crange})) { - $color_opts{'MIN'} = $ipo->{crange}->[0] if(defined($ipo->{crange}->[0])); - $color_opts{'MAX'} = $ipo->{crange}->[0] if(defined($ipo->{crange}->[1])); + + my %color_opts; + if (defined($ipo->{crange})) { + $color_opts{MIN} = $ipo->{crange}[0] if defined $ipo->{crange}[0]; + $color_opts{MAX} = $ipo->{crange}[0] if defined $ipo->{crange}[1]; } - + my $more = 0; if($ipo->{oplot} and $me->{opt}->{type} =~ m/^f/i) { @@ -212,28 +209,21 @@ sub plot { } unless($ipo->{oplot}) { - $me->{curvestyle} = 0; - $me->{logaxis} = $ipo->{logaxis}; - $po->{axis} = 0; if($ipo->{logaxis} =~ m/x/i) { $po->{axis} += 10; - $ipo->{xrange} = [ log10($ipo->{xrange}->[0]), log10($ipo->{xrange}->[1]) ]; + $ipo->{xrange} = [ map log10($_), @{$ipo->{xrange}}[0,1] ]; } if($ipo->{logaxis} =~ m/y/i) { $po->{axis} += 20; - $ipo->{yrange} = [ log10($ipo->{yrange}->[0]), log10($ipo->{yrange}->[1]) ]; + $ipo->{yrange} = [ map log10($_), @{$ipo->{yrange}}[0,1] ]; } - $me->{obj}->release; $me->{obj}->env(@{$ipo->{xrange}}, @{$ipo->{yrange}}, $po); - $me->{obj}->hold; - } else { - # Force a hold if we are held. - $me->{obj}->hold; } + $me->{obj}->hold; # ppo is "post-plot options", which are really a mix of plot and curve options. # Currently we don't parse any plot options into it (they're handled by the "env" @@ -241,56 +231,41 @@ sub plot { # are curve options that are autoincremented each curve. my %ppo = (); my $ppo = \%ppo; - - while(@_) { + while (@_) { my ($co, @data) = @{shift()}; my @extra_opts = (); - - if( defined($co->{style}) and $co->{style} ) { + if ( defined($co->{style}) and $co->{style} ) { $me->{curvestyle} = int($co->{style}); } else { $me->{curvestyle}++; } - - $ppo->{ color } = ($me->{curvestyle}-1) % 7 + 1; + $ppo->{ color } = $me->{curvestyle}-1 % 7 + 1; $ppo->{ linestyle } = ($me->{curvestyle}-1) % 5 + 1; - - - if( defined($co->{width}) and $co->{width} ) { - $ppo->{ linewidth } = int($co->{width}); - } - + $ppo->{ linewidth } = int($co->{width}) if $co->{width}; our $pgplot_methods; my $pgpm = $pgplot_methods->{$co->{with}}; die "Unknown curve option 'with $co->{with}'!" unless($pgpm); - if($pgpm eq 'imag') { for my $k(keys %color_opts) { $po->{$k} = $color_opts{$k}; } - $ppo->{ drawwedge } = ($ipo->{wedge} != 0); - # Extract transform parameters from the corners of the image... my $xcoords = shift(@data); my $ycoords = shift(@data); - my $datum_pix = [0,0]; my $datum_sci = [$xcoords->at(0,0), $ycoords->at(0,0)]; - my $t1 = ($xcoords->slice("(-1),(0)") - $xcoords->slice("(0),(0)")) / ($xcoords->dim(0)-1); my $t2 = ($xcoords->slice("(0),(-1)") - $xcoords->slice("(0),(0)")) / ($xcoords->dim(1)-1); my $t4 = ($ycoords->slice("(-1),(0)") - $ycoords->slice("(0),(0)")) / ($ycoords->dim(0)-1); my $t5 = ($ycoords->slice("(0),(-1)") - $ycoords->slice("(0),(0)")) / ($ycoords->dim(1)-1); - my $transform = pdl( $datum_sci->[0] - $t1 * $datum_pix->[0] - $t2 * $datum_pix->[1], $t1, $t2, $datum_sci->[1] - $t4 * $datum_pix->[0] - $t5 * $datum_pix->[1], $t4, $t5 )->flat; - { # sepia color table my $r = (xvals(256)/255)->sqrt; my $g = (xvals(256)/255); @@ -298,33 +273,24 @@ sub plot { $me->{obj}->ctab($g, $r, $g, $b); } } - - if($me->{logaxis} =~ m/x/i) { - $data[0] = $data[0]->log10; - } - if($me->{logaxis} =~ m/y/i) { - $data[1] = $data[1]->log10; - } - - if(ref($pgpm) eq 'CODE') { - &$pgpm($me, $ipo, \@data, $ppo); + $data[0] = $data[0]->log10 if $me->{logaxis} =~ m/x/i; + $data[1] = $data[1]->log10 if $me->{logaxis} =~ m/y/i; + if (ref $pgpm eq 'CODE') { + $pgpm->($me, $ipo, \@data, $ppo); } else { - my $str= sprintf('$me->{obj}->%s(@data,%s);%s',$pgpm,'$ppo',"\n"); - eval $str; + $me->{obj}->$pgpm(@data,$ppo); } - unless($pgpm eq 'imag') { $ppo->{linestyle}++; $ppo->{color}++; } - $me->{obj}->hold; } ############################## # End of curve plotting. # Now place the legend if necessary. - if($ipo->{legend}) { + if ($ipo->{legend}) { my $xp; my $xrdiff = $ipo->{xrange}->[1] - $ipo->{xrange}->[0]; if( $ipo->{legend}=~ m/l/i ) { @@ -334,7 +300,6 @@ sub plot { } else { $xp = 0.4 * $xrdiff + $ipo->{xrange}->[0]; } - my $yp; my $yrdiff = $ipo->{yrange}->[1] - $ipo->{yrange}->[0]; if( $ipo->{legend}=~ m/t/i ) { @@ -344,7 +309,6 @@ sub plot { } else { $yp = 0.6 * $yrdiff + $ipo->{yrange}->[0]; } - print "keys is [".join(",",@{$me->{keys}})."]; xp is $xp; yp is $yp\n"; $me->{obj}->legend( $me->{keys}, @@ -354,9 +318,7 @@ sub plot { } ); } - $me->{obj}->release; - } sub DESTROY {