Chart
view release on metacpan or search on metacpan
lib/Chart/Manual/Types.pod view on Meta::CPAN
);
$g->png("composite.png");
=head2 Direction
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/direction.png" alt="polar chart">
</p>
The class Chart::Direction creates a diagram based on polar coordinates.
This type of diagram is occasionally referred to as a radial or as a radar
chart, which has a circle as x-axis and its values define an angle.
The y-value in the center is L<min_value|Chart::Manual::Properties/min_value> and
the most outer circle is at L<max_value|Chart::Manual::Properties/max_value>.
In order to reverse that you have to set L<polar|Chart::Manual::Properties/polar> C<'true'>.
In our example we preferred to have a real comparison between arrow lengths
so we artificially put zero as C<min_value> by setting L<include_zero|Chart::Manual::Properties/include_zero>
C<'true'>. The just mentioned arrow style we achieved by deactivating
L<point|Chart::Manual::Properties/point> (drawing small circles) and
turning on L<arrow|Chart::Manual::Properties/arrow>. An additional
C<'true'> L<line|Chart::Manual::Properties/line> would connect the
points (arrow heads) with lines (as well as the first and last).
As in L<\Lines> and L<\Points>: L<pt_size|Chart::Manual::Properties/pt_size>
defines the point size and L<brush_size|Chart::Manual::Properties/brush_size>
the line thickness. Usually the background of each chart (just inside
the coordinate system) is a light gray - here deactivated. Radial
grid lines are drawn every 45 degrees (L<angle_interval|Chart::Manual::Properties/angle_interval>).
As with most chart types, the first data set defines the domain :
the x-values of all following data sets, which then define the associated
y-values (and therefore all sets have to have the same length).
Because we set in this example L<pairs|Chart::Manual::Properties/pairs>
C<'true'>, now every odd numbered data set is the domain for the next set.
But they still all sets have to have the same length.
use Chart::Direction;
my $g = Chart::Direction->new( 500, 500 );
$g->add_dataset( 210, 220, 200, 215, 225, 200 );
$g->add_dataset( 30, 40, 20, 35, 45, 20 );
$g->add_dataset( 30, 40, 20, 35, 45, 20 );
$g->add_dataset( 30, 40, 20, 35, 45, 20 );
$g->add_dataset( 120, 130, 110, 125, 135, 110 );
$g->add_dataset( 30, 40, 20, 35, 45, 20 );
$g->add_dataset( 300, 310, 290, 305, 315, 290 );
$g->add_dataset( 30, 40, 20, 35, 45, 20 );
$g->set(
title => 'Direction Demo',
angle_interval => 45,
precision => 0,
arrow => 'true',
point => 'false',
include_zero => 'true',
legend => 'none',
grey_background => 'false',
pairs => 'true',
);
$g->png("direction.png");
=head2 ErrorBars
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/error.png" alt="error bar chart">
</p>
The class Chart::ErrorBars creates a point chart with error bars, which
are vertical lines, depicting the uncertainty - a range which is possible
for that value. As seen in the example, we need four data sets to define
a series of error bars. The first data set are the x-values of the error
bars, the second the y-values. The third set holds the lenght of the upper
part of the error bar and the fourth set the lower part (distance between
main point and the lower bound of the error bar). The fourth set might be
omitted, when property L<same_error|Chart::Manual::Properties/same_error>
is set C<'true'>. In this case upper and lower part of the error bar have
the same size. Because all this produced only one set of error bars
with one color, there is no need for a legend. That is why it is switched
off by setting property L<legend|Chart::Manual::Properties/legend> to
C<'none'>. The label on the x-axis are painted in the C<'staggered'> style,
so they don't overlap. L<pt_size|Chart::Manual::Properties/pt_size> refers
to the diameter in pixel of the errors bars central point.
L<brush_size|Chart::Manual::Properties/brush_size> is the thickness of the bar.
For better readability C<'both'> L<y_axes|Chart::Manual::Properties/y_axes> were labeled.
use Chart::ErrorBars;
my $g = Chart::ErrorBars->new( 500, 400 );
$g->add_dataset(qw(1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5));
$g->add_dataset(qw(1 1.1 1.2 1.1 1.14 1.15 1.26 1.2 1.1 1.19 1.2 1.4 1.6 2.0 2.5 3.1));
$g->add_dataset(qw(0.4 0.1 0.2 0.1 0.14 0.15 0.26 0.27 0.1 0.19 0.2 0.1 0.1 0.2 0.1 0.3));
$g->add_dataset(qw(0.2 0.11 0.12 0.11 0.2 0.3 0.12 0.27 0.11 0.3 0.2 0.2 0.2 0.1 0.1 0.2));
$g->set(
title => 'Error Bars Demo',
legend => 'none',
y_axes => 'both',
x_ticks => 'staggered',
pt_size => 12,
brush_size => 2,
grid_lines => 'true',
colors => {
grid_lines => 'gray70',
misc => 'gray65',
},
);
$g->png("error.png");
=head2 HorizontalBars
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/hbars.png" alt="horizontal bar chart">
</p>
The class Chart::HorizontalBars creates a chart of horizontally oriented bars.
Same rules apply as in L</Bars>, except due negative values in our data sets
here L<min_val|Chart::Manual::Properties/min_val> doesn't have to be set to zero.
And instead of L<y_grid_lines|Chart::Manual::Properties/y_grid_lines>
we activate and color L<x_grid_lines|Chart::Manual::Properties/x_grid_lines>.
Deactivating the grey background of the plot just adds a little friendliness.
use Chart::HorizontalBars;
my $g = Chart::HorizontalBars->new( 600, 600 );
$g->add_dataset( qw/ camel cat dog bear shell/ );
$g->add_dataset( -300, 400, 800, -500, 200 );
$g->add_dataset( 800, -600, 300, 300, 400 );
$g->set(
title => 'Bars !',
x_label => 'Group',
y_label => 'Value',
x_grid_lines => 'true',
precision => 0,
colors => {
x_grid_lines => 'gray70',
misc => 'gray55',
text => 'gray55',
x_label => 'gray40',
y_label => 'gray40',
title => 'gray20',
}
grey_background => 'false',
);
$g->png("hbars.png");
=head2 Lines
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/lines.png" alt="xy chart with lines">
</p>
The class Chart::Lines creates a chart with lines that connect the would
be data points. If you want to make the points more visible,
use L</LinesPoints>. The only special property is L<brush_size|Chart::Manual::Properties/brush_size>,
the thickness of the lines, which was not even utilized in this example.
To make things nicer we put only some softer colors to the horizontal (y)
grid lines and the box and ticks (misc) and placed the legend on the bottom
so that the chart doesn't get sqeezed by it.
use Chart::Lines;
my $g = Chart::Lines->new( 600, 400 );
$g->add_dataset( 'foo', 'bar', 'whee', 'ding', 'bat', 'bit' );
$g->add_dataset( 3.2, 4.34, 9.456, 10.459, 11.24234, 14.0234 );
$g->add_dataset( -1.3, 8.4, 5.34, 3.234, 4.33, 13.09 );
$g->add_dataset( 5, 7, 2, 10, 12, 2.3445 );
$g->set(
title => 'Lines Chart',
legend => 'bottom' ,
y_label => 'y label 1',
precision => 0,
y_grid_lines => 'true',
colors => {
y_label => 'orangepeel',
y_grid_lines => [ 190, 190, 190 ],
misc => [ 100, 100, 100 ],
},
);
$g->png("lines.png");
=head2 LinesPoints
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/linespoints.png" alt="xy chart with connected points">
</p>
The class Chart::LinesPoints creates chart, which is a combination of
L</Points> and L</Lines>: shaped symbols connected by lines. This
requires a combination of the special properties of both chart types:
L<brushStyle|Chart::Manual::Properties/brushStyle> (point shape),
L<pt_size|Chart::Manual::Properties/pt_size> (point diameter) and
L<brush_size|Chart::Manual::Properties/brush_size> (line thickness).
In our example we named both axis via L<x_label|Chart::Manual::Properties/x_label>
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)
only every 250 by setting L<skip_int_ticks|Chart::Manual::Properties/skip_int_ticks>
and activating L<integer_ticks_only|Chart::Manual::Properties/integer_ticks_only>.
use Chart::Pareto;
my $g = Chart::Pareto->new( 450, 400 );
$g->add_dataset( 'Mo', 'Tue', 'We', 'Th', 'Fr', 'Sa', 'Su' );
$g->add_dataset( 2500, 1000, 250, 700, 100, 610, 20 );
$g->set(
title => 'Sold Tickets for Beethovens 9th',
y_label => 'Sold Tickets',
x_label => '! sold out in the first week !',
sort => 'true',
max_val => 5500,
integer_ticks_only => 'true',
skip_int_ticks => 250,
y_grid_lines => 'true',
spaced_bars => 'false',
legend => 'none',
colors => {
title => 'darkblue',
dataset0 => 'green',
dataset1 => 'aurorared',
x_label => 'aurorared',
y_grid_lines => 'white',
},
);
$g->png("pareto.png");
=head2 Pie
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/pie.png" alt="pie chart">
</p>
The class Chart::Pie creates a pie or ring chart. The first added data
set must contain the labels and the second set the values. Our example
displays a ring chart with a thickness of 35% (of the radius). If the
L<ring|Chart::Manual::Properties/ring> property is omitted, the chart form
falls back to regular pie. Every ring slice is labeled with the stated label,
plus the percentage of its value, as defined with the property
L<label_values|Chart::Manual::Properties/label_values>. Connecting lines
between label and slice are drawn because L<legend_lines|Chart::Manual::Properties/legend_lines>
is set to C<'true'>. The actual legend is placed on the bottom, in order
to leave the ring as large as possible. The legend again shows the
association between color and data point and its value because
L<legend_label_values|Chart::Manual::Properties/legend_label_values>
is set to C<'values'>. Unlike other chart types, where one data set is
correlated with a color, here every slice has to have its own color.
Thats why the first data point has the color of L<dataset0|Chart::Manual::Properties/colors-datasetx>
the second of dataset1 and so forth. In most cases the default colors
are good enough, unless you have special meanings in mind. Please also
note the multi line (row) title text.
use Chart::Pie;
my $g = Chart::Pie->new( 500, 450 );
$g->add_dataset( qw/eins zwei drei vier fuenf sechs sieben acht neun zehn/ );
$g->add_dataset( 120, 50, 100, 80, 40, 45, 150, 60, 110, 50 );
$g->set( 'title' => 'Pie\nDemo Chart',
'legend' => 'bottom',
'legend_label_values' => 'value',
'label_values' => 'percent',
'legend_lines' => 'true',
'ring' => 0.35,
);
$g->png("pie.png");
=head2 Points
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/points.png" alt="xy chart with points">
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/test/points_5.png" alt="point styles demo">
</p>
The class Chart::Points creates a xy-chart (also called scattergram),
where the individual data points are marked with a symbol.
The shape of the symbol is selected by the property L<brushStyle|Chart::Manual::Properties/brushStyle>,
which was not utilized in this example, in order to use the default shape:
a circle of a diameter set by L<pt_size|Chart::Manual::Properties/pt_size>.
All shapes can be seen in the demo above, right.
(If you want in addition lines, connecting the points, check L</LinesPoints>.)
The first data set comprises the domain set, displayed on the x-axis.
L<precision|Chart::Manual::Properties/precision> set to zero cuts the
decimals on the y-axis labels and keeps it clean. The method C<add_pt>
appends to every data set another value, adding another column to the chart.
L<png_border|Chart::Manual::Properties/png_border> does just adds frame
of 10 pixel width around the entire image. They middle gray grid lines
are just easy for the eyes.
use Chart::Points;
my $g = Chart::Points->new();
$g->add_dataset( 'foo', 'bar', 'blank' );
$g->add_dataset( 3, 4, 9 );
$g->add_dataset( 8, 6, 0 );
$g->add_dataset( 5, 7, 2 );
$g->add_pt( 'dat', 1, 5, 7 );
$g->set(
title => 'Points Chart',
pt_size => 18,
# brushStyle => 'Star',
precision => 0,
grid_lines => 'true',
png_border => 10,
colors => {
grid_lines => 'gray70',
},
);
$g->png("points.png");
=head2 Split
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/split.png" alt="multi chart">
</p>
The class Chart::Split creates a L</Lines> chart where both x and y axes
are assumed to be numeric. Split charts are mainly intended for cases
where many data points are spread over a wide x range while at the
same time the y range is limited. Typical examples are weather or
seismic data. The x axis will be split into several intervals of the
same length (specified with the mandatory option L<interval|Chart::Manual::Properties/interval>
and starting at L<start|Chart::Manual::Properties/start>). Chart::Split will
draw only positive x coordinates. The y axis will not be labelled with
the y values. Rather, the axis will show only the sequence numbers
of the intervals.
use Chart::Split;
my $g = Chart::Split->new( 500, 500 );
my @domain = 1 .. 4000;
my @rnd = map { srand( time() / $_ * 50 ); rand(10) } @domain, 4001;
my @diff = map { abs $rnd[$_-1] - $rnd[$_] } @domain;
pop @rnd;
$g->add_dataset(@domain);
$g->add_dataset(@rnd);
$g->add_dataset(@diff);
$g->set(
title => "Random Numbers Test",
x_label => "4000 Random Numbers",
start => 0,
interval => 400,
brush_size => 1,
interval_ticks => 0,
legend => 'bottom',
legend_labels => ['random numbers', 'difference' ],
colors => {
title => 'darkblue',
text => 'gray45',
misc => 'gray45',
},
);
$g->png("split.png");
=head2 StackedBars
=for HTML <p>
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/stackedbars.png" alt="stacked bar chart">
</p>
The class Chart::StackedBars is a variant of L</Bars> that stacks
bars belonging to one x-value on top of each other, instead of putting
them beside each other. Data sets 0..n are ordered from the bottom up.
They are in most cases more intuitive than L</Pie> charts, because its
easier to intuit linear than quadratic ratios. As in the Bars example we
activated horizontal grid lines, which were subtle colored. To surpress
decimals on the y-axis L<precision|Chart::Manual::Properties/precision> was turned down.
And as in most cases - the first data set is the domain, which will be
drawn as x-axis labels.
use Chart::StackedBars;
my $g = Chart::StackedBars->new( 600, 400 );
$g->add_dataset( 'camel', 'dromedar', 'llama', 'vicuna');
$g->add_dataset( 3, 4, 9, 10, );
$g->add_dataset( 8, 6, 1, 12, );
$g->add_dataset( 5, 7, 2, 13, );
$g->set(
title => 'Stacked Bars',
legend => 'left',
precision => 0,
y_grid_lines => 'true',
grey_background => 'false',
colors => {
grid_lines => 'gray80',
misc => 'gray55',
text => 'gray55',
x_label => 'gray40',
y_label => 'gray40',
title => 'gray20',
}
);
$g->png("stackedbars.png");
=head1 COPYRIGHT & LICENSE
Copyright 2022 David Bonner, Herbert Breunung.
This program is free software; you can redistribute it and/or modify it
under same terms as Perl itself.
=head1 AUTHOR
David Bonner,
Herbert Breunung, <lichtkind@cpan.org>
=cut
( run in 0.617 second using v1.01-cache-2.11-cpan-5837b0d9d2c )