App-Widget-ChartDirector
view release on metacpan or search on metacpan
#########################################
# CHANGE LOG
#########################################
VERSION 0.966
x Fixed: The x-axis does not show -1
x Fixed: the graph in the report section overlaps the legend since the max value is zero
VERSION 0.60
x Now have the ability to use themes, get_theme_value()
x use Makefile.PL instead of Build.PL
x added step graph
x per-module VERSION (svn)
VERSION 0.50
x Initial release
lib/App/Widget/ChartDirector.pm view on Meta::CPAN
$total_x_length += length($_) + 2; #Taking 2 as the gap value
push (@x_values, length($_));
}
@x_values = sort { $a <=> $b} @x_values;
$self->{total_x_length} = $total_x_length;
$self->{width} = $width;
if ($total_x_length > $width/8 && $x->[0] !~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/) {
$width += ($#$x) * ceil (3 * $#$x / 100 ) + 0.5 if ($#$x > 50);
}
}
my $legend_pos = $spec->{legend_pos} || "default"; # Any one from default, top or bottom
my (@length_y_value, $max_legend_length, $num_legend_columns, $legend_width, $margin, $textsize, $number_legend_rows, $length_labels, $length_labels_avg);
my (@length_all_labels, $diff_largest_and_avg, $gap_legends);
if ($legend_pos ne "default") {
$legend_width = $width - 2 * $right_margin;
($margin,$textsize) = (6,10);
$max_legend_length = ceil ( $legend_width / ($textsize * 3/4)); # This is a nearyby approximation of number of characters inside a row in the legends
$length_labels = 0;
foreach my $y_value (@{$spec->{y_labels}}) {
$length_labels += length($y_value);
push (@length_all_labels, length($y_value) );
}
@length_all_labels = sort { $a <=> $b } @length_all_labels;
$length_labels_avg = ceil ($length_labels / ($#{$spec->{y_labels}} + 1));
$diff_largest_and_avg = $length_all_labels[-1] - $length_labels_avg;
# Calculation of gap b/w the legends
$gap_legends = $margin;
if($diff_largest_and_avg > $gap_legends) {
$gap_legends += ceil ($diff_largest_and_avg / 2);
}
else {
$gap_legends += 1;
$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.
}
# Calculation of number of columns
for (my $cols = 1; $cols <= 20; $cols++) { # Taking min. 1 and max. 20 columns
$num_legend_columns = $cols if (($length_labels_avg + $gap_legends) < ($max_legend_length / $cols));
}
$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
if ($spec->{num_legend_columns} ) {
$num_legend_columns = $spec->{num_legend_columns};
}
do {
$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
$height = $spec->{height};
$number_legend_rows = int (($#{$spec->{y_labels}} + 1) / $num_legend_columns);
if (($#{$spec->{y_labels}} + 1) % $num_legend_columns != 0 ) {
$number_legend_rows += 1;
}
$height += (($margin + $textsize) * $number_legend_rows) + (4 * $margin);
} while ($height > (2 * $spec->{height})); # Max. graph height should not be greater than twice of chosen graph height
if ($#{$spec->{y_labels}} <= 0) {
$height = $spec->{height};
}
}
my $graph_height_increment = 0;
my $fontsize = 10;
lib/App/Widget/ChartDirector.pm view on Meta::CPAN
$graph_height_increment = 1/2 * $x_values[-1] * floor ($fontsize);
$height += $graph_height_increment;
}
}
my $c = new XYChart($width, $height);
my $graph_adjusted_height = $height - $spec->{height}-$graph_height_increment;
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-$graph_height_increment;
}
elsif ($legend_pos eq "bottom") {
$top_margin_adjusted = $top_margin;
$bottom_margin_adjusted = $height-$top_margin-$bottom_margin-$graph_adjusted_height-$graph_height_increment;
}
else {
$top_margin_adjusted = $top_margin;
$bottom_margin_adjusted = $height-$top_margin-$bottom_margin-$graph_height_increment;
}
my $plot_area = $c->setPlotArea($left_margin , $top_margin_adjusted,
$width-$left_margin-$right_margin,
$bottom_margin_adjusted);
lib/App/Widget/ChartDirector.pm view on Meta::CPAN
$spec->{plot_bgcolor} = $plot_bgcolor;
$plot_area->setBackground(hex($plot_bgcolor), hex($plot_bgcolor));
$c->addTitle($spec->{title}, "arialbd.ttf", 12, hex($titlecolor)) if ($spec->{title});
if ($y_max_zero == 0) {
$c->yAxis()->setMargin($top_margin_adjusted + 15);
}
#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;
if ($spec->{"3D"}) {
$x_adj += 5;
$y_adj += -5;
}
if (($#{$spec->{y_labels}} > 0) && ($legend_pos eq "top" || $legend_pos eq "bottom")) {
if ($legend_pos eq "top" ) {
$legend = $c->addLegend($right_margin, $top_margin + $y_adj, 0,"arial.ttf",10);
}
elsif ($legend_pos eq "bottom" ) {
$legend = $c->addLegend($right_margin, $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($legend_width,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);
}
if ($spec->{x_label_pos} && $total_x_length > $width/8 && $x->[0] !~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/) {
$spec->{height} = $height;
$spec->{width} = $width;
}
$c->yAxis()->setTitle($spec->{y_title}, "arial.ttf", 10, hex($y_titlecolor)) if ($spec->{y_title});
lib/App/Widget/ChartDirector.pm view on Meta::CPAN
sub sym {
&App::sub_entry if ($App::trace);
my ($self, $series, $symbols) = @_;
my $idx = $series % ($#$symbols + 1);
my $symboldef = $symbols->[$idx];
&App::sub_exit($symboldef->{symbol}, $symboldef->{size}) if ($App::trace);
return($symboldef->{symbol}, $symboldef->{size});
}
##Add a legend box at (400, 100)
#$c->addLegend(400, 100);
##Add a stacked bar layer and set the layer 3D depth to 8 pixels
#my $layer = $c->addBarLayer2($perlchartdir::Stack, 8);
##Add the three data sets to the bar layer
#$layer->addDataSet($data0, 0xff8080, "Server # 1");
#$layer->addDataSet($data1, 0x80ff80, "Server # 2");
#$layer->addDataSet($data2, 0x8080ff, "Server # 3");
# TODO: this needs more work before it really works
sub write_meter_graph_image {
lib/App/Widget/ChartDirector.pm view on Meta::CPAN
require "perlchartdir.pm";
#Create a XYChart object of size 500 x 270 pixels, with a pale blue (0xe0e0ff)
#background, a light blue (0xccccff) border, and 1 pixel 3D border effect.
my $c = new XYChart(600, 350, 0xe0e0ff, 0xccccff, 1); #800
#Set the plotarea at (50, 50) and of size 420 x 180 pixels, using white
#(0xffffff) as the plot area background color. Turn on both horizontal and
#vertical grid lines with light grey color (0xc0c0c0)
$c->setPlotArea(50, 50, 320, 260, 0xffffff)->setGridColor(0xc0c0c0, 0xc0c0c0); #720
#Add a legend box at (55, 25) (top of the chart) with horizontal layout. Use 10
#pts Arial Bold Italic font. Set the background and border color to Transparent.
$c->addLegend(55, 20, 0, "arialbi.ttf", 10)->setBackground($perlchartdir::Transparent);
#Add a title to the chart using 14 points Times Bold Itatic font, using blue
#(0x9999ff) as the background color
$c->addTitle("Rate History", "arialbi.ttf", 12)->setBackground(0x9999ff);
#Set the y axis label format to display a percentage sign
#$c->yAxis()->setLabelFormat("{value}%");
( run in 1.211 second using v1.01-cache-2.11-cpan-49f99fa48dc )