From 0c2d2199d5aac0870e4c15f51ae184649326a5fa Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Mon, 2 Aug 2010 09:50:04 +0000 Subject: [PATCH] feature control over drawing line and area line markers --- Changes | 19 -------------- Graph.pm | 18 ++++++------- lib/Imager/Graph/Horizontal.pm | 32 ++++++++++++++++++++--- lib/Imager/Graph/Vertical.pm | 47 +++++++++++++++++++++++++++++----- t/t12column.t | 32 +++++++++++++++++------ t/t13stacked_col.t | 35 +++++++++++++++++++------ 6 files changed, 128 insertions(+), 55 deletions(-) diff --git a/Changes b/Changes index b30ab55..e6a2dad 100644 --- a/Changes +++ b/Changes @@ -32,7 +32,6 @@ Other changes: - for vertical and horizontal charts, allow the fill of the graph area to be controlled separately from the base bg (graph.fill) - - tests Bug fixes: @@ -50,24 +49,6 @@ Bug fixes: add_bar_data_series() and now adds the series as a "bar" series. add_column_data_series() did nothing useful for a horizontal chart. -TODO: - - - control of drawing markers for: - - - area - - - line - - - control of x/yaxis separate from graph area outline - - - test line markers - - - document: - - - features: horizontal_gridlines, graph_outline, graph_fill - - - graph outline and gridline styles - Imager-Graph 0.07 - 21 May 2009 ================= diff --git a/Graph.pm b/Graph.pm index 34506ee..5694cef 100644 --- a/Graph.pm +++ b/Graph.pm @@ -1266,6 +1266,15 @@ my %style_defs = aa => 'lookup(aa)', }, lineaa => 'lookup(aa)', + + line_markers =>[ + { shape => 'circle', radius => 4 }, + { shape => 'square', radius => 4 }, + { shape => 'diamond', radius => 4 }, + { shape => 'triangle', radius => 4 }, + { shape => 'x', radius => 4 }, + { shape => 'plus', radius => 4 }, + ], ); =item _error($message) @@ -1416,14 +1425,6 @@ my %styles = colors => [ qw(FF0000 00FF00 0000FF C0C000 00C0C0 FF00FF) ], - line_markers =>[ - { shape => 'circle', radius => 4 }, - { shape => 'square', radius => 4 }, - { shape => 'diamond', radius => 4 }, - { shape => 'triangle', radius => 4 }, - { shape => 'x', radius => 4 }, - { shape => 'plus', radius => 4 }, - ], back=>{ fountain=>'linear', xa_ratio=>0, ya_ratio=>0, xb_ratio=>1.0, yb_ratio=>1.0, @@ -1562,7 +1563,6 @@ $styles{'ocean_flat'} = { }; - =item $self->_style_setup(\%opts) Uses the values from %opts, the custom style set by methods, the style diff --git a/lib/Imager/Graph/Horizontal.pm b/lib/Imager/Graph/Horizontal.pm index 4466c68..954cf9f 100644 --- a/lib/Imager/Graph/Horizontal.pm +++ b/lib/Imager/Graph/Horizontal.pm @@ -459,9 +459,11 @@ sub _draw_lines { my $x2 = $left + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_width; - push @marker_positions, [$x2, $y2]; - foreach my $position (@marker_positions) { - $self->_draw_line_marker($position->[0], $position->[1], $series_counter); + if ($self->_feature_enabled("linemarkers")) { + push @marker_positions, [$x2, $y2]; + foreach my $position (@marker_positions) { + $self->_draw_line_marker($position->[0], $position->[1], $series_counter); + } } $series_counter++; } @@ -603,6 +605,28 @@ sub set_vertical_gridline_style { return 1; } +=item show_line_markers() + +=item show_line_markers($value) + +Feature: linemarkers. + +If $value is missing or true, draw markers on a line data series. + +Note: line markers are drawn by default. + +=cut + +sub show_line_markers { + my ($self, $value) = @_; + + @_ > 1 or $value = 1; + + $self->{custom_style}{features}{linemarkers} = $value; + + return 1; +} + =item use_automatic_axis() Automatically scale the Y axis, based on L. If Chart::Math::Axis isn't installed, this sets an error and returns undef. Returns 1 if it is installed. @@ -861,7 +885,7 @@ sub _style_defs { my ($self) = @_; my %work = %{$self->SUPER::_style_defs()}; - push @{$work{features}}, qw/graph_outline graph_fill/; + push @{$work{features}}, qw/graph_outline graph_fill linemarkers/; $work{vgrid} = { color => "lookup(fg)", diff --git a/lib/Imager/Graph/Vertical.pm b/lib/Imager/Graph/Vertical.pm index b91dd4b..4a92f58 100644 --- a/lib/Imager/Graph/Vertical.pm +++ b/lib/Imager/Graph/Vertical.pm @@ -38,6 +38,7 @@ use strict; use vars qw(@ISA); use Imager::Graph; @ISA = qw(Imager::Graph); +use Imager::Fill; use constant STARTING_MIN_VALUE => 99999; @@ -639,9 +640,11 @@ sub _draw_lines { my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height; - push @marker_positions, [$x2, $y2]; - foreach my $position (@marker_positions) { - $self->_draw_line_marker($position->[0], $position->[1], $series_counter); + if ($self->_feature_enabled("linemarkers")) { + push @marker_positions, [$x2, $y2]; + foreach my $position (@marker_positions) { + $self->_draw_line_marker($position->[0], $position->[1], $series_counter); + } } $series_counter++; } @@ -1057,20 +1060,50 @@ sub set_graph_fill_style { =item show_area_markers() +=item show_area_markers($value) + Feature: areamarkers. -Draw line markers along the top of area data series. +If $value is missing or true, draw markers along the top of area data +series. + +eg. + + $chart->show_area_markers(); =cut sub show_area_markers { - my ($self) = @_; + my ($self, $value) = @_; - $self->{custom_style}{features}{areamarkers} = 1; + @_ > 1 or $value = 1; + + $self->{custom_style}{features}{areamarkers} = $value; return 1; } +=item show_line_markers() + +=item show_line_markers($value) + +Feature: linemarkers. + +If $value is missing or true, draw markers on a line data series. + +Note: line markers are drawn by default. + +=cut + +sub show_line_markers { + my ($self, $value) = @_; + + @_ > 1 or $value = 1; + + $self->{custom_style}{features}{linemarkers} = $value; + + return 1; +} =item use_automatic_axis() @@ -1376,7 +1409,7 @@ sub _style_defs { { opacity => 0.5, }; - push @{$work{features}}, qw/graph_outline graph_fill/; + push @{$work{features}}, qw/graph_outline graph_fill linemarkers/; $work{hgrid} = { color => "lookup(fg)", diff --git a/t/t12column.t b/t/t12column.t index b686dcb..a9dbc4c 100644 --- a/t/t12column.t +++ b/t/t12column.t @@ -13,7 +13,7 @@ use Test::More; use Imager qw(:handy); -plan tests => 3; +plan tests => 5; my @warned; local $SIG{__WARN__} = @@ -31,16 +31,32 @@ my $font = Imager::Font::Test->new(); my @data = ( 100, 180, 80, 20, 2, 1, 0.5 ); my @labels = qw(alpha beta gamma delta epsilon phi gi); -my $column = Imager::Graph::Column->new(); -ok($column, "creating column chart object"); +{ + my $column = Imager::Graph::Column->new(); + ok($column, "creating column chart object"); -$column->add_data_series(\@data); -$column->set_labels(\@labels); + $column->add_data_series(\@data); + $column->set_labels(\@labels); -my $img1 = $column->draw(); -ok($img1, "drawing column chart"); + my $img1 = $column->draw(); + ok($img1, "drawing column chart"); -$img1->write(file=>'testout/t12_basic.ppm') or die "Can't save img1: ".$img1->errstr."\n"; + $img1->write(file=>'testout/t12_column.ppm') or die "Can't save img1: ".$img1->errstr."\n"; +} + +{ + my $column = Imager::Graph::Column->new(); + ok($column, "creating column chart object"); + + $column->add_data_series(\@data); + $column->add_data_series([ -50, -30, 20, 10, -10, 25, 10 ]); + $column->set_labels(\@labels); + + my $img1 = $column->draw(features => "outline"); + ok($img1, "drawing column chart"); + + $img1->write(file=>'testout/t12_column2.ppm') or die "Can't save img1: ".$img1->errstr."\n"; +} unless (is(@warned, 0, "should be no warnings")) { diag($_) for @warned; diff --git a/t/t13stacked_col.t b/t/t13stacked_col.t index c9cdf13..f3f6209 100644 --- a/t/t13stacked_col.t +++ b/t/t13stacked_col.t @@ -13,7 +13,7 @@ use Test::More; use Imager qw(:handy); -plan tests => 3; +plan tests => 5; my @warned; local $SIG{__WARN__} = @@ -31,17 +31,36 @@ my $font = Imager::Font::Test->new(); my @data = ( 100, 180, 80, 20, 2, 1, 0.5 ); my @labels = qw(alpha beta gamma delta epsilon phi gi); -my $stacked_col = Imager::Graph::StackedColumn->new(); -ok($stacked_col, "creating stacked_col chart object"); +{ + my $stacked_col = Imager::Graph::StackedColumn->new(); + ok($stacked_col, "creating stacked_col chart object"); -$stacked_col->add_data_series(\@data); -$stacked_col->set_labels(\@labels); + $stacked_col->add_data_series(\@data); + $stacked_col->set_labels(\@labels); -my $img1 = $stacked_col->draw(); -ok($img1, "drawing stacked_col chart"); + my $img1 = $stacked_col->draw(); + ok($img1, "drawing stacked_col chart"); -$img1->write(file=>'testout/t13_basic.ppm') or die "Can't save img1: ".$img1->errstr."\n"; + $img1->write(file=>'testout/t13_stacked.ppm') or die "Can't save img1: ".$img1->errstr."\n"; +} + +{ + my $stacked_col = Imager::Graph::StackedColumn->new(); + ok($stacked_col, "creating stacked_col chart object"); + + $stacked_col->add_data_series(\@data); + $stacked_col->add_data_series([ -50, -30, 20, 10, -10, 25, 10 ]); + $stacked_col->set_labels(\@labels); + my $img1 = $stacked_col->draw + ( + features => "outline", + column_padding => 10, + ); + ok($img1, "drawing stacked_col chart"); + + $img1->write(file=>'testout/t13_stacked2.ppm') or die "Can't save stacked2: ".$img1->errstr."\n"; +} unless (is(@warned, 0, "should be no warnings")) { diag($_) for @warned; -- 2.30.2