| 1 | # ----------------------------------------------------------------------------- | ||
| 2 | |||
| 3 | =encoding utf8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Quiq::Gd::Component::Grid - Gitter eines XY-Plot | ||
| 8 | |||
| 9 | =head1 BASE CLASS | ||
| 10 | |||
| 11 | L<Quiq::Gd::Component> | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Ein Objekt der Klasse definiert ein Gitter für den Hintergrund | ||
| 16 | (oder Vordergrund, je nach Reihenfolge beim Zeichnen) eines | ||
| 17 | XY-Plot. Das Gitter besteht aus gepunkteten Linien an den | ||
| 18 | Haupt-Ticks der X- und der Y-Achse. | ||
| 19 | |||
| 20 | =head1 ATTRIBUTES | ||
| 21 | |||
| 22 | Fett hervorgehobene Attribute sind Pflicht-Attribute. Sie müssen | ||
| 23 | beim Konstruktoraufruf immer angegeben werden. | ||
| 24 | |||
| 25 | =over 4 | ||
| 26 | |||
| 27 | =item B<< xAxix => $axis >> | ||
| 28 | |||
| 29 | X-Achse (Subklasse von Quiq::Axis). | ||
| 30 | |||
| 31 | =item B<< yAxix => $axis >> | ||
| 32 | |||
| 33 | Y-Achse (Subklasse von Quiq::Axis). | ||
| 34 | |||
| 35 | =item color => $color (Default: '#000000') | ||
| 36 | |||
| 37 | Farbe der Gitterlinien. | ||
| 38 | |||
| 39 | =back | ||
| 40 | |||
| 41 | =head1 EXAMPLE | ||
| 42 | |||
| 43 | Code: | ||
| 44 | |||
| 45 | use Quiq::Gd::Image; | ||
| 46 | use Quiq::Gd::Component::Grid; | ||
| 47 | use Quiq::Axis::Numeric; | ||
| 48 | use Quiq::Axis::Time; | ||
| 49 | |||
| 50 | my ($width,$height) = (500,200); | ||
| 51 | |||
| 52 | # Achsen definieren | ||
| 53 | |||
| 54 | my $ax = Quiq::Axis::Numeric->new( | ||
| 55 | orientation => 'x', | ||
| 56 | font => Quiq::Gd::Font->new('gdSmallFont'), | ||
| 57 | length => $width, | ||
| 58 | min => 0, | ||
| 59 | max => 20, | ||
| 60 | ); | ||
| 61 | my $gAx = Quiq::Gd::Component::Axis->new( | ||
| 62 | axis => $ax, | ||
| 63 | tickDirection => 'u', | ||
| 64 | ); | ||
| 65 | |||
| 66 | my $ay = Quiq::Axis::Time->new( | ||
| 67 | orientation => 'y', | ||
| 68 | font => Quiq::Gd::Font->new('gdSmallFont'), | ||
| 69 | length => $height, | ||
| 70 | min => Quiq::Epoch->new('2019-11-08 08:00:00')->epoch, | ||
| 71 | max => Quiq::Epoch->new('2019-11-08 16:00:00')->epoch, | ||
| 72 | debug => 0, | ||
| 73 | ); | ||
| 74 | my $gAy = Quiq::Gd::Component::Axis->new( | ||
| 75 | axis => $ay, | ||
| 76 | reverse => 1, | ||
| 77 | ); | ||
| 78 | |||
| 79 | # Gitter definieren | ||
| 80 | |||
| 81 | my $grid = Quiq::Gd::Component::Grid->new( | ||
| 82 | xAxis => $ax, | ||
| 83 | yAxis => $ay, | ||
| 84 | color => '#ff0000', | ||
| 85 | ); | ||
| 86 | |||
| 87 | # Rasterbild erzeugen | ||
| 88 | |||
| 89 | my $axHeight = $gAx->height; | ||
| 90 | my $ayWidth = $gAy->width; | ||
| 91 | |||
| 92 | my $img = Quiq::Gd::Image->new( | ||
| 93 | $width + 2*$ayWidth, | ||
| 94 | $height + 2*$axHeight, | ||
| 95 | # $width, | ||
| 96 | # $height, | ||
| 97 | ); | ||
| 98 | $img->background('#ffffff'); | ||
| 99 | |||
| 100 | # Grid und Achsen zeichnen | ||
| 101 | |||
| 102 | $grid->render($img,$ayWidth,$axHeight); | ||
| 103 | $gAx->render($img,$ayWidth,$axHeight); | ||
| 104 | $gAx->render($img,$ayWidth,$axHeight+$height-1,tickDirection=>'d'); | ||
| 105 | $gAy->render($img,$ayWidth,$axHeight); | ||
| 106 | $gAy->render($img,$ayWidth+$width-1,$axHeight,tickDirection=>'r'); | ||
| 107 | |||
| 108 | Grafik: | ||
| 109 | |||
| 110 | [Grafik: Gitter eines XY-Plot] | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | # ----------------------------------------------------------------------------- | ||
| 115 | |||
| 116 | package Quiq::Gd::Component::Grid; | ||
| 117 | 1 | ▶ | use base qw/Quiq::Gd::Component/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 118 | |||
| 119 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 120 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 121 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 122 | |||
| 123 | our $VERSION = '1.240'; | ||
| 124 | |||
| 125 | 0 | ▶ | use GD (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 126 | |||
| 127 | # ----------------------------------------------------------------------------- | ||
| 128 | |||
| 129 | =head1 METHODS | ||
| 130 | |||
| 131 | =head2 Konstruktor | ||
| 132 | |||
| 133 | =head3 new() - Konstruktor | ||
| 134 | |||
| 135 | =head4 Synopsis | ||
| 136 | |||
| 137 | $g = $class->new(@keyVal); | ||
| 138 | |||
| 139 | =head4 Description | ||
| 140 | |||
| 141 | Instantiiere ein Grafik-Objekt mit den Eigenschaften @keyVal | ||
| 142 | (s. Abschnitt L<ATTRIBUTES|"ATTRIBUTES">) und liefere eine Referenz auf das | ||
| 143 | Objekt zurück. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | # ----------------------------------------------------------------------------- | ||
| 148 | |||
| 149 | sub new { | ||
| 150 | my $class = shift; | ||
| 151 | # @_: @keyVal | ||
| 152 | |||
| 153 | my $self = $class->SUPER::new( | ||
| 154 | xAxis => undef, | ||
| 155 | yAxis => undef, | ||
| 156 | color => '#000000', | ||
| 157 | ); | ||
| 158 | $self->set(@_); | ||
| 159 | |||
| 160 | return $self; | ||
| 161 | } | ||
| 162 | |||
| 163 | # ----------------------------------------------------------------------------- | ||
| 164 | |||
| 165 | =head2 Zeichnen | ||
| 166 | |||
| 167 | =head3 render() - Zeichne Gitter | ||
| 168 | |||
| 169 | =head4 Synopsis | ||
| 170 | |||
| 171 | $g->render($img); | ||
| 172 | $g->render($img,$x,$y,@keyVal); | ||
| 173 | $class->render($img,$x,$y,@keyVal); | ||
| 174 | |||
| 175 | =head4 Description | ||
| 176 | |||
| 177 | Zeichne das Gitter in Bild $img an Position ($x,$y). | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | # ----------------------------------------------------------------------------- | ||
| 182 | |||
| 183 | sub render { | ||
| 184 | my $this = shift; | ||
| 185 | my $img = shift; | ||
| 186 | my $x = shift || 0; | ||
| 187 | my $y = shift || 0; | ||
| 188 | # @_: @keyVal | ||
| 189 | |||
| 190 | my $self = $this->self(@_); | ||
| 191 | |||
| 192 | # Attribute | ||
| 193 | |||
| 194 | my ($color,$xAxis,$yAxis) = $self->get(qw/color xAxis yAxis/); | ||
| 195 | |||
| 196 | # Zeichnen | ||
| 197 | |||
| 198 | $color = $img->color($color); | ||
| 199 | |||
| 200 | my $gdTransparent = GD->gdTransparent; | ||
| 201 | my $gdStyled = GD->gdStyled; | ||
| 202 | $img->setStyle($color,$gdTransparent); # Pixel+Transparent = Punktlinie | ||
| 203 | |||
| 204 | # Grid zur X-Achse | ||
| 205 | |||
| 206 | my $length = $yAxis->length; | ||
| 207 | for my $tik ($xAxis->ticks) { | ||
| 208 | my $pos = $tik->position; | ||
| 209 | $img->line($x+$pos,$y,$x+$pos,$y+$length-1,$gdStyled); | ||
| 210 | } | ||
| 211 | |||
| 212 | # Grid zur Y-Achse | ||
| 213 | |||
| 214 | $length = $xAxis->length; | ||
| 215 | for my $tik ($yAxis->ticks) { | ||
| 216 | my $pos = $tik->position; | ||
| 217 | $img->line($x,$y+$pos,$x+$length-1,$y+$pos,$gdStyled); | ||
| 218 | } | ||
| 219 | |||
| 220 | return; | ||
| 221 | } | ||
| 222 | |||
| 223 | # ----------------------------------------------------------------------------- | ||
| 224 | |||
| 225 | =head2 Objektmethoden | ||
| 226 | |||
| 227 | siehe L<BASE CLASS|"BASE CLASS"> | ||
| 228 | |||
| 229 | =head1 VERSION | ||
| 230 | |||
| 231 | 1.240 | ||
| 232 | |||
| 233 | =head1 AUTHOR | ||
| 234 | |||
| 235 | Frank Seitz, L<http://fseitz.de/> | ||
| 236 | |||
| 237 | =head1 COPYRIGHT | ||
| 238 | |||
| 239 | Copyright (C) 2026 Frank Seitz | ||
| 240 | |||
| 241 | =head1 LICENSE | ||
| 242 | |||
| 243 | This code is free software; you can redistribute it and/or modify | ||
| 244 | it under the same terms as Perl itself. | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | # ----------------------------------------------------------------------------- | ||
| 249 | |||
| 250 | 1; | ||
| 251 | |||
| 252 | # eof | ||