package Imager::Graph;
-require 5.005;
+require 5.006;
=head1 NAME
use Imager qw(:handy);
use Imager::Fountain;
-$VERSION = '0.07';
+$VERSION = '0.12';
# the maximum recursion depth in determining a color, fill or number
use constant MAX_DEPTH => 10;
=head1 FEATURES
Each graph type has a number of features. These are used to add
-various items that are displayed in the graph area. Some common
-methods are:
+various items that are displayed in the graph area.
+
+Features can be controlled by calling methods on the graph object, or
+by passing a C<features> parameter to draw().
+
+Some common features are:
=over
=item show_legend()
-adds a box containing boxes filled with the data filess, with
+Feature: legend
+X<legend><features, legend>
+
+adds a box containing boxes filled with the data fills, with
the labels provided to the draw method. The legend will only be
displayed if both the legend feature is enabled and labels are
supplied.
=item show_outline()
-draws a border around the data areas.
+Feature: outline
+X<outline>X<features, outline>
+
+If enabled, draw a border around the elements representing data in the
+graph, eg. around each pie segments on a pie chart, around each bar on
+a bar chart.
=cut
=item show_labels()
+Feature: labels
+X<labels>X<features, labels>
+
labels each data fill, usually by including text inside the data fill.
If the text does not fit in the fill, they could be displayed in some
-other form, eg. as callouts in a pie graph. There usually isn't much
-point in including both labels and a legend.
+other form, eg. as callouts in a pie graph.
+
+For pie charts there isn't much point in enabling both the C<legend>
+and C<labels> features.
+
+For other charts, the labels label the independent variable, while the
+legend describes the color used to plot the dependent variables.
=cut
=item show_drop_shadow()
+Feature: dropshadow
+X<dropshadow>X<features, dropshadow>
+
a simple drop shadow is shown behind some of the graph elements.
=cut
=item reset_features()
-Unsets all of the features
+Unsets all of the features.
+
+Note: this disables all features, even those enabled by default for a
+style. They can then be enabled by calling feature methods or by
+supplying a C<feature> parameter to the draw() method.
=cut
=back
-Additionally, features can be set by passing them into the draw() method:
+Additionally, features can be set by passing them into the draw()
+method, named as above:
=over
-=item legend
+=item *
-adds a box containing boxes filled with the data filess, with
-the labels provided to the draw method. The legend will only be
-displayed if both the legend feature is enabled and labels are
-supplied.
+if supplied as an array reference, then any element C<no>I<featurename> will
+disable that feature, while an element I<featurename> will enable it.
-=item labels
+=item *
-labels each data fill, usually by including text inside the data fill.
-If the text does not fit in the fill, they could be displayed in some
-other form, eg. as callouts in a pie graph. There usually isn't much
-point in including both labels and a legend.
+if supplied as a scalar, it is treated as if it were a reference to
+an array containing only that scalar.
-=item dropshadow
+=item *
-a simple drop shadow is shown behind some of the graph elements.
+if supplied as a hash reference, then a C<reset> key with a true value
+will avoid inheriting any default features, a key I<feature> with a
+false value will disable that feature and a key I<feature> with a true
+value will enable that feature.
=back
Used to define basic background and foreground colors for the graph.
The bg color may be used for the background of the graph, and is used
-as a default for the background of hatcheed fills. The fg is used as
+as a default for the background of hatched fills. The fg is used as
the default for line and text colors.
=item font
=item padding
-the gap between the legend patches and text and the outside of it's
+the gap between the legend patches and text and the outside of its
box, or to the legend border, if any.
=item outsidepadding
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)
blur=>undef,
},
aa => 0,
+ line_markers =>
+ [
+ { shape => "x", radius => 4 },
+ { shape => "plus", radius => 4 },
+ { shape => "open_circle", radius => 4 },
+ { shape => "open_diamond", radius => 5 },
+ { shape => "open_square", radius => 4 },
+ { shape => "open_triangle", radius => 4 },
+ { shape => "x", radius => 8 },
+ { shape => "plus", radius => 8 },
+ { shape => "open_circle", radius => 8 },
+ { shape => "open_diamond", radius => 10 },
+ { shape => "open_square", radius => 8 },
+ { shape => "open_triangle", radius => 8 },
+ ],
},
fount_lin =>
{
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,
};
-
=item $self->_style_setup(\%opts)
-Uses the values from %opts to build a customized hash describing the
-way the graph should be drawn.
+Uses the values from %opts, the custom style set by methods, the style
+set by the style parameter or the set_style() method and the built in
+chart defaults to build a working style.
+
+The working style features member is also populated with the active
+features for the chart.
+
+The working style is stored in the C<_style> member of $self.
=cut
$self->_line(x1 => $xmin+1, y1 => $ymax, x2 => $xmax-1, y2 => $ymax, %work_opts);
}
$self->_line(x1 => $xmin, y1 => $ymin, x2 => $xmin, y2 => $ymax, %work_opts);
- return $self->_line(x1 => $xmin, y1 => $ymin, x2 => $xmin, y2 => $ymax, %work_opts);
+ return $self->_line(x1 => $xmax, y1 => $ymin, x2 => $xmax, y2 => $ymax, %work_opts);
+ }
+}
+
+=item _feature_enabled($feature_name)
+
+Check if the given feature is enabled in the work style.
+
+=cut
+
+sub _feature_enabled {
+ my ($self, $name) = @_;
+
+ return $self->{_style}{features}{$name};
+}
+
+sub _line_marker {
+ my ($self, $index) = @_;
+
+ my $markers = $self->{'_style'}{'line_markers'};
+ if (!$markers) {
+ return;
+ }
+ my $marker = $markers->[$index % @$markers];
+
+ return $marker;
+}
+
+sub _draw_line_marker {
+ my $self = shift;
+ my ($x1, $y1, $series_counter) = @_;
+
+ my $img = $self->_get_image();
+
+ my $style = $self->_line_marker($series_counter);
+ return unless $style;
+
+ my $type = $style->{'shape'};
+ my $radius = $style->{'radius'};
+
+ my $line_aa = $self->_get_number("lineaa");
+ my $fill_aa = $self->_get_number("fill.aa");
+
+ if ($type eq 'circle') {
+ my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
+ $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
+ }
+ elsif ($type eq 'open_circle') {
+ my $color = $self->_data_color($series_counter);
+ $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 0, color => $color);
+ }
+ elsif ($type eq 'open_square') {
+ my $color = $self->_data_color($series_counter);
+ $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, filled => 0, color => $color);
+ }
+ elsif ($type eq 'open_triangle') {
+ my $color = $self->_data_color($series_counter);
+ $img->polyline(
+ points => [
+ [$x1 - $radius, $y1 + $radius],
+ [$x1 + $radius, $y1 + $radius],
+ [$x1, $y1 - $radius],
+ [$x1 - $radius, $y1 + $radius],
+ ],
+ color => $color, aa => $line_aa);
+ }
+ elsif ($type eq 'open_diamond') {
+ my $color = $self->_data_color($series_counter);
+ $img->polyline(
+ points => [
+ [$x1 - $radius, $y1],
+ [$x1, $y1 + $radius],
+ [$x1 + $radius, $y1],
+ [$x1, $y1 - $radius],
+ [$x1 - $radius, $y1],
+ ],
+ color => $color, aa => $line_aa);
+ }
+ elsif ($type eq 'square') {
+ my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
+ $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
+ }
+ elsif ($type eq 'diamond') {
+ # The gradient really doesn't work for diamond
+ my $color = $self->_data_color($series_counter);
+ $img->polygon(
+ points => [
+ [$x1 - $radius, $y1],
+ [$x1, $y1 + $radius],
+ [$x1 + $radius, $y1],
+ [$x1, $y1 - $radius],
+ ],
+ filled => 1, color => $color, aa => $fill_aa);
+ }
+ elsif ($type eq 'triangle') {
+ # The gradient really doesn't work for triangle
+ my $color = $self->_data_color($series_counter);
+ $img->polygon(
+ points => [
+ [$x1 - $radius, $y1 + $radius],
+ [$x1 + $radius, $y1 + $radius],
+ [$x1, $y1 - $radius],
+ ],
+ filled => 1, color => $color, aa => $fill_aa);
+
+ }
+ elsif ($type eq 'x') {
+ my $color = $self->_data_color($series_counter);
+ $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
+ $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
+ }
+ elsif ($type eq 'plus') {
+ my $color = $self->_data_color($series_counter);
+ $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
+ $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
}
}
1;
+
__END__
=back
=head1 AUTHOR
-Tony Cook <tony@develop-help.com>
+Tony Cook <tonyc@cpan.org>, Patrick Michaud
=head1 LICENSE