xylinespoints.pm untested

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