Quiq/Axis/Numeric.pm

filter: Statementstmt 17.6% 25 / 142 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 66.6% 8 / 12 Podpod 100% 4 / 4 totaltotal 13.7% 37 / 269 CC 51 SCAR 73.2
CC
51
Cov
11%
new
2070.0
firstTick
30.0
labelSize
6.0
CRAP 1505.4
« Quiq/Gnuplot/Process.pm ↑ Summary Quiq/LaTeX/Figure.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Axis::Numeric - Definition einer numerischen Achse
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Axis>
12  
13 =head1 SYNOPSIS
14  
15 $ax = Quiq::Axis::Numeric->new(
16 orientation => $str, # 'x', 'y'
17 font => $font,
18 length => $int,
19 min => $float,
20 max => $float,
21 logarithmic => $bool,
22 minTickGap => $int,
23 debug => $bool,
24 );
25
26 # Haupt-Ticks
27 @ticks = $ax->ticks;
28  
29 =head1 DESCRIPTION
30  
31 Ein Objekt der Klasse repräsentiert die Definition einer numerischen
32 Achse eines XY-Plots. Die Achse kann eine X- oder eine Y-Achse sein.
33 Die Klasse berechnet eine geeignete Einteilung der Achse in Ticks
34 und Unter-Ticks unter Berücksichtigung der echten Breite/Höhe der
35 Tick-Label. Die betreffenden Tick-Objekte werden von den Methoden
36 $ax->ticks() und $ax->subTicks() geliefert.
37  
38 =head2 Font-Klasse
39  
40 Die Achseneinteilung kann für beliebige Fonts berechnet werden. Es
41 wird lediglich vorausgesetzt, dass die Font-Klasse zwei Methoden zur
42 Berechnung der Label-Breite und der Label-Höhe implementiert:
43  
44 $n = $fnt->stringWidth($str);
45 $n = $fnt->stringHeight($str);
46  
47 =head1 ATTRIBUTES
48  
49 =over 4
50  
51 =item orientation => 'x'|'y' (Default: 'x')
52  
53 Orientierung der Achse: 'x' oder 'y'.
54  
55 =item font => $font (Default: keiner)
56  
57 Font für die Tick-Label.
58  
59 =item length => $int (Default: keiner)
60  
61 Länge der Achse.
62  
63 =item min => $float (Default: keiner)
64  
65 Kleinster Wert auf der Achse.
66  
67 =item max => $float (Default: keiner)
68  
69 Größter Wert auf der Achse.
70  
71 =item logarithmic => $bool (Default: 0)
72  
73 Die Achse erhält eine logarithmische Einteilung (Basis 10).
74  
75 =item minTickGap => $int (Default: 5)
76  
77 Mindestabstand zwischen zwei Ticks: C<< LABELL<lt>--minTickGap-->LABEL >>
78  
79 =item debug => $bool (Default: 0)
80  
81 Gib Information über die Tickberechnung auf STDERR aus.
82  
83 =back
84  
85 =cut
86  
87 # -----------------------------------------------------------------------------
88  
89 package Quiq::Axis::Numeric;
90 2 ▶use base qw/Quiq::Axis/;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 2 our $VERSION = '1.240';
97  
98 2 ▶use Quiq::Math;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 2 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 2 ▶use Quiq::AxisTick;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 2 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 # -----------------------------------------------------------------------------
104  
105 =head1 METHODS
106  
107 =head2 Konstruktor
108  
109 =head3 new() - Instantiiere Achsen-Objekt
110  
111 =head4 Synopsis
112  
113 $ax = Quiq::Axis::Numeric->new(@keyVal);
114  
115 =head4 Description
116  
117 Instantiiere ein Achsen-Objekt auf Basis der Angaben @keyVal,
118 berechne die beste Achseneinteilung, und liefere eine Referenz auf
119 das Objekt zurück.
120  
121 =cut
122  
123 # -----------------------------------------------------------------------------
124  
125 0 ▶sub new {
Subroutine new: not called CC=45 SCAR=76.4
Pod documented
126 0 my $class = shift;
127 # @_: @keyVal
128  
129 0 my $self = $class->SUPER::new(
130 orientation => 'x',
131 font => undef,
132 length => undef,
133 min => undef,
134 max => undef,
135 logarithmic => 0,
136 minTickGap => undef,
137 debug => 0,
138 # ---
139 step => undef, # Referenz auf Step-Objekt
140 tickA => [],
141 subTickA => [],
142 );
143 0 $self->set(@_);
144  
145 # * Prüfe Attributwerte *
146  
147 # Default minTickGap
148  
149 0 my $minTickGap = $self->get('minTickGap');
150 0 ▶ if (!defined $minTickGap) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $minTickGap) 0 0 0
151 0 ▶ $minTickGap = $self->get('orientation') eq 'x'? 6: 6;
Branch: 1 branch
BranchTrueFalseTotal
$self->get('orientation') eq 'x' ? : 0 0 0
152 0 $self->set(minTickGap=>$minTickGap);
153 }
154  
155 # * Berechne Ticks *
156  
157 0 my ($length,$min,$max,$logarithmic,$debug) =
158 $self->get(qw/length min max logarithmic debug/);
159  
160 0 ▶ if ($logarithmic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logarithmic) 0 0 0
161 0 $min = POSIX::log10($min);
162 0 $max = POSIX::log10($max);
163 }
164  
165 0 my $minTickSize = $self->labelSize(int $min); # min. Breite bzw. Höhe
166 0 my $maxTicks = $length/$minTickSize;
167 0 my $minStep = ($max-$min)/$maxTicks;
168 0 my $maxStep = $max-$min;
169  
170 # Mögliche Schrittweiten ermitteln
171  
172 0 my @candidates;
173  
174 LOOP_EXP:
175 0 for my $exp (-10 .. 10) { # Exponent
176 LOOP_BASE:
177 0 for my $base (1,2,5) { # Basiswert
178 0 ▶ if ($logarithmic && ($base != 1 || $exp != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logarithmic and $base != 1 || $exp != 0) 0 0 0
Condition0%0/6
$logarithmic and $base != 1 || $exp != 0
!ll&&!rl&&r
000
$base != 1 or $exp != 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$logarithmic and $base != 1 || $exp != 0
$logarithmic $base != 1 $exp != 0
Truth table0%0/4
A and B or C
A= $logarithmicB= $base != 1C= $exp != 0
ABCresult
0XX0
1000
11X1
1011
179 # Im Falle von Logarithmus-Werten ist die Einteilung auf
180 # jeden Fall in 1er-Schritten. Alle anderen Schrittweiten
181 # übergehen wir.
182 0 next;
183 }
184  
185 0 my $step = $base*10**$exp; # Schrittweite
186  
187 0 ▶ if ($step < $minStep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step < $minStep) 0 0 0
188 # Weiter: die Schrittweite ist so klein, es würden
189 # mehr Ticks als theoretisch auf die Achse passen
190 0 next;
191 }
192  
193 0 my $val = $self->firstTick($step);
194 0 ▶ if ($val > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val > $max) 0 0 0
195 # Ende: erster Tick liegt jenseits der Achse
196 0 last LOOP_EXP;
197 }
198  
199 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
200 0 print STDERR "$step: ";
201 }
202  
203 # Alle Ticks für die Schrittweite durchlaufen und prüfen, ob
204 # genug Raum für jedes Label ist. Gibt es eine Überlappung,
205 # wird mit der nächsten Schrittweite weiter gemacht.
206  
207 0 my (@values,$minPos,$maxPos,$minGap);
208 # for (; $val <= $max; $val += $step) {
209 0 ▶ while ($val <= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val <= $max) 0 0 0
210 0 push @values,$val;
211 0 my $pos = Quiq::Math->valueToPixel($length,$min,$max,$val);
212 0 my $size = $self->labelSize($val); # Größe Tick
213 0 my $pos0 = int($pos-$size/2); # Anfangsposition Tick
214 0 my $pos1 = int($pos+$size/2); # Endposition Tick
215
216 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
217 0 print STDERR " $val($pos0/$pos/$pos1)";
218 }
219 0 ▶ if (defined $maxPos) { # beim ersten Step keine Überlappung
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maxPos) 0 0 0
220 0 ▶ if ($pos0 > $maxPos+$minTickGap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos0 > $maxPos + $minTickGap) { } 0 0 0
221 0 my $gap = $pos0-$maxPos;
222 0 ▶ if (!defined($minGap) || $gap < $minGap) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $minGap or $gap < $minGap) 0 0 0
Condition0%0/3
not defined $minGap or $gap < $minGap
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $minGap or $gap < $minGap
not defined $minGap $gap < $minGap
Truth table0%0/3
not(A) or B
A= not defined $minGapB= $gap < $minGap
ABresult
1X1
011
000
223 0 $minGap = $gap;
224 }
225 }
226 else {
227 # Weiter: Label überlappen sich
228 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
229 0 print STDERR " Ueberlappung\n";
230 }
231 0 next LOOP_BASE;
232 }
233 }
234 0 ▶ if (!defined $minPos) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $minPos) 0 0 0
235 0 $minPos = $pos0; # erste Pixelkoordinate
236 }
237 0 $maxPos = $pos1; # letzte Pixelkoordinate
238  
239 # Nächster Tick-Wert. Bei Schrittweiten < 0 müssen wir
240 # runden, da sonst manchmal der letzte Tick nicht
241 # hinzugenommen wird (offenbar ist der Wert > $max)
242  
243 0 ▶ $val = sprintf '%.*f',($exp < 0? abs $exp: 0),$val+$step;
Branch: 1 branch
BranchTrueFalseTotal
$exp < 0 ? : 0 0 0
244 0 ▶ if (index($val,'.') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($val, ".") != -1) 0 0 0
245 0 $val =~ s/\.?0+$//;
246 }
247 }
248  
249 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
250 0 ▶ printf STDERR " minGap=%s\n",$minGap || '';
Condition0%0/2
$minGap || ''
l!l
00
MC/DC0%0/1
$minGap || ''
$minGap
Truth table0%0/2
A
A= $minGap
Aresult
11
00
251 }
252  
253 0 push @candidates,Quiq::Hash->new(
254 # Weltkoordinaten
255 step => $step, # Schrittweite
256 base => $base, # 1, 2, oder 5
257 exp => $exp, # -10 .. 10
258 valueA => \@values, # Liste der Ticks
259 # Pixelkoordinaten
260 tickDistance => Quiq::Math->valueToPixel($length,$min,
261 $max,$step),
262 minGap => $minGap, # kleinster Pixelabstand zw. Ticks
263 minPos => $minPos, # erste Pixelposition (kann < 0 sein)
264 maxPos => $maxPos, # letzte Pixelposition
265 # (kann >= $length sein)
266 );
267  
268 0 ▶ if ($step > $maxStep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > $maxStep) 0 0 0
269 # Ende: die folgenden Schrittweiten sind so groß,
270 # dass sie auch höchstens einen Tick produzieren, wie
271 # die aktuelle Schrittweite auch schon
272 0 last LOOP_EXP;
273 }
274 }
275 }
276  
277 0 ▶ if (!@candidates) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@candidates) 0 0 0
278 # Keine Ticks, wir haben Keine passende Schrittweite gefunden.
279 # Dies kann bei einer logarithmischen Achse passieren, wenn
280 # innerhalb des Wertebereichs kein ganzahliger Logarithmus vorkommt.
281 0 return $self;
282 }
283  
284 # Die beste Schrittweite auswählen
285  
286 0 my $stp;
287 0 for my $e (@candidates) {
288 0 ▶ if ($e->get('base') == 1 && @{$e->get('valueA')} >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->get("base") == 1 and @{$e->get("valueA");} >= 6) 0 0 0
Condition0%0/3
$e->get("base") == 1 and @{$e->get("valueA");} >= 6
!ll&&!rl&&r
000
MC/DC0%0/2
$e->get("base") == 1 and @{$e->get("valueA");} >= 6
$e->get("base") == 1 @{$e->get("valueA");} >= 6
Truth table0%0/3
A and B
A= $e->get("base") == 1B= @{$e->get("valueA");} >= 6
ABresult
0X0
100
111
289 # Wir bevorzugen die 10er-Einteilung mit genügend Ticks
290 0 $stp = $e;
291 0 last;
292 }
293 }
294 0 ▶ $stp ||= $candidates[0];
Condition0%0/3
$stp ||= $candidates[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$stp ||= $candidates[0]
$stp $candidates[0]
Truth table0%0/3
A ||= B
A= $stpB= $candidates[0]
ABresult
1X1
011
000
295  
296 # Step-Definition sichern
297 0 $self->set(step=>$stp);
298  
299 # * Tick-Listen erstellen *
300  
301 # tick
302  
303 0 my @values = @{$stp->get('valueA')};
304  
305 0 my $tickA = $self->ticks;
306 0 for my $val (@values) {
307 0 push @$tickA,Quiq::AxisTick->new($self,$val);
308 }
309  
310 # subTick
311  
312 0 my ($step,$base,$tickDistance) = $stp->get(qw/step base tickDistance/);
313  
314 0 my $subTickA = $self->subTicks;
315 0 ▶ if ($self->get('logarithmic')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get('logarithmic')) { } 0 0 0
316 0 for my $log ($values[0]-1,@values) {
317 0 my $val = 10**$log;
318 0 my $subStep = $val;
319 0 for my $i (1..8) {
320 0 $val += $subStep;
321 0 my $log = POSIX::log10($val);
322 0 ▶ if ($log >= $min && $log <= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log >= $min and $log <= $max) 0 0 0
Condition0%0/3
$log >= $min and $log <= $max
!ll&&!rl&&r
000
MC/DC0%0/2
$log >= $min and $log <= $max
$log >= $min $log <= $max
Truth table0%0/3
A and B
A= $log >= $minB= $log <= $max
ABresult
0X0
100
111
323 0 push @$subTickA,Quiq::AxisTick->new($self,$log);
324 }
325 }
326 }
327 }
328 else {
329 0 my $numSubSteps;
330 0 ▶ if ($base == 1 || $base == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($base == 1 or $base == 2) { } 0 0 0
elsif ($base == 5 and $tickDistance >= 40) { } 0 0 0
Condition0%0/3
$base == 1 or $base == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$base == 1 or $base == 2
$base == 1 $base == 2
Truth table0%0/3
A or B
A= $base == 1B= $base == 2
ABresult
1X1
011
000
331 0 $numSubSteps = 1;
332 }
333 ▶ elsif ($base == 5 && $tickDistance >= 40) {
Condition0%0/3
$base == 5 and $tickDistance >= 40
!ll&&!rl&&r
000
MC/DC0%0/2
$base == 5 and $tickDistance >= 40
$base == 5 $tickDistance >= 40
Truth table0%0/3
A and B
A= $base == 5B= $tickDistance >= 40
ABresult
0X0
100
111
334 0 $numSubSteps = 4;
335 }
336  
337 0 ▶ if (defined $numSubSteps) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $numSubSteps) 0 0 0
338 0 my $subStep = $step/($numSubSteps+1);
339  
340 0 for my $tickVal ($values[0]-$step,@values) {
341 0 for my $i (1..$numSubSteps) {
342 0 my $val = $tickVal+$i*$subStep;
343 0 ▶ if ($val >= $min && $val <= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val >= $min and $val <= $max) 0 0 0
Condition0%0/3
$val >= $min and $val <= $max
!ll&&!rl&&r
000
MC/DC0%0/2
$val >= $min and $val <= $max
$val >= $min $val <= $max
Truth table0%0/3
A and B
A= $val >= $minB= $val <= $max
ABresult
0X0
100
111
344 0 push @$subTickA,Quiq::AxisTick->new($self,$val);
345 }
346 }
347 }
348 }
349 }
350  
351 0 return $self;
352 }
353  
354 # -----------------------------------------------------------------------------
355  
356 =head2 Objektmethoden
357  
358 Siehe Basisklasse
359  
360 =head2 Hilfsmethoden
361  
362 =head3 firstTick() - Wert des ersten Tick
363  
364 =head4 Synopsis
365  
366 $val = $ax->firstTick($step);
367  
368 =head4 Description
369  
370 Liefere für Schrittweite $step den ersten Tick in Weltkoordinaten.
371  
372 =cut
373  
374 # -----------------------------------------------------------------------------
375  
376 0 ▶sub firstTick {
Subroutine firstTick: not called CC=5 SCAR=34.0
Pod documented
377 0 my ($self,$step) = @_;
378  
379 0 my $val;
380  
381 0 my ($min,$logarithmic) = $self->get('min','logarithmic');
382 0 ▶ if ($logarithmic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logarithmic) 0 0 0
383 0 $min = POSIX::log10($min);
384 }
385 0 ▶ if ($min < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < 0) { } 0 0 0
386 0 $val = POSIX::ceil($min/$step)*$step;
387 0 ▶ if ($val >= $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val >= $min) 0 0 0
388 0 $val -= $step;
389 }
390 }
391 else { # $min >= 0
392 0 $val = POSIX::floor($min/$step)*$step;
393 0 ▶ if ($val < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < $min) 0 0 0
394 0 $val += $step;
395 }
396 }
397  
398 0 return $val;
399 }
400  
401 # -----------------------------------------------------------------------------
402  
403 =head3 labelSize() - Tickgröße in Pixeln
404  
405 =head4 Synopsis
406  
407 $n = $ax->labelSize($val);
408  
409 =head4 Description
410  
411 Liefere für Wert $val den Platzbedarf des Label in Pixeln.
412 FIXME: Wert in Label wandeln.
413  
414 =cut
415  
416 # -----------------------------------------------------------------------------
417  
418 0 ▶sub labelSize {
Subroutine labelSize: not called CC=2 SCAR=17.9
Pod documented
419 0 my ($self,$val) = @_;
420  
421 0 my ($orientation,$fnt) = $self->get(qw/orientation font/);
422  
423 0 my $label = $self->label($val);
424 0 ▶ if ($orientation eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($orientation eq "y") 0 0 0
425 # Y-Achse
426 0 return $fnt->stringHeight($label);
427 }
428  
429 # X-Achse
430 0 return $fnt->stringWidth($label);
431 }
432  
433 # -----------------------------------------------------------------------------
434  
435 =head3 label() - Liefere Achsenlabel zu Wert
436  
437 =head4 Synopsis
438  
439 $label = $ax->label($val);
440  
441 =head4 Description
442  
443 Liefere das Achsenlabel für Wert $val. Das Label $label kann vom
444 Wert $val verschieden sein, wenn die Darstellung logarithmisch ist.
445  
446 =cut
447  
448 # -----------------------------------------------------------------------------
449  
450 0 ▶sub label {
Subroutine label: not called CC=2 SCAR=17.9
Pod documented
451 0 my ($self,$val) = @_;
452  
453 0 ▶ if ($self->get('logarithmic')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get("logarithmic")) 0 0 0
454 0 return 10**$val;
455 }
456  
457 0 return $val;
458 }
459  
460 # -----------------------------------------------------------------------------
461  
462 =head1 VERSION
463  
464 1.240
465  
466 =head1 AUTHOR
467  
468 Frank Seitz, L<http://fseitz.de/>
469  
470 =head1 COPYRIGHT
471  
472 Copyright (C) 2026 Frank Seitz
473  
474 =head1 LICENSE
475  
476 This code is free software; you can redistribute it and/or modify
477 it under the same terms as Perl itself.
478  
479 =cut
480  
481 # -----------------------------------------------------------------------------
482  
483 2 1;
484  
485 # eof
« Quiq/Gnuplot/Process.pm ↑ Summary Quiq/LaTeX/Figure.pm »