An example on usage
For example is it often necessary to plot som (x,y) values where x are dates and
maybe the y values represent the percentage of system load as an average on the
specified date. Then we would need to plot values of the kind (7 Aug 2004, 24.4%),
(8 Aug 2004, 86.2%), (9 Aug 2004, 5.7%). But the way we add points does not support
the "formatting" of the values, i.e. it is not possible to do:
- // THIS WON'T WORK!
-
- ...
- $DataSet->addPoint("7 Aug 2004", "24.4%");
- $DataSet->addPoint("8 Aug 2004", "86.2%")
- $DataSet->addPoint("9 Aug 2004", "5.7%")
- ...
The reason is that the DataSet requires (x, y) values to be numerical.
The solution to this is to use the DataPreprocessor and its sub-classes.
There are a couple of ways to do this, but well describe in details the use of
ArrayData for the X-values and FormattedData for the Y-values.
Using ArrayData
To use the ArrayData method you would map the values on to the "displayed" value.
This can be done in the following way:
- ...
- // THIS WON'T WORK!
-
- $XArray[0] = "7 Aug 2004"; // there is of course no need to actually hardcode this
- $XArray[1] = "8 Aug 2004";
- $XArray[2] = "9 Aug 2004";
-
- $DataSet->addPoint(0, 24.4);
- $DataSet->addPoint(1, 86.2);
- $DataSet->addPoint(2, 5.7);
- ...
- $PlotArea->AxisX->setDataPreprocessor(new ArrayData($XArray));
- ...
This will cause the X-axis to display x-labels mapped on to the $XArray, i.e.
if a label is to be printed for the "internal" x-value 0, the ArrayData
will then "catch" the value before it is printed on the axis and translate it from
0 to "7 Aug 2004" and print this value instead. Likewise for other x-values.
Note however that using this way to display X-axis values will require the (@link Axis}
labels to cooincide with an index within the array, i.e. if you do the following:
- ...
- $XArray[] = "7 Aug 2004"; // there is of course no need to actually hardcode this
- $XArray[] = "8 Aug 2004";
- $XArray[] = "9 Aug 2004";
-
- $DataSet->addPoint(10, 24.4);
- $DataSet->addPoint(20, 86.2);
- $DataSet->addPoint(30, 5.7);
- ...
- $PlotArea->AxisX->setDataPreprocessor(new ArrayData($XArray));
- ...
The X-values (10, 20, 30) are not found as indices in the $XArray and will therefor
be printed as values, i.e. 10, 20 and 30.
Using FormattedData
To enable the Y-values to be displayed as percentages (i.e. 86.2% instead of "just"
86.2) we can use the FormattedData preprocessor.
This method basically uses the sprintf function
to format the output. The usage is in priciple very simple:
- ...
- $PlotArea->AxisY->setDataPreprocessor(new FormattedData("%0.1f%%"));
- ...
This will cause all Y-axis values to be formatted using the format. Another usage
could be in the Marker functionality. For example:
- ...
- $Marker =& new ValueMarker(VALUE_Y);
- $Marker->setDataPreprocessor(new FormattedData("%0.1f%%"));
- $PlotType->setMarker($Marker);
- ...
This would cause the graph to display Y-values (ValueMarker)
as markers on the graph in the specified format (i.e. 86.2%).
To summarize: a good way of achieving the above would be to do the following:
- $Graph =& new GraPHP(400, 300);
- $Graph->add(new PlotArea(), "PlotArea");
-
- $DataSet =& new TrivialDataSet();
-
- $XArray[0] = "7 Aug 2004";
- $XArray[1] = "8 Aug 2004";
- $XArray[2] = "9 Aug 2004";
-
- $DataSet->addPoint(0, 24.4);
- $DataSet->addPoint(1, 86.2);
- $DataSet->addPoint(2, 5.7);
-
- $PlotArea->addPlot(new LineChart($DataSet), "PlotType");
-
- $Marker =& new ValueMarker(VALUE_Y);
- $Marker->setDataPreprocessor(new FormattedData("%0.1f%%"));
- $PlotType->setMarker($Marker);
-
- $PlotArea->AxisX->setDataPreprocessor(new ArrayData($XArray));
-
- $Graph->Done();
Actually these results could be achieved using other DataPreprocessor's, fx.
DateData or FunctionData.