| 1 | |||
| 2 | ###################################################################### | ||
| 3 | ## $Id: ChartDirector.pm 10733 2008-02-14 19:03:32Z spadkins $ | ||
| 4 | ###################################################################### | ||
| 5 | |||
| 6 | package App::Widget::ChartDirector; | ||
| 7 | $VERSION = (q$Revision: 10733 $ =~ /(\d[\d\.]*)/)[0]; # VERSION numbers generated by svn | ||
| 8 | |||
| 9 | use App; | ||
| 10 | use App::Widget::Graph; | ||
| 11 | @ISA = ( "App::Widget::Graph" ); | ||
| 12 | |||
| 13 | use Date::Format; | ||
| 14 | use Date::Parse; | ||
| 15 | #use Data::Dumper; | ||
| 16 | use strict; | ||
| 17 | use POSIX qw(ceil floor); | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | App::Widget::ChartDirector - Graphing widget which displays graphs and charts for web applications using the ChartDirector graphing library within the App-Context Framework | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | $name = "first_name"; | ||
| 26 | |||
| 27 | # official way | ||
| 28 | use App; | ||
| 29 | $context = App->context(); | ||
| 30 | $w = $context->widget($name); | ||
| 31 | # OR ... | ||
| 32 | $w = $context->widget($name, | ||
| 33 | class => "App::Widget::ChartDirector", | ||
| 34 | ); | ||
| 35 | |||
| 36 | # internal way | ||
| 37 | use App::Widget::ChartDirector; | ||
| 38 | $w = App::Widget::ChartDirector->new($name); | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | A graphing widget which displays graphs and charts for web | ||
| 45 | applications using the ChartDirector graphing library within the | ||
| 46 | App-Context/App-Widget widget framework. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub html { | ||
| 51 | &App::sub_entry if ($App::trace); | ||
| 52 | my $self = shift; | ||
| 53 | my $name = $self->{name}; | ||
| 54 | my $spec = $self->create_graph_spec(); | ||
| 55 | my ($html); | ||
| 56 | if ($self->{defer_images}) { # write out the graph spec. produce graph image later. | ||
| 57 | my $spec_path = $spec->{spec_path}; | ||
| 58 | if (open(App::Widget::ChartDirector::FILE, "> $spec_path")) { | ||
| 59 | $self->write_graph_spec(\*App::Widget::ChartDirector::FILE, $spec); | ||
| 60 | close(App::Widget::ChartDirector::FILE); | ||
| 61 | $html .= "<img src=\"$spec->{cgi_url}\""; | ||
| 62 | $html .= " height=\"$spec->{height}\"" if ($spec->{height}); | ||
| 63 | $html .= " width=\"$spec->{width}\"" if ($spec->{width}); | ||
| 64 | $html .= ">\n"; | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | $html .= "[Error creating graph spec $spec_path: $!]"; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | else { # generate graph image now | ||
| 71 | eval { | ||
| 72 | $html = $self->write_graph_image($spec); | ||
| 73 | }; | ||
| 74 | if ($@) { | ||
| 75 | if ($@ =~ /(no columns in graph)/) { | ||
| 76 | $html .= "[Error creating graph image, $1.]"; | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | $html .= "[Error creating graph image $spec->{image_path}: $@]"; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | if ($self->{debug}) { | ||
| 84 | $html .= "<pre>\n"; | ||
| 85 | foreach my $key (sort keys %$spec) { | ||
| 86 | $html .= "$key = "; | ||
| 87 | if (ref($spec->{$key}) eq "ARRAY") { | ||
| 88 | $html .= "[ " . join(", ", @{$spec->{$key}}) . " ]\n"; | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | $html .= "$spec->{$key}\n"; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | $html .= "</pre>\n"; | ||
| 95 | } | ||
| 96 | |||
| 97 | &App::sub_exit($html) if ($App::trace); | ||
| 98 | $html; | ||
| 99 | } | ||
| 100 | |||
| 101 | my $serial = 1; | ||
| 102 | |||
| 103 | sub create_graph_spec { | ||
| 104 | &App::sub_entry if ($App::trace); | ||
| 105 | my ($self) = @_; | ||
| 106 | my $context = $self->{context}; | ||
| 107 | my $options = $context->{options}; | ||
| 108 | my $spec_tempdir = $options->{tempdir} || "$options->{prefix}/tmp"; | ||
| 109 | my $image_tempdir = "$options->{html_dir}/temp"; | ||
| 110 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); | ||
| 111 | $mon++; | ||
| 112 | $year += 1900; | ||
| 113 | my $datetime = sprintf("%04d%02d%02d-%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec); | ||
| 114 | my $unique_tag = $datetime . "-" . $$ . "-" . $serial; | ||
| 115 | my $spec_file = "$unique_tag.graph"; | ||
| 116 | my $spec_path = "$spec_tempdir/$spec_file"; | ||
| 117 | my $image_file = "$unique_tag.png"; | ||
| 118 | my $image_path = "$image_tempdir/$image_file"; | ||
| 119 | while (-f $spec_path || -f $image_path) { | ||
| 120 | $serial++; | ||
| 121 | $unique_tag = $datetime . "-" . $$ . "-" . $serial; | ||
| 122 | $spec_file = "$unique_tag.graph"; | ||
| 123 | $spec_path = "$spec_tempdir/$spec_file"; | ||
| 124 | $image_file = "$unique_tag.png"; | ||
| 125 | $image_path = "$image_tempdir/$image_file"; | ||
| 126 | } | ||
| 127 | my %spec = %$self; | ||
| 128 | delete $spec{context}; | ||
| 129 | $spec{graphtype} ||= "bar"; | ||
| 130 | if ($spec{graphtype} eq "area") { | ||
| 131 | $spec{graphtype} = "line"; | ||
| 132 | $spec{area} = 1; | ||
| 133 | } | ||
| 134 | elsif ($spec{graphtype} eq "stacked_bar") { | ||
| 135 | $spec{graphtype} = "bar"; | ||
| 136 | $spec{stacked} = 1; | ||
| 137 | } | ||
| 138 | $spec{height} ||= 280; | ||
| 139 | $spec{width} ||= 360; | ||
| 140 | $spec{spec_file} = $spec_file; | ||
| 141 | $spec{spec_path} = $spec_path; | ||
| 142 | $spec{image_file} = $image_file; | ||
| 143 | $spec{image_path} = $image_path; | ||
| 144 | $spec{image_url} = "$options->{html_url_dir}/temp/$image_file"; | ||
| 145 | $spec{cgi_url} = "$options->{script_url_dir}/app-cdgraph/$spec_file"; | ||
| 146 | &App::sub_exit(\%spec) if ($App::trace); | ||
| 147 | return(\%spec); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub write_graph_spec { | ||
| 151 | &App::sub_entry if ($App::trace); | ||
| 152 | my ($self, $fh, $spec) = @_; | ||
| 153 | foreach my $key (%$spec) { | ||
| 154 | if (!ref($spec->{$key})) { | ||
| 155 | print $fh "\$data{$key} = [ "; | ||
| 156 | foreach my $value (@{$spec->{$key}}) { | ||
| 157 | print $fh "\"$spec->{$key}\", "; | ||
| 158 | } | ||
| 159 | print $fh "]\n"; | ||
| 160 | } | ||
| 161 | else { | ||
| 162 | print $fh "\$data{$key} = \"$spec->{$key}\";\n"; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | &App::sub_exit() if ($App::trace); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub read_graph_spec { | ||
| 169 | &App::sub_entry if ($App::trace); | ||
| 170 | my ($self, $fh, $spec) = @_; | ||
| 171 | foreach my $key (%$spec) { | ||
| 172 | if (!ref($spec->{$key})) { | ||
| 173 | print $fh "\$data{$key} = [ "; | ||
| 174 | foreach my $value (@{$spec->{$key}}) { | ||
| 175 | print $fh "\"$spec->{$key}\", "; | ||
| 176 | } | ||
| 177 | print $fh "]\n"; | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | print $fh "\$data{$key} = \"$spec->{$key}\";\n"; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | &App::sub_exit() if ($App::trace); | ||
| 184 | } | ||
| 185 | |||
| 186 | sub write_graph_image { | ||
| 187 | &App::sub_entry if ($App::trace); | ||
| 188 | my ($self, $spec) = @_; | ||
| 189 | my $graphtype = $spec->{graphtype} || "bar"; | ||
| 190 | my %known_graphtype = ( | ||
| 191 | bar => 1, | ||
| 192 | area => 1, | ||
| 193 | line => 1, | ||
| 194 | pie => 1, | ||
| 195 | meter => 1, | ||
| 196 | step => 1, | ||
| 197 | ); | ||
| 198 | my $html = ""; | ||
| 199 | if ($known_graphtype{$graphtype}) { | ||
| 200 | my $method = "write_${graphtype}_graph_image"; | ||
| 201 | $self->$method($spec); | ||
| 202 | $html .= "<img src=\"$spec->{image_url}\""; | ||
| 203 | $html .= " height=\"$spec->{height}\"" if ($spec->{height}); | ||
| 204 | $html .= " width=\"$spec->{width}\"" if ($spec->{width}); | ||
| 205 | $html .= ">\n"; | ||
| 206 | } | ||
| 207 | else { | ||
| 208 | $html = "[$self->{name}: Unknown graph type ($graphtype)]\n"; | ||
| 209 | } | ||
| 210 | &App::sub_exit() if ($App::trace); | ||
| 211 | return($html); | ||
| 212 | } | ||
| 213 | |||
| 214 | sub get_num_dims { | ||
| 215 | &App::sub_entry if ($App::trace); | ||
| 216 | my ($self, $graphtype) = @_; | ||
| 217 | $graphtype = $self->{graphtype} if (!$graphtype); | ||
| 218 | my %dims = ( | ||
| 219 | bar => 2, | ||
| 220 | area => 2, | ||
| 221 | line => 2, | ||
| 222 | stacked_bar => 2, | ||
| 223 | pie => 1, | ||
| 224 | meter => 0, | ||
| 225 | ); | ||
| 226 | my $num_dims = $dims{$graphtype}; | ||
| 227 | $num_dims = 2 if (!defined $num_dims); | ||
| 228 | &App::sub_exit($num_dims) if ($App::trace); | ||
| 229 | return($num_dims); | ||
| 230 | } | ||
| 231 | |||
| 232 | sub new_xy_chart { | ||
| 233 | &App::sub_entry if ($App::trace); | ||
| 234 | my ($self, $spec) = @_; | ||
| 235 | require "perlchartdir.pm"; | ||
| 236 | my $x = $self->get_x($spec); # print STDERR Dumper $x; | ||
| 237 | my $width = $spec->{width} || 250; | ||
| 238 | my $height = $spec->{height} || 250; | ||
| 239 | my $left_margin = $spec->{left_margin}; | ||
| 240 | my $bottom_margin = $spec->{bottom_margin}; | ||
| 241 | my $right_margin = $spec->{right_margin} || 20; | ||
| 242 | my $top_margin = $spec->{top_margin}; | ||
| 243 | my $plot_bgcolor = $self->get_theme_value("plot_bgcolor", "0xfffcf0"); | ||
| 244 | my $bgcolor = $self->get_theme_value("bgcolor", "0x3f65b8"); | ||
| 245 | my $titlecolor = $self->get_theme_value("titlecolor", "0xfffcf0"); | ||
| 246 | my $x_labelcolor = $self->get_theme_value("x_labelcolor", "0xfffcf0"); | ||
| 247 | my $y_labelcolor = $self->get_theme_value("y_labelcolor", "0xfffcf0"); | ||
| 248 | my $y_titlecolor = $self->get_theme_value("y_titlecolor", "0xfffcf0"); | ||
| 249 | my $y_max_zero = 1; | ||
| 250 | |||
| 251 | if (!$top_margin) { | ||
| 252 | $top_margin = 5; | ||
| 253 | $top_margin += 20 if ($spec->{title}); | ||
| 254 | #$top_margin += 20 if ($spec->{y_labels}); | ||
| 255 | $top_margin += 8 if ($spec->{"3D"}); | ||
| 256 | } | ||
| 257 | if (!$bottom_margin) { | ||
| 258 | $bottom_margin = 10; | ||
| 259 | $bottom_margin += 9 if (!$spec->{registered}); | ||
| 260 | $bottom_margin += 15 if ($x); | ||
| 261 | $bottom_margin += 18 if ($spec->{x_title}); | ||
| 262 | } | ||
| 263 | if (!$left_margin) { | ||
| 264 | # TODO: This should be sensitive to the width of the numbers in the scale | ||
| 265 | my ($y_min, $y_max) = $self->get_y_limits($spec); | ||
| 266 | $y_min = int($y_min); | ||
| 267 | $y_max = int($y_max); | ||
| 268 | $y_max_zero = int($y_max); | ||
| 269 | my $y_label_len = length($y_max); | ||
| 270 | $y_label_len = length($y_min) if (length($y_min) > $y_label_len); | ||
| 271 | $left_margin = 20 + $y_label_len * 6; | ||
| 272 | $left_margin += 20 if ($spec->{y_title}); | ||
| 273 | } | ||
| 274 | my ($total_x_length, @x_values); | ||
| 275 | if ($spec->{x_label_pos}) { | ||
| 276 | foreach (@$x){ | ||
| 277 | $total_x_length += length($_) + 2; #Taking 2 as the gap value | ||
| 278 | push (@x_values, length($_)); | ||
| 279 | } | ||
| 280 | @x_values = sort { $a <=> $b} @x_values; | ||
| 281 | $self->{total_x_length} = $total_x_length; | ||
| 282 | $self->{width} = $width; | ||
| 283 | if ($total_x_length > $width/8 && $x->[0] !~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/) { | ||
| 284 | $width += ($#$x) * ceil (3 * $#$x / 100 ) + 0.5 if ($#$x > 50); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | my $legend_pos = $spec->{legend_pos} || "default"; # Any one from default, top or bottom | ||
| 288 | my (@length_y_value, $max_legend_length, $num_legend_columns, $legend_width, $margin, $textsize, $number_legend_rows, $length_labels, $length_labels_avg); | ||
| 289 | my (@length_all_labels, $diff_largest_and_avg, $gap_legends); | ||
| 290 | if ($legend_pos ne "default") { | ||
| 291 | $legend_width = $width - 2 * $right_margin; | ||
| 292 | ($margin,$textsize) = (6,10); | ||
| 293 | $max_legend_length = ceil ( $legend_width / ($textsize * 3/4)); # This is a nearyby approximation of number of characters inside a row in the legends | ||
| 294 | $length_labels = 0; | ||
| 295 | foreach my $y_value (@{$spec->{y_labels}}) { | ||
| 296 | $length_labels += length($y_value); | ||
| 297 | push (@length_all_labels, length($y_value) ); | ||
| 298 | } | ||
| 299 | @length_all_labels = sort { $a <=> $b } @length_all_labels; | ||
| 300 | $length_labels_avg = ceil ($length_labels / ($#{$spec->{y_labels}} + 1)); | ||
| 301 | $diff_largest_and_avg = $length_all_labels[-1] - $length_labels_avg; | ||
| 302 | # Calculation of gap b/w the legends | ||
| 303 | $gap_legends = $margin; | ||
| 304 | if($diff_largest_and_avg > $gap_legends) { | ||
| 305 | $gap_legends += ceil ($diff_largest_and_avg / 2); | ||
| 306 | } | ||
| 307 | else { | ||
| 308 | $gap_legends += 1; | ||
| 309 | $gap_legends -= ceil ($diff_largest_and_avg - $gap_legends / 3) if ($length_labels_avg >= $max_legend_length / 4); # Reduce gap if the average length of legends is higher than max. legend length by 4. | ||
| 310 | } | ||
| 311 | # Calculation of number of columns | ||
| 312 | for (my $cols = 1; $cols <= 20; $cols++) { # Taking min. 1 and max. 20 columns | ||
| 313 | $num_legend_columns = $cols if (($length_labels_avg + $gap_legends) < ($max_legend_length / $cols)); | ||
| 314 | } | ||
| 315 | $num_legend_columns += 1 if (($length_labels_avg + $gap_legends) > ($max_legend_length / 2)); #Add a new column if the avg. legend length is greater than half of the max. permissible length | ||
| 316 | |||
| 317 | if ($spec->{num_legend_columns} ) { | ||
| 318 | $num_legend_columns = $spec->{num_legend_columns}; | ||
| 319 | } | ||
| 320 | do { | ||
| 321 | $num_legend_columns += 1 if ($height > (2 * $spec->{height})); #If the legend height is greater than max. permissible limit than add a new column and the legends will truncate | ||
| 322 | $height = $spec->{height}; | ||
| 323 | $number_legend_rows = int (($#{$spec->{y_labels}} + 1) / $num_legend_columns); | ||
| 324 | if (($#{$spec->{y_labels}} + 1) % $num_legend_columns != 0 ) { | ||
| 325 | $number_legend_rows += 1; | ||
| 326 | } | ||
| 327 | $height += (($margin + $textsize) * $number_legend_rows) + (4 * $margin); | ||
| 328 | } while ($height > (2 * $spec->{height})); # Max. graph height should not be greater than twice of chosen graph height | ||
| 329 | |||
| 330 | if ($#{$spec->{y_labels}} <= 0) { | ||
| 331 | $height = $spec->{height}; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | |||
| 336 | my $graph_height_increment = 0; | ||
| 337 | my $fontsize = 10; | ||
| 338 | if ($spec->{x_label_pos}) { | ||
| 339 | if ($#$x > 30 && $total_x_length > $width/8 && $x->[0] !~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/) { | ||
| 340 | for (my $i = 30; $i <= $#$x; $i++ ) { | ||
| 341 | $fontsize = $fontsize - 0.1; | ||
| 342 | $fontsize = 1 if ($fontsize < 1); | ||
| 343 | } | ||
| 344 | $fontsize += ceil(($width - $spec->{width})/$#$x) - 1 if ($#$x > 50); | ||
| 345 | } | ||
| 346 | $self->{fontsize} = $fontsize; | ||
| 347 | if ($total_x_length > $width/8 && $x->[0] !~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/) { | ||
| 348 | $graph_height_increment = 1/2 * $x_values[-1] * floor ($fontsize); | ||
| 349 | $height += $graph_height_increment; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | my $c = new XYChart($width, $height); | ||
| 354 | |||
| 355 | my $graph_adjusted_height = $height - $spec->{height}-$graph_height_increment; | ||
| 356 | my ($top_margin_adjusted,$bottom_margin_adjusted); | ||
| 357 | |||
| 358 | if ($legend_pos eq "top" ) { | ||
| 359 | $top_margin_adjusted = $top_margin+$graph_adjusted_height; | ||
| 360 | $bottom_margin_adjusted = $height-$top_margin-$bottom_margin-$graph_adjusted_height-$graph_height_increment; | ||
| 361 | } | ||
| 362 | elsif ($legend_pos eq "bottom") { | ||
| 363 | $top_margin_adjusted = $top_margin; | ||
| 364 | $bottom_margin_adjusted = $height-$top_margin-$bottom_margin-$graph_adjusted_height-$graph_height_increment; | ||
| 365 | } | ||
| 366 | else { | ||
| 367 | $top_margin_adjusted = $top_margin; | ||
| 368 | $bottom_margin_adjusted = $height-$top_margin-$bottom_margin-$graph_height_increment; | ||
| 369 | } | ||
| 370 | my $plot_area = $c->setPlotArea($left_margin , $top_margin_adjusted, | ||
| 371 | $width-$left_margin-$right_margin, | ||
| 372 | $bottom_margin_adjusted); | ||
| 373 | |||
| 374 | $spec->{plot_area_x} = $left_margin; | ||
| 375 | $spec->{plot_area_y} = $top_margin_adjusted; | ||
| 376 | $spec->{plot_area_width} = $width - $left_margin - $right_margin; | ||
| 377 | $spec->{plot_area_height} = $bottom_margin_adjusted; | ||
| 378 | $spec->{plot_bgcolor} = $plot_bgcolor; | ||
| 379 | |||
| 380 | $plot_area->setBackground(hex($plot_bgcolor), hex($plot_bgcolor)); | ||
| 381 | |||
| 382 | $c->addTitle($spec->{title}, "arialbd.ttf", 12, hex($titlecolor)) if ($spec->{title}); | ||
| 383 | |||
| 384 | if ($y_max_zero == 0) { | ||
| 385 | $c->yAxis()->setMargin($top_margin_adjusted + 15); | ||
| 386 | } | ||
| 387 | |||
| 388 | #Add a legend box at (55, 22) using horizontal layout, with transparent | ||
| 389 | #background | ||
| 390 | my $legend; | ||
| 391 | if ($spec->{y_labels}) { | ||
| 392 | my $x_adj = 0; | ||
| 393 | my $y_adj = -2; | ||
| 394 | if ($spec->{"3D"}) { | ||
| 395 | $x_adj += 5; | ||
| 396 | $y_adj += -5; | ||
| 397 | } | ||
| 398 | |||
| 399 | if (($#{$spec->{y_labels}} > 0) && ($legend_pos eq "top" || $legend_pos eq "bottom")) { | ||
| 400 | if ($legend_pos eq "top" ) { | ||
| 401 | $legend = $c->addLegend($right_margin, $top_margin + $y_adj, 0,"arial.ttf",10); | ||
| 402 | } | ||
| 403 | elsif ($legend_pos eq "bottom" ) { | ||
| 404 | $legend = $c->addLegend($right_margin, $height - $graph_adjusted_height - $top_margin/4 , 0,"arial.ttf",10); | ||
| 405 | } | ||
| 406 | $legend->setBackground(hex($plot_bgcolor)); | ||
| 407 | $legend->setMargin(5); | ||
| 408 | $legend->setWidth($legend_width); | ||
| 409 | $legend->setCols($num_legend_columns); | ||
| 410 | $legend->setTruncate($legend_width,1); | ||
| 411 | } | ||
| 412 | else { | ||
| 413 | $legend = $c->addLegend($left_margin+$x_adj, $top_margin+$y_adj, 0); | ||
| 414 | $legend->setBackground($perlchartdir::Transparent); | ||
| 415 | $legend->setMargin(5); | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | # To handle the situation when the graph wrapping happens. | ||
| 420 | if ($legend_pos eq "top" || $legend_pos eq "bottom") { | ||
| 421 | my $graph_count = $spec->{graph_count} || 1; | ||
| 422 | $spec->{height} = $height if ($graph_count == 1); | ||
| 423 | } | ||
| 424 | |||
| 425 | if ($spec->{x_label_pos} && $total_x_length > $width/8 && $x->[0] !~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/) { | ||
| 426 | $spec->{height} = $height; | ||
| 427 | $spec->{width} = $width; | ||
| 428 | } | ||
| 429 | |||
| 430 | $c->yAxis()->setTitle($spec->{y_title}, "arial.ttf", 10, hex($y_titlecolor)) if ($spec->{y_title}); | ||
| 431 | $c->setBackground(hex($bgcolor)); | ||
| 432 | $c->yAxis()->setLabelStyle("arial.ttf", 10, hex($y_labelcolor)); | ||
| 433 | $c->xAxis()->setLabelStyle("arial.ttf", 10, hex($x_labelcolor)); | ||
| 434 | |||
| 435 | if ($spec->{add_mark} ne "" || $spec->{add_mark2} ne "") { | ||
| 436 | my $value = $spec->{add_mark}; | ||
| 437 | my $value2 = $spec->{add_mark2}; | ||
| 438 | my $color = $spec->{add_mark_color}; | ||
| 439 | my $color2 = $spec->{add_mark_color2}; | ||
| 440 | |||
| 441 | $color =~ s/^#//; | ||
| 442 | $color = "000000" if (!$color || $color eq "none"); | ||
| 443 | $color2 =~ s/^#//; | ||
| 444 | $color2 = "000000" if (!$color2 || $color2 eq "none"); | ||
| 445 | |||
| 446 | my ($add_mark_vals); | ||
| 447 | foreach my $data ([$value, $color], [$value2, $color2]) { | ||
| 448 | my $hash = {}; | ||
| 449 | if (defined $data->[0]) { | ||
| 450 | $hash->{value} = $data->[0]; | ||
| 451 | $hash->{color} = $data->[1]; | ||
| 452 | push (@$add_mark_vals, $hash); | ||
| 453 | } | ||
| 454 | } | ||
| 455 | |||
| 456 | foreach my $add_mark_val (@$add_mark_vals) { | ||
| 457 | my $amk_value = $add_mark_val->{value}; | ||
| 458 | my $amk_color = $add_mark_val->{color}; | ||
| 459 | |||
| 460 | if ($amk_value =~ /(?:^-)?\d+(?:\.)?(?:\d+)?$/ && length($amk_color) == 6) { | ||
| 461 | my $hex_color = hex($amk_color); | ||
| 462 | $c->yAxis()->addMark($amk_value, $c->dashLineColor($hex_color, $perlchartdir::DashLine)); | ||
| 463 | } | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | if (defined $spec->{add_zone}) { | ||
| 468 | my @values = split(",", $spec->{add_zone}); | ||
| 469 | |||
| 470 | foreach my $val (@values) { | ||
| 471 | my ($zone1, $zone2, $color) = split(":", $val); | ||
| 472 | $color =~ s/^#//; | ||
| 473 | |||
| 474 | if ($zone1 =~ /((?:^-)?\d+(?:\.)?(?:\d+)?)/) { | ||
| 475 | $zone1 = $1; | ||
| 476 | } | ||
| 477 | if ($zone2 =~ /((?:^-)?\d+(?:\.)?(?:\d+)?)/) { | ||
| 478 | $zone2 = $1; | ||
| 479 | } | ||
| 480 | if (length($color) == 6 && $zone1 ne "" && $zone2 ne "") { | ||
| 481 | $color = hex($color); | ||
| 482 | $c->yAxis()->addZone($zone1, $zone2, $color); | ||
| 483 | } | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | &App::sub_exit($c) if ($App::trace); # print STDERR Dumper $c; | ||
| 488 | return($c); | ||
| 489 | } | ||
| 490 | |||
| 491 | sub new_pie_chart { | ||
| 492 | &App::sub_entry if ($App::trace); | ||
| 493 | my ($self, $spec) = @_; | ||
| 494 | require "perlchartdir.pm"; | ||
| 495 | |||
| 496 | my $bgcolor = $self->get_theme_value("bgcolor", "0x3f65b8"); | ||
| 497 | my $titlecolor = $self->get_theme_value("titlecolor", "0xfffcf0"); | ||
| 498 | my $labelcolor = $self->get_theme_value("x_labelcolor", "0xfffcf0"); | ||
| 499 | |||
| 500 | my $x = $self->get_x($spec); | ||
| 501 | my $width = $spec->{width} || 250; | ||
| 502 | my $height = $spec->{height} || 250; | ||
| 503 | my $left_margin = $spec->{left_margin}; | ||
| 504 | my $bottom_margin = $spec->{bottom_margin}; | ||
| 505 | my $right_margin = $spec->{right_margin} || 20; | ||
| 506 | my $top_margin = $spec->{top_margin}; | ||
| 507 | if (!$top_margin) { | ||
| 508 | $top_margin = 5; | ||
| 509 | $top_margin += 20 if ($spec->{title}); | ||
| 510 | $top_margin += 5 if ($spec->{"3D"}); | ||
| 511 | } | ||
| 512 | if (!$bottom_margin) { | ||
| 513 | $bottom_margin = 10; | ||
| 514 | $bottom_margin += 9 if (!$spec->{registered}); | ||
| 515 | $bottom_margin += 15 if ($x); | ||
| 516 | } | ||
| 517 | if (!$left_margin) { | ||
| 518 | $left_margin = 20; | ||
| 519 | } | ||
| 520 | |||
| 521 | my $c = new PieChart($width, $height, hex($bgcolor)); | ||
| 522 | my $center_x = int($width/2); | ||
| 523 | my $center_y = int(($height - 20)/2); | ||
| 524 | my $radius = (($center_x > $center_y) ? $center_y : $center_x) - 40; | ||
| 525 | $c->setPieSize($center_x, $center_y, $radius); | ||
| 526 | |||
| 527 | $c->addTitle($spec->{title}, "arialbd.ttf", 12, hex($titlecolor)) if ($spec->{title}); | ||
| 528 | $c->setLabelStyle("arial.ttf", 10, hex($labelcolor)); | ||
| 529 | |||
| 530 | &App::sub_exit($c) if ($App::trace); | ||
| 531 | return($c); | ||
| 532 | } | ||
| 533 | |||
| 534 | sub new_meter_chart { | ||
| 535 | &App::sub_entry if ($App::trace); | ||
| 536 | my ($self, $spec) = @_; | ||
| 537 | require "perlchartdir.pm"; | ||
| 538 | |||
| 539 | my $bgcolor = $self->get_theme_value("bgcolor", "0x3f65b8"); | ||
| 540 | my $titlecolor = $self->get_theme_value("titlecolor", "0xfffcf0"); | ||
| 541 | my $labelcolor = $self->get_theme_value("x_labelcolor", "0xfffcf0"); | ||
| 542 | |||
| 543 | my $x = $self->get_x($spec); | ||
| 544 | my $width = $spec->{width} || 250; | ||
| 545 | my $height = $spec->{height} || 180; | ||
| 546 | my $left_margin = $spec->{left_margin}; | ||
| 547 | my $bottom_margin = $spec->{bottom_margin}; | ||
| 548 | my $right_margin = $spec->{right_margin} || int($width * 0.08); | ||
| 549 | my $top_margin = $spec->{top_margin}; | ||
| 550 | if (!$top_margin) { | ||
| 551 | $top_margin = 5; | ||
| 552 | $top_margin += 20 if ($spec->{title}); | ||
| 553 | $top_margin += 5 if ($spec->{"3D"}); | ||
| 554 | } | ||
| 555 | if (!$left_margin) { | ||
| 556 | $left_margin = $right_margin; | ||
| 557 | } | ||
| 558 | if (!$bottom_margin) { | ||
| 559 | $bottom_margin = 10; | ||
| 560 | $bottom_margin += 9 if (!$spec->{registered}); | ||
| 561 | $bottom_margin += 15 if ($x); | ||
| 562 | } | ||
| 563 | my $c = new AngularMeter($width, $height, hex($bgcolor)); | ||
| 564 | my $max_width_radius = int(($width - $left_margin - $right_margin)/2); | ||
| 565 | my $max_height_radius = $height - $top_margin - $bottom_margin; | ||
| 566 | my $radius = ($max_width_radius > $max_height_radius) ? $max_height_radius : $max_width_radius; | ||
| 567 | my $center_x = $left_margin + $max_width_radius; | ||
| 568 | my $center_y = int(($height - $bottom_margin - $top_margin + $radius)/2) + $top_margin; | ||
| 569 | $spec->{radius} = $radius; | ||
| 570 | $spec->{center_x} = $center_x; | ||
| 571 | $spec->{center_y} = $center_y; | ||
| 572 | $c->setMeter($center_x, $center_y, $radius, -90, 90); | ||
| 573 | #$c->setRoundedFrame(); | ||
| 574 | |||
| 575 | $c->addTitle($spec->{title}, "arialbd.ttf", 12, hex($titlecolor)) if ($spec->{title}); | ||
| 576 | |||
| 577 | &App::sub_exit($c) if ($App::trace); | ||
| 578 | return($c); | ||
| 579 | } | ||
| 580 | |||
| 581 | sub modify_plot_area { | ||
| 582 | &App::sub_entry if ($App::trace); | ||
| 583 | my ($self, $spec, $x, $c) = @_; | ||
| 584 | |||
| 585 | my $PIX_PER_CHAR = 15.5; | ||
| 586 | my $MAX_LABEL_LENGTH = 3; | ||
| 587 | my $plot_area_x = $spec->{plot_area_x}; | ||
| 588 | my $plot_area_y = $spec->{plot_area_y}; | ||
| 589 | my $plot_area_width = $spec->{plot_area_width}; | ||
| 590 | my $plot_area_height = $spec->{plot_area_height}; | ||
| 591 | |||
| 592 | #print STDERR "plot_area_x = [$plot_area_x]\n"; | ||
| 593 | #print STDERR "plot_area_y = [$plot_area_y]\n"; | ||
| 594 | #print STDERR "plot_area_width = [$plot_area_width]\n"; | ||
| 595 | #print STDERR "plot_area_height = [$plot_area_height]\n"; | ||
| 596 | |||
| 597 | my $max_length_x = 0; | ||
| 598 | my $total_length_x = 0; | ||
| 599 | for (my $i = 0; $i <= $#$x; $i++) { | ||
| 600 | if (length($x->[$i]) > $max_length_x) { | ||
| 601 | $max_length_x = length($x->[$i]); | ||
| 602 | } | ||
| 603 | $total_length_x = $total_length_x + length($x->[$i]); | ||
| 604 | } | ||
| 605 | |||
| 606 | #print STDERR "max_length_x = [$max_length_x]\n"; | ||
| 607 | #print STDERR "total_length_x = [$total_length_x]\n"; | ||
| 608 | #print STDERR "tlx * $PIX_PER_CHAR = [",$total_length_x * $PIX_PER_CHAR,"]\n"; | ||
| 609 | |||
| 610 | if ($total_length_x * $PIX_PER_CHAR > $plot_area_width && $x->[0] !~ /^\d+-\d+/) { | ||
| 611 | if ($max_length_x > $MAX_LABEL_LENGTH) { | ||
| 612 | |||
| 613 | $c->xAxis()->setLabels($spec->{y_labels})->setFontAngle(45); | ||
| 614 | |||
| 615 | my $mod_plot_area_height = $plot_area_height - ceil($max_length_x * 4.8); | ||
| 616 | #print STDERR "mod_plot_area_height = [$mod_plot_area_height]\n"; | ||
| 617 | |||
| 618 | my $plot_area = $c->setPlotArea($plot_area_x, $plot_area_y, | ||
| 619 | $plot_area_width, $mod_plot_area_height); | ||
| 620 | |||
| 621 | $plot_area->setBackground(hex($spec->{plot_bgcolor}), hex($spec->{plot_bgcolor})); | ||
| 622 | } | ||
| 623 | } | ||
| 624 | |||
| 625 | &App::sub_exit() if ($App::trace); | ||
| 626 | } | ||
| 627 | |||
| 628 | sub write_bar_graph_image { | ||
| 629 | &App::sub_entry if ($App::trace); | ||
| 630 | my ($self, $spec) = @_; | ||
| 631 | my $c = $self->new_xy_chart($spec); | ||
| 632 | my $x = $self->get_x($spec); | ||
| 633 | my $yn = $self->get_y($spec); | ||
| 634 | my ($layer); | ||
| 635 | my $data_set_colors; | ||
| 636 | |||
| 637 | if ($spec->{module_cd} eq 'D') { | ||
| 638 | $self->modify_plot_area($spec, $x, $c); | ||
| 639 | } | ||
| 640 | |||
| 641 | if ($spec->{data_set_colors}) { | ||
| 642 | foreach my $color ( split(/,(?:\s+)?/, $spec->{data_set_colors}) ) { | ||
| 643 | $color =~ s/^#//; | ||
| 644 | push (@$data_set_colors, hex($color)); | ||
| 645 | } | ||
| 646 | } | ||
| 647 | |||
| 648 | if ($#$yn > 0) { | ||
| 649 | if ($spec->{stacked}) { | ||
| 650 | my $three_d_depth = $spec->{"3D"} ? 8 : 0; | ||
| 651 | $layer = $c->addBarLayer2($perlchartdir::Stack, $three_d_depth); | ||
| 652 | my $y_labels = $spec->{y_labels} || []; | ||
| 653 | for (my $i = 0; $i <= $#$yn; $i++) { | ||
| 654 | if ($#$data_set_colors != -1) { | ||
| 655 | $layer->addDataSet($yn->[$i], $data_set_colors->[$i], $y_labels->[$i]); | ||
| 656 | } else { | ||
| 657 | $layer->addDataSet($yn->[$i],-1, $y_labels->[$i]); | ||
| 658 | } | ||
| 659 | } | ||
| 660 | #Enable bar label for the whole bar | ||
| 661 | #$layer->setAggregateLabelStyle(); | ||
| 662 | #Enable bar label for each segment of the stacked bar | ||
| 663 | #$layer->setDataLabelStyle(); | ||
| 664 | } | ||
| 665 | else { | ||
| 666 | $layer = $c->addBarLayer2($perlchartdir::Side, $#$yn + 1); | ||
| 667 | my $y_labels = $spec->{y_labels} || []; | ||
| 668 | for (my $i = 0; $i <= $#$yn; $i++) { | ||
| 669 | if ($#$data_set_colors != -1) { | ||
| 670 | $layer->addDataSet($yn->[$i], $data_set_colors->[$i], $y_labels->[$i]); | ||
| 671 | } else { | ||
| 672 | $layer->addDataSet($yn->[$i], -1, $y_labels->[$i]); | ||
| 673 | } | ||
| 674 | } | ||
| 675 | #Enable bar label for the whole bar | ||
| 676 | #$layer->setAggregateLabelStyle()->setFontAngle(90); | ||
| 677 | if ($spec->{"3D"}) { | ||
| 678 | $layer->set3D(5,0); | ||
| 679 | $layer->setBarGap(0.2, 0.03); | ||
| 680 | } | ||
| 681 | else { | ||
| 682 | $layer->set3D(0,0); | ||
| 683 | if ($spec->{overlap}) { | ||
| 684 | $layer->setOverlapRatio(($spec->{overlap} >= 1) ? 0.3 : $spec->{overlap}) | ||
| 685 | } | ||
| 686 | else { | ||
| 687 | $layer->setBarGap(0.2, 0.03); | ||
| 688 | } | ||
| 689 | } | ||
| 690 | } | ||
| 691 | } | ||
| 692 | elsif ($#$yn > -1) { | ||
| 693 | if ($#$data_set_colors != -1) { | ||
| 694 | $layer = $c->addBarLayer($yn->[0], $data_set_colors->[0]); | ||
| 695 | } else { | ||
| 696 | $layer = $c->addBarLayer($yn->[0]); | ||
| 697 | } | ||
| 698 | $layer->set3D() if ($spec->{"3D"}); | ||
| 699 | } | ||
| 700 | |||
| 701 | $self->set_x_axis($spec, $c, $layer, $x); | ||
| 702 | |||
| 703 | $c->makeChart($spec->{image_path}); | ||
| 704 | &App::sub_exit() if ($App::trace); | ||
| 705 | } | ||
| 706 | |||
| 707 | sub set_x_axis { | ||
| 708 | my ($self, $spec, $chart, $layer, $x) = @_; | ||
| 709 | my $x_title = $spec->{x_title} || ""; | ||
| 710 | if ($x && $#$x > -1) { | ||
| 711 | my ($begin_datetime, $end_datetime, $begin_time, $end_time, $begin_yr, $end_yr); | ||
| 712 | if ($x->[0] =~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/) { | ||
| 713 | my $bar_offset = ($spec->{graphtype} =~ /bar/) ? 24*3600 : 0; | ||
| 714 | #my $bar_offset = 0; | ||
| 715 | $begin_datetime = $bar_offset ? time2str("%Y-%m-%d", str2time($x->[0]) - $bar_offset) : $x->[0]; | ||
| 716 | if ($begin_datetime =~ /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/) { | ||
| 717 | $begin_yr = $1; | ||
| 718 | $begin_time = &perlchartdir::chartTime($1, $2, $3); | ||
| 719 | } | ||
| 720 | $end_datetime = $bar_offset ? time2str("%Y-%m-%d", str2time($x->[$#$x]) + $bar_offset) : $x->[$#$x]; | ||
| 721 | if ($end_datetime =~ /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/) { | ||
| 722 | $end_yr = $1; | ||
| 723 | $end_time = &perlchartdir::chartTime($1, $2, $3); | ||
| 724 | } | ||
| 725 | $chart->xAxis()->setDateScale($begin_time, $end_time); | ||
| 726 | my (@x_date); | ||
| 727 | for (my $i = 0; $i <= $#$x; $i++) { | ||
| 728 | if ($x->[$i] =~ /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/) { | ||
| 729 | #$x_date[$i] = &perlchartdir::chartTime($1, $2, $3); | ||
| 730 | $x_date[$i] = "$2/$3"; | ||
| 731 | } | ||
| 732 | else { | ||
| 733 | $x_date[$i] = $perlchartdir::NoValue; | ||
| 734 | } | ||
| 735 | } | ||
| 736 | $x_title .= " " if ($x_title); | ||
| 737 | $x_title .= "($begin_yr"; | ||
| 738 | $x_title .= "-$end_yr" if ($end_yr ne $begin_yr); | ||
| 739 | $x_title .= ")"; | ||
| 740 | |||
| 741 | $chart->xAxis()->setLabels(\@x_date); | ||
| 742 | my $r = sprintf("%.0f", $#x_date / 15); | ||
| 743 | my $w = sprintf("%.0f", (800 / $spec->{width})); | ||
| 744 | $r += $w if ($w > 1 && $r > 0); | ||
| 745 | $r += 1 if ($#x_date >= 13 && $spec->{width} <= 600); | ||
| 746 | $chart->xAxis()->setLabelStep($r); | ||
| 747 | } | ||
| 748 | elsif ($x->[0] =~ /^[0-9]+$/) { | ||
| 749 | $layer->setXData($x); | ||
| 750 | } | ||
| 751 | else { | ||
| 752 | if ($x->[0] =~ /^[0-9]+\-[0-9]+$/) { | ||
| 753 | my %m = ("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr", | ||
| 754 | "05"=>"May","06"=>"Jun","07"=>"Jul","08"=>"Aug", | ||
| 755 | "09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec"); | ||
| 756 | |||
| 757 | for (my $i = 0; $i <= $#$x; $i++) { | ||
| 758 | if ($x->[$i] =~ /^([0-9]+)\-([0-9]+)$/){ | ||
| 759 | my $yr = $1; my $month = $2; | ||
| 760 | $yr = substr($yr, 2, 2); | ||
| 761 | $x->[$i] = "$m{$month}$yr"; | ||
| 762 | } | ||
| 763 | } | ||
| 764 | $chart->xAxis()->setLabels($x); | ||
| 765 | my $r = sprintf("%.0f", $#$x / 24); | ||
| 766 | my $w = sprintf("%.0f", (900 / $spec->{width})); | ||
| 767 | $r += $w; | ||
| 768 | $chart->xAxis()->setLabelStep($r); | ||
| 769 | } | ||
| 770 | else { | ||
| 771 | foreach my $xlabel (@$x) { | ||
| 772 | if ($xlabel =~ /^-/) { | ||
| 773 | $xlabel = '\\' . $xlabel; | ||
| 774 | } | ||
| 775 | } | ||
| 776 | |||
| 777 | $chart->xAxis()->setLabels($x); | ||
| 778 | |||
| 779 | if ($spec->{x_label_pos}) { | ||
| 780 | $chart->xAxis()->setLabels($x)->setFontAngle(45) if ( $self->{total_x_length} > $self->{width}/8 ); | ||
| 781 | $chart->xAxis()->setLabels($x)->setFontSize($self->{fontsize}, 0) if ($#$x > 30); | ||
| 782 | } | ||
| 783 | } | ||
| 784 | } | ||
| 785 | } | ||
| 786 | if ($spec->{graphtype} ne "pie" && $spec->{graphtype} ne "meter") { | ||
| 787 | my $x_titlecolor = $self->get_theme_value("x_titlecolor", "0xfffcf0"); | ||
| 788 | $chart->xAxis()->setTitle($x_title, "arial.ttf", 10, hex($x_titlecolor)) if ($x_title); | ||
| 789 | } | ||
| 790 | } | ||
| 791 | |||
| 792 | sub write_line_graph_image { | ||
| 793 | &App::sub_entry if ($App::trace); | ||
| 794 | my ($self, $spec) = @_; | ||
| 795 | my $c = $self->new_xy_chart($spec); | ||
| 796 | |||
| 797 | my $data_set_colors; | ||
| 798 | if ($spec->{data_set_colors}) { | ||
| 799 | foreach my $color ( split(/,(?:\s+)?/, $spec->{data_set_colors}) ) { | ||
| 800 | $color =~ s/^#//; | ||
| 801 | push (@$data_set_colors, hex($color)); | ||
| 802 | } | ||
| 803 | } | ||
| 804 | |||
| 805 | #Display 1 out of 3 labels on the x-axis. | ||
| 806 | # $c->xAxis()->setLabelStep(3); | ||
| 807 | |||
| 808 | #Set the labels on the x axis by spreading the labels evenly between the first | ||
| 809 | #point (index = 0) and the last point (index = noOfPoints - 1) | ||
| 810 | # $c->xAxis()->setLinearScale(0, $noOfPoints - 1, $labels); | ||
| 811 | # $c->xAxis()->setLinearScale(0, 2, $x); | ||
| 812 | |||
| 813 | ############# | ||
| 814 | my @color_set = ("0xFF0000", "0xFF00", "0xFFFD00", "0xFF00FF", "0x6E641E", "0xA5BF8", | ||
| 815 | "0x44A816", "0x8B0000", "0x6495ED", "0xFF8C00", "0xA4E8B", "0xDEB887", "0x9932CC", | ||
| 816 | "0xAFC8", "0x55FF0000", "0x30D40", "0x708090", "0xBDB76B", "0xADD8E6"); | ||
| 817 | #"0xFFC0CB", - This is no more needed - RNS 070831-000229 | ||
| 818 | ############# | ||
| 819 | |||
| 820 | my $x = $self->get_x($spec); | ||
| 821 | my $yn = $self->get_y($spec); | ||
| 822 | my ($layer, $dataset); | ||
| 823 | my @symbols = ( | ||
| 824 | { symbol => $perlchartdir::SquareSymbol, size => 7, }, | ||
| 825 | { symbol => $perlchartdir::DiamondSymbol, size => 9, }, | ||
| 826 | { symbol => $perlchartdir::CircleShape, size => 7, }, | ||
| 827 | { symbol => $perlchartdir::TriangleShape, size => 8, }, | ||
| 828 | ); | ||
| 829 | if ($spec->{area}) { | ||
| 830 | $layer = $c->addAreaLayer2($perlchartdir::Stack); | ||
| 831 | $spec->{stacked} = 0; # stacking is done for us in the area layer | ||
| 832 | } | ||
| 833 | else { | ||
| 834 | $layer = $c->addLineLayer2(); | ||
| 835 | $layer->setLineWidth(2); | ||
| 836 | } | ||
| 837 | $layer->set3D(5) if ($spec->{"3D"}); | ||
| 838 | if ($#$yn > 0) { | ||
| 839 | my ($stacked_y, $y, $dataset); | ||
| 840 | if ($spec->{stacked}) { | ||
| 841 | $stacked_y = [ ]; # make a copy | ||
| 842 | } | ||
| 843 | my $y_labels = $spec->{y_labels} || []; | ||
| 844 | for (my $i = 0; $i <= $#$yn; $i++) { | ||
| 845 | $y = $yn->[$i]; | ||
| 846 | if ($spec->{stacked}) { | ||
| 847 | for (my $j = 0; $j <= $#$y; $j++) { | ||
| 848 | $stacked_y->[$j] += $y->[$j]; | ||
| 849 | } | ||
| 850 | $y = $stacked_y; | ||
| 851 | } | ||
| 852 | #$dataset = $layer->addDataSet($y, -1, $y_labels->[$i]); | ||
| 853 | ################ | ||
| 854 | #we choose colors from @color_set | ||
| 855 | if($i <= 18) { # RNS 070831-000229 | ||
| 856 | if ($#$data_set_colors != -1) { | ||
| 857 | $dataset = $layer->addDataSet($y, $data_set_colors->[$i], $y_labels->[$i]); | ||
| 858 | } else { | ||
| 859 | $dataset = $layer->addDataSet($y, hex($color_set[$i]), $y_labels->[$i]); | ||
| 860 | } | ||
| 861 | } | ||
| 862 | else { | ||
| 863 | #if number of y_labels is greater than 20, colors will be repeated | ||
| 864 | #we have declared only 20 colors | ||
| 865 | $dataset = $layer->addDataSet($y, hex($color_set[($i-19)]), $y_labels->[$i]); # RNS 070831-000229 | ||
| 866 | } | ||
| 867 | ############### | ||
| 868 | $dataset->setDataSymbol($self->sym($i, \@symbols)) if ($spec->{points}); | ||
| 869 | } | ||
| 870 | } | ||
| 871 | elsif ($#$yn > -1) { | ||
| 872 | $layer = $c->addLineLayer($yn->[0]); | ||
| 873 | $layer->setLineWidth(2); | ||
| 874 | } | ||
| 875 | if ($spec->{point_labels}) { | ||
| 876 | my $label_format = $spec->{point_labels}; | ||
| 877 | $label_format = "{value|0}" if ($label_format eq "1"); | ||
| 878 | $layer->setDataLabelFormat($label_format); | ||
| 879 | } | ||
| 880 | |||
| 881 | $self->set_x_axis($spec, $c, $layer, $x); | ||
| 882 | |||
| 883 | $c->makeChart($spec->{image_path}); | ||
| 884 | &App::sub_exit() if ($App::trace); | ||
| 885 | } | ||
| 886 | |||
| 887 | sub sym { | ||
| 888 | &App::sub_entry if ($App::trace); | ||
| 889 | my ($self, $series, $symbols) = @_; | ||
| 890 | my $idx = $series % ($#$symbols + 1); | ||
| 891 | my $symboldef = $symbols->[$idx]; | ||
| 892 | &App::sub_exit($symboldef->{symbol}, $symboldef->{size}) if ($App::trace); | ||
| 893 | return($symboldef->{symbol}, $symboldef->{size}); | ||
| 894 | } | ||
| 895 | |||
| 896 | ##Add a legend box at (400, 100) | ||
| 897 | #$c->addLegend(400, 100); | ||
| 898 | ##Add a stacked bar layer and set the layer 3D depth to 8 pixels | ||
| 899 | #my $layer = $c->addBarLayer2($perlchartdir::Stack, 8); | ||
| 900 | ##Add the three data sets to the bar layer | ||
| 901 | #$layer->addDataSet($data0, 0xff8080, "Server # 1"); | ||
| 902 | #$layer->addDataSet($data1, 0x80ff80, "Server # 2"); | ||
| 903 | #$layer->addDataSet($data2, 0x8080ff, "Server # 3"); | ||
| 904 | |||
| 905 | # TODO: this needs more work before it really works | ||
| 906 | sub write_meter_graph_image { | ||
| 907 | &App::sub_entry if ($App::trace); | ||
| 908 | my ($self, $spec) = @_; | ||
| 909 | my $c = $self->new_meter_chart($spec); | ||
| 910 | my $x = $self->get_x($spec); | ||
| 911 | my $yn = $self->get_y($spec) || [[]]; | ||
| 912 | |||
| 913 | my $value = $yn->[0][0]; | ||
| 914 | |||
| 915 | my $radius = $spec->{radius}; | ||
| 916 | my $center_x = $spec->{center_x}; | ||
| 917 | my $center_y = $spec->{center_y}; | ||
| 918 | |||
| 919 | #my $y_max = $spec->{y_max} || 100; | ||
| 920 | my $y_max = $value || 100; | ||
| 921 | |||
| 922 | my ($major_tick, $minor_tick, $micro_tick); | ||
| 923 | { | ||
| 924 | my $y_mantissa = $y_max; | ||
| 925 | my $y_scale = 1; | ||
| 926 | while ($y_mantissa > 1.0) { | ||
| 927 | $y_mantissa /= 10; | ||
| 928 | $y_scale *= 10; | ||
| 929 | } | ||
| 930 | if ($y_mantissa > 0.5) { | ||
| 931 | $y_max = $y_scale; | ||
| 932 | $minor_tick = $y_max/10; | ||
| 933 | $micro_tick = $y_max/20; | ||
| 934 | } | ||
| 935 | elsif ($y_mantissa > 0.2) { | ||
| 936 | $y_max = 0.5 * $y_scale; | ||
| 937 | $minor_tick = $y_max/25; | ||
| 938 | $micro_tick = undef; | ||
| 939 | } | ||
| 940 | else { | ||
| 941 | $y_max = 0.2 * $y_scale; | ||
| 942 | $minor_tick = $y_max/10; | ||
| 943 | $micro_tick = $y_max/20; | ||
| 944 | } | ||
| 945 | $major_tick = $y_max/5; | ||
| 946 | } | ||
| 947 | |||
| 948 | my $y_red = $spec->{y_red} || ($y_max * 0.80); | ||
| 949 | my $y_yellow = $spec->{y_yellow} || ($y_max * 0.60); | ||
| 950 | |||
| 951 | #Meter scale is 0 - 100, with major tick every 20 units, minor tick every 10 | ||
| 952 | #units, and micro tick every 5 units | ||
| 953 | $c->setScale(0, $y_max, $major_tick, $minor_tick, $micro_tick); | ||
| 954 | |||
| 955 | #Set 0 - 60 as green (66FF66) zone | ||
| 956 | $c->addZone(0, $y_yellow, 0, $radius, 0x66ff66); | ||
| 957 | |||
| 958 | #Set 60 - 80 as yellow (FFFF33) zone | ||
| 959 | $c->addZone($y_yellow, $y_red, 0, $radius, 0xffff33); | ||
| 960 | |||
| 961 | #Set 80 - 100 as red (FF6666) zone | ||
| 962 | $c->addZone($y_red, $y_max, 0, $radius, 0xff6666); | ||
| 963 | |||
| 964 | #Add a text label centered at (100, 60) with 12 pts Arial Bold font | ||
| 965 | if ($spec->{y_title}) { | ||
| 966 | $c->addText($center_x, $center_y-int($radius * 0.35), $spec->{y_title}, | ||
| 967 | "arialbd.ttf", 11, $perlchartdir::TextColor, $perlchartdir::Center); | ||
| 968 | } | ||
| 969 | |||
| 970 | my $x_title = ""; | ||
| 971 | $x_title = $spec->{x_title} if ($spec->{x_title}); | ||
| 972 | if ($x) { | ||
| 973 | $x_title .= ": " if ($x_title); | ||
| 974 | $x_title .= $x->[0]; | ||
| 975 | } | ||
| 976 | if ($spec->{y_labels}) { | ||
| 977 | if ($x_title) { | ||
| 978 | $x_title .= " ($spec->{y_labels}[0])"; | ||
| 979 | } | ||
| 980 | else { | ||
| 981 | $x_title = $spec->{y_labels}[0]; | ||
| 982 | } | ||
| 983 | } | ||
| 984 | if ($x_title) { | ||
| 985 | $c->addText($center_x, $center_y+18, $x_title, | ||
| 986 | "arialbd.ttf", 10, $perlchartdir::TextColor, $perlchartdir::Center); | ||
| 987 | } | ||
| 988 | |||
| 989 | #Add a text box at the top right corner of the meter showing the value formatted | ||
| 990 | #to 2 decimal places, using white text on a black background, and with 1 pixel | ||
| 991 | #3D depressed border | ||
| 992 | $c->addText($center_x + int($radius * 0.7), $center_y - int($radius * 1.0), | ||
| 993 | $c->formatValue($value, "2"), | ||
| 994 | "arial.ttf", 8, 0xffffff)->setBackground(0x0, 0, -1); | ||
| 995 | |||
| 996 | #Add a semi-transparent blue (40666699) pointer with black border at the | ||
| 997 | #specified value | ||
| 998 | $value = $y_max if ($value > $y_max); | ||
| 999 | $value = 0 if ($value < 0); | ||
| 1000 | $c->addPointer($value, 0x40666699, 0x0); | ||
| 1001 | $c->makeChart($spec->{image_path}); | ||
| 1002 | &App::sub_exit() if ($App::trace); | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | sub write_pie_graph_image { | ||
| 1006 | &App::sub_entry if ($App::trace); | ||
| 1007 | my ($self, $spec) = @_; | ||
| 1008 | |||
| 1009 | my $c = $self->new_pie_chart($spec); | ||
| 1010 | my $x = $self->get_x($spec); | ||
| 1011 | my $yn = $self->get_y($spec); | ||
| 1012 | |||
| 1013 | my $data; | ||
| 1014 | foreach my $y (@$yn) { push (@$data, @{$y}); } | ||
| 1015 | |||
| 1016 | if ($#$x > 0) { | ||
| 1017 | $c->setData($data, $x); | ||
| 1018 | } | ||
| 1019 | else { | ||
| 1020 | $c->setData($data, $spec->{y_labels}); | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | # The depths for the sectors | ||
| 1024 | #my $depths = [30, 20, 10, 10]; | ||
| 1025 | #$c->set3D2($depths); | ||
| 1026 | #$c->setStartAngle(225); | ||
| 1027 | |||
| 1028 | $c->set3D() if ($spec->{"3D"}); | ||
| 1029 | $c->makeChart($spec->{image_path}); | ||
| 1030 | &App::sub_exit() if ($App::trace); | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | #my $data = [25, 18, 15, 12, 8, 30, 35]; | ||
| 1034 | ##The labels for the pie chart | ||
| 1035 | #my $labels = ["Labor", "Licenses", "Taxes", "Legal", "Insurance", "Facilities", | ||
| 1036 | # "Production"]; | ||
| 1037 | ##Create a PieChart object of size 360 x 300 pixels | ||
| 1038 | #my $c = new PieChart(360, 300); | ||
| 1039 | ##Set the center of the pie at (180, 140) and the radius to 100 pixels | ||
| 1040 | #$c->setPieSize(180, 140, 100); | ||
| 1041 | ##Add a title to the pie chart | ||
| 1042 | #$c->addTitle("Project Cost Breakdown"); | ||
| 1043 | ##Draw the pie in 3D | ||
| 1044 | #$c->set3D(); | ||
| 1045 | ##Set the pie data and the pie labels | ||
| 1046 | #$c->setData($data, $labels); | ||
| 1047 | |||
| 1048 | # TODO: This one doesn't work yet | ||
| 1049 | sub write_step_graph_image_step { | ||
| 1050 | &App::sub_entry if ($App::trace); | ||
| 1051 | my ($self, $spec) = @_; | ||
| 1052 | require "perlchartdir.pm"; | ||
| 1053 | #Create a XYChart object of size 500 x 270 pixels, with a pale blue (0xe0e0ff) | ||
| 1054 | #background, a light blue (0xccccff) border, and 1 pixel 3D border effect. | ||
| 1055 | my $c = new XYChart(600, 350, 0xe0e0ff, 0xccccff, 1); #800 | ||
| 1056 | #Set the plotarea at (50, 50) and of size 420 x 180 pixels, using white | ||
| 1057 | #(0xffffff) as the plot area background color. Turn on both horizontal and | ||
| 1058 | #vertical grid lines with light grey color (0xc0c0c0) | ||
| 1059 | |||
| 1060 | $c->setPlotArea(50, 50, 320, 260, 0xffffff)->setGridColor(0xc0c0c0, 0xc0c0c0); #720 | ||
| 1061 | |||
| 1062 | #Add a legend box at (55, 25) (top of the chart) with horizontal layout. Use 10 | ||
| 1063 | #pts Arial Bold Italic font. Set the background and border color to Transparent. | ||
| 1064 | $c->addLegend(55, 20, 0, "arialbi.ttf", 10)->setBackground($perlchartdir::Transparent); | ||
| 1065 | |||
| 1066 | #Add a title to the chart using 14 points Times Bold Itatic font, using blue | ||
| 1067 | #(0x9999ff) as the background color | ||
| 1068 | $c->addTitle("Rate History", "arialbi.ttf", 12)->setBackground(0x9999ff); | ||
| 1069 | |||
| 1070 | #Set the y axis label format to display a percentage sign | ||
| 1071 | #$c->yAxis()->setLabelFormat("{value}%"); | ||
| 1072 | |||
| 1073 | my $labels = $spec->{labels} || [ "Unknown" ]; | ||
| 1074 | my $default_colors = | ||
| 1075 | [ 0x0000ff, 0x00ff00, 0xff0000, 0x00ffff, 0xff00ff, 0xffff00, | ||
| 1076 | 0x111199, 0x119911, 0x991111, 0x119999, 0x991199, 0x999911, | ||
| 1077 | 0x3333dd, 0x33dd33, 0xdd3333, 0x33dddd, 0xdd33dd, 0xdddd33, | ||
| 1078 | 0x2222bb, 0x22bb22, 0xbb2222, 0x22bbbb, 0xbb22bb, 0xbbbb22 ]; | ||
| 1079 | my $colors = $spec->{colors} || $default_colors; | ||
| 1080 | |||
| 1081 | my ($label, $color, $step_xaxis, $xaxis, $yaxis, $layer); | ||
| 1082 | for (my $i = 0; $i <= $#$labels; $i++) { | ||
| 1083 | $label = $labels->[$i]; | ||
| 1084 | $color = $colors->[$i] || 0; | ||
| 1085 | if ($color =~ /^0[xX][0-9A-Fa-f]+$/) { | ||
| 1086 | $color = eval $color; | ||
| 1087 | } | ||
| 1088 | $xaxis = $spec->{"x$i"} || [ $i, $i+1, $i+2, $i+3 ]; | ||
| 1089 | $yaxis = $spec->{"y$i"} || [ $i, $i+1, $i+2, $i+3 ]; | ||
| 1090 | |||
| 1091 | # set the xAxis scale | ||
| 1092 | #$c->xAxis()->setLinearScale($xaxis->[0] - 1, $xaxis->[$#$xaxis], 1, 0); | ||
| 1093 | $c->xAxis()->setAutoScale(0,0,1); | ||
| 1094 | $c->yAxis()->setAutoScale(0,0,1); | ||
| 1095 | $c->xAxis()->setIndent(1); | ||
| 1096 | |||
| 1097 | # we decrement the $step_xaxis values by a day to account for the fact that | ||
| 1098 | # the step function runs from left to right but that the spec-> occurred | ||
| 1099 | # from right to left. | ||
| 1100 | $step_xaxis = [ @$xaxis ]; # make a copy | ||
| 1101 | for (my $x = 0; $x <= $#$step_xaxis; $x++) { | ||
| 1102 | $step_xaxis->[$x]--; | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | #Add a step line layer to the chart and set the line width to 2 pixels | ||
| 1106 | $layer = $c->addStepLineLayer($yaxis, $color, $label); | ||
| 1107 | #$layer->setXData($step_xaxis); | ||
| 1108 | $layer->setLineWidth(2); | ||
| 1109 | |||
| 1110 | # Add a line layer to the chart | ||
| 1111 | # $layer = $c->addLineLayer(); | ||
| 1112 | # Add the line. Plot the points with a 9 pixel diamond symbol | ||
| 1113 | # $layer->addDataSet($yaxis, $color)->setDataSymbol( $perlchartdir::DiamondSymbol, 9); | ||
| 1114 | # Enable data label on the data points. Set the label format to nn%. | ||
| 1115 | # $layer->setDataLabelFormat("{value}"); | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | print $c->makeChart($spec->{image_path}); | ||
| 1119 | &App::sub_exit() if ($App::trace); | ||
| 1120 | } | ||
| 1121 | |||
| 1122 | sub write_step_graph_image { | ||
| 1123 | &App::sub_entry if ($App::trace); | ||
| 1124 | my ($self, $spec) = @_; | ||
| 1125 | my $c = $self->new_xy_chart($spec); | ||
| 1126 | my $x = $self->get_x($spec); | ||
| 1127 | my $yn = $self->get_y($spec); | ||
| 1128 | my ($layer, $dataset); | ||
| 1129 | my @symbols = ( | ||
| 1130 | { symbol => $perlchartdir::SquareSymbol, size => 7, }, | ||
| 1131 | { symbol => $perlchartdir::DiamondSymbol, size => 9, }, | ||
| 1132 | { symbol => $perlchartdir::CircleShape, size => 7, }, | ||
| 1133 | { symbol => $perlchartdir::TriangleShape, size => 8, }, | ||
| 1134 | ); | ||
| 1135 | $layer = $c->addStepLineLayer(); | ||
| 1136 | $layer->setLineWidth(2); | ||
| 1137 | |||
| 1138 | if ($#$yn > 0) { | ||
| 1139 | my ($stacked_y, $y, $dataset); | ||
| 1140 | my $y_labels = $spec->{y_labels} || []; | ||
| 1141 | for (my $i = 0; $i <= $#$yn; $i++) { | ||
| 1142 | $y = $yn->[$i]; | ||
| 1143 | |||
| 1144 | $dataset = $layer->addDataSet($y, -1, $y_labels->[$i]); | ||
| 1145 | $dataset->setDataSymbol($self->sym($i, \@symbols)) if ($spec->{points}); | ||
| 1146 | } | ||
| 1147 | } | ||
| 1148 | elsif ($#$yn > -1) { | ||
| 1149 | $layer = $c->addStepLineLayer($yn->[0]); | ||
| 1150 | $layer->setLineWidth(5); | ||
| 1151 | } | ||
| 1152 | if ($spec->{point_labels}) { | ||
| 1153 | my $label_format = $spec->{point_labels}; | ||
| 1154 | $label_format = "{value|0}" if ($label_format eq "1"); | ||
| 1155 | $layer->setDataLabelFormat($label_format); | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | $self->set_x_axis($spec, $c, $layer, $x); | ||
| 1159 | |||
| 1160 | $c->makeChart($spec->{image_path}); | ||
| 1161 | &App::sub_exit() if ($App::trace); | ||
| 1162 | } | ||
| 1163 | |||
| 1164 | 1; | ||
| 1165 |