File Coverage

blib/lib/Chart/Split.pm
Criterion Covered Total %
statement 188 318 59.1
branch 51 142 35.9
condition 11 23 47.8
subroutine 8 8 100.0
pod n/a
total 258 491 52.5


line stmt bran cond sub pod time code
1             #====================================================================
2             # Chart::Split
3             #
4             # written by Chart-Group
5             #
6             # maintained by the Chart Group
7             # Chart@wettzell.ifag.de
8             #
9             #---------------------------------------------------------------------
10             # History:
11             #----------
12             # $RCSfile: Split.pm,v $ $Revision: 1.2 $ $Date: 2003/02/14 14:25:30 $
13             # $Author: dassing $
14             # $Log: Split.pm,v $
15             # Revision 1.2 2003/02/14 14:25:30 dassing
16             # First setup to cvs
17             #
18             #====================================================================
19              
20             package Chart::Split;
21              
22 2     2   66 use Chart::Base 2.3;
  2         52  
  2         38  
23 2     2   41 use GD;
  2         18  
  2         37  
24 2     2   37 use Carp;
  2         20  
  2         35  
25 2     2   37 use strict;
  2         19  
  2         30  
26              
27             @Chart::Split::ISA = qw(Chart::Base);
28             $Chart::Split::VERSION = '2.3';
29              
30             #>>>>>>>>>>>>>>>>>>>>>>>>>>#
31             # public methods go here #
32             #<<<<<<<<<<<<<<<<<<<<<<<<<<#
33              
34              
35              
36             #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
37             # private methods go here #
38             #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
39              
40             #draw the ticks
41             sub _draw_x_number_ticks {
42 2     2   20     my $self = shift;
43 2         20     my $data = $self->{'dataref'};
44 2         22     my $font = $self->{'tick_label_font'};
45 2         26     my $textcolor = $self->_color_role_to_index('text');
46 2         31     my $misccolor = $self->_color_role_to_index('misc');
47 2         26     my $num_points = $self->{'num_datapoints'};
48 2         20     my ($h, $w, $width, $step, $start, $interval, $label, $stag, @labels);
49 2         20     my ($x_start, $y_start, $y, $x, $lines, $delta, $ticks);
50 2         24     my $x_label_len = 1;
51 2         19     my $y_label_len = 1;
52 2         19     my $x_max = -0x80000000;
53                 
54 2         134     $self->{'grid_data'}-> {'x'} = [];
55              
56             # find the width
57 2 50       25     $width = $self->{'curr_x_max'} - $self->{'curr_x_min'}; $width = 1 if $width == 0;
  2         26  
58              
59             # make sure we got a real font
60 2 50       28     unless ((ref $font) eq 'GD::Font') {
61 0         0            croak "The tick label font you specified isn\'t a GD Font object";
62                 }
63              
64             # find out how big the font is
65 2         44     ($w, $h) = ($font->width, $font->height);
66              
67 2 50 33     41     unless (defined $self->{'start'} && defined $self->{'interval'}) {
68 0         0            croak "I need two values from you to draw a split chart: start and interval!";
69                 }
70                 else {
71 2         22       $interval = $self->{'interval'};
72 2         19       $start = $self->{'start'};
73 2         20       $ticks = $self->{'interval_ticks'}-1;
74 2         19       $label = $start;
75                 }
76              
77              
78             #look after devision by zero!
79 2 50       23     if ( $ticks ==0 ) { $ticks=1; }
  0         0  
80                 
81             #calculate the step between the ticks
82 2         21     $step = $interval/$ticks;
83              
84 2         24     for (0..$ticks) {
85 21         268          push @labels, $self->{f_x_tick}-> (sprintf ("%.".$self->{'precision'}."f", $label));
86 21         220          $label += $step;
87                 }
88              
89             #find the biggest x value
90 2         19     foreach (@{$data->[0]}) {
  2         26  
91 5201 50       57237       if ($_ > $x_max) {
92 5201         48076         $x_max = $_;
93                   }
94                 }
95              
96             #find the length of the x and y labels
97 2         111     foreach (@labels) {
98 21 100       223       if (length($_) > $x_label_len) {
99 1         10         $x_label_len = length($_);
100                   }
101                 }
102                 
103             #find the amount of lines
104 2         27     $lines = int((($x_max-$start) / $interval)+0.99999999999);
105 2 50       27     $lines = 1 if $lines == 0;
106                 
107             #find the length, of the label.
108 2         21     $y_label_len = length($lines);
109              
110             #get the starting point and the width
111 2 50       25     if ($lines > 1) { #if there are y-ticks
112 2 50       43      if ($self->{'y_axes'} =~ /^right$/i) {
    50          
113 0         0        $x_start = $self->{'curr_x_min'} ;
114 0         0        $width = $self->{'curr_x_max'}-$x_start - $self->{'text_space'}*2- $y_label_len*$w-$self->{'tick_len'};
115              
116                  }
117                  elsif ($self->{'y_axes'} =~ /^both$/i) {
118 0         0         $x_start = $self->{'curr_x_min'} + ($w * $y_label_len) +2*$self->{'text_space'} + $self->{'tick_len'};
119 0         0         $width = $self->{'curr_x_max'} - $x_start- ($w * $y_label_len)
120                              - 2 * $self->{'text_space'} - $self->{'tick_len'};
121                  }
122                  else {
123 2         25          $x_start = $self->{'curr_x_min'} + ($w * $y_label_len)
124                      + 3 * $self->{'text_space'} ;
125 2         41          $width = $self->{'curr_x_max'} - $x_start;
126                  }
127                 }
128                 else { #if there are no y-axes
129 0         0      $x_start = $self->{'curr_x_min'};
130 0         0      $width = $self->{'curr_x_max'} - $x_start;
131                 }
132                 
133             #and the y_start value
134 2         22     $y_start = $self->{'curr_y_max'} - $h - $self->{'text_space'};
135                 
136             #get the delta value
137 2         22     $delta = $width / ($ticks);
138              
139 2 50       26     if ( ! defined($self->{'skip_x_ticks'}) ) {
140 2         25      $self->{'skip_x_ticks'} = 1;
141                 }
142             #draw the labels
143 2 50       28     if ($self->{'x_ticks'} =~ /^normal$/i ) {
    0          
    0          
144 2 50       29       if ($self->{'skip_x_ticks'} > 1) { #draw a normal tick every nth label
    50          
145 0         0          for ( 0..$#labels-1) {
146 0 0       0              if ( defined ($labels[$_*$self->{'skip_x_ticks'}]) ) {
147 0         0                 $x = $x_start + $delta*($_*$self->{'skip_x_ticks'})
148                                  - ($w * length($labels[$_*$self->{'skip_x_ticks'}]))/2;
149 0         0                 $self->{'gd_obj'}->string($font, $x, $y_start,$labels[$_*$self->{'skip_x_ticks'}], $textcolor);
150                          }
151                      }
152                   }
153                   elsif($self->{'custom_x_ticks'}) { #draw only the normal ticks they wanted
154 0         0          foreach (@{$self->{'custom_x_ticks'}}) {
  0         0  
155 0 0       0              if ( defined $labels[$_] ) {
156 0         0                 $x = $x_start + $delta*$_ - ($w * length($labels[$_]))/2;
157 0         0                 $self->{'gd_obj'}->string($font, $x, $y_start, $labels[$_], $textcolor);
158                          }
159                      }
160                   }
161                   else {
162 2         28         for (0..$#labels) { #draw all ticks normal
163 21 50       219             if ( defined $labels[$_] ) {
164 21         191                $x = $x_start + $delta*($_) - ($w * length($labels[$_]))/2;
165 21         378                $self->{'gd_obj'}->string($font, $x, $y_start,$labels[$_], $textcolor);
166                         }
167                     }
168                   }
169                 }
170                 elsif ($self->{'x_ticks'} =~ /^staggered$/i ) {
171 0         0       $stag = 0;
172 0 0       0       if ($self->{'skip_x_ticks'} > 1 ) { #draw a staggered tick every nth label
    0          
173 0         0          for ( 0..$#labels-1) {
174 0 0       0              if ( defined ($labels[$_*$self->{'skip_x_ticks'}])) {
175 0         0                $x = $x_start + $delta*($_*$self->{'skip_x_ticks'})
176                                  - ($w * length($labels[$_*$self->{'skip_x_ticks'}]))/2;
177 0 0       0                if ($stag % 2 == 0) {
178 0         0                   $y_start -= $self->{'text_space'} + $h;
179                            }
180 0         0                $self->{'gd_obj'}->string($font, $x, $y_start,$labels[$_*$self->{'skip_x_ticks'}], $textcolor);
181 0 0       0                if ($stag % 2 == 0) {
182 0         0                   $y_start += $self->{'text_space'} + $h;
183                            }
184 0         0                $stag++;
185                          }
186                      }
187                   }
188                   elsif ($self->{'custom_x_ticks'}) { # draw only the wanted ticks staggered
189 0         0          foreach (sort (@{$self->{'custom_x_ticks'}})) {
  0         0  
190 0 0       0              if ( defined $labels[$_]) {
191 0         0                 $x = $x_start + $delta*$_ - ($w*(length($labels[$_])))/2;
192 0 0       0                 if ($stag % 2 == 0) {
193 0         0                   $y_start -= $self->{'text_space'} + $h;
194                             }
195 0         0                 $self->{'gd_obj'}->string($font, $x, $y_start,$labels[$_], $textcolor);
196 0 0       0                 if ($stag % 2 == 0) {
197 0         0                   $y_start += $self->{'text_space'} + $h;
198                             }
199 0         0                 $stag++;
200                          }
201                      }
202                   }
203                   else { # draw all ticks staggered
204 0         0          for (0..$#labels) {
205 0 0       0             if ( defined $labels[$_] ) {
206 0         0                $x = $x_start + $delta*$_ -($w*(length($labels[$_])))/2;
207 0 0       0               if ($stag % 2 == 0) {
208 0         0                   $y_start -= $self->{'text_space'} + $h;
209                           }
210 0         0               $self->{'gd_obj'}->string($font, $x, $y_start,$labels[$_], $textcolor);
211 0 0       0               if ($stag % 2 == 0) {
212 0         0                   $y_start += $self->{'text_space'} + $h;
213                           }
214 0         0               $stag++;
215                         }
216                      }
217                   }
218                 }
219                 elsif ( $self->{'x_ticks'} =~ /^vertical$/i ) {
220 0         0        $y_start = $self->{'curr_y_max'} - $self->{'text_space'};
221 0 0       0        if ( $self->{'skip_x_ticks'} > 1) { #draw every nth tick vertical
    0          
222 0         0           for (0..$#labels) {
223 0 0       0               if (defined $_) {
224 0         0                  $x = $x_start + $delta*($_ * $self->{'skip_x_ticks'}) - $h/2;
225 0         0                  $y = $y_start - ($x_label_len- length($labels[$_*$self->{'skip_x_ticks'}]))*$w;
226 0         0                  $self->{'gd_obj'}->stringUp($font, $x, $y,$labels[$_*$self->{'skip_x_ticks'}], $textcolor);
227                           }
228                       }
229                    }
230                    elsif ( $self->{'custom_x_ticks'} ) {
231 0         0           foreach ( @{$self->{'custom_x_ticks'}}) { #draw the ticks they want vertical
  0         0  
232 0 0       0              if (defined $labels[$_]) {
233 0         0                 $x = $x_start + $delta*$_ - $h/2;
234 0         0                 $y = $y_start - ($x_label_len- length($labels[$_]))*$w;
235 0         0                 $self->{'gd_obj'}->stringUp($font, $x, $y,$labels[$_], $textcolor);
236                          }
237                       }
238                    }
239                    else { # draw all ticks vertical
240 0         0           for ( 0..$#labels) {
241 0 0       0               if ( defined $labels[$_]) {
242 0         0                  $x = $x_start + $delta*$_ - $h/2;
243 0         0                  $y = $y_start - ($x_label_len- length($labels[$_]))*$w;
244 0         0                  $self->{'gd_obj'}->stringUp($font, $x, $y,$labels[$_], $textcolor);
245                           }
246                       }
247                    }
248                    
249                 }
250                 
251             #update the borders
252 2 100       28     if ($self->{'interval_ticks'} > 0) {
253 1 50       13      if ($self->{'x_ticks'} =~ /^normal$/i ) {
    0          
    0          
254 1         13         $self->{'curr_y_max'} -= $h + $self->{'text_space'}*2;
255                  }
256                  elsif ($self->{'x_ticks'} =~ /^staggered$/i ) {
257 0         0         $self->{'curr_y_max'} -= 2*$h + 3*$self->{'text_space'};
258                  }
259                  elsif ($self->{'x_ticks'} =~ /^vertical$/i ) {
260 0         0         $self->{'curr_y_max'} -= $w * $x_label_len + $self->{