| 1 |
|
| 2 |
=encoding UTF-8 |
| 3 |
|
| 4 |
=head1 NAME |
| 5 |
|
| 6 |
Chart::Manual::Types - all chart types by example |
| 7 |
|
| 8 |
=head1 OVERVIEW |
| 9 |
|
| 10 |
This page illustrates all supported chart types, describes their |
| 11 |
special abilities and programming needs. Detailled information is linked |
| 12 |
whenever possible. |
| 13 |
|
| 14 |
Currently available are xycharts with points, lines, mountains, |
| 15 |
bar charts, stacked bars, error bars, pie and ring charts with split and |
| 16 |
polar coordinate systems. Also composite charts are possible. |
| 17 |
|
| 18 |
Generally, each chart type here is implementd by a class: Chart::* (name), |
| 19 |
which inherits most of its methods from Chart::Base. Every |
| 20 |
L<constructor|Chart::Manual::Methods/new> takes two arguments, |
| 21 |
which are width and height of the later generated image. |
| 22 |
|
| 23 |
|
| 24 |
=head2 Bars |
| 25 |
|
| 26 |
=for HTML <p> |
| 27 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/bars.png" alt="dual bar chart" width="500" height="300"> |
| 28 |
</p> |
| 29 |
|
| 30 |
The class Chart::Bars creates a chart made up of a series of vertical bars. |
| 31 |
The length of each bar depicts one value of your second data set. |
| 32 |
The first data set however, defines the domain. In this example the first |
| 33 |
value of the first (domain) data set is 'camel'. So the first value of |
| 34 |
the second set (300) is positioned above the first tick on the x-axis |
| 35 |
labeled 'camel'. Right beside it is a differently colored bar, depticting |
| 36 |
the first value of the third data set (800). Since the option L<spaced_bars|Chart::Manual::Properties/spaced_bars> |
| 37 |
is set to C<'true'> on default, both bars are separated from the next group of bars. |
| 38 |
In this example it also makes also sense to activate the horizontal |
| 39 |
L<y_grid_lines|Chart::Manual::Properties/y_grid_lines> and give them |
| 40 |
a subtle color. Further important was it to set the property L<min_val|Chart::Manual::Properties/min_val> |
| 41 |
to zero, so that the bars could be seen in full length and not from |
| 42 |
the min value of the data sets (300) on upwards. L<precision|Chart::Manual::Properties/precision> |
| 43 |
set to zero just drops the decimals on the tick label so the chart looks |
| 44 |
a little cleaner. All the the other color set serv the same purpose. |
| 45 |
|
| 46 |
use Chart::Bars; |
| 47 |
|
| 48 |
my $g = Chart::Bars->new( 500, 300 ); |
| 49 |
$g->add_dataset( qw/ camel cat dog bear shell/ ); |
| 50 |
$g->add_dataset( 300, 400, 800, 500, 900 ); |
| 51 |
$g->add_dataset( 800, 600, 300, 300, 400 ); |
| 52 |
|
| 53 |
$g->set( |
| 54 |
title => 'Bars !', |
| 55 |
x_label => 'Group', |
| 56 |
y_label => 'Value', |
| 57 |
y_grid_lines => 'true', |
| 58 |
colors => { |
| 59 |
y_grid_lines => 'gray70', |
| 60 |
misc => 'gray55', |
| 61 |
text => 'gray55', |
| 62 |
x_label => 'gray40', |
| 63 |
y_label => 'gray40', |
| 64 |
title => 'gray20', |
| 65 |
}, |
| 66 |
min_val => 0, |
| 67 |
precision => 0, |
| 68 |
# spaced_bars => 'false', |
| 69 |
); |
| 70 |
$g->png("bars.png"); |
| 71 |
|
| 72 |
|
| 73 |
=head2 Composite |
| 74 |
|
| 75 |
=for HTML <p> |
| 76 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/composite.png" alt="composite chart"> |
| 77 |
</p> |
| 78 |
|
| 79 |
The class Chart::Composite creates a two component chart with two |
| 80 |
types of charts which are layered one above each other. Just set |
| 81 |
the option composite info. For example, you can create a two |
| 82 |
component chart with bars and lines. A composite chart does not |
| 83 |
make sense with all combinations of chart types, but it works pretty |
| 84 |
good with Lines, Points, LinesPoints and Bars. Note that two similar |
| 85 |
chart types may come into visual conflict. Chart::Composite can do |
| 86 |
only composite charts made up of two components. |
| 87 |
|
| 88 |
In this example are the data sets one and two displayed as L</Bars> and |
| 89 |
the next two als L</LinesPoints> as set by the property |
| 90 |
L<composite_info|Chart::Manual::Properties/composite_info>. |
| 91 |
Please read these sections too. Otherwise we only colored the last data |
| 92 |
set for better contrast und cut the decimals from the axis labels via |
| 93 |
L<precision|Chart::Manual::Properties/precision>. |
| 94 |
|
| 95 |
use Chart::Composite; |
| 96 |
|
| 97 |
my $g = Chart::Composite->new( ); |
| 98 |
$g->add_dataset( 1, 2, 3 ); |
| 99 |
$g->add_dataset( 10, 20, 30 ); |
| 100 |
$g->add_dataset( 15, 25, 32 ); |
| 101 |
$g->add_dataset( 7, 24, 23 ); |
| 102 |
$g->add_dataset( 5.1, 7.5, 9.9 ); |
| 103 |
$g->set( |
| 104 |
title => 'Composite Chart', |
| 105 |
composite_info => [ [ 'Bars', [ 1, 2 ] ], |
| 106 |
[ 'LinesPoints', [ 3, 4 ] ] ], |
| 107 |
include_zero => 'true', |
| 108 |
precision => 0, |
| 109 |
colors => { |
| 110 |
dataset3 => 'darkorange', |
| 111 |
} |
| 112 |
); |
| 113 |
$g->png("composite.png"); |
| 114 |
|
| 115 |
|
| 116 |
=head2 Direction |
| 117 |
|
| 118 |
=for HTML <p> |
| 119 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/direction.png" alt="polar chart"> |
| 120 |
</p> |
| 121 |
|
| 122 |
The class Chart::Direction creates a diagram based on polar coordinates. |
| 123 |
This type of diagram is occasionally referred to as a radial or as a radar |
| 124 |
chart, which has a circle as x-axis and its values define an angle. |
| 125 |
The y-value in the center is L<min_value|Chart::Manual::Properties/min_value> and |
| 126 |
the most outer circle is at L<max_value|Chart::Manual::Properties/max_value>. |
| 127 |
In order to reverse that you have to set L<polar|Chart::Manual::Properties/polar> C<'true'>. |
| 128 |
In our example we preferred to have a real comparison between arrow lengths |
| 129 |
so we artificially put zero as C<min_value> by setting L<include_zero|Chart::Manual::Properties/include_zero> |
| 130 |
C<'true'>. The just mentioned arrow style we achieved by deactivating |
| 131 |
L<point|Chart::Manual::Properties/point> (drawing small circles) and |
| 132 |
turning on L<arrow|Chart::Manual::Properties/arrow>. An additional |
| 133 |
C<'true'> L<line|Chart::Manual::Properties/line> would connect the |
| 134 |
points (arrow heads) with lines (as well as the first and last). |
| 135 |
As in L<\Lines> and L<\Points>: L<pt_size|Chart::Manual::Properties/pt_size> |
| 136 |
defines the point size and L<brush_size|Chart::Manual::Properties/brush_size> |
| 137 |
the line thickness. Usually the background of each chart (just inside |
| 138 |
the coordinate system) is a light gray - here deactivated. Radial |
| 139 |
grid lines are drawn every 45 degrees (L<angle_interval|Chart::Manual::Properties/angle_interval>). |
| 140 |
|
| 141 |
As with most chart types, the first data set defines the domain : |
| 142 |
the x-values of all following data sets, which then define the associated |
| 143 |
y-values (and therefore all sets have to have the same length). |
| 144 |
Because we set in this example L<pairs|Chart::Manual::Properties/pairs> |
| 145 |
C<'true'>, now every odd numbered data set is the domain for the next set. |
| 146 |
But they still all sets have to have the same length. |
| 147 |
|
| 148 |
|
| 149 |
use Chart::Direction; |
| 150 |
|
| 151 |
my $g = Chart::Direction->new( 500, 500 ); |
| 152 |
|
| 153 |
$g->add_dataset( 210, 220, 200, 215, 225, 200 ); |
| 154 |
$g->add_dataset( 30, 40, 20, 35, 45, 20 ); |
| 155 |
|
| 156 |
$g->add_dataset( 30, 40, 20, 35, 45, 20 ); |
| 157 |
$g->add_dataset( 30, 40, 20, 35, 45, 20 ); |
| 158 |
|
| 159 |
$g->add_dataset( 120, 130, 110, 125, 135, 110 ); |
| 160 |
$g->add_dataset( 30, 40, 20, 35, 45, 20 ); |
| 161 |
|
| 162 |
$g->add_dataset( 300, 310, 290, 305, 315, 290 ); |
| 163 |
$g->add_dataset( 30, 40, 20, 35, 45, 20 ); |
| 164 |
|
| 165 |
$g->set( |
| 166 |
title => 'Direction Demo', |
| 167 |
angle_interval => 45, |
| 168 |
precision => 0, |
| 169 |
arrow => 'true', |
| 170 |
point => 'false', |
| 171 |
include_zero => 'true', |
| 172 |
legend => 'none', |
| 173 |
grey_background => 'false', |
| 174 |
pairs => 'true', |
| 175 |
); |
| 176 |
$g->png("direction.png"); |
| 177 |
|
| 178 |
|
| 179 |
=head2 ErrorBars |
| 180 |
|
| 181 |
=for HTML <p> |
| 182 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/error.png" alt="error bar chart"> |
| 183 |
</p> |
| 184 |
|
| 185 |
The class Chart::ErrorBars creates a point chart with error bars, which |
| 186 |
are vertical lines, depicting the uncertainty - a range which is possible |
| 187 |
for that value. As seen in the example, we need four data sets to define |
| 188 |
a series of error bars. The first data set are the x-values of the error |
| 189 |
bars, the second the y-values. The third set holds the lenght of the upper |
| 190 |
part of the error bar and the fourth set the lower part (distance between |
| 191 |
main point and the lower bound of the error bar). The fourth set might be |
| 192 |
omitted, when property L<same_error|Chart::Manual::Properties/same_error> |
| 193 |
is set C<'true'>. In this case upper and lower part of the error bar have |
| 194 |
the same size. Because all this produced only one set of error bars |
| 195 |
with one color, there is no need for a legend. That is why it is switched |
| 196 |
off by setting property L<legend|Chart::Manual::Properties/legend> to |
| 197 |
C<'none'>. The label on the x-axis are painted in the C<'staggered'> style, |
| 198 |
so they don't overlap. L<pt_size|Chart::Manual::Properties/pt_size> refers |
| 199 |
to the diameter in pixel of the errors bars central point. |
| 200 |
L<brush_size|Chart::Manual::Properties/brush_size> is the thickness of the bar. |
| 201 |
For better readability C<'both'> L<y_axes|Chart::Manual::Properties/y_axes> were labeled. |
| 202 |
|
| 203 |
use Chart::ErrorBars; |
| 204 |
|
| 205 |
my $g = Chart::ErrorBars->new( 500, 400 ); |
| 206 |
$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)); |
| 207 |
$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)); |
| 208 |
$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)); |
| 209 |
$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)); |
| 210 |
$g->set( |
| 211 |
title => 'Error Bars Demo', |
| 212 |
legend => 'none', |
| 213 |
y_axes => 'both', |
| 214 |
x_ticks => 'staggered', |
| 215 |
pt_size => 12, |
| 216 |
brush_size => 2, |
| 217 |
grid_lines => 'true', |
| 218 |
colors => { |
| 219 |
grid_lines => 'gray70', |
| 220 |
misc => 'gray65', |
| 221 |
}, |
| 222 |
); |
| 223 |
$g->png("error.png"); |
| 224 |
|
| 225 |
|
| 226 |
=head2 HorizontalBars |
| 227 |
|
| 228 |
=for HTML <p> |
| 229 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/hbars.png" alt="horizontal bar chart"> |
| 230 |
</p> |
| 231 |
|
| 232 |
The class Chart::HorizontalBars creates a chart of horizontally oriented bars. |
| 233 |
Same rules apply as in L</Bars>, except due negative values in our data sets |
| 234 |
here L<min_val|Chart::Manual::Properties/min_val> doesn't have to be set to zero. |
| 235 |
And instead of L<y_grid_lines|Chart::Manual::Properties/y_grid_lines> |
| 236 |
we activate and color L<x_grid_lines|Chart::Manual::Properties/x_grid_lines>. |
| 237 |
Deactivating the grey background of the plot just adds a little friendliness. |
| 238 |
|
| 239 |
use Chart::HorizontalBars; |
| 240 |
|
| 241 |
my $g = Chart::HorizontalBars->new( 600, 600 ); |
| 242 |
$g->add_dataset( qw/ camel cat dog bear shell/ ); |
| 243 |
$g->add_dataset( -300, 400, 800, -500, 200 ); |
| 244 |
$g->add_dataset( 800, -600, 300, 300, 400 ); |
| 245 |
$g->set( |
| 246 |
title => 'Bars !', |
| 247 |
x_label => 'Group', |
| 248 |
y_label => 'Value', |
| 249 |
x_grid_lines => 'true', |
| 250 |
precision => 0, |
| 251 |
colors => { |
| 252 |
x_grid_lines => 'gray70', |
| 253 |
misc => 'gray55', |
| 254 |
text => 'gray55', |
| 255 |
x_label => 'gray40', |
| 256 |
y_label => 'gray40', |
| 257 |
title => 'gray20', |
| 258 |
} |
| 259 |
grey_background => 'false', |
| 260 |
); |
| 261 |
$g->png("hbars.png"); |
| 262 |
|
| 263 |
|
| 264 |
=head2 Lines |
| 265 |
|
| 266 |
=for HTML <p> |
| 267 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/lines.png" alt="xy chart with lines"> |
| 268 |
</p> |
| 269 |
|
| 270 |
The class Chart::Lines creates a chart with lines that connect the would |
| 271 |
be data points. If you want to make the points more visible, |
| 272 |
use L</LinesPoints>. The only special property is L<brush_size|Chart::Manual::Properties/brush_size>, |
| 273 |
the thickness of the lines, which was not even utilized in this example. |
| 274 |
To make things nicer we put only some softer colors to the horizontal (y) |
| 275 |
grid lines and the box and ticks (misc) and placed the legend on the bottom |
| 276 |
so that the chart doesn't get sqeezed by it. |
| 277 |
|
| 278 |
|
| 279 |
use Chart::Lines; |
| 280 |
|
| 281 |
my $g = Chart::Lines->new( 600, 400 ); |
| 282 |
$g->add_dataset( 'foo', 'bar', 'whee', 'ding', 'bat', 'bit' ); |
| 283 |
$g->add_dataset( 3.2, 4.34, 9.456, 10.459, 11.24234, 14.0234 ); |
| 284 |
$g->add_dataset( -1.3, 8.4, 5.34, 3.234, 4.33, 13.09 ); |
| 285 |
$g->add_dataset( 5, 7, 2, 10, 12, 2.3445 ); |
| 286 |
$g->set( |
| 287 |
title => 'Lines Chart', |
| 288 |
legend => 'bottom' , |
| 289 |
y_label => 'y label 1', |
| 290 |
precision => 0, |
| 291 |
y_grid_lines => 'true', |
| 292 |
colors => { |
| 293 |
y_label => 'orangepeel', |
| 294 |
y_grid_lines => [ 190, 190, 190 ], |
| 295 |
misc => [ 100, 100, 100 ], |
| 296 |
}, |
| 297 |
); |
| 298 |
$g->png("lines.png"); |
| 299 |
|
| 300 |
|
| 301 |
=head2 LinesPoints |
| 302 |
|
| 303 |
=for HTML <p> |
| 304 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/linespoints.png" alt="xy chart with connected points"> |
| 305 |
</p> |
| 306 |
|
| 307 |
The class Chart::LinesPoints creates chart, which is a combination of |
| 308 |
L</Points> and L</Lines>: shaped symbols connected by lines. This |
| 309 |
requires a combination of the special properties of both chart types: |
| 310 |
L<brushStyle|Chart::Manual::Properties/brushStyle> (point shape), |
| 311 |
L<pt_size|Chart::Manual::Properties/pt_size> (point diameter) and |
| 312 |
L<brush_size|Chart::Manual::Properties/brush_size> (line thickness). |
| 313 |
|
| 314 |
In our example we named both axis via L<x_label|Chart::Manual::Properties/x_label> |
| 315 |
and L<y_label|Chart::Manual::Properties/y_label> and set L<xy_plot|Chart::Manual::Properties/xy_plot> |
| 316 |
off, which is not really needed, since it is on C<'false'> per default. |
| 317 |
This allows you to give the x-axis none numerical, custom tick labels, |
| 318 |
which are by accident the numbers 1 .. 17, as the first data row shows. |
| 319 |
The purpose of this maneuver is to not have zero as the first column label. |
| 320 |
|
| 321 |
Because the origin of coordinate system is usually in the left lower |
| 322 |
corner, we used a trick to flip the y-axis having the smallest values up. |
| 323 |
We negated all values in the data, so that 8 is lower than 2, because |
| 324 |
-8 is smaller than -2. Than we transformed the y-axis labels with a function, |
| 325 |
that negates the value of the original label, erasing the minus sign. |
| 326 |
|
| 327 |
For additional clarity, we put the names of the teams into the legend, |
| 328 |
which is per default on the right side. And to make the code more compact, |
| 329 |
we packed the first (just labels) and the four real data sets (rows) |
| 330 |
together into an array or arrays and gave it as second argument directly |
| 331 |
to the drawing method. |
| 332 |
|
| 333 |
use Chart::LinesPoints; |
| 334 |
|
| 335 |
my $g = Chart::LinesPoints->new( 600, 300 ); |
| 336 |
$g->set( |
| 337 |
title => 'Soccer Season 2002', |
| 338 |
legend_labels => ['NY Soccer Club', 'Denver Tigers', |
| 339 |
'Houston Spacecats', 'Washington Presidents'], |
| 340 |
y_label => 'position in the table', |
| 341 |
x_label => 'day of play', |
| 342 |
grid_lines => 'true', |
| 343 |
f_y_tick => sub { - $_[0] }, |
| 344 |
# xy_plot => 'true', |
| 345 |
integer_ticks_only => 'true', |
| 346 |
colors => { |
| 347 |
grid_lines => 'gray70', |
| 348 |
}, |
| 349 |
); |
| 350 |
$g->png("linespoints.png", [ |
| 351 |
[qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17)], |
| 352 |
[qw(-7 -5 -6 -8 -9 -7 -5 -4 -3 -2 -4 -6 -3 -5 -3 -4 -6)], |
| 353 |
[qw(-1 -1 -1 -1 -2 -2 -3 -3 -4 -4 -6 -3 -2 -2 -2 -1 -1)], |
| 354 |
[qw(-4 -4 -3 -2 -1 -1 -1 -2 -1 -1 -3 -2 -4 -3 -4 -2 -2)], |
| 355 |
[qw(-6 -3 -2 -3 -3 -3 -2 -1 -2 -3 -1 -1 -1 -1 -1 -3 -3)], |
| 356 |
]); |
| 357 |
|
| 358 |
|
| 359 |
=head2 Mountain |
| 360 |
|
| 361 |
=for HTML <p> |
| 362 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/mountain.png" alt="mountain chart"> |
| 363 |
</p> |
| 364 |
|
| 365 |
The class Chart::Mountain creates a mountain chart, which is a line chart |
| 366 |
(see L</Lines>), where the area under the curve, right to the curve below |
| 367 |
is filled with the color of the data set. In the following example we use |
| 368 |
custom colors in hex notation (as supported by Chart::Color) which are |
| 369 |
getting mapped onto the colors settings of |
| 370 |
L<dataset0|Chart::Manual::Properties/colors-datasetx> .. dataset4. |
| 371 |
As always, the first data set (row in the data table) holds the domain or |
| 372 |
x-axis labels. Another specialty of mountain charts are patterns (not provided !), |
| 373 |
to fill the area with. We load them via GD and give them over to the |
| 374 |
C<'patterns'> property. Patterns are small images with one color and the |
| 375 |
second being transparent. |
| 376 |
|
| 377 |
use Chart::Mountain; |
| 378 |
|
| 379 |
my @data = ( |
| 380 |
["1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th" ], |
| 381 |
[ 3, 7, 8, 2, 4 , 8.5, 2, 5, 9], |
| 382 |
[ 4, 2, 5, 6, 3 , 2.5, 3, 3, 4], |
| 383 |
[ 7, 3, 2, 8, 8.5, 2 , 9, 4, 5], |
| 384 |
); |
| 385 |
my @hex_colors = ('#0099FF', '#00CC00', '#EEAA00', '#FF0099','#3333FF'); |
| 386 |
my $PNG; |
| 387 |
my @patterns = map { |
| 388 |
open( $PNG, '<', "./patterns/PATTERN$_.PNG" ) or die "Can't load pattern $_"; |
| 389 |
GD::Image->newFromPng( $PNG ); |
| 390 |
} 0 .. 4; |
| 391 |
|
| 392 |
my $g = new Chart::Mountain( 500, 300); |
| 393 |
$g->set( |
| 394 |
title => 'Mountain Chart with Patterns', |
| 395 |
x_label => 'Lengths', |
| 396 |
y_label => 'Height', |
| 397 |
grid_lines => 'true', |
| 398 |
patterns => \@patterns, |
| 399 |
precision => 1, |
| 400 |
colors => { |
| 401 |
grid_lines => 'gray70', |
| 402 |
misc => 'gray55', |
| 403 |
map { ( "dataset$_" => $hex_colors[$_] ) } 0 .. $#hex_colors, |
| 404 |
}, |
| 405 |
); |
| 406 |
$g->png( 'mountain.png', \@data ); |
| 407 |
|
| 408 |
|
| 409 |
=head2 Pareto |
| 410 |
|
| 411 |
=for HTML <p> |
| 412 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/pareto.png" alt="pareto chart"> |
| 413 |
</p> |
| 414 |
|
| 415 |
The class Chart::Pareto creates a combination of a L</Bars> and a L</Lines> |
| 416 |
chart. The bars display absolute values of the data set (Pareto accepts |
| 417 |
only one), while the line represent the accumulation (sum of all values |
| 418 |
from the start on the left up to this value). For a better orientation |
| 419 |
the absolute values should be sorted. In case your data set is not already, |
| 420 |
change the property L<sort|Chart::Manual::Properties/sort> to C<'true'>. |
| 421 |
(Note that the days of the week are not in chronological order, |
| 422 |
but in the order of decreasing sale amounts.) The first given data set |
| 423 |
is like in most cases the domain (x-axis labels). So the color of the first |
| 424 |
data set containing numbers has the color of |
| 425 |
L<dataset0|Chart::Manual::Properties/colors-datasetx> and the accumulation |
| 426 |
gets the color of dataset1 (which are per default also red and green, |
| 427 |
but in reverse order). We choose a Pantone Report designer red, which |
| 428 |
sticks out but is not too shrill. |
| 429 |
|
| 430 |
For better optics we set L<spaced_bars|Chart::Manual::Properties/spaced_bars> |
| 431 |
off, so that the bars touch each other and give a nice counterweight to |
| 432 |
the red color of the line. It's also a bit nicer, when the red labels |
| 433 |
above the red line don't stick out the chart, so we increased the |
| 434 |
L<max_val|Chart::Manual::Properties/max_val> from 5180 to 5500. Finally |
| 435 |
to prevent the y-axis from overcrowding, we set labels (and y_grid_lines) |
| 436 |
only every 250 by setting L<skip_int_ticks|Chart::Manual::Properties/skip_int_ticks> |
| 437 |
and activating L<integer_ticks_only|Chart::Manual::Properties/integer_ticks_only>. |
| 438 |
|
| 439 |
use Chart::Pareto; |
| 440 |
|
| 441 |
my $g = Chart::Pareto->new( 450, 400 ); |
| 442 |
$g->add_dataset( 'Mo', 'Tue', 'We', 'Th', 'Fr', 'Sa', 'Su' ); |
| 443 |
$g->add_dataset( 2500, 1000, 250, 700, 100, 610, 20 ); |
| 444 |
$g->set( |
| 445 |
title => 'Sold Tickets for Beethovens 9th', |
| 446 |
y_label => 'Sold Tickets', |
| 447 |
x_label => '! sold out in the first week !', |
| 448 |
sort => 'true', |
| 449 |
max_val => 5500, |
| 450 |
integer_ticks_only => 'true', |
| 451 |
skip_int_ticks => 250, |
| 452 |
y_grid_lines => 'true', |
| 453 |
spaced_bars => 'false', |
| 454 |
legend => 'none', |
| 455 |
colors => { |
| 456 |
title => 'darkblue', |
| 457 |
dataset0 => 'green', |
| 458 |
dataset1 => 'aurorared', |
| 459 |
x_label => 'aurorared', |
| 460 |
y_grid_lines => 'white', |
| 461 |
}, |
| 462 |
); |
| 463 |
$g->png("pareto.png"); |
| 464 |
|
| 465 |
|
| 466 |
=head2 Pie |
| 467 |
|
| 468 |
=for HTML <p> |
| 469 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/pie.png" alt="pie chart"> |
| 470 |
</p> |
| 471 |
|
| 472 |
The class Chart::Pie creates a pie or ring chart. The first added data |
| 473 |
set must contain the labels and the second set the values. Our example |
| 474 |
displays a ring chart with a thickness of 35% (of the radius). If the |
| 475 |
L<ring|Chart::Manual::Properties/ring> property is omitted, the chart form |
| 476 |
falls back to regular pie. Every ring slice is labeled with the stated label, |
| 477 |
plus the percentage of its value, as defined with the property |
| 478 |
L<label_values|Chart::Manual::Properties/label_values>. Connecting lines |
| 479 |
between label and slice are drawn because L<legend_lines|Chart::Manual::Properties/legend_lines> |
| 480 |
is set to C<'true'>. The actual legend is placed on the bottom, in order |
| 481 |
to leave the ring as large as possible. The legend again shows the |
| 482 |
association between color and data point and its value because |
| 483 |
L<legend_label_values|Chart::Manual::Properties/legend_label_values> |
| 484 |
is set to C<'values'>. Unlike other chart types, where one data set is |
| 485 |
correlated with a color, here every slice has to have its own color. |
| 486 |
Thats why the first data point has the color of L<dataset0|Chart::Manual::Properties/colors-datasetx> |
| 487 |
the second of dataset1 and so forth. In most cases the default colors |
| 488 |
are good enough, unless you have special meanings in mind. Please also |
| 489 |
note the multi line (row) title text. |
| 490 |
|
| 491 |
|
| 492 |
use Chart::Pie; |
| 493 |
|
| 494 |
my $g = Chart::Pie->new( 500, 450 ); |
| 495 |
$g->add_dataset( qw/eins zwei drei vier fuenf sechs sieben acht neun zehn/ ); |
| 496 |
$g->add_dataset( 120, 50, 100, 80, 40, 45, 150, 60, 110, 50 ); |
| 497 |
$g->set( 'title' => 'Pie\nDemo Chart', |
| 498 |
'legend' => 'bottom', |
| 499 |
'legend_label_values' => 'value', |
| 500 |
'label_values' => 'percent', |
| 501 |
'legend_lines' => 'true', |
| 502 |
'ring' => 0.35, |
| 503 |
); |
| 504 |
$g->png("pie.png"); |
| 505 |
|
| 506 |
|
| 507 |
=head2 Points |
| 508 |
|
| 509 |
=for HTML <p> |
| 510 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/points.png" alt="xy chart with points"> |
| 511 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/test/points_5.png" alt="point styles demo"> |
| 512 |
</p> |
| 513 |
|
| 514 |
The class Chart::Points creates a xy-chart (also called scattergram), |
| 515 |
where the individual data points are marked with a symbol. |
| 516 |
The shape of the symbol is selected by the property L<brushStyle|Chart::Manual::Properties/brushStyle>, |
| 517 |
which was not utilized in this example, in order to use the default shape: |
| 518 |
a circle of a diameter set by L<pt_size|Chart::Manual::Properties/pt_size>. |
| 519 |
All shapes can be seen in the demo above, right. |
| 520 |
(If you want in addition lines, connecting the points, check L</LinesPoints>.) |
| 521 |
|
| 522 |
The first data set comprises the domain set, displayed on the x-axis. |
| 523 |
L<precision|Chart::Manual::Properties/precision> set to zero cuts the |
| 524 |
decimals on the y-axis labels and keeps it clean. The method C<add_pt> |
| 525 |
appends to every data set another value, adding another column to the chart. |
| 526 |
L<png_border|Chart::Manual::Properties/png_border> does just adds frame |
| 527 |
of 10 pixel width around the entire image. They middle gray grid lines |
| 528 |
are just easy for the eyes. |
| 529 |
|
| 530 |
use Chart::Points; |
| 531 |
|
| 532 |
my $g = Chart::Points->new(); |
| 533 |
$g->add_dataset( 'foo', 'bar', 'blank' ); |
| 534 |
$g->add_dataset( 3, 4, 9 ); |
| 535 |
$g->add_dataset( 8, 6, 0 ); |
| 536 |
$g->add_dataset( 5, 7, 2 ); |
| 537 |
$g->add_pt( 'dat', 1, 5, 7 ); |
| 538 |
$g->set( |
| 539 |
title => 'Points Chart', |
| 540 |
pt_size => 18, |
| 541 |
# brushStyle => 'Star', |
| 542 |
precision => 0, |
| 543 |
grid_lines => 'true', |
| 544 |
png_border => 10, |
| 545 |
colors => { |
| 546 |
grid_lines => 'gray70', |
| 547 |
|
| 548 |
}, |
| 549 |
); |
| 550 |
$g->png("points.png"); |
| 551 |
|
| 552 |
|
| 553 |
=head2 Split |
| 554 |
|
| 555 |
=for HTML <p> |
| 556 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/split.png" alt="multi chart"> |
| 557 |
</p> |
| 558 |
|
| 559 |
The class Chart::Split creates a L</Lines> chart where both x and y axes |
| 560 |
are assumed to be numeric. Split charts are mainly intended for cases |
| 561 |
where many data points are spread over a wide x range while at the |
| 562 |
same time the y range is limited. Typical examples are weather or |
| 563 |
seismic data. The x axis will be split into several intervals of the |
| 564 |
same length (specified with the mandatory option L<interval|Chart::Manual::Properties/interval> |
| 565 |
and starting at L<start|Chart::Manual::Properties/start>). Chart::Split will |
| 566 |
draw only positive x coordinates. The y axis will not be labelled with |
| 567 |
the y values. Rather, the axis will show only the sequence numbers |
| 568 |
of the intervals. |
| 569 |
|
| 570 |
use Chart::Split; |
| 571 |
|
| 572 |
my $g = Chart::Split->new( 500, 500 ); |
| 573 |
|
| 574 |
my @domain = 1 .. 4000; |
| 575 |
my @rnd = map { srand( time() / $_ * 50 ); rand(10) } @domain, 4001; |
| 576 |
my @diff = map { abs $rnd[$_-1] - $rnd[$_] } @domain; |
| 577 |
pop @rnd; |
| 578 |
|
| 579 |
$g->add_dataset(@domain); |
| 580 |
$g->add_dataset(@rnd); |
| 581 |
$g->add_dataset(@diff); |
| 582 |
$g->set( |
| 583 |
title => "Random Numbers Test", |
| 584 |
x_label => "4000 Random Numbers", |
| 585 |
start => 0, |
| 586 |
interval => 400, |
| 587 |
brush_size => 1, |
| 588 |
interval_ticks => 0, |
| 589 |
legend => 'bottom', |
| 590 |
legend_labels => ['random numbers', 'difference' ], |
| 591 |
colors => { |
| 592 |
title => 'darkblue', |
| 593 |
text => 'gray45', |
| 594 |
misc => 'gray45', |
| 595 |
}, |
| 596 |
); |
| 597 |
$g->png("split.png"); |
| 598 |
|
| 599 |
|
| 600 |
=head2 StackedBars |
| 601 |
|
| 602 |
=for HTML <p> |
| 603 |
<img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/manual/stackedbars.png" alt="stacked bar chart"> |
| 604 |
</p> |
| 605 |
|
| 606 |
The class Chart::StackedBars is a variant of L</Bars> that stacks |
| 607 |
bars belonging to one x-value on top of each other, instead of putting |
| 608 |
them beside each other. Data sets 0..n are ordered from the bottom up. |
| 609 |
They are in most cases more intuitive than L</Pie> charts, because its |
| 610 |
easier to intuit linear than quadratic ratios. As in the Bars example we |
| 611 |
activated horizontal grid lines, which were subtle colored. To surpress |
| 612 |
decimals on the y-axis L<precision|Chart::Manual::Properties/precision> was turned down. |
| 613 |
And as in most cases - the first data set is the domain, which will be |
| 614 |
drawn as x-axis labels. |
| 615 |
|
| 616 |
use Chart::StackedBars; |
| 617 |
|
| 618 |
my $g = Chart::StackedBars->new( 600, 400 ); |
| 619 |
$g->add_dataset( 'camel', 'dromedar', 'llama', 'vicuna'); |
| 620 |
$g->add_dataset( 3, 4, 9, 10, ); |
| 621 |
$g->add_dataset( 8, 6, 1, 12, ); |
| 622 |
$g->add_dataset( 5, 7, 2, 13, ); |
| 623 |
$g->set( |
| 624 |
title => 'Stacked Bars', |
| 625 |
legend => 'left', |
| 626 |
precision => 0, |
| 627 |
y_grid_lines => 'true', |
| 628 |
grey_background => 'false', |
| 629 |
colors => { |
| 630 |
grid_lines => 'gray80', |
| 631 |
misc => 'gray55', |
| 632 |
text => 'gray55', |
| 633 |
x_label => 'gray40', |
| 634 |
y_label => 'gray40', |
| 635 |
title => 'gray20', |
| 636 |
} |
| 637 |
); |
| 638 |
$g->png("stackedbars.png"); |
| 639 |
|
| 640 |
|
| 641 |
=head1 COPYRIGHT & LICENSE |
| 642 |
|
| 643 |
Copyright 2022 David Bonner, Herbert Breunung. |
| 644 |
|
| 645 |
This program is free software; you can redistribute it and/or modify it |
| 646 |
under same terms as Perl itself. |
| 647 |
|
| 648 |
=head1 AUTHOR |
| 649 |
|
| 650 |
David Bonner, |
| 651 |
|
| 652 |
Herbert Breunung, <[email protected]> |
| 653 |
|
| 654 |
|
| 655 |
=cut |