PostScript::Graph::XY - graph lines and points |
sequence()
PostScript::Graph::XY - graph lines and points
Draw a graph from data in the CSV file 'results.csv', and saves it as 'results.ps':
use PostScript::Graph::XY;
my $xy = new PostScript::Graph::XY(); $xy->build_chart("results.csv"); $xy->output("results");
=head2 Typical
With more direct control:
use PostScript::Graph::XY; use PostScript::Graph::Style;
my $seq = PostScript::Graph::Sequence; $seq->setup('color', [ [ 1, 1, 0 ], # yellow [ 0, 1, 0 ], # green [ 0, 1, 1 ], ], # cyan );
my $xy = new PostScript::Graph::XY( file => { errors => 1, eps => 0, landscape => 1, paper => 'Letter', },
layout => { dots_per_inch => 72, heading => "Example", background => [ 0.9, 0.9, 1 ], heavy_color => [ 0, 0.2, 0.8 ], mid_color => [ 0, 0.5, 1 ], light_color => [ 0.7, 0.8, 1 ], },
x_axis => { smallest => 4, title => "Control variable", font => "Courier", }, y_axis => { smallest => 3, title => "Dependent variable", font => "Courier", },
style => { auto => [qw(color dashes)], color => 0, line => { inner_width => 2, outer_width => 2.5, outer_dashes => [], }, point => { shape => "circle", size => 8, color => [ 1, 0, 0 ], }, },
key => { background => 0.9, }, );
$xy->line_from_array( [ [ qw(Control First Second Third Fourth), qw(Fifth Sixth Seventh Eighth Nineth)], [ 1, 0, 1, 2, 3, 4, 5, 6, 7, 8 ], [ 2, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], [ 3, 2, 3, 4, 5, 6, 7, 8, 9,10 ], [ 4, 3, 4, 5, 6, 7, 8, 9,10,11 ], ] ); $xy->build_chart(); $xy->output("controlled");
=head2 All options
$xy = new PostScript::Graph::XY( file => { # see PostScript::File }, layout => { # see PostScript::Graph::Paper }, x_axis => { # see PostScript::Graph::Paper }, y_axis => { # see PostScript::Graph::Paper }, style => { # see PostScript::Graph::Style }, key => { # see PostScript::Graph::Key }, chart => { # see 'new' below }, );
=head1 DESCRIPTION
A graph is drawn on a PostScript file from one or more sets of numeric data. Scales are automatically adjusted for each data set and the style of lines and points varies between them. A title, axis labels and a key are also provided.
options
may be either a list of hash keys and values or a hash reference. Either way, the hash should have the
same structure - made up of keys to several sub-hashes. Only one (chart) holds options for this module. The
other sections are passed on to the appropriate module as it is created.
Hash Key Module ======== ====== file PostScript::File layout PostScript::Graph::Paper x_axis PostScript::Graph::Paper y_axis PostScript::Graph::Paper style PostScript::Graph::Style key PostScript::Graph::Key chart this one, see below
This may be either an array or the name of a CSV file. See line_from_array or line_from_file for details. If data is given here, the chart is built automatically. There is no opportunity to add extra lines (they should be included in this data) but there is no need to call build_chart explicitly as the chart is ready for output.
Set to 0 if key panel is not required. (Default: 1)
Set to 0 to hide lines and make a scatter graph. (Default: 1)
Set to 0 to hide points. (Default: 1)
All the settings are optional and the defaults work reasonably well. See the other PostScript manpages for details of their options.
One or more lines of data is added to the chart. This may be called many times before the chart is finalized with build_chart.
Each position is the data array contains an x value and one or more y values. For example, the following points will be plotted on an x axis from 2 to 4 a y axis including from 49 to 57.
[ [ 2, 49.7 ], [ 3, 53.4 ], [ 4. 56.1 ], ]
This will plot three lines with 6 points each.
[ ["X", "Y", "Yb", "Yc"], [x0, y0, yb0, yc0], [x1, y1, yb1, yc1], [x2, y2, yb2, yc2], [x3, y3, yb3, yc3], [x4, y4, yb4, yc4], [x5, y5, yb5, yc5], ]
The first line is made up of (x0,y0), (x1,y1)... and these must be there. The second line comes from (x0,yb0), (x1,yp1)... and so on. Optionally, the first row of data in the array may be labels for the X and Y axis, and then for each line.
Where multiple lines are given, it is best to specify label
as an option. Otherwise it will default to the
name of the first line - rarely what you want. Of course this is ignored if the new option 'y_axis => title'
was given.
file
label
opts
style
The comma seperated file should contain data in the form:
x0, y0 x1, y1 x2, y2
Optionally, the first line may hold labels. Any additional columns are interpreted as y-values for additional lines. For example:
Volts, R1k2, R1k8, R2k2 4.0, 3.33, 2.22, 1.81 4.5, 3.75, 2.50, 2.04 5.0, 4.16, 2.78, 2.27 5.5, 4.58, 3.05, 2.50
Where multiple lines are given, it is best to specify label
as an option. Otherwise it will default to the
name of the first line - rarely what you want. Of course the new option 'y_axis => title' takes precedence
over both.
Note that the headings have to begin with a non-digit in order to be recognized as such.
If the first parameter is an array they are all passed to line_from_array, otherwise if there are any parameters they are passed to line_from_file. With no parameters, either of these two functions must have already been called.
This method then calculates the scales from the data collected, draws the graph paper, puts the lines on it and adds a key.
Return the underlying PostScript::File object.
Return the underlying PostScript::Graph::Key object. Only available after a call to build_chart.
Return the underlying PostScript::Graph::Paper object. Only available after a call to build_chart.
sequence()
Return the style sequence being used. This is only required when you wish to alter the ranges used by the auto style feature.
Output the chart as a file. See output in the PostScript::File manpage.
Start a new page in the underlying PostScript::File object. See newpage in the PostScript::File manpage and set_page_label in the PostScript::File manpage.
Add functions to the underlying PostScript::File object. See add_function in the PostScript::File manpage for details.
Add postscript code to the underlying PostScript::File object. See add_to_page in the PostScript::File manpage for details.
The PostScript functions are provided as a class method so they are available to modules not needing an XY object.
This is still alpha software. It has only been tested in limited, predictable conditions and the interface is subject to change.
Chris Willmot, chris@willmot.org.uk
the PostScript::File manpage, the PostScript::Graph::Style manpage, the PostScript::Graph::Key manpage, the PostScript::Graph::Paper manpage, the PostScript::Graph::Bar manpage, the Finance::Shares::Chart manpage.
PostScript::Graph::XY - graph lines and points |