Skip to content

Commit

Permalink
tidy Gnuplot
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed May 6, 2024
1 parent 5d34b77 commit 86e9c16
Showing 1 changed file with 47 additions and 57 deletions.
104 changes: 47 additions & 57 deletions lib/PDL/Graphics/Simple/Gnuplot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ sub check {
$mod->{msg} = $@;
return 0;
}
if($PDL::Graphics::Gnuplot::VERSION < $required_PGG_version) {
if ($PDL::Graphics::Gnuplot::VERSION < $required_PGG_version) {
$mod->{msg} = sprintf("PDL::Graphics::Gnuplot was found, but is too old (v%s < v%s). Ignoring it.\n",
$PDL::Graphics::Gnuplot::VERSION,
$required_PGG_version
Expand Down Expand Up @@ -138,7 +138,7 @@ sub new {
my $conv_tempfile = '';

# Do different things for interactive and file types
if($opt->{type} =~ m/^i/i) {
if ($opt->{type} =~ m/^i/i) {
push(@params, title=>$opt->{output}) if defined $opt->{output};
# Interactive - try known terminals unless PDL_SIMPLE_DEVICE given
push @params, font=>"=16", dashed=>1;
Expand All @@ -156,7 +156,7 @@ sub new {
eval { $gpw = gpwin($try, @params,
($disp_opts->{$try} // {})->{persist} ? (persist=>0) : ()
); };
last attempt if($gpw);
last attempt if $gpw;
}
}
die "Couldn't start a gnuplot interactive window" unless($gpw);
Expand All @@ -168,7 +168,7 @@ sub new {

# Filename extension -- 2-4 characters
my $ext;
if($opt->{output} =~ m/\.(\w{2,4})$/) {
if ($opt->{output} =~ m/\.(\w{2,4})$/) {
$ext = $1;
} else {
$ext = '.png';
Expand All @@ -184,22 +184,22 @@ sub new {
my $ft = $filetypes->{$ext};
if (ref $ft eq 'ARRAY') {
try:for my $try (@$ft) {
if($mod->{valid_terms}->{$try}) {
if ($mod->{valid_terms}{$try}) {
$ft = $try;
last try;
}
}
if (ref($ft)) {
$ft = undef;
}
} elsif (!defined($mod->{valid_terms}->{$ft})) {
} elsif (!defined($mod->{valid_terms}{$ft})) {
$ft = undef;
}

# Now $ext has the file type - check if its a supported type. If not, make a
# tempfilename to hold gnuplot's output.
unless ( defined($ft) ) {
unless ($mod->{valid_terms}->{'pscairo'} or $mod->{valid_terms}->{'postscript'}) {
unless ($mod->{valid_terms}{pscairo} or $mod->{valid_terms}{postscript}) {
die "PDL::Graphics::Simple: $ext isn't a valid output file type for your gnuplot,\n\tand it doesn't support .ps either. Sorry, I give up.\n";
}

Expand All @@ -209,20 +209,20 @@ sub new {
close $fh;
unlink($conv_tempfile); # just to be sure;
$conv_tempfile .= ".ps";
$ft = $mod->{valid_terms}->{'pscairo'} ? 'pscairo' : 'postscript';
$ft = $mod->{valid_terms}{pscairo} ? 'pscairo' : 'postscript';
}
push(@params, "output" => ($conv_tempfile || $opt->{output}) );
push(@params, "color" => 1 ) if( $PDL::Graphics::Gnuplot::termTab->{$ft}->{'color'} );
push(@params, "dashed" => 1 ) if( $PDL::Graphics::Gnuplot::termTab->{$ft}->{'dashed'} );
push @params, output => ($conv_tempfile || $opt->{output});
push @params, color => 1 if $PDL::Graphics::Gnuplot::termTab->{$ft}{color};
push @params, dashed => 1 if $PDL::Graphics::Gnuplot::termTab->{$ft}{dashed};
$gpw = gpwin( $ft, @params );
}


my $me = { opt => $opt, conv_fn => $conv_tempfile, obj=>$gpw };

# Deal with multiplot setup...
if(defined($opt->{multi})) {
$me->{nplots} = $opt->{multi}->[0] * $opt->{multi}->[1];
if (defined($opt->{multi})) {
$me->{nplots} = $opt->{multi}[0] * $opt->{multi}[1];
$me->{plot_no} = 0;
} else {
$me->{nplots} = 0;
Expand Down Expand Up @@ -265,19 +265,19 @@ our $curve_types = {
for my $i(0..$data[0]->dim(0)-1) {
my $j = "";
my $s = $data[2]->[$i];
if( $s =~ s/^([\<\>\| ])// ) {
if ( $s =~ s/^([\<\>\| ])// ) {
$j = $1;
}

my @spec = ("$s", at=>[$data[0]->at($i), $data[1]->at($i)]);
push(@spec,"left") if($j eq '<');
push(@spec,"center") if($j eq '|');
push(@spec,"right") if($j eq '>');
push( @{$label_list}, \@spec );
push @spec,"left" if $j eq '<';
push @spec,"center" if $j eq '|';
push @spec,"right" if $j eq '>';
push @{$label_list}, \@spec;
}
$po->{label} = $label_list;
$co->{with} = "labels";
return [ $co, [$po->{xrange}->[0]], [$po->{yrange}->[0]], [""] ];
return [ $co, [$po->{xrange}[0]], [$po->{yrange}[0]], [""] ];
}

};
Expand All @@ -299,50 +299,49 @@ sub plot {
clut => 'sepia',
};

if( defined($ipo->{legend}) ) {
if ( defined($ipo->{legend}) ) {
my $legend = "";
if( $ipo->{legend} =~ m/l/i ) {
if ( $ipo->{legend} =~ m/l/i ) {
$legend .= ' left ';
} elsif($ipo->{legend} =~ m/r/i) {
} elsif ($ipo->{legend} =~ m/r/i) {
$legend .= ' right ';
} else {
$legend .= ' center ';
}
if( $ipo->{legend} =~ m/t/i) {
if ( $ipo->{legend} =~ m/t/i) {
$legend .= ' top ';
} elsif( $ipo->{legend} =~ m/b/i) {
} elsif ( $ipo->{legend} =~ m/b/i) {
$legend .= ' bottom ';
} else {
$legend .= ' center ';
}
$po->{key} = $legend;
}

$po->{logscale} = [$ipo->{logaxis}] if($ipo->{logaxis});
$po->{logscale} = [$ipo->{logaxis}] if $ipo->{logaxis};

unless($ipo->{oplot}) {
$me->{curvestyle} = 0;
}

my @arglist = ($po);
my @arglist = $po;

for my $block(@_) {
for my $block (@_) {
die "PDL::Graphics::Simple::Gnuplot: undefined curve type $block->[0]{with}"
unless my $ct = $curve_types->{ $block->[0]{with} };
if(ref($ct) eq 'CODE') {
if (ref($ct) eq 'CODE') {
$block = &$ct($me, $po, @$block);
} else {
$block->[0]->{with} = $ct;
$block->[0]{with} = $ct;
}

# Now parse out curve options and deal with line styles...
my $co = shift @$block;
my $gco = {};
$gco->{with} = $co->{with};
my $gco = { with => $co->{with} };

unless($co->{with} eq 'labels') {

if(defined($co->{style}) and $co->{style}) {
if (defined $co->{style}) {
$me->{curvestyle} = $co->{style};
} else {
$me->{curvestyle}++;
Expand All @@ -351,53 +350,44 @@ sub plot {
$gco->{linetype} = $me->{curvestyle};
}

if( defined($co->{width}) and $co->{width} and $co->{with} !~ m/^label/ ) {
my $s;
if($co->{with} =~ m/^points/) {
$gco->{pointsize} = $co->{width};
}
if ( $co->{width} and $co->{with} !~ m/^label/ ) {
$gco->{pointsize} = $co->{width} if $co->{with} =~ m/^points/;
$gco->{linewidth} = $co->{width};
}
$gco->{legend} = $co->{key} if(defined($co->{key}));
$gco->{legend} = $co->{key} if defined $co->{key};

push(@arglist, ($gco, @$block));
push @arglist, $gco, @$block;
}

if($me->{nplots}) {
if ($me->{nplots}) {
unless($me->{plot_no}) {
$me->{obj}->multiplot( layout=>[$me->{opt}->{multi}->[0], $me->{opt}->{multi}->[1]] );
$me->{obj}->multiplot( layout=>[@{$me->{opt}{multi}}[0,1]] );
}
}

if($ipo->{oplot}) {
delete $po->{logaxis};
delete $po->{xrange};
delete $po->{yrange};
delete $po->{cbrange};
delete $po->{justify};
if ($ipo->{oplot}) {
delete @$po{qw(logaxis xrange yrange cbrange justify)};
$me->{obj}->replot(@arglist);
} else {
$me->{obj}->plot(@arglist);
}


if($me->{nplots}) {
if ($me->{nplots}) {
$me->{plot_no}++;
if($me->{plot_no} >= $me->{nplots}) {
$me->{obj}->end_multi();
if ($me->{plot_no} >= $me->{nplots}) {
$me->{obj}->end_multi;
$me->{plot_no} = 0;

$me->{obj}->close() if($me->{opt}->{type} =~ m/^f/i);

$me->{obj}->close if $me->{opt}{type} =~ m/^f/i;
}
} else {
$me->{obj}->close() if($me->{opt}->{type} =~ m/^f/i);
$me->{obj}->close if $me->{opt}{type} =~ m/^f/i;
}

if($me->{opt}->{type} =~ m/^f/i and $me->{conv_fn}) {
print "converting $me->{conv_fn} to $me->{opt}->{output}...";
if ($me->{opt}{type} =~ m/^f/i and $me->{conv_fn}) {
print "converting $me->{conv_fn} to $me->{opt}{output}...";
$a = rim($me->{conv_fn});
wim($a->slice('-1:0:-1')->mv(1,0), $me->{opt}->{output});
wim($a->slice('-1:0:-1')->mv(1,0), $me->{opt}{output});
unlink($me->{conv_fn});
}
}
Expand Down

0 comments on commit 86e9c16

Please sign in to comment.