From 4cb01c6bb768df0fc6a0116deec9d014a8f90b2d Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Sat, 5 May 2012 14:24:35 +1000 Subject: [PATCH] [rt #59532] fix the zero position calculation for horizontal charts also allow negative_bg to be a fill for horizontal charts --- lib/Imager/Graph/Horizontal.pm | 26 +++++++++++-------- t/t14bar.t | 47 +++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/lib/Imager/Graph/Horizontal.pm b/lib/Imager/Graph/Horizontal.pm index 954cf9f..059f56f 100644 --- a/lib/Imager/Graph/Horizontal.pm +++ b/lib/Imager/Graph/Horizontal.pm @@ -167,10 +167,14 @@ sub draw { --$fill_box[3]; } - $img->box( - $self->_get_fill("graph.fill"), - box => \@fill_box, - ); + { + my @back_fill = $self->_get_fill("graph.fill", \@fill_box) + or return; + $img->box( + @back_fill, + box => \@fill_box, + ); + } my $min_value = $self->_get_min_value(); my $max_value = $self->_get_max_value(); @@ -178,17 +182,17 @@ sub draw { my $zero_position; if ($value_range) { - $zero_position = $left + $graph_width + (-1*$min_value / $value_range) * ($graph_width-1); + $zero_position = $left + (-1*$min_value / $value_range) * ($graph_width-1); } if ($min_value < 0) { + my @neg_box = ( $left+1, $top+1, $zero_position, $top+$graph_height - 1 ); + my @neg_fill = $self->_get_fill('negative_bg', \@neg_box) + or return; + $img->box( - color => $self->_get_color('negative_bg'), - xmin => $left+1, - xmax => $zero_position, - ymin => $top+1, - ymax => $top+$graph_height - 1, - filled => 1, + @neg_fill, + box => \@neg_box, ); $img->line( x1 => $zero_position, diff --git a/t/t14bar.t b/t/t14bar.t index ebf1443..f426a0f 100644 --- a/t/t14bar.t +++ b/t/t14bar.t @@ -13,7 +13,7 @@ use Test::More; use Imager qw(:handy); -plan tests => 7; +plan tests => 11; my @warned; local $SIG{__WARN__} = @@ -61,6 +61,51 @@ my @labels = qw(alpha beta gamma delta epsilon phi gi); cmpimg($img1, 'testimg/t14_bar.ppm', 80_000); } +{ + my $bar = Imager::Graph::Bar->new(); + $bar->set_font($font); + ok($bar, "creating bar chart object"); + + $bar->add_data_series([ @data, -25 ]); + $bar->set_labels([ @labels, "neg" ]); + + my $img1 = $bar->draw(); + ok($img1, "drawing bar chart (negative values)"); + + $img1->write(file=>'testout/t14_bar3.ppm') or die "Can't save img1: ".$img1->errstr."\n"; + #cmpimg($img1, 'testimg/t14_bar.ppm', 80_000); +} + +{ + my $bar = Imager::Graph::Bar->new(); + $bar->set_font($font); + ok($bar, "creating bar chart object"); + + $bar->add_data_series([ @data, -25 ]); + $bar->set_labels([ @labels, "neg" ]); + + my $fountain = Imager::Fountain->simple(colors => [ "#C0C0FF", "#E0E0FF" ], + positions => [ 0, 1 ]); + + my %fill = + ( + fountain => "linear", + segments => $fountain, + xa_ratio => 0.5, + ya_ratio => -0.1, + xb_ratio => 0.55, + yb_ratio => 1.1, + ); + + $bar->set_negative_background(\%fill); + + my $img1 = $bar->draw(); + ok($img1, "drawing bar chart (negative values, custom fill)"); + + $img1->write(file=>'testout/t14_bar4.ppm') or die "Can't save img1: ".$img1->errstr."\n"; + #cmpimg($img1, 'testimg/t14_bar.ppm', 80_000); +} + unless (is(@warned, 0, "should be no warnings")) { diag($_) for @warned; } -- 2.39.2