xypoints.pm untested

filter: Statementstmt 0.0% 0 / 201 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 68 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 414 CC 40 SCAR 50.1
CC
40
Cov
0%
set_max_min
306.0
setup_coords
156.0
draw_x_ticks_number
90.0
CRAP 150.2
1 #==========================================================================
2 # Copyright (c) 1995-1998 Martien Verbruggen
3 # Modified for XY by George Fitch
4 #--------------------------------------------------------------------------
5 #
6 # Name:
7 # GD::Graph::xypoints.pm
8 #
9 #
10 #==========================================================================
11  
12 package GD::Graph::xypoints;
13  
14 $GD::Graph::xypoints::VERSION = '$Revision: 1.10 $' =~ /\s([\d.]+)/;
15  
16 use strict;
17
18 use GD::Graph::axestype;
19 use GD::Graph::utils qw(:all);
20  
21 @GD::Graph::xypoints::ISA = qw( GD::Graph::axestype );
22  
23 use constant PI => 4 * atan2(1,1);
24  
25 my %Defaults = (
26
27 #Pad things a bit to make them look nicer
28 b_margin => 5,
29 r_margin => 5,
30  
31 # We want long ticks by default
32 x_long_ticks => 1,
33 y_long_ticks => 1,
34
35 # Number of ticks for the y axis
36 y_tick_number => 10,
37 x_tick_number => 13, # CONTRIB Scott Prahl
38 x_precision => undef,
39
40 # Skip every nth label. if 1 will print every label on the axes,
41 # if 2 will print every second, etc..
42 x_label_skip => 1,
43 y_label_skip => 1,
44  
45 # Do we want ticks on the x axis?
46 x_ticks => 1,
47 x_all_ticks => 0,
48  
49 # Where to place the x and y labels
50 x_label_position => 1/2,
51 y_label_position => 1/2,
52  
53 # vertical printing of x labels
54 x_labels_vertical => 1,
55  
56 # Draw axes as a box? (otherwise just left and bottom)
57 box_axis => 1,
58
59 );
60  
61 sub initialise
62 {
63 my $self = shift;
64  
65 $self->SUPER::initialise();
66  
67 while (my($key, $val) = each %Defaults)
68 { $self->{$key} = $val }
69  
70 $self->set_x_label_font(GD::gdSmallFont);
71 $self->set_y_label_font(GD::gdSmallFont);
72 $self->set_x_axis_font(GD::gdTinyFont);
73 $self->set_y_axis_font(GD::gdTinyFont);
74 $self->set_legend_font(GD::gdTinyFont);
75 $self->set_values_font(GD::gdTinyFont);
76 }
77  
78 # PRIVATE
79 sub set_max_min
80 {
81 my $self = shift;
82  
83 my $x_max = undef;
84 my $x_min = undef;
85 my $y_max = undef;
86 my $y_min = undef;
87
88 for my $i ( 1 .. $self->{_data}->num_sets ) # 1 because x-labels are [0]
89 {
90 # Contributed by Andrew Crabb - ahc@sol.jhoc1.jhmi.edu
91 my $num_points_limit = $self->{_data}->num_points - 1;
92 for my $j ( 0 .. $num_points_limit )
93 {
94 my $val = $self->{_data}->[$i][$j];
95 $y_max = $val if ((not defined($y_max)) or ($val > $y_max));
96 $y_min = $val if ((not defined($y_min)) or ($val < $y_min));
97 }
98 }
99  
100 # Contributed by Andrew Crabb - ahc@sol.jhoc1.jhmi.edu
101 my $num_points_limit = $self->{_data}->num_points - 1;
102 for my $k ( 0 .. $num_points_limit ) # x-values are at [0]
103 {
104 my $val = $self->{_data}->[0][$k];
105 $x_max = $val if ((not defined($x_max)) or ($val > $x_max));
106 $x_min = $val if ((not defined($x_min)) or ($val < $x_min));
107 }
108  
109 # Set the min and max's
110 $self->{y_min}[1] = $y_min;
111 $self->{y_max}[1] = $y_max;
112  
113 $self->{y_min}[2] = $y_min;
114 $self->{y_max}[2] = $y_max;
115  
116 $self->{x_min} = $x_min;
117 $self->{x_max} = $x_max;
118  
119 # Calculate the needed precision.
120  
121 my $x_pre = int(log(abs(($self->{x_max} - $self->{x_min}) / ($self->{x_tick_number} - 1) + $self->{x_min}))) + 1;
122  
123 $x_pre = $x_pre < 0 ? 0 : $x_pre;
124  
125 # Overwrite these with any user supplied ones
126 $self->{y_min}[1] = $self->{y_min_value} if defined $self->{y_min_value};
127 $self->{y_max}[1] = $self->{y_max_value} if defined $self->{y_max_value};
128  
129 $self->{y_min}[1] = $self->{y1_min_value} if defined $self->{y1_min_value};
130 $self->{y_max}[1] = $self->{y1_max_value} if defined $self->{y1_max_value};
131  
132 $self->{y_min}[2] = $self->{y2_min_value} if defined $self->{y2_min_value};
133 $self->{y_max}[2] = $self->{y2_max_value} if defined $self->{y2_max_value};
134  
135 $self->{x_min} = $self->{x_min_value} if defined $self->{x_min_value};
136 $self->{x_max} = $self->{x_max_value} if defined $self->{x_max_value};
137  
138 $self->{true_x_min} = $self->{x_min};
139 $self->{true_x_max} = $self->{x_max};
140  
141 $self->{true_y_min} = $self->{y_min}[1];
142 $self->{true_y_max} = $self->{y_max}[1];
143  
144 $self->{x_tick_number} = 13 unless defined $self->{x_tick_number};
145 $self->{y_tick_number} = 10 unless defined $self->{y_tick_number};
146  
147 $self->{x_precision} = $x_pre unless defined $self->{x_precision};
148  
149 return $self;
150  
151 }
152
153 sub setup_coords
154 {
155 my $s = shift;
156  
157 # Do some sanity checks
158 $s->{two_axes} = 0 if $s->{_data}->num_sets != 2 || $s->{two_axes} < 0;
159 $s->{two_axes} = 1 if $s->{two_axes} > 1;
160  
161 delete $s->{y_label2} unless $s->{two_axes};
162  
163 # Set some heights for text
164 $s->{tfh} = 0 unless $s->{title};
165 $s->{xlfh} = 0 unless $s->{x_label};
166  
167 # Make sure the y1 axis has a label if there is one set for y in
168 # general
169 $s->{y1_label} = $s->{y_label} if !$s->{y1_label} && $s->{y_label};
170  
171 # Set axis tick text heights and widths to 0 if they don't need to
172 # be plotted.
173 $s->{xafh} = 0, $s->{xafw} = 0 unless $s->{x_plot_values};
174 $s->{yafh} = 0, $s->{yafw} = 0 unless $s->{y_plot_values};
175  
176 # Calculate minima and maxima for the axes
177 $s->set_max_min() or return;
178  
179 # Create the labels for the axes, and calculate the max length
180 $s->create_y_labels();
181 $s->create_x_labels(); # CONTRIB Scott Prahl
182  
183 # Calculate the boundaries of the chart
184 $s->_setup_boundaries() or return;
185  
186 # get the zero axis level
187 (undef, $s->{zeropoint}) = $s->val_to_pixel(0, 0, 1);
188  
189 # More sanity checks
190 $s->{x_label_skip} = 1 if $s->{x_label_skip} < 1;
191 $s->{y_label_skip} = 1 if $s->{y_label_skip} < 1;
192 $s->{y_tick_number} = 1 if $s->{y_tick_number} < 1;
193  
194 return $s;
195 }
196  
197 #
198 # Ticks and values for x axes
199 #
200 sub draw_x_ticks_number
201 {
202  
203 my $self = shift;
204  
205 for (my $i = 0; $i < $self->{x_tick_number}; $i++)
206 {
207  
208 my $x_val = sprintf "%0.$self->{x_precision}f", $i * ($self->{x_max} - $self->{x_min}) / ($self->{x_tick_number} - 1) + $self->{x_min};
209  
210 my ($x, $y) = $self->val_to_pixel($x_val, 0, 1);
211  
212 if (defined $self->{x_number_format})
213 {
214 $x_val = ref $self->{x_number_format} eq 'CODE' ?
215 &{$self->{x_number_format}}($x_val) :
216 sprintf($self->{x_number_format}, $x_val);
217 }
218
219 $y = $self->{bottom} unless $self->{zero_axis_only};
220  
221 # CONTRIB Damon Brodie for x_tick_offset
222 next if (!$self->{x_all_ticks} and
223 ($i - $self->{x_tick_offset}) % $self->{x_label_skip} and
224 $i != $self->{_data}->num_points - 1
225 );
226  
227 if ($self->{x_ticks})
228 {
229 if ($self->{x_long_ticks})
230 {
231 $self->{graph}->line($x, $self->{bottom}, $x, $self->{top},
232 $self->{fgci});
233 }
234 else
235 {
236 $self->{graph}->line($x, $y, $x, $y - $self->{x_tick_length},
237 $self->{fgci});
238 }
239 }
240  
241 # CONTRIB Damon Brodie for x_tick_offset
242 next if
243 ($i - $self->{x_tick_offset}) % ($self->{x_label_skip}) and
244 $i != $self->{_data}->num_points - 1;
245  
246 $self->{gdta_x_axis}->set_text($x_val);
247  
248 my $yt = $y + $self->{axis_space};
249  
250 if ($self->{x_labels_vertical})
251 {
252 $self->{gdta_x_axis}->set_align('center', 'right');
253 $self->{gdta_x_axis}->draw($x, $yt, PI/2);
254 }
255 else
256 {
257 $self->{gdta_x_axis}->set_align('top', 'center');
258 $self->{gdta_x_axis}->draw($x, $yt);
259 }
260 }
261  
262 return $self;
263 }
264  
265  
266 sub draw_data_set
267 {
268 my $self = shift;
269 my $ds = shift;
270  
271 my @values = $self->{_data}->y_values($ds) or
272 return $self->_set_error("Impossible illegal data set: $ds",
273 $self->{_data}->error);
274  
275 # Pick a colour
276 my $dsci = $self->set_clr($self->pick_data_clr($ds));
277 my $type = $self->pick_marker($ds);
278  
279 for (my $i = 0; $i < @values; $i++)
280 {
281 next unless defined $values[$i];
282 my ($xp, $yp) = $self->val_to_pixel(
283 $self->{_data}->get_x($i), $values[$i], $ds);
284 $self->marker($xp, $yp, $type, $dsci );
285 $self->{_hotspots}->[$ds]->[$i] =
286 ['rect', $self->marker_coordinates($xp, $yp)];
287 }
288  
289 return $ds;
290 }
291  
292 # Pick a marker type
293  
294 sub pick_marker # number
295 {
296 my $self = shift;
297 my $num = shift;
298  
299 ref $self->{markers} ?
300 $self->{markers}[ $num % (1 + $#{$self->{markers}}) - 1 ] :
301 ($num % 8) || 8;
302 }
303  
304 # Draw a marker
305  
306 sub marker_coordinates
307 {
308 my $self = shift;
309 my ($xp, $yp) = @_;
310 return (
311 $xp - $self->{marker_size},
312 $xp + $self->{marker_size},
313 $yp + $self->{marker_size},
314 $yp - $self->{marker_size},
315 );
316 }
317  
318 sub marker # $xp, $yp, type (1-7), $colourindex
319 {
320 my $self = shift;
321 my ($xp, $yp, $mtype, $mclr) = @_;
322  
323 my ($l, $r, $b, $t) = $self->marker_coordinates($xp, $yp);
324  
325 MARKER: {
326  
327 ($mtype == 1) && do
328 { # Square, filled
329 $self->{graph}->filledRectangle( $l, $t, $r, $b, $mclr );
330 last MARKER;
331 };
332 ($mtype == 2) && do
333 { # Square, open
334 $self->{graph}->rectangle( $l, $t, $r, $b, $mclr );
335 last MARKER;
336 };
337 ($mtype == 3) && do
338 { # Cross, horizontal
339 $self->{graph}->line( $l, $yp, $r, $yp, $mclr );
340 $self->{graph}->line( $xp, $t, $xp, $b, $mclr );
341 last MARKER;
342 };
343 ($mtype == 4) && do
344 { # Cross, diagonal
345 $self->{graph}->line( $l, $b, $r, $t, $mclr );
346 $self->{graph}->line( $l, $t, $r, $b, $mclr );
347 last MARKER;
348 };
349 ($mtype == 5) && do
350 { # Diamond, filled
351 $self->{graph}->line( $l, $yp, $xp, $t, $mclr );
352 $self->{graph}->line( $xp, $t, $r, $yp, $mclr );
353 $self->{graph}->line( $r, $yp, $xp, $b, $mclr );
354 $self->{graph}->line( $xp, $b, $l, $yp, $mclr );
355 $self->{graph}->fillToBorder( $xp, $yp, $mclr, $mclr );
356 last MARKER;
357 };
358 ($mtype == 6) && do
359 { # Diamond, open
360 $self->{graph}->line( $l, $yp, $xp, $t, $mclr );
361 $self->{graph}->line( $xp, $t, $r, $yp, $mclr );
362 $self->{graph}->line( $r, $yp, $xp, $b, $mclr );
363 $self->{graph}->line( $xp, $b, $l, $yp, $mclr );
364 last MARKER;
365 };
366 ($mtype == 7) && do
367 { # Circle, filled
368 $self->{graph}->arc( $xp, $yp, 2 * $self->{marker_size},
369 2 * $self->{marker_size}, 0, 360, $mclr );
370 $self->{graph}->fillToBorder( $xp, $yp, $mclr, $mclr );
371 last MARKER;
372 };
373 ($mtype == 8) && do
374 { # Circle, open
375 $self->{graph}->arc( $xp, $yp, 2 * $self->{marker_size},
376 2 * $self->{marker_size}, 0, 360, $mclr );
377 last MARKER;
378 };
379 }
380 }
381  
382 #
383 # Convert value coordinates to pixel coordinates on the canvas.
384 #
385 sub val_to_pixel # ($x, $y, $i) in real coords ($Dataspace),
386 { # return [x, y] in pixel coords
387 my $self = shift;
388 my ($x, $y, $i) = @_;
389  
390 my $x_min = $self->{x_min};
391 my $x_max = $self->{x_max};
392  
393 my $x_step = abs(($self->{right} - $self->{left})/($x_max - $x_min));
394  
395 my $ret_x = $self->{left} + ($x - $x_min) * $x_step;
396  
397 my $y_min = ($self->{two_axes} && $i == 2) ?
398 $self->{y_min}[2] : $self->{y_min}[1];
399  
400 my $y_max = ($self->{two_axes} && $i == 2) ?
401 $self->{y_max}[2] : $self->{y_max}[1];
402  
403 my $y_step = abs(($self->{bottom} - $self->{top})/($y_max - $y_min));
404  
405 my $ret_y = $self->{bottom} - ($y - $y_min) * $y_step;
406  
407 return(_round($ret_x), _round($ret_y));
408 }
409  
410  
411 sub draw_legend_marker
412 {
413 my $self = shift;
414 my $n = shift;
415 my $x = shift;
416 my $y = shift;
417  
418 my $ci = $self->set_clr($self->pick_data_clr($n));
419  
420 my $old_ms = $self->{marker_size};
421 my $ms = _min($self->{legend_marker_height}, $self->{legend_marker_width});
422  
423 ($self->{marker_size} > $ms/2) and $self->{marker_size} = $ms/2;
424
425 $x += int($self->{legend_marker_width}/2);
426 $y += int($self->{lg_el_height}/2);
427  
428 $n = $self->pick_marker($n);
429  
430 $self->marker($x, $y, $n, $ci);
431  
432 $self->{marker_size} = $old_ms;
433 }
434  
435 "Just another true value";
436  
437 =head1 NAME
438  
439 XYpoints - XY plotting module for GD::Graph.
440  
441 =head1 SYNOPSIS
442  
443 use GD::Graph::xypoints;
444  
445 =head1 DESCRIPTION
446  
447 B<xypoints> is a I<perl5> module that uses GD::Graph, GD,
448 to create and display PNG output for XY graphs with points.
449  
450 =head1 USAGE
451  
452 See GD::Graph documentation for usage for all graphs.
453  
454 =head1 METHODS AND FUNCTIONS
455  
456 See GD::Graph documentation for methods for all GD::Graph graphs.
457  
458 =head1 OPTIONS
459  
460 =head2 Options for all graphs
461  
462 See GD::Graph documentation for options for all graphs.
463  
464 =head2 Options for graphs with axes
465  
466 See GD::Graph documentation for options for graphs with axes.
467  
468 =head1 CHANGE LOG
469  
470 =head2 GDGraph-XY-0.92
471  
472 B<x_number_format>
473  
474 Added x_number_format functionality that mimics y_number_format
475 at the request of Ramon Acedo Rodriguez E<lt>rar@same-si.com<gt>
476  
477 =head2 GDGraph-XY-0.91
478  
479 B<Pass -w>
480  
481 Thanks to some contributions by Andrew Crabb, ahc@sol.jhoc1.jhmi.edu,
482 the modules now pass the -w. Yes, they should have done this in the
483 first place, but I forgot.
484  
485 =head1 AUTHOR
486  
487 Written by: Martien Verbruggen E<lt>mgjv@comdyn.com.auE<gt>
488 Modified by: George 'Gaffer' Fitch E<lt>gaf3@gaf3.com<gt>
489  
490 =head2 Copyright
491  
492 GIFgraph: Copyright (c) 1995-1999 Martien Verbruggen.
493 Chart::PNGgraph: Copyright (c) 1999 Steve Bonds.
494 GD::Graph: Copyright (c) 1999 Martien Verbruggen.
495  
496 All rights reserved. This package is free software; you can redistribute
497 it and/or modify it under the same terms as Perl itself.
498  
499 =cut