Chart
view release on metacpan or search on metacpan
lib/Chart/Manual/Types.pod view on Meta::CPAN
and L<y_label|Chart::Manual::Properties/y_label> and set L<xy_plot|Chart::Manual::Properties/xy_plot>
off, which is not really needed, since it is on C<'false'> per default.
This allows you to give the x-axis none numerical, custom tick labels,
which are by accident the numbers 1 .. 17, as the first data row shows.
The purpose of this maneuver is to not have zero as the first column label.
Because the origin of coordinate system is usually in the left lower
corner, we used a trick to flip the y-axis having the smallest values up.
We negated all values in the data, so that 8 is lower than 2, because
-8 is smaller than -2. Than we transformed the y-axis labels with a function,
that negates the value of the original label, erasing the minus sign.
For additional clarity, we put the names of the teams into the legend,
which is per default on the right side. And to make the code more compact,
we packed the first (just labels) and the four real data sets (rows)
together into an array or arrays and gave it as second argument directly
to the drawing method.
use Chart::LinesPoints;
my $g = Chart::LinesPoints->new( 600, 300 );
$g->set(
title => 'Soccer Season 2002',
legend_labels => ['NY Soccer Club', 'Denver Tigers',
'Houston Spacecats', 'Washington Presidents'],
y_label => 'position in the table',
x_label => 'day of play',
grid_lines => 'true',
f_y_tick => sub { - $_[0] },
# xy_plot => 'true',
integer_ticks_only => 'true',
colors => {
grid_lines => 'gray70',
},
);
$g->png("linespoints.png", [
[qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17)],
[qw(-7 -5 -6 -8 -9 -7 -5 -4 -3 -2 -4 -6 -3 -5 -3 -4 -6)],
[qw(-1 -1 -1 -1 -2 -2 -3 -3 -4 -4 -6 -3 -2 -2 -2 -1 -1)],
[qw(-4 -4 -3 -2 -1 -1 -1 -2 -1 -1 -3 -2 -4 -3 -4 -2 -2)],
[qw(-6 -3 -2 -3 -3 -3 -2 -1 -2 -3 -1 -1 -1 -1 -1 -3 -3)],
]);
=head2 Mountain
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/mountain.png" alt="mountain chart">
</p>
The class Chart::Mountain creates a mountain chart, which is a line chart
(see L</Lines>), where the area under the curve, right to the curve below
is filled with the color of the data set. In the following example we use
custom colors in hex notation (as supported by Chart::Color) which are
getting mapped onto the colors settings of
L<dataset0|Chart::Manual::Properties/colors-datasetx> .. dataset4.
As always, the first data set (row in the data table) holds the domain or
x-axis labels. Another specialty of mountain charts are patterns (not provided !),
to fill the area with. We load them via GD and give them over to the
C<'patterns'> property. Patterns are small images with one color and the
second being transparent.
use Chart::Mountain;
my @data = (
["1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th" ],
[ 3, 7, 8, 2, 4 , 8.5, 2, 5, 9],
[ 4, 2, 5, 6, 3 , 2.5, 3, 3, 4],
[ 7, 3, 2, 8, 8.5, 2 , 9, 4, 5],
);
my @hex_colors = ('#0099FF', '#00CC00', '#EEAA00', '#FF0099','#3333FF');
my $PNG;
my @patterns = map {
open( $PNG, '<', "./patterns/PATTERN$_.PNG" ) or die "Can't load pattern $_";
GD::Image->newFromPng( $PNG );
} 0 .. 4;
my $g = new Chart::Mountain( 500, 300);
$g->set(
title => 'Mountain Chart with Patterns',
x_label => 'Lengths',
y_label => 'Height',
grid_lines => 'true',
patterns => \@patterns,
precision => 1,
colors => {
grid_lines => 'gray70',
misc => 'gray55',
map { ( "dataset$_" => $hex_colors[$_] ) } 0 .. $#hex_colors,
},
);
$g->png( 'mountain.png', \@data );
=head2 Pareto
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/pareto.png" alt="pareto chart">
</p>
The class Chart::Pareto creates a combination of a L</Bars> and a L</Lines>
chart. The bars display absolute values of the data set (Pareto accepts
only one), while the line represent the accumulation (sum of all values
from the start on the left up to this value). For a better orientation
the absolute values should be sorted. In case your data set is not already,
change the property L<sort|Chart::Manual::Properties/sort> to C<'true'>.
(Note that the days of the week are not in chronological order,
but in the order of decreasing sale amounts.) The first given data set
is like in most cases the domain (x-axis labels). So the color of the first
data set containing numbers has the color of
L<dataset0|Chart::Manual::Properties/colors-datasetx> and the accumulation
gets the color of dataset1 (which are per default also red and green,
but in reverse order). We choose a Pantone Report designer red, which
sticks out but is not too shrill.
For better optics we set L<spaced_bars|Chart::Manual::Properties/spaced_bars>
off, so that the bars touch each other and give a nice counterweight to
the red color of the line. It's also a bit nicer, when the red labels
above the red line don't stick out the chart, so we increased the
L<max_val|Chart::Manual::Properties/max_val> from 5180 to 5500. Finally
to prevent the y-axis from overcrowding, we set labels (and y_grid_lines)
( run in 2.079 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )