blib/lib/Chart.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod n/a totaltotal 0.0% 0 / 57 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use v5.12;
2  
3 package Chart;
4 our $VERSION = 'v2.403.9';
5  
6 use Chart::Points;
7 use Chart::Lines;
8 use Chart::LinesPoints;
9 use Chart::Mountain;
10  
11 use Chart::ErrorBars;
12 use Chart::Direction;
13 use Chart::Split;
14 use Chart::Pareto;
15 use Chart::Composite;
16  
17 use Chart::Bars;
18 use Chart::HorizontalBars;
19 use Chart::StackedBars;
20  
21 use Chart::Pie;
22  
23 1;
24  
25 =pod
26  
27 =head1 NAME
28  
29 Chart - a series of charting modules
30  
31 =head1 SYNOPSIS
32  
33 use Chart::type; (type is one of: Points, Lines, Bars, LinesPoints, Composite,
34 StackedBars, Mountain, Pie, HorizontalBars, Split, ErrorBars, Pareto, Direction)
35  
36 $obj = Chart::type->new;
37 $obj = Chart::type->new ( $png_width, $png_height );
38  
39 $obj->set ( $key_1, $val_1, ... ,$key_n, $val_n );
40 $obj->set ( $key_1 => $val_1,
41 ...
42 $key_n => $val_n );
43 $obj->set ( %hash );
44  
45 # GIFgraph.pm-style API to produce png formatted charts
46 @data = ( \@x_tick_labels, \@dataset1, ... , \@dataset_n );
47 $obj->png ( "filename", \@data );
48 $obj->png ( $filehandle, \@data );
49 $obj->png ( FILEHANDLE, \@data );
50 $obj->cgi_png ( \@data );
51  
52 # Graph.pm-style API
53 $obj->add_pt ($label, $val_1, ... , $val_n);
54 $obj->add_dataset ($val_1, ... , $val_n);
55 $obj->png ( "filename" );
56 $obj->png ( $filehandle );
57 $obj->png ( FILEHANDLE );
58 $obj->cgi_png ();
59  
60 The similar functions are available for j-peg
61  
62 # Retrieve image map information
63 $obj->set ( 'imagemap' => 'true' );
64 $imagemap_ref = $obj->imagemap_dump ();
65  
66  
67 =head1 DESCRIPTION
68  
69 Chart helps you to create PNG and JPG images with visualizations of numeric data.
70 This page gives you a summary how to use it.
71 For a more thorough documentation and lots of example code please visit the L<Chart::Manual>.
72  
73  
74 =for HTML <p>
75 <img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/landing/points.png" alt="point chart" width="300" height="225">
76 <img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/landing/composite.png" alt="composite of bars and lines" width="300" height="225">
77 <img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/landing/sbars.png" alt="stacked bars" width="300" height="200">
78 <img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/landing/bars.png" alt="multi bar chart" width="300" height="250">
79 <img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/landing/hbars.png" alt="horizontal bar chart" width="300" height="240">
80 <img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/landing/polar.png" alt="polar chart" width="250" height="250">
81 <img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/landing/ring.png" alt="pie chart" width="250" height="225">
82 <img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/landing/mountain.png" alt="mountain chart" width="300" height="225">
83 <img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/landing/split.png" alt="split chart" width="250" height="250">
84 <img src="https://raw.githubusercontent.com/lichtkind/Chart/main/dev/example/landing/error.png" alt="error bar chart" width="300" height="225">
85 </p>
86  
87 =head2 use-ing Chart
88  
89 Okay, so you caught me. There's really no Chart::type module.
90 All of the different chart types (Points, Lines, Bars, LinesPoints,
91 Composite, StackedBars, Pie, Pareto, HorizontalBars, Split, ErrorBars,
92 Direction and Mountain so far) are classes by themselves, each inheriting
93 a bunch of methods from the Chart::Base class. Simply replace
94 the word type with the type of chart you want and you're on your way.
95 For example,
96  
97 use Chart::Lines;
98  
99 would invoke the lines module.
100 Alternatively load all chart types at ones and write:
101  
102 use Chart;
103  
104 =head2 Getting an object
105  
106 The new method can either be called without arguments, in which
107 case it returns an object with the default image size (400x300 pixels),
108 or you can specify the width and height of the image. Just remember
109 to replace type with the type of graph you want. For example,
110  
111 $obj = Chart::Bars->new (600,400);
112  
113 would return a Chart::Bars object containing a 600x400 pixel
114 image. New also initializes most of the default variables, which you
115 can subsequently change with the set method.
116  
117  
118 =head2 Setting different options
119  
120 This is where the fun begins. Set looks for a hash of keys and
121 values. You can pass it a hash that you've already constructed, like
122  
123 %hash = ( property_name => 'new value' );
124 $obj->set (%hash);
125  
126 or you can try just constructing the hash inside the set call, like
127  
128 $obj->set ( property_name => 'new value' );
129  
130  
131 L<Chart::Manual::Properties> lists all currently supported keys and values.
132  
133  
134 =head2 GIFgraph.pm-style API
135  
136 =over 4
137  
138 =item Sending the image to a file
139  
140 Invoking the png method causes the graph to be plotted and saved to
141 a file. It takes the name of the output file and a reference to the
142 data as arguments. For example,
143  
144 $obj->png ("foo.png", \@data);
145  
146 would plot the data in @data, and the save the image to foo.png.
147 Of course, this then beggars the question "What should @data look
148 like?". Well, just like GIFgraph, @data should contain references
149 to arrays of data, with the first array reference pointing to an
150 array of x-tick labels. For example,
151  
152 @data = ( [ 'foo', 'bar', 'junk' ],
153 [ 30.2, 23.5, 92.1 ] );
154  
155 would set up a graph with one dataset, and three data points in that
156 set. In general, the @data array should look something like
157  
158 @data = ( \@x_tick_labels, \@dataset1, ... , \@dataset_n );
159  
160 And no worries, I make my own internal copy of the data, so that it doesn't
161 mess with yours.
162  
163 =item CGI and Chart
164  
165 Okay, so you're probably thinking, "Do I always have to save these images
166 to disk? What if I want to use Chart to create dynamic images for my
167 web site?" Well, here's the answer to that.
168  
169 $obj->cgi_png ( \@data );
170  
171 The cgi_png method will print the chart, along with the appropriate http
172 header, to stdout, allowing you to call chart-generating scripts directly
173 from your html pages (ie. with a <lt>img src=image.pl<gt> HTML tag). The @data
174 array should be set up the same way as for the normal png method.
175  
176 =back
177  
178 =head2 column based API
179  
180 You might ask, "But what if I just want to add a few points to the graph,
181 and then display it, without all those references to references?". Well,
182 friend, the solution is simple. Borrowing the add_pt idea from Matt
183 Kruse's Graph module, you simply make a few calls to the add_pt method,
184 like so:
185  
186 $obj->add_pt ('foo', 30, 25);
187 $obj->add_pt ('bar', 16, 32);
188  
189 Or, if you want to be able to add entire datasets, simply use the add_dataset
190 method:
191  
192 $obj->add_dataset ('foo', 'bar');
193 $obj->add_dataset (30, 16);
194 $obj->add_dataset (25, 32);
195  
196 These methods check to make sure that the points and datasets you are
197 adding are the same size as the ones already there. So, if you have
198 two datasets currently stored, and try to add a data point with three
199 different values, it will carp (per the Carp module) an error message.
200 Similarly, if you try to add a dataset with 4 data points,
201 and all the other datasets have 3 data points, it will carp an error
202 message.
203  
204 Don't forget, when using this API, that I treat the first dataset as
205 a series of x-tick labels. So, in the above examples, the graph would
206 have two x-ticks, labeled 'foo' and 'bar', each with two data points.
207 Pie and ErrorBars handle it different, look at the documentation
208 to see how it works.
209  
210 =over 4
211  
212 =item Adding a datafile
213  
214 You can also add a complete datafile to a chart object. Just use the
215 add_datafile() method.
216  
217 $obj->add_datafile('file', 'set' or 'pt');
218  
219 file can be the name of the data file or a filehandle.
220 'set' or 'pt is the type of the datafile.
221 If the parameter is 'set' then each line in the data file
222 has to be a complete data set. The value of the set has to be
223 separated by white spaces. For example the file looks like this:
224  
225 'foo' 'bar'
226 30 16
227 25 32
228  
229 If the parameter is 'pt', one line has to include all values
230 of one data point separated by white spaces. For example:
231  
232 'foo' 30 25
233 'bar' 16 32
234  
235  
236 =item Clearing the data
237  
238 A simple call to the clear_data method empties any values that may
239 have been entered.
240  
241 $obj->clear_data ();
242  
243 =item Getting a copy of the data
244  
245 If you want a copy of the data that has been added so far, make a call
246 to the get_data method like so:
247  
248 $dataref = $obj->get_data;
249  
250 It returns (you guessed it!) a reference to an array of references to
251 datasets. So the x-tick labels would be stored as
252  
253 @x_labels = @{$dataref->[0]};
254  
255 =item Sending the image to a file
256  
257 If you just want to print this chart to a file, all you have to do
258 is pass the name of the file to the png() method.
259  
260 $obj->png ("foo.png");
261  
262 =item Sending the image to a filehandle
263  
264 If you want to do something else with the image, you can also pass
265 a filehandle (either a typeglob or a FileHandle object) to png, and
266 it will print directly to that.
267  
268 $obj->png ($filehandle);
269 $obj->png (FILEHANDLE);
270  
271  
272 =item CGI and Chart
273  
274 Okay, so you're probably thinking (again), "Do I always have to save these
275 images to disk? What if I want to use Chart to create dynamic images for
276 my web site?" Well, here's the answer to that.
277  
278 $obj->cgi_png ();
279  
280 The cgi_png method will print the chart, along with the appropriate http
281 header, to stdout, allowing you to call chart-generating scripts directly
282 from your html pages (ie. with a <lt>img src=image.pl<gt> HTML tag).
283  
284  
285 =item Produce a png image as a scalar
286  
287 Like scalar_jpeg() the image is produced as a scalar
288 so that the programmer-user can do whatever the heck
289 s/he wants to with it:
290  
291 $obj-scalar_png($dataref)
292  
293  
294  
295 =item Produce a jpeg image as a scalar
296  
297 Like scalar_png() the image is produced as a scalar
298 so that the programmer-user can do whatever the heck
299 s/he wants to with it:
300  
301 $obj-scalar_jpeg($dataref)
302
303 =back
304  
305 =head2 Imagemap Support
306  
307 Chart can also return the pixel positioning information so that you can
308 create image maps from the pngs Chart generates. Simply set the 'imagemap'
309 option to 'true' before you generate the png, then call the imagemap_dump()
310 method afterwards to retrieve the information. You will be returned a
311 data structure almost identical to the @data array described above to pass
312 the data into Chart.
313  
314 $imagemap_data = $obj->imagemap_dump ();
315  
316 Instead of single data values, you will be passed references to arrays
317 of pixel information. For Bars, HorizontalBars and StackedBars charts,
318 the arrays will contain two x-y pairs (specifying the upper left and
319 lower right corner of the bar), like so
320  
321 ( $x1, $y1, $x2, $y2 ) = @{ $imagemap_data->[$dataset][$datapoint] };
322  
323 For Lines, Points, ErrorBars, Split and LinesPoints, the arrays will contain
324 a single x-y pair (specifying the center of the point), like so
325  
326 ( $x, $y ) = @{ $imagemap_data->[$dataset][$datapoint] };
327  
328 A few caveats apply here. First of all, GD treats the upper-left corner
329 of the png as the (0,0) point, so positive y values are measured from the
330 top of the png, not the bottom. Second, these values will most likely
331 contain long decimal values. GD, of course, has to truncate these to
332 single pixel values. Since I don't know how GD does it, I can't truncate
333 it the same way he does. In a worst-case scenario, this will result in
334 an error of one pixel on your imagemap. If this is really an issue, your
335 only option is to either experiment with it, or to contact Lincoln Stein
336 and ask him. Third, please remember that the 0th dataset will be empty,
337 since that's the place in the @data array for the data point labels.
338  
339  
340 =head1 PLAN
341  
342 This module is currently under a complete rebuild, that will take place
343 in two phases. First: rewrite all functionality within a modular
344 architecture and hierarchical property system. This will be accessed
345 via a central API using the so far unutilized Chart module
346 'my $c = Chart->new(...);'. This API will have in part different
347 method and property names, but the old API will not be touched.
348 In a second phase we will see hoch much new code can be used by the old
349 modules and which new features can be brought to the legacy parts,
350 which will be than discouraged, but not scrapped.
351  
352  
353 =head1 TO DO
354  
355 =over 4
356  
357 =item *
358  
359 Include True Type Fonts
360  
361 =item *
362  
363 Violine and Box plots
364  
365 =item *
366  
367 Add some 3-D graphs.
368  
369 =back
370  
371 For more please check the TODO file.
372  
373 =head1 BUGS
374  
375 Probably quite a few, since it's been completely rewritten. As usual,
376 please mail me with any bugs, patches, suggestions, comments, flames,
377 death threats, etc.
378  
379 =head1 AUTHOR
380  
381 David Bonner (dbonner@cs.bu.edu)
382  
383 =head1 MAINTAINER
384  
385 =over 4
386  
387 =item *
388  
389 Chart Group (Chart@fs.wettzell.de)
390  
391 =item *
392  
393 Herbert Breunung (lichtkind@cpan.org)
394  
395 =back
396  
397 =head1 CONTRIBUTORS
398  
399 =over 4
400  
401  
402 =item *
403  
404 Gregor Herrmann (gregoa@debian.org)
405  
406 =item *
407  
408 Chris Dolan (chris+rt@chrisdolan.net)
409  
410 =item *
411  
412 (jarmzet@yahoo.com)
413  
414 =item *
415  
416 Ricardo Signes (rjbs@cpan.org)
417  
418 =item *
419  
420 Petr Pisar (ppisar@redhat.com)
421  
422 =back
423  
424 =head1 COPYRIGHT
425  
426 Copyright(c) 1997-1998 by David Bonner, 1999 by Peter Clark,
427 2001 by the Chart group at BKG-Wettzell.
428 2022 by Herbert Breunung and Chart group
429  
430 All rights reserved. This program is free software; you can
431 redistribute it and/or modify it under the same terms as Perl
432 itself.
433  
434 =cut