App-Licensecheck
view release on metacpan or search on metacpan
t/devscripts/artistic-2-0-modules.pm view on Meta::CPAN
my $max_x = @.values[0].elems;
# maximum value of the sum over each column
my $max_y = [max] @.values[0].keys.map: {
[+] @.values.map: -> $a { $a[$_] }
};
my $datasets = +@.values;
my $step_x = $.plot-width / $max_x;
my $step_y = $.plot-height / $max_y;
my @svg_d = gather {
my $bar-width = $.fill-width * $step_x;
for flat @.values[0].keys Z @.labels -> $k, $l {
my $y-offset = 0;
for ^$datasets -> $d {
my $v = @.values[$d][$k];
my $p = 'rect' => [
:y(-$v * $step_y - $y-offset),
:x($k * $step_x),
:width($bar-width),
:height($v * $step_y),
:style("fill:{ @.colors[$d % *] }; stroke: none"),
];
$y-offset += $v * $step_y;
take |$.linkify($k, $p);
}
}
$.plot-x-labels(:$step_x, :$label-skip);
$.y-ticks(0, $max_y, $step_y);
}
my $svg = $.apply-standard-transform(
@svg_d,
@.eyecandy(),
);
@.wrap-in-svg-header-if-necessary($svg, :wrap($full));
}
# snip...
=begin Pod
=head1 NAME
SVG::Plot - simple SVG bar charts
=head1 VERSION
$very_early
=head1 SYNOPSIS
use SVG;
use SVG::Plot;
my @d1 = (0..100).map: { sin($_ / 10.0) };
my @d2 = (0..100).map: { cos($_ / 10.0) };
say SVG.serialize:
SVG::Plot.new(
width => 400,
height => 250,
values => ([@d1], [@d2]),
title => 'sin(x/10), cos(x/10)',
).plot(:lines);
=head1 DESCRIPTION
SVG::Plot turns a set of data points (and optionally labels) into a data
structure which Carl Mäsak's module L<SVG> serializes into SVG, and displays a
bar chart of the data.
See L<http://perlgeek.de/blog-en/perl-6/svg-adventures.html> for the initial
announcement and future plans.
Note that the module itself does not depend on SVG.pm, only the examples (and
maybe in future the tests).
=head1 A WORD OF WARNING
Please note that the interface of this module is still in flux, and might
change without further notice. If you actually use it, send the author an
email to inform him, maybe he'll try to keep the interface backwards
compatible, or notify you on incompatible changes.
=head1 METHODS
=head2 new(*%options)
Constructs a L<SVG::Plot> object. You can set various attributes as options,
see their documentation below. No attribute is mandatory.
=head2 multi method plot(:$type!, :$full = True)
If the argument C<$!full> is provided, the returned data structure contains
only the body of the SVG, not the C<< <svg xmlns=...> >> header.
Each multi method renders one type of chart, and has a mandatory named
parameter with the name of the type. Currently available are C<bars>,
C<stacked-bars>, C<lines> and C<points>.
=head1 Attributes
The following attributes can be set with the C<new> constructor, and can be
queried later on (those marked with C<is rw> can also be set later on).
=head2 @.values is rw
The values to be plotted
=head2 @.labels is rw
The labels printed below the bars. Note that this must be either left empty
(in which case C<@.values.keys> is used as a default), or of the same length
as C<@.values>. To suppress printing of labels just set them all to the empty
string, C<$svg.labels = ('' xx $svg.values.elems)>.
=head2 @.links is rw
If some values of @.links are set to defined values, the corresponding bars
and labels will be turned into links
=head2 $.width
( run in 1.390 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )