Quiq/Gd/Component/ColorLegend.pm

filter: Statementstmt 27.1% 22 / 81 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 7 / 10 Podpod 100% 3 / 3 totaltotal 32.6% 32 / 98 CC 3 SCAR 10.1
CC
3
Cov
26%
render
6.0
height
6.0
new
2.0
CRAP 2.8
« Quiq/Hash/Ordered.pm ↑ Summary Quiq/TimeLapse/File.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Gd::Component::ColorLegend - Legende zu einem Farb-Plot
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Gd::Component>
12  
13 =head1 DESCRIPTION
14  
15 Grafischer Aufbau der Legende:
16  
17 [SVG kann nicht in POD eingebettet werden]
18  
19 =head1 ATTRIBUTES
20  
21 =over 4
22  
23 =item width => $int (Default: keiner)
24  
25 Breite der Legende in Pixeln.
26  
27 =item axisColor => $color (Default: keiner)
28  
29 Farbe der Achse, also der Linie und der Ticks.
30  
31 =item colors => \@colors (Default: [])
32  
33 Array der Farben.
34  
35 =item min => $float (Default: keiner)
36  
37 Kleinster Wert des Wertebereichs.
38  
39 =item max => $float (Default: keiner)
40  
41 Größter Wert des Wertebereichs.
42  
43 =item logarithmic => $bool (Default: 0)
44  
45 Die Skala erhält eine logarithmische Einteilung (Basis 10).
46  
47 =item labelColor => $color (Default: keiner)
48  
49 Farbe der Beschriftung.
50  
51 =item labelFont => $font (Default: keiner)
52  
53 Font für die Beschriftung.
54  
55 =item blockWidth => $int (Default: keiner)
56  
57 Breite der Überschreitungsblöcke in Pixeln. Hat nur eine Bedeutung, wenn
58 C<ltColor> und/oder C<gtColor> definiert sind.
59  
60 =item blockHeight => $int (Default: keiner)
61  
62 Höhe der Farbblöcke in Pixeln.
63  
64 =item blockGap => $int (Default: keiner)
65  
66 Lücke zwischen den Farbblöcken in Pixeln. Hat nur eine Bedeutung, wenn
67 C<blockLtColor> und/oder C<blockGtColor> definiert sind.
68  
69 =item blockLtColor => $color (Default: keiner)
70  
71 Farbe des Blocks, der die Werte, die I<min> unterschreiten, repräsentiert.
72  
73 =item blockGtColor => $color (Default: keiner)
74  
75 Farbe des Blocks, der die Werte, die I<max> überschreiten, repräsentiert.
76  
77 =item title => $str (Default: undef)
78  
79 Titel (optional).
80  
81 =item titleColor => $color (Default: keiner)
82  
83 Farbe des Titels.
84  
85 =item titleFont => $font (Default: keiner)
86  
87 Font des Titels.
88  
89 =item titleGap => $int (Default: keiner)
90  
91 Vertikale Lücke zwischen dem Titel und den Farbblöcken in Pixeln.
92  
93 =back
94  
95 =head1 EXAMPLE
96  
97 Eine Farblegende mit Titel. Die Höhe des Bildes geben wir nicht vor,
98 sie richtet sich nach der Höhe der Grafik. Wir ermitteln sie mit
99 $g->height. Die Farben definieren wir erst beim Rendern, da das Bild
100 bei der Instantiierung des Grafik-Objekts noch nicht existiert.
101  
102 my $width = 400;
103
104 my $g = Quiq::Gd::Component::ColorLegend->new(
105 title => 'Test',
106 titleFont => Quiq::Gd::Font->new('Blob/font/pala.ttf,14'),
107 labelFont => Quiq::Gd::Font->new('Blob/font/pala.ttf,10'),
108 width => $width,
109 min => 0,
110 max => 100,
111 logarithmic => 0,
112 blockWidth => 50,
113 blockHeight => 18,
114 blockGap => 20,
115 );
116
117 $img = Quiq::Gd::Image->new($width,$g->height);
118 my $white = $img->background('ffffff');
119 $img->transparent($white);
120
121 $g->render($img,0,0,
122 colors => scalar $img->rainbowColors(512),
123 blockLtColor => $img->color('000080'),
124 blockGtColor => $img->color('ff00ff'),
125 titleColor => $img->color('ff00ff'),
126 labelColor => $img->color('ff00ff'),
127 );
128  
129 Erzeugte Grafik (der Rahmen ist per CSS hinzugefügt):
130  
131 [Bild nur im Browser sichtbar]
132  
133 =cut
134  
135 # -----------------------------------------------------------------------------
136  
137 package Quiq::Gd::Component::ColorLegend;
138 1 ▶use base qw/Quiq::Gd::Component/;
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 1 our $VERSION = '1.240';
145  
146 1 ▶use Quiq::Axis::Numeric;
Subroutine BEGIN: called CC=1 SCAR=0.0
147 1 ▶use Quiq::Gd::Component::Axis;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 1 ▶use Quiq::Gd::Component::ColorBar;
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 # -----------------------------------------------------------------------------
151  
152 =head1 METHODS
153  
154 =head2 Konstruktor
155  
156 =head3 new() - Konstruktor
157  
158 =head4 Synopsis
159  
160 $g = $class->new(@keyVal);
161  
162 =head4 Description
163  
164 Instantiiere die Legende mit den Eigenschaften @keyVal
165 (s. Abschnitt L<ATTRIBUTES|"ATTRIBUTES">) und liefere eine Referenz auf das Objekt
166 zurück.
167  
168 =cut
169  
170 # -----------------------------------------------------------------------------
171  
172 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
173 0 my $class = shift;
174 # @_: @keyVal
175  
176 0 my $self = $class->SUPER::new(
177 width => undef,
178 axisColor => undef,
179 colors => [],
180 min => undef,
181 max => undef,
182 logarithmic => 0,
183 labelColor => undef,
184 labelFont => undef,
185 title => undef,
186 titleColor => undef,
187 titleFont => undef,
188 titleGap => 2,
189 blockWidth => 0,
190 blockHeight => undef,
191 blockGap => 0,
192 blockLtColor => undef,
193 blockGtColor => undef,
194 # interne Attribute
195 axis => undef,
196 );
197 0 $self->set(@_);
198  
199 # Instantiiere Achse (brauchen wir für die Höhenbestimmung)
200  
201 0 my ($width,$blockWidth,$blockGap) =
202 $self->get(qw/width blockWidth blockGap/);
203  
204 0 my $ax = Quiq::Axis::Numeric->new(
205 orientation => 'x',
206 font => $self->get('labelFont'),
207 length => $width-2*$blockWidth-2*$blockGap,
208 min => $self->get('min'),
209 max => $self->get('max'),
210 logarithmic => $self->get('logarithmic'),
211 debug => 0,
212 );
213  
214 0 my $g = Quiq::Gd::Component::Axis->new(
215 axis => $ax,
216 );
217 0 $self->set(axis=>$g);
218  
219 0 return $self;
220 }
221  
222 # -----------------------------------------------------------------------------
223  
224 =head2 Zeichnen
225  
226 =head3 render() - Zeichne Legende
227  
228 =head4 Synopsis
229  
230 $g->render($img,$x,$y,@keyVal);
231 $class->render($img,$x,$y,@keyVal);
232  
233 =head4 Description
234  
235 Zeichne die Legende in Bild $img an Position ($x,$y).
236  
237 =cut
238  
239 # -----------------------------------------------------------------------------
240  
241 0 ▶sub render {
Subroutine render: not called CC=2 SCAR=17.9
Pod documented
242 0 my $this = shift;
243 0 my $img = shift;
244 0 my $x = shift;
245 0 my $y = shift;
246 # @_: @keyVal
247  
248 0 my $self = $this->self(@_);
249  
250 # Attribute
251  
252 0 my $ax = $self->{'axis'};
253 0 my $axisColor = $self->{'axisColor'};
254 0 my $blockWidth = $self->{'blockWidth'};
255 0 my $blockHeight = $self->{'blockHeight'};
256 0 my $blockGap = $self->{'blockGap'};
257 0 my $blockLtColor = $self->{'blockLtColor'};
258 0 my $blockGtColor = $self->{'blockGtColor'};
259 0 my $colorA = $self->{'colors'};
260 0 my $labelColor = $self->{'labelColor'};
261 0 my $labelFont = $self->{'labelFont'};
262 0 my $logarithmic = $self->{'logarithmic'};
263 0 my $min = $self->{'min'};
264 0 my $max = $self->{'max'};
265 0 my $width = $self->{'width'};
266 0 my $title = $self->{'title'};
267 0 my $titleColor = $self->{'titleColor'};
268 0 my $titleFont = $self->{'titleFont'};
269 0 my $titleGap = $self->{'titleGap'};
270  
271 # Zeichnen
272  
273 # 1. Titel (optional)
274  
275 0 ▶ if ($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 0 0 0
276 0 $img->stringCentered($titleFont,'h',$x+$width/2,$y,$title,$titleColor);
277 0 $y += $titleFont->stringHeight($title)+$titleGap;
278 }
279  
280 # 2. Achse
281  
282 $ax->set(
283 0 axisColor => $axisColor,
284 labelColor => $labelColor,
285 );
286 0 $ax->render($img,$x+$blockWidth+$blockGap,$y+$blockHeight-1);
287  
288 # 3. Gradient
289  
290 0 my $g = Quiq::Gd::Component::ColorBar->new(
291 width => $ax->length,
292 height => $blockHeight,
293 );
294 0 $g->render($img,$x+$blockWidth+$blockGap,$y,
295 colors => $colorA,
296 );
297  
298 # 4. Blöcke
299  
300 0 $img->filledRectangle($x,$y,$x+$blockWidth-1,$y+$blockHeight-1,
301 $blockLtColor);
302 0 $img->filledRectangle($x+$width-$blockWidth,$y,$x+$width-1,
303 $y+$blockHeight-1,$blockGtColor);
304 0 $y += $blockHeight-1;
305  
306 # 5. Blockbeschriftung
307  
308 0 my ($tickLength,$tickLabelGap) = $ax->get(qw/tickLength tickLabelGap/);
309 0 my $tickOffset = $tickLength+$tickLabelGap+$labelFont->alignTopOffset;
310  
311 0 $img->stringCentered($labelFont,'h',$x+$blockWidth/2,
312 $y+$tickOffset,"<$min",$labelColor);
313 0 $img->stringCentered($labelFont,'h',
314 $x+$blockWidth+2*$blockGap+$ax->length+$blockWidth/2,
315 $y+$tickOffset,">$max",$labelColor);
316  
317 0 return;
318 }
319  
320 # -----------------------------------------------------------------------------
321  
322 =head2 Objektmethoden
323  
324 =head3 height() - Höhe der Farb-Legende
325  
326 =head4 Synopsis
327  
328 $height = $g->height;
329  
330 =head4 Description
331  
332 Liefere die Höhe der Legende. Die Höhe wird aus den Komponenten berechnet.
333  
334 =cut
335  
336 # -----------------------------------------------------------------------------
337  
338 0 ▶sub height {
Subroutine height: not called CC=2 SCAR=17.9
Pod documented
339 0 my $self = shift;
340  
341 # Attribute
342  
343 0 my $ax = $self->{'axis'};
344 0 my $blockHeight = $self->{'blockHeight'};
345 0 my $title = $self->{'title'};
346 0 my $titleFont = $self->{'titleFont'};
347 0 my $titleGap = $self->{'titleGap'};
348  
349 # Höhe berechnen
350  
351 0 my $n = 0;
352 0 ▶ if ($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 0 0 0
353 # Titel-Höhe
354 0 $n += $titleFont->stringHeight($title)+$titleGap;
355 }
356 0 $n += $blockHeight; # Block-Höhe
357 0 $n += $ax->height; # Höhe der X-Achse einschl. Label
358 0 $n += -1; # da 1 Pixel Überlappung
359  
360 0 return $n;
361 }
362  
363 # -----------------------------------------------------------------------------
364  
365 =head1 VERSION
366  
367 1.240
368  
369 =head1 AUTHOR
370  
371 Frank Seitz, L<http://fseitz.de/>
372  
373 =head1 COPYRIGHT
374  
375 Copyright (C) 2026 Frank Seitz
376  
377 =head1 LICENSE
378  
379 This code is free software; you can redistribute it and/or modify
380 it under the same terms as Perl itself.
381  
382 =cut
383  
384 # -----------------------------------------------------------------------------
385  
386 1 1;
387  
388 # eof
« Quiq/Hash/Ordered.pm ↑ Summary Quiq/TimeLapse/File.pm »