This object holds data about the different types of elements that can be added to a Plots graph. This is a hash with some helper methods. Data objects are created and modified using the Plots methods, and do not need to generally be modified in a PG problem. Each PG add method returns the related data object which can be used if needed.
Each data object contains the following:
The name is used to identify what type of data is being stored, such as a function, dataset, label, etc.
The array of the data points x-value.
The array of the data points y-value.
A function (stored as a hash) to generate the x and y data points.
An hash of different style options and values that can be used to store additional data for things like color, width, etc.
The main methods for adding data and accessing the data are:
$data->name
Sets, $data->name($string)
, or gets $data->name
the name of the data object.
$data->add
Adds a single data point, $data->add($x, $y)
, or adds multiple data points, $data->add([$x1, $y1], [$x2, $y2], ..., [$xn, $yn])
.
$data->set_function
Configures a function to generate data points. Fx
and Fy
are strings (which are turned into MathObjects), MathObjects, or per subroutines. The core function data is stored in the $data->{function}
hash, though other data is stored as a style.
$data->set_function(
$self->context,
Fx => Formula('t'),
Fy => Formula('t^2'),
var => 't',
min => -5,
max => 5,
steps => 50,
);
Note, the first argument must be $self->context when called from Plots::Plot
to use a single context for all Plost::Data
objects.
This is also used to set a two variable function (used for slope or vector fields):
$data->set_function(
$self->context,
Fx => Formula('x^2 + y^2'),
Fy => Formula('x - y'),
xvar => 'x',
yvar => 'y',
xmin => -5,
xmax => 5,
ymin => -5,
ymax => 5
xsteps => 15,
ysteps => 15,
);
Note a function always stores the coordinate variables as xmin
, xmax
, xvar
, etc. When using a single variable function just use the x-coordinate values. min
, max
, var
, steps
, will set the x-coordinate values and will override any xmin
, xmax
, etc settings.
$data->gen_data
Generate the data points from a function. This can only be done when there is no data, so once the data has been generated this will do nothing (to avoid generating data again).
$data->size
Returns the current number of points being stored.
$data->x
and $data->y
Without any inputs, these return either the x array or y array of data points being stored. A single input can be used to return only the n-th data point, $data->x($n)
.
$data->style
Sets or gets style information. Use $data->style($name)
to get the style value of a single style name. $data->style
will returns a reference to the full style hash. Last, input a hash to add / change the styles.
$data->style(color => 'blue', width => 3);
$str = $data->function_string($formula, $type, $xvar, $yvar);
Takes a MathObject $formula
and replaces the function with either a JavaScript or PGF function string. If the function contains any function tokens not supported, a warning and empty string is returned.
$formula The mathobject formula object, either $self->{function}{Fx} or $self->{function}{Fy}.
$type 'js' or 'PGF' (falls back to js for any input except 'PGF').
$xvar The x-variable name, $self->{function}{xvar}.
$yvar The y-variable name, $self->{function}{yvar}, for vector fields.
Leave undefined for single variable functions.
$data->update_min_max
Updates a functions xmin
, xmax
, ymin
, and ymax
values to reals using MathObjects. This allows end points like 'pi', 'e', etc.
$data->get_start_point
Gets the starting (left end) point of a function. This should be used when using function strings to avoid generating the function data.
$data->get_end_point
Gets the ending (right end) point of a function. This should be used when using function strings to avoid generating the function data.