Graph/Plot/bars/numerical.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 163 CC 20 SCAR 44.4
CC
20
Cov
0%
new
110.0
count
90.0
valuesInRange
12.0
CRAP 84.7
1 package CGI::Graph::Plot::bars::numerical;
2  
3 use CGI::Graph::Plot::bars;
4  
5 @ISA = ("CGI::Graph::Plot::bars");
6  
7 %default = (
8 label_size => 5,
9 bins => 10
10 );
11  
12 #
13 # calls parent class to initialize values. Selection values are changed, if
14 # necessary.
15 #
16  
17 sub new {
18 my ($pkg, $vars) = @_;
19 my $class = ref($pkg) || $pkg;
20 my $self=$class->SUPER::new($vars);
21  
22 if ($self->{rand}) {
23 return bless $self,$class;
24 }
25  
26 $self->graphBounds;
27  
28 if ($self->{select} || $self->{select_list} eq 'Visible' || $self->{unselect_list} eq 'Visible') {
29 my @selected = split("",$self->{selected});
30 my @row = $self->{table}->col('_row');
31 my ($Xref) = $self->valuesInRange();
32 my @X = @$Xref;
33 my ($X1,$X2);
34  
35 # obtain range for selected X bar(s)
36 if ($self->{select}) {
37 ($X1,$X2) = $X[$self->{select}-1] =~ /^(.*)->(.*)$/;
38 }
39 unless ($self->{select}) {
40 ($X1) = $X[0] =~ /^(.*)->/;
41 ($X2) = $X[-1] =~ /->(.*)$/;
42 }
43  
44 # update selection values for selected elements
45 foreach (0..$self->{table}->nofRow()-1) {
46 last if ($self->{table}->elm($_,$self->{X}) > $X2);
47  
48 if ($self->{table}->elm($_,$self->{X}) >= $X1) {
49 if ($self->{select}) {
50 $selected[$row[$_]-1] =
51 ($self->{select_type} eq'select')?1:0;
52 }
53 else {
54 $selected[$row[$_]-1] =
55 ($self->{select_list} eq 'Visible')?1:0;
56 }
57 }
58 }
59  
60 $self->{selected} = join("",@selected);
61 $self->write_selected();
62 }
63  
64 return bless $self, $class;
65 }
66  
67 #
68 # returns X and Y values for a histogram
69 #
70  
71 sub count {
72 my $self = shift;
73 my $drawPartial = shift if @_;
74  
75 my @selected = split("",$self->{selected});
76  
77 my @S = $self->{table}->col($self->{X});
78 my @row = $self->{table}->col('_row');
79  
80 my $bins = $default{bins};
81 my ($start,$end);
82  
83 # determine start and end values depending on
84 # what portion of the graph is to be drawn
85  
86 if ($drawPartial) {
87 my $delta = $S[-1]-$S[0];
88 $start = $self->{x_min}*$delta+$S[0];
89 $end = $self->{x_max}*$delta+$S[0];
90 }
91  
92 else {
93 $start = $S[0];
94 $end = $S[-1];
95 }
96  
97 my $int = $self->{X}=~/^[i]_/;
98 my $size = ($int)?($end-$start+1)/$bins:($end-$start)/$bins;
99  
100 # if using integer values, do not allow more bins than needed
101 while ($int) {
102 $size = ($end-$start+1)/$bins;
103 last if ($size >= 1);
104 $bins-- if ($size < 1);
105 }
106  
107 # put range values into X array
108 my @X;
109 my $range;
110 foreach (0..$bins-1) {
111 $range = ($int) ?
112 int($_*$size+$start).'->'.int(($_+1)*$size+$start-1)
113 :($_*$size+$start).'->'.(($_+1)*$size+$start);
114 push @X, $range;
115 }
116  
117 # count values in each X range and store results in Y
118 # also note how many selected elements are in each range
119 my @Y;
120 my @selectDraw;
121 foreach (0..$#X) {
122 push @Y,0;
123 $X[$_] =~ /(.*)->(.*)/;
124  
125 foreach $count (0..$#S) {
126 if (($S[$count] >= $1) && ($S[$count] <= $2)) {
127 $selectDraw[$_]++ if ($selected[$row[$count]-1]
128 == 1);
129 $Y[$_]++;
130 }
131 }
132 }
133  
134 return (\@X,\@Y,\@selectDraw);
135 }
136  
137 #
138 # depending on what type of histogram is used, returns X and Y values.
139 #
140  
141 sub valuesInRange {
142 my $self = shift;
143  
144 my (@dataX,@drawX,@dataY,@selectDraw);
145  
146 # bar graph style, zooms in without re-calculating histogram
147 if ($self->{histogram_type} eq "fixed") {
148 my ($Xref,$Yref,$Sref) = $self->count();
149 my @X = @$Xref;
150 my @Y = @$Yref;
151 my @S = @$Sref;
152  
153 for ($i=0; $i<=$#X; $i++) {
154 if ( (($i+1)/($#X+2) >= $self->{x_min}) &&
155 (($i+1)/($#X+2) <= $self->{x_max}) ) {
156 push (@dataX, $X[$i]);
157 push (@dataY, $Y[$i]);
158 push (@selectDraw, $S[$i]);
159 }
160 }
161 }
162  
163 # "variable" graph, re-calculates histogram when zooming in
164 else {
165 my ($Xref,$Yref,$Sref) = $self->count(1);
166 @dataX = @$Xref;
167 @dataY = @$Yref;
168 @selectDraw = @$Sref;
169 }
170  
171 # shorten labels
172 foreach (0..$#dataX) {
173 $dataX[$_] =~ /(.*)->(.*)/;
174 $drawX[$_] = sprintf("%.$default{label_size}g->%".
175 ".$default{label_size}g", $1,$2);
176 }
177 return (\@dataX,\@drawX,\@dataY,\@selectDraw);
178 }