Quiq/Gd/Component/Axis.pm

filter: Statementstmt 15.6% 16 / 102 Branchbran 0.0% 0 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 5 / 10 Podpod 100% 5 / 5 totaltotal 16.3% 26 / 159 CC 24 SCAR 55.5
CC
24
Cov
11%
render
420.0
width
12.0
height
12.0
CRAP 257.0
« Quiq/Axis/Time.pm ↑ Summary Quiq/Html/Tag.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Gd::Component::Axis - Achse eines XY-Plot
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Gd::Component>
12  
13 =head1 SYNOPSIS
14  
15 Numerische X-Achse definieren:
16  
17 $ax = Quiq::Axis::Numeric->new(
18 orientation => 'x',
19 font => Quiq::Gd::Font->new('gdSmallFont'),
20 length => 400,
21 min => 0,
22 max => 100,
23 );
24  
25 Achsengrafik-Objekt erzeugen:
26  
27 $g = Quiq::Gd::Component::Axis->new(axis=>$ax);
28  
29 Vertikalen Platzbedarf der Achsengrafik ermitteln:
30  
31 $height = $g->height;
32  
33 Achsengrafik in Bild rendern:
34  
35 $g->render($img,$x,$y);
36  
37 =head1 DESCRIPTION
38  
39 Ein Objekt der Klasse repräsentiert eine gezeichnete X- oder Y-Achse
40 einschließlich Ticks und Labeln. Mit den Methoden C<< $g->width() >>
41 und C<< $g->height() >> kann der Platzbedarf der Achse ermittelt werden,
42 I<bevor> sie konkret gezeichnet wird.
43  
44 =head1 ATTRIBUTES
45  
46 =over 4
47  
48 =item axis => $ax (Pflichtargument des Konstruktors)
49  
50 Referenz auf die Achsen-Definition.
51  
52 =item axisColor => $color (Default: '000000')
53  
54 Farbe der Achse.
55  
56 =item labelColor => $color (Default: Farbe der Achse)
57  
58 Farbe der Schrift.
59  
60 =item subTickColor => $color (Default: Farbe der Achse)
61  
62 Farbe der Sub-Ticks.
63  
64 =item tickColor => $color (Default: Farbe der Achse)
65  
66 Farbe der Ticks.
67  
68 =item tickDirection => $direction (Default: 'd' bei X-Achse, 'l' bei Y-Achse)
69  
70 Richtung, die die Ticks der Achse sowie die Label gezeichnet werden.
71 Mögliche Werte bei einer X-Achse: 'u' (up), 'd' (down). Mögliche Werte
72 bei einer Y-Achse: 'l' (left), 'r' (right).
73  
74 =item tickLabelGap => $n (Default: 1)
75  
76 Abstand zwischen Tick und Label.
77  
78 =item tickLength => $n (Default: 4)
79  
80 Länge eines beschrifteten Tick.
81  
82 =back
83  
84 =head1 EXAMPLES
85  
86 Quelltext:
87  
88 r1-gd-graphic-axis-example
89  
90 =head2 X-Achse bei verschiedenen Fontgrößen
91  
92 [Bild nur im Browser sichtbar]
93  
94 =head2 Y-Achse bei verschiedenen Fontgrößen
95  
96 [Bild nur im Browser sichtbar]
97  
98 =head2 X-Achse mit logarithmischer Skala
99  
100 [Bild nur im Browser sichtbar]
101  
102 =head2 XY-Diagramm
103  
104 [Bild nur im Browser sichtbar]
105  
106 =cut
107  
108 # -----------------------------------------------------------------------------
109  
110 package Quiq::Gd::Component::Axis;
111 2 ▶use base qw/Quiq::Gd::Component/;
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 2 our $VERSION = '1.240';
118  
119 2 ▶use Quiq::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
120  
121 # -----------------------------------------------------------------------------
122  
123 =head1 METHODS
124  
125 =head2 Konstruktor
126  
127 =head3 new() - Konstruktor
128  
129 =head4 Synopsis
130  
131 $g = $class->new(@keyVal);
132  
133 =head4 Description
134  
135 Instantiiere die Grafik einer X- oder Y-Achse mit den
136 Darstellungseigenschaften @keyVal (s. Abschnitt L<ATTRIBUTES|"ATTRIBUTES">) und
137 liefere eine Referenz auf das Objekt zurück.
138  
139 =cut
140  
141 # -----------------------------------------------------------------------------
142  
143 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
144 0 my $class = shift;
145 # @_: @keyVal
146  
147 0 my $self = $class->SUPER::new(
148 axis => undef,
149 axisColor => '000000',
150 labelColor => undef,
151 reverse => 0,
152 subTickColor => undef,
153 tickColor => undef,
154 tickDirection => undef,
155 tickLabelGap => 1,
156 tickLength => 4,
157 );
158 0 $self->set(@_);
159  
160 0 return $self;
161 }
162  
163 # -----------------------------------------------------------------------------
164  
165 =head2 Rendern
166  
167 =head3 render() - Zeichne Achse
168  
169 =head4 Synopsis
170  
171 $g->render($img,$x,$y,@keyVal);
172 $class->render($img,$x,$y,@keyVal);
173  
174 =head4 Description
175  
176 Zeichne die Achse in Bild $img an Position ($x,$y). Die Postion ($x,$y)
177 befindet sich am Anfang der jeweiligen Achsenlinie, also an der Position
178 des Achsen-Minimums.
179  
180 Y-Achse
181
182 max +
183 |
184 |
185 |
186 |($x,$y)
187 min x----------+ X-Achse
188 min max
189  
190 =cut
191  
192 # -----------------------------------------------------------------------------
193  
194 0 ▶sub render {
Subroutine render: not called CC=20 SCAR=60.4
Pod documented
195 0 my $this = shift;
196 0 my $img = shift;
197 0 my $x = shift;
198 0 my $y = shift;
199 # @_: @keyVal
200  
201 0 my $self = $this->self(@_);
202  
203 # Attribute
204  
205 0 my ($ax,$axisColor,$labelColor,$reverse,$subTickColor,$tickColor,
206 $tickDirection,$tickLabelGap,$tickLength) =
207 $self->get(qw/axis axisColor labelColor reverse subTickColor
208 tickColor tickDirection tickLabelGap tickLength/);
209  
210 0 $axisColor = $img->color($axisColor);
211 0 ▶ if (!defined $labelColor) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $labelColor) 0 0 0
212 0 $labelColor = $axisColor;
213 }
214 0 ▶ if (!defined $tickColor) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tickColor) 0 0 0
215 0 $tickColor = $axisColor;
216 }
217 0 ▶ if (!defined $subTickColor) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $subTickColor) 0 0 0
218 0 $subTickColor = $tickColor;
219 }
220  
221 # Achseninformation
222  
223 0 my $orientation = $ax->get('orientation');
224 0 my $length = $ax->get('length');
225 0 my $fnt = $ax->font;
226  
227 # Zeichnen
228  
229 # Achsenlinie
230  
231 0 ▶ if ($orientation eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($orientation eq 'y') { } 0 0 0
232 0 ▶ if ($reverse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse) { } 0 0 0
233 # Der Achsenursprung liegt immer beim kleinsten Wert, d.h.
234 # bei reverse=>1 zeichnen wir die Achse von oben nach unten.
235 0 $img->line($x,$y,$x,$y+$length-1,$axisColor);
236 }
237 else {
238 # Warum $y-1, $y-$length?
239 0 $img->line($x,$y-1,$x,$y-$length,$axisColor);
240 }
241 }
242 else {
243 0 $img->line($x,$y,$x+$length-1,$y,$axisColor);
244 }
245  
246 # Ticks und Label
247  
248 0 ▶ if (!$tickDirection) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tickDirection) 0 0 0
249 # Defaultwert tickDirection
250 0 ▶ $tickDirection = $orientation eq 'y'? 'l': 'd';
Branch: 1 branch
BranchTrueFalseTotal
$orientation eq 'y' ? : 0 0 0
251 }
252 0 Quiq::Assert->isEnumValue($tickDirection,[qw/d u l r/]);
253  
254 0 my @ticks = $ax->ticks;
255 0 ▶ if (!@ticks) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ticks) 0 0 0
256 # Keine Tick-Einteilung
257  
258 0 ▶ if ($orientation eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($orientation eq 'y') { } 0 0 0
259 # Noch nicht implementiert
260 #$img->stringCentered($fnt,'v',$x-$tickLength-$tik->width-
261 # $tickLabelGap+$fnt->alignRightOffset,
262 # $y-$pos,$tik->label,$labelColor);
263 }
264 else {
265 0 $img->stringCentered($fnt,'h',$x+($length/2),
266 $y+$tickLength+$tickLabelGap+$fnt->alignTopOffset,
267 'no ticks',$labelColor);
268 }
269 }
270 0 for my $tik (@ticks) {
271 0 my $pos = $tik->position;
272 0 ▶ if ($orientation eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($orientation eq 'y') { } 0 0 0
273 # Warum $y-$pos-1?
274 0 ▶ my $yPos = $reverse? $y+$pos: $y-$pos-1;
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
275 0 ▶ if ($tickDirection eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tickDirection eq 'r') { } 0 0 0
276 0 $img->line($x+$tickLength,$yPos,$x+1,$yPos,$tickColor);
277 0 $img->stringCentered($fnt,'v',$x+$tickLength+
278 $tickLabelGap+$fnt->alignLeftOffset,
279 $yPos,$tik->label,$labelColor);
280 }
281 else { # 'l'
282 0 $img->line($x-$tickLength,$yPos,$x-1,$yPos,$tickColor);
283 0 $img->stringCentered($fnt,'v',$x-$tickLength-$tickLabelGap-
284 $tik->width+1+$fnt->alignRightOffset,
285 $yPos,$tik->label,$labelColor);
286 }
287 }
288 else {
289 0 ▶ if ($tickDirection eq 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tickDirection eq 'u') { } 0 0 0
290 0 $img->line($x+$pos,$y,$x+$pos,$y-$tickLength,$tickColor);
291 0 $img->stringCentered($fnt,'h',
292 $x+$pos,$y-$tickLength-$tickLabelGap-
293 $fnt->alignTopOffset-$fnt->digitHeight,
294 $tik->label,$labelColor);
295 }
296 else { # 'd'
297 0 $img->line($x+$pos,$y,$x+$pos,$y+$tickLength,$tickColor);
298 0 $img->stringCentered($fnt,'h',
299 $x+$pos,$y+$tickLength+$tickLabelGap+$fnt->alignTopOffset,
300 $tik->label,$labelColor);
301 }
302 }
303 }
304  
305 #my $base = $ax->get('step')->get('base');
306 #my $exp = $ax->get('step')->get('exp');
307 0 my $subTickLength = $tickLength-2; # ($base == 1 || $exp < 0? 2: 0);
308 0 for my $tik ($ax->subTicks) {
309 0 my $pos = $tik->position;
310 0 ▶ if ($orientation eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($orientation eq 'y') { } 0 0 0
311 # Warum $y-$pos-1?
312 0 ▶ my $yPos = $reverse? $y+$pos: $y-$pos-1;
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
313 0 ▶ if ($tickDirection eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tickDirection eq 'r') { } 0 0 0
314 0 $img->line($x+$subTickLength,$yPos,$x+1,$yPos,
315 $subTickColor);
316 }
317 else { # 'l'
318 0 $img->line($x-$subTickLength,$yPos,$x-1,$yPos,
319 $subTickColor);
320 }
321 }
322 else {
323 0 ▶ if ($tickDirection eq 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tickDirection eq 'u') { } 0 0 0
324 0 $img->line($x+$pos,$y,$x+$pos,$y-$subTickLength,$subTickColor);
325 }
326 else { # 'd'
327 0 $img->line($x+$pos,$y,$x+$pos,$y+$subTickLength,$subTickColor);
328 }
329 }
330 }
331  
332 0 return;
333 }
334  
335 # -----------------------------------------------------------------------------
336  
337 =head2 Objektmethoden
338  
339 =head3 length() - Länge der Achse
340  
341 =head4 Synopsis
342  
343 $length = $g->length;
344  
345 =head4 Description
346  
347 Liefere die Länge der Achse (= Achsenlinie) in Pixeln.
348  
349 =cut
350  
351 # -----------------------------------------------------------------------------
352  
353 0 ▶sub length {
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
354 0 return shift->get('axis')->get('length');
355 }
356  
357 # -----------------------------------------------------------------------------
358  
359 =head3 width() - Breite der Achsen-Grafik
360  
361 =head4 Synopsis
362  
363 $width = $g->width;
364  
365 =head4 Description
366  
367 Liefere die Gesamt-Breite der Achsen-Grafik in Pixeln. Im Falle
368 einer X-Achse kann die Achsen-Grafik wegen überstehender Label-Texte
369 links und rechts breiter sein als die Länge der Achse.
370  
371 =cut
372  
373 # -----------------------------------------------------------------------------
374  
375 0 ▶sub width {
Subroutine width: not called CC=3 SCAR=24.8
Pod documented
376 0 my $self = shift;
377  
378 # Attribute
379  
380 0 my ($ax,$tickLength,$tickLabelGap) =
381 $self->get(qw/axis tickLength tickLabelGap/);
382 0 my $orientation = $ax->get('orientation');
383  
384 # Breite berechnen
385  
386 0 ▶ if ($orientation eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($orientation eq 'y') { } 0 0 0
387 0 my $fnt = $ax->font;
388  
389 0 ▶ my $fac = $fnt->isTrueType? 1.5: 2; # Heuristik
Branch: 1 branch
BranchTrueFalseTotal
$fnt->isTrueType ? : 0 0 0
390  
391 0 my $n = 1; # Dicke Achsenlinie
392 0 $n += $tickLength; # Länge Tick
393 0 $n += $tickLabelGap; # Abstand zw. Tick und Label
394 0 $n -= $fac*$fnt->alignRightOffset; # Korrektur überstehnende Label
395 0 $n += $ax->width;
396 0 return $n;
397 }
398 else { # X-Achse
399 0 $self->throw('Not implemented');
400 }
401  
402 # not reached
403 }
404  
405 # -----------------------------------------------------------------------------
406  
407 =head3 height() - Höhe der Achsen-Grafik
408  
409 =head4 Synopsis
410  
411 $height = $g->height;
412  
413 =head4 Description
414  
415 Liefere die Gesamt-Höhe der Achsen-Grafik in Pixeln. Im Falle
416 einer Y-Achse kann die Achsen-Grafik wegen überstehender Label-Texte
417 oben und unten höher sein als die Länge der Achse.
418  
419 =cut
420  
421 # -----------------------------------------------------------------------------
422  
423 0 ▶sub height {
Subroutine height: not called CC=3 SCAR=24.8
Pod documented
424 0 my $self = shift;
425  
426 # Attribute
427  
428 0 my ($ax,$tickLength,$tickLabelGap) =
429 $self->get(qw/axis tickLength tickLabelGap/);
430 0 my $orientation = $ax->get('orientation');
431  
432 # Höhe berechnen
433  
434 0 ▶ if ($orientation eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($orientation eq 'y') { } 0 0 0
435 0 $self->throw('Not implemented');
436 }
437 else { # X-Achse
438 0 my $fnt = $ax->font;
439 0 my $min = $ax->get('min');
440  
441 0 ▶ my $fac = $fnt->isTrueType? 1: 2; # Heuristik
Branch: 1 branch
BranchTrueFalseTotal
$fnt->isTrueType ? : 0 0 0
442  
443 0 my $n = 1; # Dicke Achsenlinie
444 0 $n += $tickLength; # Länge Tick
445 0 $n += $tickLabelGap; # Abstand zw. Tick und Label
446 0 $n += $fac*$fnt->alignTopOffset; # Korrektur-Offset
447 0 $n += $ax->height; # Höhe Label
448 0 return $n;
449 }
450  
451 # not reached
452 }
453  
454 # -----------------------------------------------------------------------------
455  
456 =head1 VERSION
457  
458 1.240
459  
460 =head1 AUTHOR
461  
462 Frank Seitz, L<http://fseitz.de/>
463  
464 =head1 COPYRIGHT
465  
466 Copyright (C) 2026 Frank Seitz
467  
468 =head1 LICENSE
469  
470 This code is free software; you can redistribute it and/or modify
471 it under the same terms as Perl itself.
472  
473 =cut
474  
475 # -----------------------------------------------------------------------------
476  
477 2 1;
478  
479 # eof
« Quiq/Axis/Time.pm ↑ Summary Quiq/Html/Tag.pm »