Quiq/Gd/Component/ScatterGraph.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 109 CC 9 SCAR 29.1
CC
9
Cov
0%
render
42.0
new
20.0
zMin
2.0
CRAP 18.4
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Gd::Component::ScatterGraph - Farbpunkte in einer Fläche
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Gd::Component::Graph>
12  
13 =head1 DESCRIPTION
14  
15 Die Klasse erweitert die Basisklasse Quiq::Gd::Component::Graph um
16 eine Z-Komponente. Die Werte dieser Komponente werden in Farbwerte
17 übersetzt und in den Positionen der X,Y-Komponenten abgetragen.
18  
19 =head1 ATTRIBUTES
20  
21 Weitere Attribute sind in der Basisklassen-Dokumentation beschrieben.
22  
23 =over 4
24  
25 =item z => \@z (Default: [])
26  
27 Array der Z-Werte (Weltkoordinate).
28  
29 =item zMin => $float (Default: Minimum der Z-Werte)
30  
31 Anfang des Z-Wertebereichs (Weltkoodinate).
32  
33 =item zMax => $float (Default: Maximum der Z-Werte)
34  
35 Ende des Z-Wertebereichs (Weltkoodinate).
36  
37 =item zLogarithmic => $bool (Default: 0)
38  
39 Bilde den Z-Wertebereich logarithmisch auf die Farbwerte ab.
40  
41 =item lowColor => $color (Default: undef)
42  
43 Farbe für Z-Werte, die zMin unterschreiten.
44  
45 =item highColor => $color (Default: undef)
46  
47 Farbe für Z-Werte, die zMax überschreiten.
48  
49 =back
50  
51 =head1 EXAMPLE
52  
53 Code:
54  
55 use Quiq::Gd::Image;
56 use Quiq::Gd::Component::ScatterGraph;
57
58 # Konfiguration
59
60 my $width = 200;
61 my $height = 200;
62 my $numColors = 128;
63 my $pointSize = 10;
64
65 # Zufallsdaten erzeugen
66
67 my (@x,@y,@z);
68 for (1 .. 300) {
69 push @x,int rand 100;
70 push @y,int rand 100;
71 push @z,int rand 128;
72 }
73
74 # Grafik-Objekt instantiieren
75
76 my $g = Quiq::Gd::Component::ScatterGraph->new(
77 width=>$width,
78 height=>$height,
79 pointSize=>$pointSize,
80 pointStyle=>'circle',
81 adaptPlotRegion=>1,
82 x=>\@x,
83 y=>\@y,
84 z=>\@z,
85 lineThickness=>0,
86 );
87
88 # Bild-Objekt instantiieren
89
90 my $img = Quiq::Gd::Image->new($width,$height);
91 $img->background('ffffff');
92 $img->border('a0a0a0');
93
94 # Grafik auf Bild zeichnen
95
96 $g->render($img,0,0,
97 colors=>[$img->rainbowColors($numColors)],
98 lowColor=>$img->color('003366'),
99 highColor=>$img->color('ff00ff'),
100 );
101  
102 Grafik:
103  
104 [Nur im Browser sichtbar]
105  
106 =cut
107  
108 # -----------------------------------------------------------------------------
109  
110 package Quiq::Gd::Component::ScatterGraph;
111 use base qw/Quiq::Gd::Component::Graph/;
112  
113 use v5.10;
114 use strict;
115 use warnings;
116  
117 our $VERSION = '1.240';
118  
119 use Quiq::Option;
120 use Quiq::Array;
121 use POSIX ();
122  
123 # -----------------------------------------------------------------------------
124  
125 =head1 METHODS
126  
127 =head2 Konstruktor
128  
129 =head3 new() - Konstruktor
130  
131 =head4 Synopsis
132  
133 $g = $class->new(@keyVal);
134  
135 =head4 Description
136  
137 Instantiiere ein Grafik-Objekt mit den Eigenschaften @keyVal
138 (s. Abschnitt L<ATTRIBUTES|"ATTRIBUTES">) und liefere eine Referenz auf das Objekt
139 zurück.
140  
141 =cut
142  
143 # -----------------------------------------------------------------------------
144  
145 sub new {
146 my $class = shift;
147 # @_: @keyVal
148  
149 my $opt = Quiq::Option->extract(-properties=>1,-mode=>'sloppy',\@_,
150 z => [],
151 zMin => undef,
152 zMax => undef,
153 zLogarithmic => 0,
154 lowColor => undef,
155 highColor => undef,
156 );
157 my $self = $class->SUPER::new(@_);
158 $self->add(%$opt);
159  
160 # Z-Minimum und Z-Maximum berechnen, falls nicht angegeben.
161 # Dies müssen wir im Konstruktor machen, damit die Objektmethoden
162 # zMin() und zMax() korrekte Wert liefern.
163  
164 my $zMin = $self->{'zMin'};
165 my $zMax = $self->{'zMax'};
166  
167 if (!defined $zMin) {
168 $zMin = Quiq::Array->min($self->{'z'}) // 0;
169 }
170 if (!defined $zMax) {
171 $zMax = Quiq::Array->max($self->{'z'}) // 0;
172 }
173 if ($zMin == $zMax) {
174 $zMin -= 1;
175 $zMax += 1;
176 }
177  
178 $self->{'zMin'} = $zMin;
179 $self->{'zMax'} = $zMax;
180  
181 return $self;
182 }
183  
184 # -----------------------------------------------------------------------------
185  
186 =head2 Zeichnen
187  
188 =head3 render() - Zeichne Grafik
189  
190 =head4 Synopsis
191  
192 $g->render($img);
193 $g->render($img,$x,$y,@keyVal);
194 $class->render($img,$x,$y,@keyVal);
195  
196 =head4 Description
197  
198 Zeichne die Grafik in Bild $img an Position ($x,$y).
199 Die Methode liefert keinen Wert zurück.
200  
201 =cut
202  
203 # -----------------------------------------------------------------------------
204  
205 sub render {
206 my $this = shift;
207 my $img = shift;
208 my $x = shift || 0;
209 my $y = shift || 0;
210 # @_: @keyVal
211  
212 my $self = $this->self(@_);
213  
214 # Attribute
215  
216 my $zA = $self->{'z'};
217 my $zMin = $self->{'zMin'};
218 my $zMax = $self->{'zMax'};
219 my $zLogarithmic = $self->{'zLogarithmic'};
220 my $colorA = $self->{'colors'};
221 my $lowColor = $self->{'lowColor'};
222 my $highColor = $self->{'highColor'};
223  
224 # Daten für das Rendern durch die Basisklasse aufbereiten
225  
226 my $min = $zLogarithmic? POSIX::log10($zMin): $zMin;
227 my $max = $zLogarithmic? POSIX::log10($zMax): $zMax;
228  
229 my @c;
230 for (@$zA) {
231 my $z = $zLogarithmic? POSIX::log10($_): $_;
232  
233 my $c;
234 if ($z < $min) {
235 $c = $lowColor;
236 }
237 elsif ($z > $max) {
238 $c = $highColor;
239 }
240 else {
241 my $i = Quiq::Math->valueToPixel(scalar(@$colorA),$min,$max,$z);
242 $c = $colorA->[$i];
243 }
244 push @c,$c;
245 }
246  
247 # Zeichnen (übernimmt die Basisklasse)
248  
249 $self->SUPER::render($img,$x,$y,
250 colors => \@c,
251 );
252  
253 return;
254 }
255  
256 # -----------------------------------------------------------------------------
257  
258 =head2 Objektmethoden
259  
260 =head3 zMin() - Minimum des Z-Wertebereichs
261  
262 =head4 Synopsis
263  
264 $zMin = $g->zMin;
265  
266 =head4 Description
267  
268 Liefere das Minimum des Z-Wertebereichs, das entweder beim
269 Konstruktoraufruf gesetzt oder aus den Daten ermittelt wurde.
270  
271 =cut
272  
273 # -----------------------------------------------------------------------------
274  
275 sub zMin {
276 return shift->{'zMin'};
277 }
278  
279 # -----------------------------------------------------------------------------
280  
281 =head3 zMax() - Maximum des Z-Wertebereichs
282  
283 =head4 Synopsis
284  
285 $zMax = $g->zMax;
286  
287 =head4 Description
288  
289 Liefere das Maximum des Z-Wertebereichs, das entweder beim
290 Konstruktoraufruf gesetzt oder aus den Daten ermittelt wurde.
291  
292 =cut
293  
294 # -----------------------------------------------------------------------------
295  
296 sub zMax {
297 return shift->{'zMax'};
298 }
299  
300 # -----------------------------------------------------------------------------
301  
302 =head1 VERSION
303  
304 1.240
305  
306 =head1 AUTHOR
307  
308 Frank Seitz, L<http://fseitz.de/>
309  
310 =head1 COPYRIGHT
311  
312 Copyright (C) 2026 Frank Seitz
313  
314 =head1 LICENSE
315  
316 This code is free software; you can redistribute it and/or modify
317 it under the same terms as Perl itself.
318  
319 =cut
320  
321 # -----------------------------------------------------------------------------
322  
323 1;
324  
325 # eof