Simple/Prima.pm

filter: Statementstmt 11.0% 34 / 307 Branchbran 4.3% 5 / 114 Conditioncond 6.6% 2 / 30 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 27.7% 5 / 18 Podpod 0.0% 0 / 3 totaltotal 9.5% 47 / 491 SCAR 83.1
CC
73
Cov
9%
plot
1056.0
DESTROY
210.0
new
56.0
CRAP 4076.8
« Simple/PLplot.pm ↑ Summary Simple/Gnuplot.pm »
1 ######################################################################
2 ######################################################################
3 ######################################################################
4 ###
5 ###
6 ### Prima backend for PDL::Graphics:Simple
7 ###
8 ### See the PDL::Graphics::Simple docs for details
9 ###
10 ### Prima setup is borrowed from D. Mertens' PDL::Graphics::Prima::Simple
11 ###
12 ##
13 #
14  
15 # Still to do:
16 # - file output
17  
18  
19 package PDL::Graphics::Simple::Prima;
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use PDL::Options q/iparse/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use File::Temp qw/tempfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 our $mod = {
27 shortname => 'prima',
28 module => 'PDL::Graphics::Simple::Prima',
29 engine => 'PDL::Graphics::Prima',
30 synopsis => 'Prima (interactive, fast, PDL-specific)',
31 pgs_version => '1.005'
32 };
33 1 PDL::Graphics::Simple::register('PDL::Graphics::Simple::Prima');
34  
35  
36 ##########
37 # PDL::Graphics::Simple::Prima::check
38 # Checker
39 sub check {
40 5 ▶ my $force = shift;
Subroutine check: called CC=9 SCAR=35.4
Pod undocumented
41 5 ▶ $force = 0 unless(defined($force));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $force 4 1 5
42
43 5 ▶ return $mod->{ok} unless( $force or !defined($mod->{ok}));
Branch: 1 branch
BranchTrueFalseTotal
unless $force or not defined $PDL::Graphics::Simple::Prima::mod->{"ok"} 4 1 5
Condition66%2/3
$force or not defined $PDL::Graphics::Simple::Prima::mod->{"ok"}
l!l&&r!l&&!r
104
MC/DC50%1/2
$force or not defined $PDL::Graphics::Simple::Prima::mod->{"ok"}
$force not defined $PDL::Graphics::Simple::Prima::mod->{"ok"}
Truth table66%2/3
A or not(B)
A= $forceB= not defined $PDL::Graphics::Simple::Prima::mod->{"ok"}
ABresult
1X1
011
000
44 1 $mod->{ok} = 0; # makes default case simpler
45  
46 # Check Prima availability
47 1 my $min_version = 0.13;
48 1 eval { require PDL::Graphics::Prima; };
49 1 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
50 1 $mod->{msg} = "Couldn't load PDL::Graphics::Prima: ".$@;
51 1 undef $@;
52 1 return 0;
53 }
54 0 ▶ if ($PDL::Graphics::Prima::VERSION < $min_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDL::Graphics::Prima::VERSION < $min_version) 0 0 0
55 0 $mod->{msg} = "Prima version $PDL::Graphics::Prima::VERSION is too low ($min_version required)";
56 0 return 0;
57 }
58  
59 0 eval { require PDL::Graphics::Prima::Simple; };
60 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
61 0 $mod->{msg} = "Couldn't load PDL::Graphics::Prima::Simple: ".$@;
62 0 undef $@;
63 0 return 0;
64 }
65
66 0 eval {
67 0 require Prima::Application;
68 0 Prima::Application->import();
69 };
70 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
71 0 $mod->{msg} = "Couldn't load Prima application: ".$@;
72 0 undef $@;
73 0 return 0;
74 }
75  
76 # Don't know if all these are actually needed; I'm stealing from the demo.
77 # --CED
78 0 eval {
79 0 require Prima::Label;
80 0 require Prima::PodView;
81 0 require Prima::Buttons;
82 0 require Prima::Utils;
83 0 require Prima::Edit;
84 };
85 0 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
86 0 $mod->{msg} = "Couldn't load auxiliary Prima modules: ".$@;
87 0 undef $@;
88 0 return 0;
89 }
90
91 0 $mod->{ok} =1;
92 0 return 1;
93 }
94  
95  
96 ##############################
97 # New - constructor
98 1 our $new_defaults = {
99 size => [6,4.5,'in'],
100 type=>'i',
101 output=>'',
102 multi=>undef
103 };
104  
105 ## Much of this boilerplate is stolen from PDL::Graphics::Prima::Simple...
106 1 our $N_windows = 0;
107  
108 sub new {
109 0 ▶ my $class = shift;
Subroutine new: not called CC=7 SCAR=40.3
Pod undocumented
110 0 my $opt_in = shift;
111 0 ▶ $opt_in = {} unless(defined($opt_in));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt_in 0 0 0
112 0 my $opt = { iparse($new_defaults, $opt_in) };
113  
114
115 0 ▶ unless( check() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (check()) 0 0 0
116 0 ▶ die "$mod->{shortname} appears nonfunctional\n" unless(check(1));
Branch: 1 branch
BranchTrueFalseTotal
unless check(1) 0 0 0
117 }
118  
119 0 my $size = PDL::Graphics::Simple::_regularize_size($opt->{size},'px');
120  
121 my $pw = Prima::Window->create( text => $opt->{output} || "PDL/Prima Plot",
122 size => [$size->[0], $size->[1]],
123 0 ▶ onCreate => sub { $PDL::Graphics::Prima::Simple::N_windows++; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 0 ▶ onDestroy => sub { $PDL::Graphics::Prima::Simple::N_windows--;
Subroutine __ANON__: not called CC=2 SCAR=17.9
125 0 ▶ $PDL::Graphics::Prima::Simple::is_twiddling = 0 if($PDL::Graphics::Prima::Simple::N_windows==0);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::Graphics::Prima::Simple::N_windows == 0 0 0 0
126 }
127 0 ▶ );
Condition0%0/2
$opt->{'output'} || 'PDL/Prima Plot'
l!l
00
MC/DC0%0/1
$opt->{'output'} || 'PDL/Prima Plot'
$opt->{'output'}
Truth table0%0/2
A
A= $opt->{'output'}
Aresult
11
00
128 0 ▶ die "Couldn't create a Prima window!" unless(defined($pw));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pw 0 0 0
129  
130 0 ▶ if($opt_in->{type} =~ m/^f/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_in->{"type"} =~ /^f/i) 0 0 0
131 0 $pw->hide;
132 }
133 my $me = { obj => $pw,
134 widgets => [],
135 next_plotno=>0,
136 multi=>$opt_in->{multi},
137 type=>$opt->{type},
138 output=>$opt->{output}
139 0 };
140 0 return bless($me, "PDL::Graphics::Simple::Prima");
141 }
142  
143 sub DESTROY {
144 0 ▶ my $me = shift;
Subroutine DESTROY: not called CC=14 SCAR=53.5
145  
146 0 print "DESTROYING $me\n";
147  
148 0 ▶ if($me->{type} =~ m/f/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"type"} =~ /f/i) 0 0 0
149 ##############################
150 # File-saving code...
151 0 ▶ unless( $me->{multi} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{'multi'}) { } 0 0 0
152  
153 ##############################
154 # Save plot to file
155  
156 0 ▶ if($me->{widgets}->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{'widgets'}[0]) { } 0 0 0
157 0 eval q{$me->{widgets}->[0]->save_to_file($me->{output})};
158 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
159 0 print $@;
160 0 undef $@;
161 }
162 } else {
163 0 print STDERR "No plot was sent to $me->{output}\n";
164 }
165 } else {
166  
167 ##############################
168 # Multiplot - save the plots individually, then splice them together.
169 # Lame, lame - I think this can be done in memory with Prima.
170 # But it gets us to a place where we are supporting stuff.
171  
172 0 ▶ if(@{$me->{widgets}} < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$me->{'widgets'};} < 1) { } 0 0 0
173 0 print STDERR "No plot was sent to $me->{output}\n";
174 } else {
175 0 print STDERR "WARNING - multiplot support is experimental for the Prima engine\n";
176
177 0 my ($h,$tmpfile) = tempfile('PDL-Graphics-Simple-XXXX');
178 0 close $h;
179 0 unlink($tmpfile);
180
181 0 my $suffix;
182 0 ▶ if($me->{output}=~ s/(\.\w{2,4})$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{'output'} =~ s/(\.\w{2,4})$//) { } 0 0 0
183 0 $suffix = $1;
184 } else {
185 0 $suffix = ".png";
186 }
187 0 $tmpfile .= $suffix;
188
189 0 my $widget_dex = 0;
190 0 my $im = undef;
191 0 my $ztile = undef;
192 0 ROW:for my $row(0..$me->{multi}->[1]-1) {
193 0 my $imrow = undef;
194 0 for my $col(0..$me->{multi}->[0]-1) {
195
196 0 my $tile;
197
198 0 ▶ if($widget_dex < @{$me->{widgets}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($widget_dex < @{$me->{'widgets'};}) { } 0 0 0
199 0 eval q{ $me->{widgets}->[$widget_dex++]->save_to_file($tmpfile) };
200 0 ▶ last ROW if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
201 0 $tile = rim($tmpfile);
202 0 $ztile = zeroes($tile)+255;
203 0 unlink($tmpfile);
204 } else {
205 # ztile is always initialized by first run through...
206 0 $tile = $ztile;
207 }
208
209 0 ▶ if(!defined($imrow)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $imrow) { } 0 0 0
210 0 $imrow = $tile;
211 } else {
212 0 $imrow = $imrow->glue(0,$tile);
213 }
214 } # end of row loop
215
216 0 ▶ if(!defined($im)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $im) { } 0 0 0
217 0 $im = $imrow;
218 } else {
219 0 $im = $imrow->glue(1,$im);
220 }
221 }
222 0 ▶ unless($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
223 0 wim($im, $me->{output}.$suffix);
224 } else {
225 0 print STDERR $@;
226 0 undef $@;
227 }
228 }
229 }
230 }
231  
232  
233  
234 0 $me->{obj}->hide;
235 0 $me->{obj}->destroy;
236 }
237  
238  
239 1 our @colors =qw/
240 cl::Black cl::Red cl::Green cl::Blue cl::Cyan cl::Magenta cl::Yellow cl::Brown cl::LighttRed cl::LightGreen cl::LightBlue cl::Gray/;
241  
242 1 our @patterns = qw/
243 lp::Solid lp::Dash lp::LongDash lp::ShortDash lp::DotDot lp::DashDot lp::DashDotDot/;
244  
245  
246 ##############################
247 # Fake-o apply method makes sepiatone values for input data.
248 # We have to mock up an object method to match the style of PDL::Graphics::Prima::Palette,
249 # in order to make the Matrix plot type happy (for 'with=>image').
250 1 @PDL::Graphics::Simple::Prima::Sepia_Palette::ISA = 'PDL::Graphics::Prima::Palette';
251 sub PDL::Graphics::Simple::Prima::Sepia_Palette::apply {
252 0 ▶ my $h = shift;
Subroutine apply: not called CC=5 SCAR=34.0
253 0 my $data = shift;
254  
255 0 my $crange = $h->{crange};
256 0 my($min, $max);
257 0 ▶ if(defined($crange)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $crange) 0 0 0
258 0 ($min,$max) = @$crange;
259 }
260 0 ▶ $min = $data->min unless(defined($min));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 0 0 0
261 0 ▶ $max = $data->max unless(defined($max));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 0 0
262  
263 0 ▶ my $g = ($min==$max)?$ data->zeroes : (($data->double - $min)/($max-$min))->clip(0,1);
Branch: 1 branch
BranchTrueFalseTotal
$min == $max ? : 0 0 0
264 0 my $r = $g->sqrt;
265 0 my $b = $g*$g;
266
267 0 return (pdl($r,$g,$b)*255.999)->floor->mv(-1,0)->rgb_to_color;
268 }
269  
270
271 ##############################
272 # Plot types
273 #
274 # This probably needs a little more smarts.
275 # Currently each entry is either a ppair::<foo> return or a sub that implements
276 # the plot type in terms of others.
277  
278 our $types = {
279 lines => q{ppair::Lines},
280  
281 6 points => [ map { 'ppair::'.$_ } qw/Blobs Triangles Squares Crosses Xs Asterisks/ ],
282  
283 bins => sub {
284 0 ▶ my ($me, $plot, $block, $cprops) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
285 0 my $x = $block->[0];
286 0 my $x1 = $x->range( [[0],[-1]], [$x->dim(0)], 'e' )->average;
287 0 my $x2 = $x->range( [[1],[0]], [$x->dim(0)], 'e' )->average;
288 0 my $newx = pdl($x1, $x2)->mv(-1,0)->clump(2)->sever;
289  
290 0 my $y = $block->[1];
291 0 my $newy = $y->dummy(0,2)->clump(2)->sever;
292
293 0 $plot->dataSets()->{ 1+keys(%{$plot->dataSets()}) } =
294 ds::Pair($newx,$newy,plotType=>eval q{ppair::Lines}, @$cprops);
295 },
296  
297  
298  
299 image => sub {
300 0 ▶ my($me, $plot, $data, $cprops, $co) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
301  
302 0 my ($xmin, $xmax) = $data->[0]->minmax;
303 0 ▶ my $dx = 0.5 * ($xmax-$xmin) / ($data->[0]->dim(0) - (($data->[0]->dim(0)==1) ? 0 : 1));
Branch: 1 branch
BranchTrueFalseTotal
$data->[0]->dim(0) == 1 ? : 0 0 0
304 0 $xmin -= $dx;
305 0 $xmax += $dx;
306  
307 0 my ($ymin, $ymax) = $data->[1]->minmax;
308 0 ▶ my $dy = 0.5 * ($ymax-$ymin) / ($data->[0]->dim(1) - (($data->[1]->dim(1)==1) ? 0 : 1));
Branch: 1 branch
BranchTrueFalseTotal
$data->[1]->dim(1) == 1 ? : 0 0 0
309 0 $ymin -= $dy;
310 0 $ymax += $dy;
311  
312 0 $plot->dataSets()->{ 1+keys(%{$plot->dataSets()}) } =
313 ds::Grid($data->[2],
314 x_bounds=>[ $xmin, $xmax ],
315 y_bounds=>[ $ymin, $ymax ],
316 0 plotType=>pgrid::Matrix( palette => bless({crange=>$me->{ipo}->{crange},data=>$data,co=>$co},'PDL::Graphics::Simple::Prima::Sepia_Palette')),
317 );
318  
319 0 ▶ if(!!$co->{wedge}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not !$co->{"wedge"}) 0 0 0
320 0 print STDERR "Color wedges are not supported (yet) in Prima\n";
321 }
322 },
323  
324  
325 circles => sub {
326 0 ▶ my($me, $plot, $data, $cprops) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
327 0 our $cstash;
328 0 ▶ unless(defined($cstash)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $PDL::Graphics::Simple::Prima::cstash) 0 0 0
329 0 my $ang = PDL->xvals(362)*3.14159/180;
330 0 $cstash = {};
331 0 $cstash->{c} = $ang->cos;
332 0 $cstash->{s} = $ang->sin;
333 0 $cstash->{s}->slice("361") .= $cstash->{c}->slice("361") .= PDL->pdl(1.1)->acos; # NaN
334 }
335 0 my $dr = $data->[2]->flat;
336 0 my $dx = ($data->[0]->flat->dummy(0,1) + $dr->dummy(0,1)*$cstash->{c})->flat;
337 0 my $dy = ($data->[1]->flat->dummy(0,1) + $dr->dummy(0,1)*$cstash->{s})->flat;
338 0 $plot->dataSets()->{ 1+keys(%{$plot->dataSets()}) } =
339 ds::Pair( $dx, $dy, plotType=>eval q{ppair::Lines}, @$cprops);
340 },
341  
342  
343 labels => sub {
344 0 ▶ my($me,$plot,$block,$cprops,$co) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
345  
346 0 my $x = $block->[0]->flat->sever;
347 0 my $y = $block->[1]->flat->sever;
348 0 my @labels = @{$block->[2]};
349  
350 0 my @lrc = ();
351 0 for my $i(0..$x->dim(0)-1) {
352 0 my $j =0;
353 0 ▶ if($labels[$i] =~ s/^([\<\|\> ])//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($labels[$i] =~ s/^([\<\|\> ])//) 0 0 0
354 0 my $ch = $1;
355 0 ▶ if($ch =~ m/[\|\>]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[\|\>]/) 0 0 0
356 0 my $tw = $plot->get_text_width($labels[$i]);
357 0 ▶ $tw /= 2 if($ch eq '|');
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "|" 0 0 0
358 0 $x->slice("($i)") .=
359 $plot->x->pixels_to_reals(
360 $plot->x->reals_to_pixels( $x->slice("($i)") ) - $tw
361 );
362 }
363 }
364 }
365  
366 0 $plot->dataSets()->{1+keys(%{$plot->dataSets()})} =
367 ds::Note(
368 0 map { eval q{pnote::Text($labels[$_],x=>$x->slice("($_)"),y=>$y->slice("($_)"))}; } (0..$#labels)
369 );
370 }
371 1 };
372  
373 $types->{limitbars} = sub {
374 # Strategy: make T-errorbars out of the x/y/height data and generate a Line
375 # plot. The T-errorbar width is 4x the LineWidth (+/- 2x).
376 0 ▶ my($me, $plot, $block, $cprops, $co) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
377 0 my $x = $block->[0]->flat;
378 0 my $y = $block->[1]->flat;
379 0 my $ylo = $block->[2]->flat;
380 0 my $yhi = $block->[3]->flat;
381
382 # Calculate T bar X ranges
383 0 ▶ my $of = ($co->{width}||1) * 2;
Condition0%0/2
$co->{'width'} || 1
l!l
00
MC/DC0%0/1
$co->{'width'} || 1
$co->{'width'}
Truth table0%0/2
A
A= $co->{'width'}
Aresult
11
00
384 0 my $xp = $plot->x->reals_to_pixels($x);
385 0 my $xlo = $plot->x->pixels_to_reals( $xp - $of );
386 0 my $xhi = $plot->x->pixels_to_reals( $xp + $of );
387 0 my $nan = PDL->new_from_specification($x->dim(0)); $nan .= asin(pdl(1.1));
388
389 0 my $xdraw = pdl($xlo,$xhi,$x, $x, $xlo,$xhi,$nan)->mv(1,0)->flat;
390 0 my $ydraw = pdl($ylo,$ylo,$ylo,$yhi,$yhi,$yhi,$nan)->mv(1,0)->flat;
391 0 $plot->dataSets()->{ 1+keys(%{$plot->dataSets()}) } =
392 ds::Pair($xdraw,$ydraw,plotType=>eval q{ppair::Lines}, @$cprops);
393 0 $plot->dataSets()->{ 1+keys(%{$plot->dataSets()}) } =
394 0 ds::Pair($x,$y,plotType=>eval ($types->{points}->[ ($me->{curvestyle}-1) %(0+@{$types->{points}}) ]), @$cprops);
395 1 };
396  
397  
398 $types->{errorbars} = sub {
399 # Strategy: make T-errorbars out of the x/y/height data and generate a Line
400 # plot. The T-errorbar width is 4x the LineWidth (+/- 2x).
401 0 ▶ my($me, $plot, $block, $cprops, $co) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
402 0 my $halfwidth = $block->[2]->flat;
403 0 $block->[2] = $block->[1] - $halfwidth;
404 0 $block->[3] = $block->[1] + $halfwidth;
405 0 &{$types->{limitbars}}($me, $plot, $block, $cprops, $co);
406 1 };
407  
408
409  
410  
411 ##############################
412 # Plot subroutine
413 #
414 #
415 sub plot {
416 0 ▶ my $me = shift;
Subroutine plot: not called CC=32 SCAR=69.6
Pod undocumented
417 0 my $ipo = shift;
418  
419 0 $me->{ipo} = $ipo;
420  
421 0 ▶ if(defined($ipo->{legend})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ipo->{"legend"}) 0 0 0
422 0 printf(STDERR "WARNING: Ignoring 'legend' option (Legends not yet supported by PDL::Graphics::Simple::Prima v%s)",$PDL::Graphics::Simple::VERSION);
423 }
424
425 0 my $plot;
426
427 0 ▶ if($ipo->{oplot} and defined($me->{last_plot})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipo->{'oplot'} and defined $me->{'last_plot'}) { } 0 0 0
Condition0%0/3
$ipo->{"oplot"} and defined $me->{"last_plot"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ipo->{"oplot"} and defined $me->{"last_plot"}
$ipo->{"oplot"} defined $me->{"last_plot"}
Truth table0%0/3
A and B
A= $ipo->{"oplot"}B= defined $me->{"last_plot"}
ABresult
0X0
100
111
428 0 $plot = $me->{last_plot};
429 } else {
430 0 $me->{curvestyle} = 0;
431
432 0 ▶ if( $me->{multi} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{'multi'}) { } 0 0 0
433 # Multiplot - handle logic and plot placement
434  
435 # Advance to the next plot position. Erase the window if necessary.
436 0 ▶ if($me->{next_plotno} and $me->{next_plotno} >= $me->{multi}->[0] * $me->{multi}->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"next_plotno"} and $me->{"next_plotno"} >= $me->{"multi"}[0] * $me->{"multi"}[1]) 0 0 0
Condition0%0/3
$me->{"next_plotno"} and $me->{"next_plotno"} >= $me->{"multi"}[0] * $me->{"multi"}[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$me->{"next_plotno"} and $me->{"next_plotno"} >= $me->{"multi"}[0] * $me->{"multi"}[1]
$me->{"next_plotno"} $me->{"next_plotno"} >= $me->{"multi"}[0] * $me->{"multi"}[1]
Truth table0%0/3
A and B
A= $me->{"next_plotno"}B= $me->{"next_plotno"} >= $me->{"multi"}[0] * $me->{"multi"}[1]
ABresult
0X0
100
111
437 0 map {$_->destroy} @{$me->{widgets}};
438 0 $me->{widgets} = [];
439 0 $me->{next_plotno} = 0;
440 }
441  
442 0 my $pno = $me->{next_plotno};
443 $plot = $me->{obj}->insert('Plot',
444 place => {
445 relx => ($pno % $me->{multi}->[0])/$me->{multi}->[0],
446 relwidth => 1.0/$me->{multi}->[0],
447 rely => 1.0 - (1 + int($pno / $me->{multi}->[0]))/$me->{multi}->[1],
448 0 relheight => 1.0/$me->{multi}->[1],
449 anchor => 'sw'});
450 0 $me->{next_plotno}++;
451 } else {
452 # No multiplot - just instantiate a plot (and destroy any widgets from earlier)
453 0 map { $_->destroy } @{$me->{widgets}};
454 0 $me->{widgets} = [];
455 0 $plot = $me->{obj}->insert('Plot',
456 pack=>{fill=>'both',expand=>1}
457 );
458 }
459  
460 }
461  
462 0 push(@{$me->{widgets}}, $plot);
463 0 $me->{last_plot} = $plot;
464
465 ## Set global plot options: titles, axis labels, and ranges.
466 0 $plot->hide;
467 0 $plot->lock;
468 0 ▶ $plot->title( $ipo->{title} ) if(defined($ipo->{title}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ipo->{"title"} 0 0 0
469 0 ▶ $plot->x->label( $ipo->{xlabel} ) if(defined($ipo->{xlabel}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ipo->{"xlabel"} 0 0 0
470 0 ▶ $plot->y->label( $ipo->{ylabel} ) if(defined($ipo->{ylabel}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ipo->{"ylabel"} 0 0 0
471
472 0 ▶ $plot->x->scaling(eval q{sc::Log}) if($ipo->{logaxis}=~ m/x/i);
Branch: 1 branch
BranchTrueFalseTotal
if $ipo->{"logaxis"} =~ /x/i 0 0 0
473 0 ▶ $plot->y->scaling(eval q{sc::Log}) if($ipo->{logaxis}=~ m/y/i);
Branch: 1 branch
BranchTrueFalseTotal
if $ipo->{"logaxis"} =~ /y/i 0 0 0
474
475 0 ▶ $plot->x->min($ipo->{xrange}->[0]) if(defined($ipo->{xrange}) and defined($ipo->{xrange}->[0]));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ipo->{"xrange"} and defined $ipo->{"xrange"}[0] 0 0 0
Condition0%0/3
defined $ipo->{"xrange"} and defined $ipo->{"xrange"}[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ipo->{"xrange"} and defined $ipo->{"xrange"}[0]
defined $ipo->{"xrange"} defined $ipo->{"xrange"}[0]
Truth table0%0/3
A and B
A= defined $ipo->{"xrange"}B= defined $ipo->{"xrange"}[0]
ABresult
0X0
100
111
476 0 ▶ $plot->x->max($ipo->{xrange}->[1]) if(defined($ipo->{xrange}) and defined($ipo->{xrange}->[1]));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ipo->{"xrange"} and defined $ipo->{"xrange"}[1] 0 0 0
Condition0%0/3
defined $ipo->{"xrange"} and defined $ipo->{"xrange"}[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ipo->{"xrange"} and defined $ipo->{"xrange"}[1]
defined $ipo->{"xrange"} defined $ipo->{"xrange"}[1]
Truth table0%0/3
A and B
A= defined $ipo->{"xrange"}B= defined $ipo->{"xrange"}[1]
ABresult
0X0
100
111
477 0 ▶ $plot->y->min($ipo->{yrange}->[0]) if(defined($ipo->{yrange}) and defined($ipo->{yrange}->[0]));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ipo->{"yrange"} and defined $ipo->{"yrange"}[0] 0 0 0
Condition0%0/3
defined $ipo->{"yrange"} and defined $ipo->{"yrange"}[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ipo->{"yrange"} and defined $ipo->{"yrange"}[0]
defined $ipo->{"yrange"} defined $ipo->{"yrange"}[0]
Truth table0%0/3
A and B
A= defined $ipo->{"yrange"}B= defined $ipo->{"yrange"}[0]
ABresult
0X0
100
111
478 0 ▶ $plot->y->max($ipo->{yrange}->[1]) if(defined($ipo->{yrange}) and defined($ipo->{yrange}->[1]));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ipo->{"yrange"} and defined $ipo->{"yrange"}[1] 0 0 0
Condition0%0/3
defined $ipo->{"yrange"} and defined $ipo->{"yrange"}[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ipo->{"yrange"} and defined $ipo->{"yrange"}[1]
defined $ipo->{"yrange"} defined $ipo->{"yrange"}[1]
Truth table0%0/3
A and B
A= defined $ipo->{"yrange"}B= defined $ipo->{"yrange"}[1]
ABresult
0X0
100
111
479
480 ##############################
481 # I couldn't find a way to scale the plot to make the plot area justified, so
482 # we cheat and adjust the axis values instead.
483 # This is a total hack, but at least it produces justified plots.
484 0 ▶ if( !!($ipo->{justify}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $ipo->{"justify"}) 0 0 0
485 0 my ($dmin,$pmin,$dmax,$pmax,$xscale,$yscale);
486
487 0 ($dmin,$dmax) = $plot->x->minmax;
488 0 $pmin = $plot->x->reals_to_pixels($dmin);
489 0 $pmax = $plot->x->reals_to_pixels($dmax);
490 0 $xscale = ($pmax-$pmin)/($dmax-$dmin);
491
492 0 ($dmin,$dmax) = $plot->y->minmax;
493 0 $pmin = $plot->y->reals_to_pixels($dmin);
494 0 $pmax = $plot->y->reals_to_pixels($dmax);
495 0 $yscale = ($pmax-$pmin)/($dmax-$dmin);
496
497 0 my $ratio = $yscale / $xscale;
498 0 print "ratio=$ratio\n";
499 0 ▶ if($ratio > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ratio > 1) { } 0 0 0
elsif ($ratio < 1) { } 0 0 0
500 # More Y pixels per datavalue than X pixels. Hence we expand the Y range.
501 0 my $ycen = ($dmax+$dmin)/2;
502 0 my $yof = ($dmax-$dmin)/2;
503 0 my $new_yof = $yof * $yscale/$xscale;
504 0 $plot->y->min($ycen-$new_yof);
505 0 $plot->y->max($ycen+$new_yof);
506 } elsif($ratio < 1) {
507 # More X pixels per datavalue than Y pixels. Hence we expand the X range.
508 0 ($dmin,$dmax) = $plot->x->minmax;
509 0 my $xcen = ($dmax+$dmin)/2;
510 0 my $xof = ($dmax-$dmin)/2;
511 0 my $new_xof = $xof * $xscale/$yscale;
512 0 $plot->x->min($xcen-$new_xof);
513 0 $plot->x->max($xcen+$new_xof);
514 }
515 }
516  
517  
518 ##############################
519 # Rubber meets the road -- loop over data blocks and
520 # ship out each curve to the appropriate dispatcher in the $types table
521 0 for my $block(@_) {
522 0 my $co = shift @$block;
523
524 # Parse out curve style (for points type selection)
525 0 ▶ if(defined($co->{style}) and $co->{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $co->{'style'} and $co->{'style'}) { } 0 0 0
Condition0%0/3
defined $co->{"style"} and $co->{"style"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $co->{"style"} and $co->{"style"}
defined $co->{"style"} $co->{"style"}
Truth table0%0/3
A and B
A= defined $co->{"style"}B= $co->{"style"}
ABresult
0X0
100
111
526 0 $me->{curvestyle} = $co->{style};
527 } else {
528 0 $me->{curvestyle}++;
529 }
530
531 my $cprops = [
532 color => eval $colors[ ($me->{curvestyle}-1) % @colors ],
533 linePattern => eval $patterns[ ($me->{curvestyle}-1) % @patterns ],
534 0 ▶ lineWidth => $co->{width} || 1
Condition0%0/2
$co->{'width'} || 1
l!l
00
MC/DC0%0/1
$co->{'width'} || 1
$co->{'width'}
Truth table0%0/2
A
A= $co->{'width'}
Aresult
11
00
535 ];
536
537 0 my $type = $types->{$co->{with}};
538 0 ▶ if( ref($type) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq 'CODE') { } 0 0 0
539 0 &{$type}($me, $plot, $block, $cprops, $co);
540 } else {
541 0 my $pt;
542 0 ▶ if(ref($type) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $type eq 'ARRAY') { } 0 0 0
elsif (not defined $type) { } 0 0 0
543 0 $pt = eval sprintf("%s",$type->[ ($me->{curvestyle}-1) % (0+@{$type}) ] );
544 } elsif(!defined($type)) {
545 0 die "$co->{with} is not yet implemented in PDL::Graphics::Simple for Prima.\n";
546 } else {
547 0 $pt = eval qq{$type};
548 }
549
550 0 $plot->dataSets()->{ 1+keys(%{$plot->dataSets()}) } = ds::Pair(@$block, plotType => $pt, @$cprops);
551 }
552 }
553  
554 0 ▶ if($me->{type} !~ m/f/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $me->{'type'} =~ /f/i) { } 0 0 0
555 0 $plot->show;
556 0 $plot->unlock;
557 } else {
558 # Belt-and-suspenders to stay hidden
559 0 $plot->hide;
560 0 $me->{obj}->hide;
561 }
562
563 ##############################
564 # Another lame kludge. Run the event loop for 50 milliseconds, to enable a redraw,
565 # then exit it.
566 Prima::Timer->create(
567 0 ▶ onTick=>sub{$_[0]->stop; die "done with event loop\n"},
Subroutine __ANON__: not called CC=1 SCAR=6.9
568 0 timeout=>50
569 )->start;
570 0 eval { $::application->go };
571 0 ▶ die unless $@ =~ /^done with event loop/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /^done with event loop/ 0 0 0
572 0 undef $@;
573  
574 }
575
576
« Simple/PLplot.pm ↑ Summary Simple/Gnuplot.pm »