| 1 | =head1 NAME |
| 2 | |
| 3 | PDL::Graphics::PGPLOTOptions - Setting PGPLOT options |
| 4 | |
| 5 | =head1 SYNOPSIS |
| 6 | |
| 7 | use PGPLOTOptions qw('default_options'); |
| 8 | |
| 9 | =head1 DESCRIPTION |
| 10 | |
| 11 | This package contains one function (at present) which returns PDL::Option |
| 12 | objects for default settings for plot windows and plot commands. This |
| 13 | should be complemented by functions that could affect this such as |
| 14 | file reading commands etc. |
| 15 | |
| 16 | =head1 OPTIONS |
| 17 | |
| 18 | The following is a listing of options that are set in this file and what |
| 19 | they do and what their default value is |
| 20 | |
| 21 | =head2 Window specific options |
| 22 | |
| 23 | These options modify the appearance of windows and can also modify the |
| 24 | default settings for creation of plot axes etc. |
| 25 | |
| 26 | =over |
| 27 | |
| 28 | =item Device |
| 29 | |
| 30 | The default PGPLOT device to use. The default value is set to the PGPLOT_DEV |
| 31 | environment variable if set, otherwise to '?'. |
| 32 | |
| 33 | =item AxisColour |
| 34 | |
| 35 | The colour with which to draw axes. Default value=3 (Green) |
| 36 | |
| 37 | =item HardLW, HardCH, HardFont, HardAxisColour, HardColour |
| 38 | |
| 39 | The linewidth, character height, font and axis colour to use on hardcopy |
| 40 | devices. The default values are HardLW=1, HardCH=1.4, HardFont=2 (Roman), |
| 41 | HardAxisColour=1 (Black) and HardColour=1 as well. The latter is the default |
| 42 | plot colour to use on hardcopy devices. |
| 43 | |
| 44 | =item Axis |
| 45 | |
| 46 | The axis style to use. See the L<PDL::Graphics::PGPLOT::Window> documentation |
| 47 | for details. It defaults to 'Normal' which is a labelled box. Valid arguments |
| 48 | are 'Empty', 'Box', 'Normal', 'Axes', 'Grid', 'LogX', 'LogY', 'LogXY'. |
| 49 | |
| 50 | =item AspectRatio |
| 51 | |
| 52 | The aspect ratio of the output device. The default value is device dependent. |
| 53 | |
| 54 | =item WindowWidth |
| 55 | |
| 56 | The width of the output window in inches and defaults to as big as possible. |
| 57 | |
| 58 | =item WindowXSize and WindowYSize |
| 59 | |
| 60 | These are alternatives to AspectRatio and WindowWidth. |
| 61 | |
| 62 | =item WindowName |
| 63 | |
| 64 | The name of the window - can later be retrieved using name(). It defaults |
| 65 | to 'Window'+Window ID. |
| 66 | |
| 67 | =item NXPanel |
| 68 | |
| 69 | The number of panels in the X-direction - defaults to 1 |
| 70 | |
| 71 | =item NYPanel |
| 72 | |
| 73 | The number of panels in the Y-direction - defaults to 1 |
| 74 | |
| 75 | =item Justify |
| 76 | |
| 77 | A boolean value which, if true, causes both axes to drawn |
| 78 | to the same scale; see |
| 79 | the PGPLOT C<pgenv()> command for more information. |
| 80 | |
| 81 | =item TightLabels |
| 82 | |
| 83 | Boolean value which, if true, causes axis labels to be pulled |
| 84 | slightly closer to the main viewport than usual. That's handy |
| 85 | for making multi-panel plots. Undef (the default) is equivalent |
| 86 | to 0 for panels with NYPanels <= 1 and 1 for panels with NYPanels > 1. |
| 87 | |
| 88 | =item TitleSize |
| 89 | |
| 90 | The relative size of a plot or image title, compared to other annotations. |
| 91 | Defaults to 1.0 (original behavior) but can be set to, e.g., 1.5 to |
| 92 | emphasize graph titles in a multipanel plot. |
| 93 | |
| 94 | =item Border |
| 95 | |
| 96 | Adjust the spacing around the plot. See the documentation in |
| 97 | L<PDL::Graphics::PGPLOT::Window> for details. |
| 98 | |
| 99 | =item CharSize |
| 100 | |
| 101 | The default charsize for the plot - used when annotating the axes for |
| 102 | instance. It defaults to 1. |
| 103 | |
| 104 | =item PlotPosition |
| 105 | |
| 106 | The position of the plot in normalised coordinates. |
| 107 | |
| 108 | =item Erase |
| 109 | |
| 110 | Explicitly erase the plotting surface, normally required when making new |
| 111 | plots with PlotPosition. |
| 112 | |
| 113 | =back |
| 114 | |
| 115 | =head2 Plot specific options |
| 116 | |
| 117 | |
| 118 | For the moment see the C<PDL::Graphics::PGPLOT::Window> documentation for |
| 119 | these. |
| 120 | |
| 121 | =cut |
| 122 | |
| 123 | package PDL::Graphics::PGPLOTOptions; |
| 124 | |
| 125 | # use PDL::Core qw/:Func :Internal/; |
| 126 | use Exporter; |
| 127 | use strict; |
| 128 | use warnings; |
| 129 | |
| 130 | our @ISA = ('Exporter'); |
| 131 | our @EXPORT_OK = qw(default_options set_pgplot_options); |
| 132 | |
| 133 | # |
| 134 | # To be able to set options outside of PGPLOT in the .perldlrc I will |
| 135 | # have to define these local variables. |
| 136 | # |
| 137 | my %options = ( |
| 138 | Device => undef, |
| 139 | AxisColour => 3, |
| 140 | BackgroundColour => -1, # Text background colour |
| 141 | HardLW => 1, |
| 142 | HardCH => 1.4, |
| 143 | HardFont => 2, |
| 144 | HardAxisColour => 1, |
| 145 | HardColour => 1, |
| 146 | Axis => 'BCNST', # see kludge in Window::imag if you change this |
| 147 | AspectRatio => undef, |
| 148 | WindowWidth => undef, |
| 149 | WindowXSize => undef, |
| 150 | WindowYSize => undef, |
| 151 | Size => undef, |
| 152 | Unit=> undef, |
| 153 | WindowName => '', |
| 154 | NXPanel => 1, |
| 155 | NYPanel => 1, |
| 156 | Justify => 0, # Justification of boxes & axes |
| 157 | Scale=> undef, # device pixels per data pixel |
| 158 | Pitch=> undef, # Horizontal data pixels per <unit> |
| 159 | Unit => undef, # Unit for pitch |
| 160 | Pix => undef, # Pixel aspect ratio |
| 161 | Align => undef, # Alignment of viewport within plot area |
| 162 | DirAxis=> undef, # Default direction of axes |
| 163 | Border => 0, |
| 164 | CharSize => 1, |
| 165 | Symbol => 17, |
| 166 | Colour => 5, |
| 167 | ErrTerm => 1, |
| 168 | LineStyle => 1, |
| 169 | Font => 1, |
| 170 | Fill => 1, |
| 171 | ITF => 0, |
| 172 | Transform => undef, |
| 173 | LineWidth => 1, |
| 174 | XRange => undef, |
| 175 | YRange => undef, |
| 176 | Arrow => {FS => 1, Angle => 45.0, Vent => 0.3, |
| 177 | ArrowSize => undef}, |
| 178 | Hatch => {Angle => 45.0, Separation => 1.0, Phase => 0.0}, |
| 179 | XTitle => '', |
| 180 | YTitle => '', |
| 181 | Title => '', |
| 182 | ); |
| 183 | |
| 184 | |
| 185 | sub default_options { |
| 186 | |
| 187 | |
| 188 | my $DEV=undef; |
| 189 | # Use the standard PGPLOT environment variable. |
| 190 | $DEV = $ENV{"PGPLOT_DEV"} if defined $ENV{"PGPLOT_DEV"}; |
| 191 | # However if the user has specified the Perl-ish variable use that. |
| 192 | $DEV = $options{Device} if defined($options{Device}); |
| 193 | $DEV = "?" if !defined($DEV) || $DEV eq ""; # Safe default |
| 194 | |
| 195 | # Options specific (primarily) to window creation |
| 196 | my $wo = { |
| 197 | Device => $DEV, ### Tidy this up. |
| 198 | AxisColour => $options{AxisColour}, # Axis colour |
| 199 | HardLW => $options{HardLW}, # Line width for hardcopy devices, |
| 200 | HardCH => $options{HardCH}, # Character height for hardcopy devices |
| 201 | HardFont => $options{HardFont}, # For for hardcopy devices |
| 202 | HardAxisColour => $options{HardAxisColour}, # Black colour as default on hardcopy devices. |
| 203 | HardColour => $options{HardColour}, # Black as default plot colour on hardcopy devices. |
| 204 | Axis => $options{Axis}, # The type of box |
| 205 | AspectRatio => $options{AspectRatio}, # The aspect ratio of the plot window. |
| 206 | WindowWidth => $options{WindowWidth}, # The width of the plot window in inches. |
| 207 | WindowXSize => $options{WindowXSize}, # The X&Y size of a window, these will be |
| 208 | WindowYSize => $options{WindowYSize}, # used to give the aspect ratio if defined. |
| 209 | Size => $options{Size}, # alternative window size spec |
| 210 | Unit => $options{Unit}, # Units for size spec |
| 211 | WindowName => $options{WindowName}, # The window name given |
| 212 | NXPanel => $options{NXPanel}, # The number of plotting panels |
| 213 | NYPanel => $options{NYPanel}, # Ditto. |
| 214 | TightLabels => undef, |
| 215 | TitleSize => 1.0, |
| 216 | Justify => $options{Justify}, # Justification of boxes & axes |
| 217 | Scale => $options{Justify}, # device pixels per data pixel |
| 218 | Pitch => $options{Pitch}, # Horizontal data pixels per unit |
| 219 | Unit => $options{Unit}, # PGPLOT unit for pitch |
| 220 | Pix => $options{Pix}, # Pixel aspect ratio |
| 221 | Align => $options{Align}, # Alignment of vp in plot area |
| 222 | DirAxis => $options{DirAxis}, # The default axis direction |
| 223 | Border => $options{Border}, |
| 224 | CharSize => $options{CharSize}, # Character size for annotation |
| 225 | Erase => 0, |
| 226 | Recording => 0, # Off by default. |
| 227 | PlotPosition => 'Default' # The position of the plot on the page. |
| 228 | }; |
| 229 | |
| 230 | # Options specific to plotting commands |
| 231 | my $o = { |
| 232 | Symbol => $options{Symbol}, # Symbol for points |
| 233 | Colour => $options{Colour}, # Colour for plots |
| 234 | CharSize => $options{CharSize}, # Character height |
| 235 | ErrTerm => $options{ErrTerm}, # Size of error-bar terminators |
| 236 | Erase => 0, # Whether to erase a panel when switching. |
| 237 | Panel => undef, # What panel to switch to. |
| 238 | LineStyle => $options{LineStyle}, # Solid linestyle |
| 239 | Font => $options{Font}, # Normal font |
| 240 | Fill => $options{Fill}, # Solid fill |
| 241 | ITF => $options{ITF}, # Linear ITF |
| 242 | Axis => $options{Axis}, # Standard axis-type |
| 243 | |
| 244 | Transform => $options{Transform}, # The transform used for plots. |
| 245 | Justify => $options{Justify}, # Justification of boxes & axes |
| 246 | Scale => $options{Justify}, # device pixels per data pixel |
| 247 | Pitch => $options{Pitch}, # Horizontal data pixels per unit |
| 248 | Unit => $options{Unit}, # PGPLOT unit for pitch |
| 249 | Pix => $options{Pix}, # Pixel aspect ratio |
| 250 | Align => $options{Align}, # Alignment of vp in plot area |
| 251 | DirAxis => $options{DirAxis}, # The default axis direction |
| 252 | |
| 253 | LineWidth => $options{LineWidth}, |
| 254 | TightLabels => $options{TightLabels}, |
| 255 | TitleSize => $options{TitleSize}, |
| 256 | XRange => $options{XRange}, |
| 257 | YRange => $options{YRange}, |
| 258 | BackgroundColour => $options{BackgroundColour}, |
| 259 | # The following two should really be implemented as an Options |
| 260 | # object, but that will make I/O of options somewhat difficult. |
| 261 | # Note that the arrowsize is implemented as a synonym for the |
| 262 | # charsize this should not cause any problems but might be worth |
| 263 | # noting... |
| 264 | # In addition to this the arrowsize below is also set to be undefined |
| 265 | # by default which will automatically use the character size. |
| 266 | # All these problems are historical.. |
| 267 | Arrow => $options{Arrow}, |
| 268 | Hatch => $options{Hatch}, |
| 269 | XTitle => $options{XTitle}, # Label for X-axis |
| 270 | YTitle => $options{YTitle}, # Label for Y-axis |
| 271 | Title => $options{Title}, # Title for plot |
| 272 | }; |
| 273 | |
| 274 | |
| 275 | # Now for the synonyms |
| 276 | my $s = {Color => 'Colour', 'Line-style' => 'LineStyle', |
| 277 | 'Line-width' => 'LineWidth', 'Hatching' => 'Hatch', |
| 278 | FillType => 'Fill', 'ArrowSize' => 'CharSize', |
| 279 | AxisColor => 'AxisColour', HardAxisColor => 'HardAxisColour', |
| 280 | HardColor => 'HardColor', BackgroundColor => 'BackgroundColour'}; |
| 281 | # |
| 282 | # And now for the lookup tables.. |
| 283 | # |
| 284 | my $t = { |
| 285 | Colour => { |
| 286 | 'White' => 0, 'Black' => 1, 'Red' => 2, |
| 287 | 'Green' => 3, 'Blue' => 4, 'Cyan' => 5, |
| 288 | 'Magenta' => 6, 'Yellow' => 7, 'Orange' => 8, |
| 289 | 'DarkGray' => 14, 'DarkGrey' => 14, |
| 290 | 'LightGray' => 15, 'LightGrey' => 15, |
| 291 | 'CosmicSpectrum' => [0.269, 0.388, 0.342] |
| 292 | }, |
| 293 | BackgroundColour => { |
| 294 | 'White' => 0, 'Black' => 1, 'Red' => 2, |
| 295 | 'Green' => 3, 'Blue' => 4, 'Cyan' => 5, |
| 296 | 'Magenta' => 6, 'Yellow' => 7, 'Orange' => 8, |
| 297 | 'DarkGray' => 14, 'DarkGrey' => 14, |
| 298 | 'LightGray' => 15, 'LightGrey' => 15 |
| 299 | }, |
| 300 | Symbol => { |
| 301 | 'Square' => 0, 'Dot' => 1, 'Plus' => 2, |
| 302 | 'Asterisk' => 3, 'Circle' => 4, 'Cross' => 5, |
| 303 | 'Triangle' => 7, 'Earth' => 8, 'Sun' => 9, |
| 304 | 'Diamond' => 11, 'Star' => 12, Default => 17 |
| 305 | }, |
| 306 | ITF => { |
| 307 | 'Linear' => 0, 'Log' => 1, 'Sqrt' => 2 |
| 308 | }, |
| 309 | LineStyle => { |
| 310 | 'Solid' => 1, 'Dashed' => 2, 'Dot-Dash' => 3, |
| 311 | 'Dotted' => 4, 'Dash-Dot-Dot' => 5, |
| 312 | '-' => 1, '--' => 2, '.-' => 3, '.' => 4, |
| 313 | '-..' => 5 |
| 314 | }, |
| 315 | Font => { |
| 316 | Normal => 1, Roman => 2,Italic => 3, Script => 4 |
| 317 | }, |
| 318 | Fill => { |
| 319 | Solid => 1, Outline => 2, Hatched => 3, |
| 320 | Cross_Hatched => 4, CrossHatched => 4 |
| 321 | }, |
| 322 | }; |
| 323 | |
| 324 | my $wt = { |
| 325 | # valid values for axis parameter (eg env()) |
| 326 | Axis => { |
| 327 | Empty => '', Box => 'BC', Normal => 'BCNST', |
| 328 | Axes => 'ABCNST', Grid => 'ABCGNST', |
| 329 | LogX => ['BCLNST', 'BCNST'], |
| 330 | LogY => ['BCNST', 'BCLNST'], |
| 331 | LogXY => ['BCLNST', 'BCLNST'], |
| 332 | '-2' => '', '-1' => 'BC', '0' => 'BCNST', |
| 333 | '1' => 'ABCNST', '2' => 'ABCGNST', |
| 334 | '10' => ['BCLNST', 'BCNST'], |
| 335 | '20' => ['BCNST', 'BCLNST'], |
| 336 | '30' => ['BCLNST', 'BCLNST'] |
| 337 | }, |
| 338 | AxisColour => { |
| 339 | 'White' => 0, 'Black' => 1, 'Red' => 2, |
| 340 | 'Green' => 3, 'Blue' => 4, 'Cyan' => 5, |
| 341 | 'Magenta' => 6, 'Yellow' => 7, 'Orange' => 8, |
| 342 | 'DarkGray' => 14, 'DarkGrey' => 14, |
| 343 | 'LightGray' => 15, 'LightGrey' => 15 |
| 344 | }, |
| 345 | HardFont => { |
| 346 | Normal => 1, Roman => 2,Italic => 3, Script => 4 |
| 347 | }, |
| 348 | HardAxisColour => { |
| 349 | 'White' => 0, 'Black' => 1, 'Red' => 2, |
| 350 | 'Green' => 3, 'Blue' => 4, 'Cyan' => 5, |
| 351 | 'Magenta' => 6, 'Yellow' => 7, 'Orange' => 8, |
| 352 | 'DarkGray' => 14, 'DarkGrey' => 14, |
| 353 | 'LightGray' => 15, 'LightGrey' => 15 |
| 354 | } |
| 355 | }; |
| 356 | |
| 357 | |
| 358 | # Set up the two primary sets of options for PGPLOT commands. |
| 359 | my $window_options = PDL::Options->new($wo); |
| 360 | $window_options->translation($wt); |
| 361 | |
| 362 | my $general_options = PDL::Options->new($o); |
| 363 | $general_options->translation($t); |
| 364 | $general_options->synonyms($s); |
| 365 | |
| 366 | return ($general_options, $window_options); |
| 367 | |
| 368 | } |
| 369 | |
| 370 | |
| 371 | =head2 set_pgplot_options |
| 372 | |
| 373 | This function allows the user to set the default PGPLOT options. It |
| 374 | is particularly useful in the C<.perldlrc> file since one can do |
| 375 | |
| 376 | use PDL::Graphics::PGPLOTOptions ('set_pgplot_options'); |
| 377 | set_pgplot_options('Device' => '/xs', 'HardLW' => 3); |
| 378 | |
| 379 | for instance to set the default values. The main drawback is that the |
| 380 | routine is rather unflexible with no synonyms or case-insensitivity. |
| 381 | |
| 382 | =cut |
| 383 | |
| 384 | sub set_pgplot_options { |
| 385 | my %o; |
| 386 | if (ref($_[0]) eq 'HASH') { |
| 387 | %o = %{$_[0]}; |
| 388 | } else { |
| 389 | %o = @_; |
| 390 | } |
| 391 | |
| 392 | foreach my $k (keys %o) { |
| 393 | if (exists($options{$k})) { |
| 394 | $options{$k} = $o{$k}; |
| 395 | } elsif ($k =~ /Color/) { |
| 396 | my $knew = $k; |
| 397 | $knew =~ s/Color/Colour/; |
| 398 | if (!exists($options{$knew})) { |
| 399 | warn "Option $k is not recognised!\n"; |
| 400 | } else { |
| 401 | $options{$knew} = $o{$k}; |
| 402 | } |
| 403 | } else { |
| 404 | warn "Option $k is not recognised!\n"; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | } |
| 409 | |
| 410 | |
| 411 | 1; |