Author: spadkins
Date: Fri May 18 08:53:26 2007
New Revision: 9575
Modified:
p5ee/trunk/App-Widget-ChartDirector/lib/App/Widget/ChartDirector.pm
Log:
add {legend_pos} options [default,top,bottom]
Modified: p5ee/trunk/App-Widget-ChartDirector/lib/App/Widget/ChartDirector.pm
==============================================================================
--- p5ee/trunk/App-Widget-ChartDirector/lib/App/Widget/ChartDirector.pm
(original)
+++ p5ee/trunk/App-Widget-ChartDirector/lib/App/Widget/ChartDirector.pm Fri May
18 08:53:26 2007
@@ -6,12 +6,12 @@
package App::Widget::ChartDirector;
$VERSION = (q$Revision$ =~ /(\d[\d\.]*)/)[0]; # VERSION numbers generated by
svn
-use App;
+use App;
use App::Widget::Graph;
@ISA = ( "App::Widget::Graph" );
use Date::Format;
-use Date::Parse;
+use Date::Parse;
#use Data::Dumper;
use strict;
@@ -51,7 +51,6 @@
my $self = shift;
my $name = $self->{name};
my $spec = $self->create_graph_spec();
-
my ($html);
if ($self->{defer_images}) { # write out the graph spec. produce graph
image later.
my $spec_path = $spec->{spec_path};
@@ -233,7 +232,6 @@
&App::sub_entry if ($App::trace);
my ($self, $spec) = @_;
require "perlchartdir.pm";
-
my $x = $self->get_x($spec); #
print STDERR Dumper $x;
my $width = $spec->{width} || 250;
my $height = $spec->{height} || 250;
@@ -270,12 +268,64 @@
$left_margin = 20 + $y_label_len * 6;
$left_margin += 20 if ($spec->{y_title});
}
+
+ my $legend_pos = $spec->{legend_pos} || "default"; # Any one from default,
top or bottom
+ my ($count, $count_uc, @length_y_value, $max_legend_length,
$num_legend_columns, $legend_width, $margin, $textsize, $number_legend_row );
+
+ if ($legend_pos eq "top" || $legend_pos eq "bottom") {
+ $count = 0;
+ $count_uc = 0;
+ $max_legend_length = 75; # This is a nearyby approximation of
number of characters inside a row in the legends
+ $num_legend_columns = $spec->{num_legend_columns} || 2;
+ $legend_width = $width - 2 * $right_margin;
+ ($margin,$textsize) = (6,10);
+
+ foreach my $y_value (@{$spec->{y_labels}}) {
+ push( @length_y_value, length($y_value) );
+ $count++;
+ $count_uc++ if ($y_value !~ /[a-z]/);
+ }
+ @length_y_value = sort { $a <=> $b } @length_y_value;
+ $max_legend_length = 40 if ($count == $count_uc); # This is a nearyby
approximation of number of characters inside a row in the legends
+
+ for (my $cols = 2;$cols <= 8; $cols++) { # Taking min 2 and max. 8
colums
+ $num_legend_columns = $cols if ($length_y_value[-1] <
$max_legend_length / $cols);
+ }
+
+ $num_legend_columns = 2 if ($#{$spec->{y_labels}} < 2);
+ $num_legend_columns = 3 if ($length_y_value[-1] > $max_legend_length
/ 2 && $#{$spec->{y_labels}} > 20);
+
+ $number_legend_row = int ($count / $num_legend_columns);
+ if ($count % $num_legend_columns != 0 ) {
+ $number_legend_row += 1;
+ }
- my $c = new XYChart($width, $height);
+ $height += (($margin + $textsize) * $number_legend_row) + (4 *
$margin);
- my $plot_area = $c->setPlotArea($left_margin, $top_margin,
+ if ($#{$spec->{y_labels}} <= 0) {
+ $height = $spec->{height};
+ }
+ }
+ my $c = new XYChart($width, $height);
+
+ my $graph_adjusted_height = $height - $spec->{height};
+ my ($top_margin_adjusted,$bottom_margin_adjusted);
+
+ if ($legend_pos eq "top" ) {
+ $top_margin_adjusted = $top_margin+$graph_adjusted_height;
+ $bottom_margin_adjusted =
$height-$top_margin-$bottom_margin-$graph_adjusted_height;
+ }
+ elsif ($legend_pos eq "bottom") {
+ $top_margin_adjusted = $top_margin;
+ $bottom_margin_adjusted =
$height-$top_margin-$bottom_margin-$graph_adjusted_height;
+ }
+ else {
+ $top_margin_adjusted = $top_margin;
+ $bottom_margin_adjusted = $height-$top_margin-$bottom_margin;
+ }
+ my $plot_area = $c->setPlotArea($left_margin , $top_margin_adjusted,
$width-$left_margin-$right_margin,
- $height-$top_margin-$bottom_margin);
+ $bottom_margin_adjusted);
$plot_area->setBackground(hex($plot_bgcolor), hex($plot_bgcolor));
@@ -283,6 +333,7 @@
#Add a legend box at (55, 22) using horizontal layout, with transparent
#background
+ my $legend;
if ($spec->{y_labels}) {
my $x_adj = 0;
my $y_adj = -2;
@@ -290,11 +341,33 @@
$x_adj += 5;
$y_adj += -5;
}
- my $legend = $c->addLegend($left_margin+$x_adj, $top_margin+$y_adj, 0);
- $legend->setBackground($perlchartdir::Transparent);
- $legend->setMargin(5);
+
+ if (($#{$spec->{y_labels}} > 0) && ($legend_pos eq "top" ||
$legend_pos eq "bottom")) {
+ if ($legend_pos eq "top" ) {
+ $legend = $c->addLegend($left_margin/4 + $x_adj, $top_margin +
$y_adj, 0,"arial.ttf",10);
+ }
+ elsif ($legend_pos eq "bottom" ) {
+ $legend = $c->addLegend($left_margin/4 + $x_adj, $height -
$graph_adjusted_height - $top_margin/4 , 0,"arial.ttf",10);
+ }
+ $legend->setBackground(hex($plot_bgcolor));
+ $legend->setMargin(5);
+ $legend->setWidth($legend_width);
+ $legend->setCols($num_legend_columns);
+ $legend->setTruncate("",1);
+ }
+ else {
+ $legend = $c->addLegend($left_margin+$x_adj, $top_margin+$y_adj,
0);
+ $legend->setBackground($perlchartdir::Transparent);
+ $legend->setMargin(5);
+ }
}
-
+
+ # To handle the situation when the graph wrapping happens.
+ if ($legend_pos eq "top" || $legend_pos eq "bottom") {
+ my $graph_count = $spec->{graph_count} || 1;
+ $spec->{height} = $height if ($graph_count == 1);
+ }
+
$c->yAxis()->setTitle($spec->{y_title}, "arial.ttf", 10,
hex($y_titlecolor)) if ($spec->{y_title});
$c->setBackground(hex($bgcolor));
$c->yAxis()->setLabelStyle("arial.ttf", 10, hex($y_labelcolor));