blib/lib/PDL/Graphics/Prima/Axis.pm untested

filter: Statementstmt 0.0% 0 / 286 Branchbran 0.0% 0 / 128 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 496 CC 65 SCAR 35.6
CC
65
Cov
0%
draw
90.0
init
72.0
update_edges
72.0
CRAP 35.1
1 use strict;
2 use warnings;
3  
4 # We'll make extensive use of limits in this:
5 use PDL::Graphics::Prima::Limits;
6  
7 # Here's a package to handle the axes for me:
8 package PDL::Graphics::Prima::Axis;
9  
10 our $VERSION = 0.17; # update with update-version.pl
11  
12 use PDL::Graphics::Prima::Limits;
13 use PDL::Graphics::Prima::Scaling;
14 use Carp;
15  
16 use PDL;
17 use Prima;
18 our @ISA = qw(Prima::Component);
19  
20 ###################
21 # Default Profile #
22 ###################
23  
24 sub default_tick_format {
25 return sprintf("%1.8g", $_[0]);
26 }
27  
28 sub profile_default {
29 my %def = %{$_[ 0]-> SUPER::profile_default};
30  
31 return {
32 %def,
33 # default properties go here
34 scaling => sc::Linear,
35 min => lm::Auto,
36 max => lm::Auto,
37 label => '',
38 format_tick => \&default_tick_format,
39 };
40 }
41  
42 sub em_dims {
43 my $self = shift;
44 my $points = $self->owner->get_text_box('M');
45 return ($points->[4] - $points->[0], $points->[1] - $points->[3]);
46 }
47  
48 # This initializes self's data from the profile:
49 sub init {
50 my $self = shift;
51 my %profile = $self->SUPER::init(@_);
52 $self->{scaling} = $profile{scaling};
53
54 # I will be using M-widths and heights for various calculations:
55 my (undef, $em_height) = $self->em_dims;
56
57 # The edge requirements depend on whether we're working with an x or a
58 # y axis. Set the non-changing parts here and then set the changing
59 # parts by calling _label. The four elements are the left edge, the
60 # bottom edge, the right edge, and the top edge:
61 if ($self->name =~ /^x/) {
62 $self->{edge_requirements} = [$em_height, 0, $em_height, 0];
63 }
64 else {
65 $self->{edge_requirements} = [0, $em_height, 0, $em_height];
66 }
67 # 're'set the label to force the last set of calculations:
68 $self->_label($profile{label});
69 # verify if format_tick is really a subref if it is defined
70 if (defined $profile{format_tick}) {
71 if (ref($profile{format_tick}) eq ref(sub{})) {
72 $self->{format_tick} = $profile{format_tick};
73 }
74 else {
75 croak ("format_tick must be a sub-routine");
76 }
77 }
78
79 # Process the minima and maxima.
80 if ($profile{min} == lm::Auto) {
81 $self->{minAuto} = 1;
82 $self->{minValue} = 1;
83 }
84 else {
85 croak ("min value must be a real number or lm::Auto")
86 if $profile{min} != $profile{min};
87 $self->{minValue} = $profile{min};
88 $self->{minAuto} = 0;
89 }
90 if ($profile{max} == lm::Auto) {
91 $self->{maxAuto} = 1;
92 $self->{maxValue} = 2;
93 }
94 else {
95 croak ("max value must be a real number or lm::Auto")
96 if $profile{max} != $profile{max};
97 $self->{maxAuto} = 0;
98 $self->{maxValue} = $profile{max};
99 }
100 }
101  
102 # Return the space needed to draw axis labels and tick labels.
103 sub get_edge_requirements {
104 return @{$_[0]->{edge_requirements}};
105 }
106  
107 sub recalculate_edge_requirements {
108 my ($axis, $canvas) = @_;
109
110 # x edge requirements never change (at the moment)
111 return if $axis->name =~ /^x/;
112
113 my ($Ticks) = $axis->get_Ticks_and_ticks;
114 my (undef, $em_height) = $axis->em_dims;
115
116 my $largest_width = 0;
117 for (my $i = 0; $i < $Ticks->nelem; $i++) {
118 # Compute its left extent:
119 my $string = $axis->{format_tick}->($Ticks->at($i));
120 my $points = $canvas->get_text_box($string);
121 $largest_width = $points->[4] if $points->[4] > $largest_width;
122 }
123 $axis->{edge_requirements}->[0] = $largest_width + $em_height/2;
124 $axis->{edge_requirements}->[0] += $em_height * 1.5
125 if (defined $axis->{label} and $axis->{label} ne '');
126 }
127  
128 sub update_edges {
129 my ($self) = @_;
130 return if $self->{initializing};
131
132 # Get and store the new edges:
133 my @edges = $self->owner->get_edge_requirements;
134 if ($self->name =~ /^x/) {
135 $self->{lowerEdge} = $edges[0];
136 $self->{edgeWidth} = $self->owner->width - $edges[2] - $edges[0];
137 }
138 else {
139 $self->{lowerEdge} = $edges[1];
140 $self->{edgeWidth} = $self->owner->height - $edges[3] - $edges[1];
141 }
142
143 return unless $self->{minAuto} or $self->{maxAuto};
144 my ($min, $max) = $self->owner->compute_min_max_for($self->name);
145
146 # Important edge case: when there is no data, or no displayable data (all
147 # bad values or something), we will get undef. We need to be careful to not
148 # be thrown off by that possible return value.
149 if (defined $min and defined $max) {
150 $self->{minValue} = $min if $self->{minAuto};
151 $self->{maxValue} = $max if $self->{maxAuto};
152 }
153
154 # Get and store the new edges, again, as autoscaling might have changed
155 # things:
156 @edges = $self->owner->get_edge_requirements;
157 if ($self->name =~ /^x/) {
158 $self->{lowerEdge} = $edges[0];
159 $self->{edgeWidth} = $self->owner->width - $edges[2] - $edges[0];
160 }
161 else {
162 $self->{lowerEdge} = $edges[1];
163 $self->{edgeWidth} = $self->owner->height - $edges[3] - $edges[1];
164 }
165 }
166  
167 # Accessor functions. There are a bunch of private functions that do
168 # exactly what the public functions would have done, except that they
169 # do not issue a notification.
170 sub _min {
171 my ($self, $new_value) = @_;
172 my ($value, $is_auto) = ($self->{minValue}, $self->{minAuto});
173
174 if (not defined $new_value) {
175 # Handle the case of degenerate min/max in the getter. I suppose I
176 # should always correct the value, since I return the original value
177 # both in the getter and the setter, but I want to avoid unnecessary
178 # logic in the setter if I can get away with it. See also _max and
179 # minmax.
180 ($value) = $self->scaling->min_max_for_degenerate($value)
181 if $self->{minValue} == $self->{maxValue};
182 }
183 elsif ($new_value == lm::Auto) {
184 $self->{minAuto} = 1;
185 $self->update_edges;
186 }
187 elsif ($new_value == lm::HoldNoWarn) {
188 # Hold means they want to keep the current bounds, so stop
189 # auto-scaling:
190 $self->{minAuto} = 0;
191 }
192 else {
193 # Set the min to the new value:
194 $self->{minAuto} = 0;
195 croak("Invalid minimum value: $@")
196 unless ($self->{scaling}->is_valid_extremum($new_value));
197 # Check for degeneracy
198 if ($self->_max == $new_value) {
199 my ($min, $max) = $self->owner->compute_min_max_for($self->name);
200 }
201 else {
202 $self->{minValue} = $new_value;
203 }
204 }
205
206 return $value unless wantarray;
207 return ($value, $is_auto);
208 }
209 {
210 # Create the min function without issuing a redefinition warning
211 no warnings 'redefine';
212 sub min {
213 # If no new value, just calling to get the current values:
214 my $self = shift;
215 my ($value, $is_auto) = $self->_min(@_);
216 $self->notify('ChangeBounds') if (@_);
217
218 return $value unless wantarray;
219 return ($value, $is_auto);
220 }
221 }
222 sub _max {
223 my ($self, $new_value) = @_;
224 my ($value, $is_auto) = ($self->{maxValue}, $self->{maxAuto});
225
226 if (not defined $new_value) {
227 # Handle the case of degenerate min/max in the getter. I suppose I
228 # should always correct the value, since I return the original value
229 # both in the getter and the setter, but I want to avoid unnecessary
230 # logic in the setter if I can get away with it. See also _min and
231 # minmax.
232 (undef, $value) = $self->scaling->min_max_for_degenerate($value)
233 if $self->{minValue} == $self->{maxValue};
234 }
235 elsif ($new_value == lm::Auto) {
236 $self->{maxAuto} = 1;
237 $self->update_edges;
238 }
239 elsif ($new_value == lm::HoldNoWarn) {
240 # Hold means they want to keep the current bounds, so stop
241 # auto-scaling:
242 $self->{maxAuto} = 0;
243 }
244 else {
245 # Set the max to the new value:
246 croak("Invalid maximum value: $@")
247 unless ($self->{scaling}->is_valid_extremum($new_value));
248 $self->{maxAuto} = 0;
249 $self->{maxValue} = $new_value;
250 }
251
252 return $value unless wantarray;
253 return ($value, $is_auto);
254 }
255 {
256 # Create the max function without issuing a redefinition warning
257 no warnings 'redefine';
258 sub max {
259 # If no new value, just calling to get the current values:
260 my $self = shift;
261 my ($value, $is_auto) = $self->_max(@_);
262 $self->notify('ChangeBounds') if (@_);
263
264 return $value unless wantarray;
265 return ($value, $is_auto);
266 }
267 }
268  
269 {
270 # Create the minmax function without issuing a redefinition warning
271 no warnings 'redefine';
272  
273 sub minmax {
274 my $self = shift;
275 my @minmax = ($self->{minValue}, $self->{maxValue});
276 @minmax = $self->scaling->min_max_for_degenerate($minmax[0])
277 if $minmax[0] == $minmax[1];
278 if (@_ > 0) {
279 my ($min, $max) = @_;
280 # Handle autoscaling specially. When both autoscale, the
281 # recompute should only happen once:
282 if ($min == lm::Auto and $max == lm::Auto) {
283 $self->{minAuto} = 1;
284 $self->{maxAuto} = 1;
285 $self->update_edges;
286 }
287 # otherwise call the individual min/max functions:
288 else {
289 $self->_min($min);
290 $self->_max($max);
291 }
292 $self->notify('ChangeBounds');
293 }
294 return @minmax;
295 }
296 }
297  
298 sub scaling {
299 return $_[0]->{scaling} unless $#_;
300 # working here - what if the old limits are invalid with the new scaling,
301 # such as negative limits with logarithmic scaling?
302 $_[0]->{scaling} = $_[1];
303 $_[0]->notify('ChangeScaling');
304 }
305  
306 # low-level setter; does not notify
307 sub _label {
308 my ($self, $label) = @_;
309
310 # Consider recomputing these each time, or setting an accessor for font
311 # settings:
312 my ($em_width, $em_height) = $self->em_dims;
313
314 # Is the label blank or undefined?
315 if (not defined $label or $label eq '') {
316 # Remove the label in that case:
317 $self->{label} = '';
318
319 # Set the edge requirements based on axis type:
320 if ($self->name =~ /^x/) {
321 # Set bottom edge requirement:
322 $self->{edge_requirements}->[1] = 1.5 * $em_height;
323 }
324 else {
325 # Set left edge requirement:
326 $self->{edge_requirements}->[0] = 4 * $em_width;
327 }
328 }
329 else {
330 # Set the label:
331 $self->{label} = $label;
332
333 # Set the edge requirements absed on axis type:
334 if ($self->name =~ /^x/) {
335 # Set bottom edge requirement:
336 $self->{edge_requirements}->[1] = 3 * $em_height;
337 }
338 else {
339 # Set left edge requirement to accomodate the tick labels and
340 # the axis label:
341 $self->{edge_requirements}->[0]
342 = 4 * $em_width + 1.5 * $em_height;
343 }
344 }
345 }
346  
347 sub label {
348 return $_[0]->{label} if @_ == 1;
349 my ($self, $label) = @_;
350 return if $label eq $self->{label};
351 $self->_label($label);
352 $self->notify('ChangeLabel');
353 }
354  
355 #################
356 # Notifications #
357 #################
358 # Add a new notification_type for each of the notifications.
359 {
360 # Keep the notifications hash in its own lexically scoped block so that
361 # other's can't mess with it.
362 my %notifications = (
363 %{Prima::Component-> notification_types()},
364 map { ("Change$_" => nt::Default) } qw(Bounds Scaling Label)
365 );
366
367 sub notification_types { return \%notifications }
368 }
369  
370 sub repaint_parent {
371 my $axis = shift;
372 $axis->owner->notify('Paint');
373
374 # If running in the PDL shell, clear the event queue so this hits
375 # immediately
376 $::application->yield if defined $PERLDL::TERM;
377 }
378 *on_changebounds = \&repaint_parent;
379 *on_changescaling = \&repaint_parent;
380 sub on_changelabel {
381 my $self = shift;
382 $self->update_edges;
383 $self->repaint_parent;
384 }
385  
386 sub reals_to_relatives {
387 my ($axis, $dataset, $min, $max) = @_;
388 $min = $axis->{minValue} unless defined $min;
389 $max = $axis->{maxValue} unless defined $max;
390 ($min, $max) = $axis->scaling->min_max_for_degenerate($min)
391 if $min == $max;
392 return $axis->{scaling}->transform($min, $max, $dataset);
393 }
394  
395 sub relatives_to_reals {
396 my ($axis, $dataset, $min, $max) = @_;
397 $min = $axis->{minValue} unless defined $min;
398 $max = $axis->{maxValue} unless defined $max;
399 ($min, $max) = $axis->scaling->min_max_for_degenerate($min)
400 if $min == $max;
401 return $axis->{scaling}->inv_transform($min, $max, $dataset);
402 }
403  
404 sub pixels_to_relatives {
405 my ($axis, $dataset, $ratio) = @_;
406 $ratio = 1 unless defined $ratio;
407 # Recall: $axis->{edgeWidth} is the width of the plot in pixels, computed
408 # during the edge_requirements updates
409 return ($dataset - ($ratio * $axis->{lowerEdge})) / ($ratio * $axis->{edgeWidth});
410 }
411  
412 sub relatives_to_pixels {
413 my ($axis, $dataset, $ratio) = @_;
414 $ratio = 1 unless defined $ratio;
415 # Recall: $axis->{edgeWidth} is the width of the plot in pixels, computed
416 # during the edge_requirements updates
417 return ($ratio * $axis->{edgeWidth}) * $dataset + ($ratio * $axis->{lowerEdge});
418 }
419  
420 sub reals_to_pixels {
421 my ($axis, $dataset, $ratio, @args) = @_;
422 $ratio = 1 unless defined $ratio;
423 return $axis->relatives_to_pixels($axis->reals_to_relatives($dataset, @args), $ratio);
424 }
425  
426 sub pixels_to_reals {
427 my ($axis, $dataset, $ratio, @args) = @_;
428 $ratio = 1 unless defined $ratio;
429 return $axis->relatives_to_reals($axis->pixels_to_relatives($dataset, $ratio), @args);
430 }
431  
432 sub get_Ticks_and_ticks {
433 my ($axis, $ratio) = @_;
434
435 my ($em_width, $em_height) = $axis->em_dims;
436 my ($padded_min, $padded_max);
437
438 if ($axis->name =~ /^x/) {
439 $padded_min = $axis->pixels_to_reals(
440 $axis->reals_to_pixels(scalar ($axis->min), $ratio) - $em_width,
441 $ratio);
442 $padded_max = $axis->pixels_to_reals(
443 $axis->reals_to_pixels(scalar ($axis->max), $ratio) + $em_width,
444 $ratio);
445 }
446 else {
447 $padded_min = $axis->pixels_to_reals(
448 $axis->reals_to_pixels(scalar ($axis->min), $ratio) - $em_height/2,
449 $ratio);
450 $padded_max = $axis->pixels_to_reals(
451 $axis->reals_to_pixels(scalar ($axis->max), $ratio) + $em_height/2,
452 $ratio);
453 }
454
455 # Get the locations for the major and minor ticks:
456 my ($Ticks, $ticks) = $axis->{scaling}->compute_ticks($padded_min, $padded_max);
457 croak("Something is wrong with the axis scaling type " . ref($axis->{scaling})
458 . ": it returned only a single major tick mark for the axis range "
459 . $axis->min . " to " . $axis->max)
460 if $Ticks->nelem < 2;
461
462 return ($Ticks, $ticks);
463 }
464  
465 # Needed for refaddr
466 use Scalar::Util qw(refaddr);
467  
468 sub draw {
469 my ($axis, $canvas, $clip_left, $clip_bottom, $clip_right, $clip_top
470 , $ratio) = @_;
471
472 # We're going to ask for tick marks that extend slightly beyond the edge
473 # of the viewable area so that we can do fancy tick labeling that slides
474 # off the edge
475 my ($em_width, $em_height) = $axis->em_dims;
476 $em_width *= $ratio;
477 $em_height *= $ratio;
478 my ($Ticks, $ticks) = $axis->get_Ticks_and_ticks($ratio);
479
480 # Rescale to pixels:
481 my $ticks_pixels = $axis->reals_to_pixels($ticks, $ratio);
482 my $Ticks_pixels = $axis->reals_to_pixels($Ticks, $ratio);
483
484 # At some point, make this call the function $axis->{drawing_function} or
485 # some such. In the meantime, just draw it on the edges:
486 # working here - make this prettier
487 my ($canv_width, $canv_height) = $canvas->size;
488 my $tick_length = 8 * $ratio; #0.8 * sqrt($canv_width < $canv_height ? $canv_height : $canv_width);
489 my $Tick_size = pdl($tick_length, -$tick_length)->transpose;
490 my $tick_size = $Tick_size / 2;
491
492 my $top_bottom = pdl($clip_bottom, $clip_top)->transpose;
493 my $left_right = pdl($clip_left, $clip_right)->transpose;
494
495 if ($axis->name =~ /^x/) {
496 # Ensure the tick marks are exactly clipped:
497 $canvas->clipRect($clip_left-0.5, 0, $clip_right, $canvas->height);
498
499 # Tweak for postscript output (though perhaps this should be applied to
500 # all vector formats)
501 $canvas->clipRect($clip_left-1.75, 0, $clip_right+0.3, $canvas->height)
502 if $canvas->isa('Prima::PS::Drawable');
503  
504 # Draw the minor tick marks:
505 $canvas->pdl_lines($ticks_pixels, $top_bottom, $ticks_pixels, $top_bottom + $tick_size
506 , lineWidth => 2);
507 # Draw the major tick marks:
508 $canvas->pdl_lines($Ticks_pixels, $top_bottom, $Ticks_pixels, $top_bottom + $Tick_size
509 , lineWidth => 2);
510 # Draw lines on the top/bottom, drawing wider so that the edges are
511 # correctly handled by the clipping
512 $canvas->pdl_lines($left_right->at(0,0)-3, $top_bottom, $left_right->at(0,1)+1, $top_bottom
513 , lineWidth => 2);
514
515 # Figure out the top of the axis labels:
516 my $label_top = $clip_bottom - $em_height/4;
517
518 # Ensure that the text can flow slightly beyond the edges, but not
519 # too far:
520 $canvas-> clipRect( $clip_left - $em_width/2, 0
521 , $clip_right + $em_width/2, $canvas-> height);
522
523 # Draw all the tick labels
524 for (my $i = 0; $i < $Ticks->nelem; $i++) {
525 my $x = $Ticks_pixels->at($i);
526 my $string = $axis->{format_tick}->($Ticks->at($i));
527
528 # Draw the label:
529 $canvas->draw_text($string, $x-80, 0, $x+80, $label_top
530 , dt::Top | dt::Center | dt::NoWordWrap
531 | dt::UseExternalLeading);
532
533 }
534 # Recompute the lowerEdge and edgeWidth. XXX Is this still necessary?
535 my @edges = $axis->owner->get_edge_requirements;
536 $axis->{lowerEdge} = $edges[0];
537 $axis->{edgeWidth} = $axis->owner->width - $edges[2] - $edges[0];
538
539 # Restore the canvas clipping
540 $canvas-> clipRect( 0, 0, $canvas->size);
541  
542 # Draw the label, if it exists
543 if ($axis->{label}) {
544 $canvas->draw_text($axis->{label}, $clip_left, 0.25 * $em_height
545 , $clip_right, 1.5 * $em_height
546 , dt::Center | dt::Top | dt::NewLineBreak | dt::NoWordWrap
547 | dt::UseExternalLeading);
548 }
549 }
550 else { # y-axis drawing
551 # Clip the canvas so the tick marks are not drawn outside the
552 # boundign box:
553 $canvas->clipRect(0, $clip_bottom, $canvas->width, $clip_top);
554
555 # draw the minor tick marks:
556 $canvas->pdl_lines($left_right, $ticks_pixels, $left_right + $tick_size, $ticks_pixels
557 , lineWidth => 2);
558 # draw the major tick marks:
559 $canvas->pdl_lines($left_right, $Ticks_pixels, $left_right + $Tick_size, $Ticks_pixels
560 , lineWidth => 2);
561 # Draw lines on the left/right:
562 $canvas->pdl_lines($left_right, $top_bottom->at(0,0), $left_right, $top_bottom->at(0,1)
563 , lineWidth => 2);
564
565 # Figure out the right edge of the axis labels:
566 my $label_right = $clip_left - $em_height/4;
567
568 # Clip the canvas so that the tick labels can flow off the screen
569 $canvas-> clipRect( 0, $clip_bottom - $em_height/2
570 , $canvas-> width, $clip_top + $em_height/2);
571  
572 # Draw all the labels, storing the minimum left edge used, so that
573 # future calls to the draw function look reasonable:
574 my $largest_width = 0;
575 for (my $i = 0; $i < $Ticks->nelem; $i++) {
576 my $y = $Ticks_pixels->at($i);
577 $y += $em_height/6 if $canvas->isa('Prima::PS::Drawable');
578 my $string = $axis->{format_tick}->($Ticks->at($i));
579
580 # Draw the label:
581 $canvas->draw_text($string
582 , 0, $y - $em_height, $label_right, $y + $em_height
583 , dt::Right | dt::VCenter | dt::NoWordWrap
584 | dt::UseExternalLeading);
585
586 # Compute its left extent:
587 my $points = $canvas->get_text_box($string);
588 $largest_width = $points->[4] if $points->[4] > $largest_width;
589 }
590 if (refaddr($canvas) == refaddr($axis->owner)) {
591 $axis->{edge_requirements}->[0] = $largest_width + $em_height/2;
592 $axis->{edge_requirements}->[0] += $em_height * 1.5
593 if (defined $axis->{label} and $axis->{label} ne '');
594 }
595
596 # Restore the canvas clipping
597 $canvas-> clipRect( 0, 0, $canvas->size);
598  
599 # Draw the label if it exists
600 if ($axis->{label}) {
601 # Determine the width of the text that we will draw:
602 my $text_width = $canvas->get_text_width($axis->{label});
603
604 # Rotate so we have vertical text. However, the rotation is
605 # performed around the upper- (lower-?) left corner (regardless of the
606 # dt:: constants, as far as I can tell) and occurs after the
607 # location of the text has been determined. It would be nice to
608 # be able to specify the point about which the rotation should
609 # take place.
610 $canvas->font(direction => 90);
611 # I'd like to say this:
612 #$canvas->draw_text($axis->{label}, 0.25 * $em_height, $clip_bottom,
613 # , 1.5 * $em_height, $clip_top
614 # , dt::VCenter | dt::Left | dt::NewLineBreak | dt::NoWordWrap
615 # | dt::UseExternalLeading);
616 # But instead I have to say this:
617 $canvas->draw_text($axis->{label}, 1.25 * $em_height
618 , ($clip_top + $clip_bottom) / 2 - $text_width / 2,
619 , 1.5 * $em_height, $clip_top,
620 , dt::Bottom | dt::Left | dt::NewLineBreak | dt::NoWordWrap
621 | dt::UseExternalLeading);
622 $canvas->font(direction => 0);
623 }
624 }
625 }
626  
627 1;
628  
629 __END__