File Coverage

blib/lib/Chart/Points.pm
Criterion Covered Total %
statement 63 77 81.8
branch 13 28 46.4
condition 1 9 11.1
subroutine 6 6 100.0
pod n/a
total 83 120 69.2


line stmt bran cond sub pod time code
1             #====================================================================
2             # Chart::Points
3             #
4             # written by david bonner
5             # dbonner@cs.bu.edu
6             #
7             # maintained by the Chart Group
8             # Chart@wettzell.ifag.de
9             #
10             #---------------------------------------------------------------------
11             # History:
12             #----------
13             # $RCSfile: Points.pm,v $ $Revision: 1.4 $ $Date: 2003/02/14 14:22:05 $
14             # $Author: dassing $
15             # $Log: Points.pm,v $
16             # Revision 1.4 2003/02/14 14:22:05 dassing
17             # First setup to cvs
18             #
19             #====================================================================
20              
21             package Chart::Points;
22              
23 3     3   109 use Chart::Base 2.3;
  3         528  
  3         95  
24 3     3   103 use GD;
  3         26  
  3         57  
25 3     3   73 use Carp;
  3         28  
  3         52  
26 3     3   46 use strict;
  3         27  
  3         42  
27              
28             @Chart::Points::ISA = qw(Chart::Base);
29             $Chart::Points::VERSION = '2.3';
30              
31             #>>>>>>>>>>>>>>>>>>>>>>>>>>#
32             # public methods go here #
33             #<<<<<<<<<<<<<<<<<<<<<<<<<<#
34              
35              
36              
37             #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
38             # private methods go here #
39             #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
40              
41             ## finally get around to plotting the data
42             sub _draw_data {
43 3     3   239   my $self = shift;
44 3         50   my $data = $self->{'dataref'};
45 3         44   my $misccolor = $self->_color_role_to_index('misc');
46 3         30   my ($x1, $x2, $x3, $y1, $y2, $y3, $mod);
47 3         29   my ($width, $height, $delta, $map, $delta_num, $zero_offset);
48 3         29   my ($i, $j, $color, $brush);
49 3         28   my $diff;
50              
51             # init the imagemap data field if they want it
52 3 50       38   if ($self->{'imagemap'} =~ /^true$/i) {
53 0         0     $self->{'imagemap_data'} = [];
54               }
55              
56             # find the delta value between data points, as well
57             # as the mapping constant
58 3         34   $width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
59 3         33   $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
60 3 50       41   $delta = $width / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1);
61 3         33   $diff = ($self->{'max_val'} - $self->{'min_val'});
62 3 50       34   $diff = 1 if $diff == 0;
63 3         30   $map = $height / $diff;
64              
65             #for a xy-plot, use this delta and maybe an offset for the zero-axes
66 3 50       39   if ($self->{'xy_plot'} =~ /^true$/i ) {
67 0         0     $diff = ($self->{'x_max_val'} - $self->{'x_min_val'});
68 0 0       0     $diff = 1 if $diff == 0;
69 0         0     $delta_num = $width / $diff;
70              
71 0 0 0     0     if ($self->{'x_min_val'} <= 0 && $self->{'x_max_val'} >= 0) {
    0 0        
72 0         0        $zero_offset = abs($self->{'x_min_val'}) * abs($delta_num);
73                 }
74                 elsif ($self->{'x_min_val'} > 0 || $self->{'x_max_val'} < 0) {
75 0         0        $zero_offset = -$self->{'x_min_val'} * $delta_num;
76                 }
77                 else {
78 0         0        $zero_offset = 0;
79                 }
80               }
81               
82             # get the base x-y values
83 3 50       39   if ($self->{'xy_plot'} =~ /^false$/i ) {
84 3         35     $x1 = $self->{'curr_x_min'} + ($delta / 2);
85               }
86               else {
87 0         0     $x1 = $self->{'curr_x_min'};
88               }
89 3 100       39   if ($self->{'min_val'} >= 0) {
    50          
90 2         286     $y1 = $self->{'curr_y_max'};
91 2         19     $mod = $self->{'min_val'};
92               }
93               elsif ($self->{'max_val'} <= 0) {
94 0         0     $y1 = $self->{'curr_y_min'};
95 0         0     $mod = $self->{'max_val'};
96               }
97               else {
98 1         12     $y1 = $self->{'curr_y_min'} + ($map * $self->{'max_val'});
99 1         10     $mod = 0;
100 1         33     $self->{'gd_obj'}->line ($self->{'curr_x_min'}, $y1,
101                                          $self->{'curr_x_max'}, $y1,
102                                          $misccolor);
103               }
104               
105             # draw the points
106 3         36   for $i (1..$self->{'num_datasets'}) {
107             # get the color for this dataset, and set the brush
108 5         74     $color = $self->_color_role_to_index('dataset'.($i-1));
109 5         353     $brush = $self->_prepare_brush ($color);
110 5         97     $self->{'gd_obj'}->setBrush ($brush);
111              
112             # draw every point for this dataset
113 5         55     for $j (0..$self->{'num_datapoints'}) {
114             # don't try to draw anything if there's no data
115 3846 100       52509       if (defined ($data->[$i][$j])) {
116 3832 50       56445         if ($self->{'xy_plot'} =~ /^true$/i ) {
117 0         0            $x2 = $x1 + $delta_num * $data->[0][$j] + $zero_offset;
118 0         0            $x3 = $x2 ;
119                     }
120                     else {
121 3832         42703            $x2 = $x1 + ($delta * $j);
122 3832         41601            $x3 = $x2;
123                     }
124 3832         49569 $y2 = $y1 - (($data->[$i][$j] - $mod) * $map);
125 3832         44851 $y3 = $y2;
126              
127             # draw the point only if it is within the chart borders
128 3832 50 33     56305         if ($data->[$i][$j] <= $self->{'max_val'} && $data->[$i][$j] >= $self->{'min_val'}) {
129 3832         49092           $self->{'gd_obj'}->line($x2, $y2, $x3, $y3, gdBrushed);
130                     }
131              
132             # store the imagemap data if they asked for it
133 3832 50       54939 if ($self->{'imagemap'} =~ /^true$/i) {
134 0         0 $self->{'imagemap_data'}->[$i][$j] = [ $x2, $y2 ];
135             }
136                   }
137                 }
138               }
139                   
140             # and finaly box it off
141 3         401   $self->{'gd_obj'}->rectangle ($self->{'curr_x_min'},
142                $self->{'curr_y_min'},
143             $self->{'curr_x_max'},
144             $self->{'curr_y_max'},
145             $misccolor);
146 3         135   return;
147              
148             }
149              
150              
151             ## set the gdBrush object to have nice circular points
152             sub _prepare_brush {
153 9     9   79   my $self = shift;
154 9         83   my $color = shift;
155 9         91   my $radius = $self->{'pt_size'}/2;
156 9         79   my (@rgb, $brush, $white, $newcolor);
157              
158             # get the rgb values for the desired color
159 9         289   @rgb = $self->{'gd_obj'}->rgb($color);
160              
161             # create the new image
162 9         185   $brush = GD::Image->new ($radius*2, $radius*2);
163              
164             # get the colors, make the background transparent
165 9         1001   $white = $brush->colorAllocate (255,255,255);
166 9         142   $newcolor = $brush->colorAllocate (@rgb);
167 9         102   $brush->transparent ($white);
168              
169             # draw the circle
170 9         715   $brush->arc ($radius-1, $radius-1, $radius, $radius, 0, 360, $newcolor);
171              
172             # and fill it
173 9         146   $brush->fill ($radius-1, $radius-1, $newcolor);
174              
175             # set the new image as the main object's brush
176 9         108   return $brush;
177             }
178              
179             ## be a good module and return 1
180             1;
181