Creating the dataset
After creating (or before) the PlotArea and GraPHP, but before (or in the same
"command) you create the specific plot, you need to specify the data to use. This is done using
the dataset classes. How to use the datasets depends on the specific type of set. They
are all demonstrated below, starting with the most simple.
Trivial Dataset
The TrivialDataSet is basically an array which hold (X, Y) data pairs. All data are added
sequentially using DataSet::addPoint(). See the example below:
- $DataSet =& new TrivialDataSet(); // create the dataset
- $DataSet->addPoint(0, 10); // add point (0, 10)
- $DataSet->addPoint(1, 17); // add point (1, 17)
- $DataSet->addPoint("Dog", pi()); // add point (Dog, 3.14159...)
- ...
Random Dataset
Another dataset is one used especially for demonstrational purposes, sinces it provide no
real information, the RandomDataSet. Data points (X, Y) are X as
sequential and Y as a random value.
- $DataSet =& new RandomDataSet(100, 10, 200);
- // generate a random 100 points between 10 and 200
- ...
Function Dataset
It is (often) very usefull to visualize a mathematical function on a graph. To do this
the FunctionDataSet is provided. The data points (X, Y) are
evaluated as X is sequential and Y is the function applied to the X value, ie Y = f(X).
The function needs to be a single parameter function and return a single primitive value,
like for instance sin, cos, etc.
The dataset below is an example of how to use a standard function.
- $DataSet =& new FunctionDataSet(0, 20, "sin", 100);
- // generate a 100 points on the sine curve between 0 and 20
- ...
The dataset below is an example of how to use a user function.
- function aFunc($X) {
- return sin($X)+cos($X)*$X;
- }
-
- $DataSet =& new FunctionDataSet(-10, 10, "aFunc", 100);
- // generate a 100 points using the aFunc function between -10 and 10
- ...
The dataset below is demonstrates the same as the above but using create_function().
- $DataSet =& new FunctionDataSet(-10, 10,
- create_function('$X', 'return sin($X)+cos($X)*$X;'), 100);
- // generate a 100 points using the aFunc function between -10 and 10
- ...
Vector Function Dataset
The VectorFunctionDataSet is in many ways the same as
the FunctionDataSet. The main (and basically the only) difference
is that the (X, Y) data is calculated as (fx($T), fy($T)) instead of ($X, f($X)).
The dataset below is an example of how to use a vector function.
- $DataSet =& new VectorFunctionDataSet(0, 2*pi(), "cos", "sin", 100);
- // generate a 100 points on the (cos, sin) curve between 0 and 2*pi
- // this will produce a perfect circle
-
- ...
The dataset below is an example of how to use a vector function with 2 user functions.
- function tcost($t) { return $t*cos($t); }
- function tsint($t) { return $t*sin($t); }
- $DataSet =& new VectorFunctionDataSet(0, 20, "tcost", "tsint", 100);
- // generate a 100 points on the (cos, sin) curve between 0 and 20
- // this will produce a spiral with center in (0, 0)
-
- ...
Similarly to the function dataset it's possible to use the create_function() to specify
the function-parameters to use.