The graph components enables you to create line, pie and bar charts. The
output driver mechanism allows you to create different image types from each
chart, and the available renderers make the chart output customizeable from
simple two dimensional charts up to beautiful three dimensional data
projections.
ezcGraph separates different parts of the graph in chart elements, which
display one part of a graph, like the title, the legend or one axis, which are
all independently configurable. This design not only allows you to use
different colors or fonts for each chart element, but also define their
position and size. The main chart elements are the same for all chart types.
For easy definition of a overall layout for your graph you can use palettes,
which define colors, symbols, fonts and spacings.
The data is provided through ezcGraphDataSets which are normally create from
simple arrays, but also can perform statistical operations on the data, as you
will see later.
The following graph is a simple example how to create a pie chart using the
default driver, palette and renderer.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $graph = new ezcGraphPieChart();
6. $graph->title = 'Access statistics';
7.
8. $graph->data['Access statistics'] = new ezcGraphArrayDataSet( array(
9. 'Mozilla' => 19113,
10. 'Explorer' => 10917,
11. 'Opera' => 1464,
12. 'Safari' => 652,
13. 'Konqueror' => 474,
14. ) );
15. $graph->data['Access statistics']->highlight['Opera'] = true;
16.
17. $graph->render( 400, 150, 'tutorial_example_01.svg' );
18.
19. ?>
You just create a new chart object, optionally set a title for the chart
assign the data and render it. To assign data you access the dataset container
like an array to define an identifier for your new created dataset. The
dataset in this example is created from an array, where the keys are used as
the identifiers for the data points.
Pie charts accept only one dataset, and the data point identifiers are used to
create the legend. To generate the output the default SVG renderer is used
with the default 2D renderer. For the automatic colorization colors are
applied from the default Tango palette, which uses colors defined by the Tango
Desktop Project: http://tango.freedesktop.org/Tango_Desktop_Project
There are several preparations on the data sets and data points which will be
mentioned during this tutorial, one thing you can do is highlighting one special
data set or point. In line 15 the data point Opera is highlighted which means
in case of pie charts, that it is moved out of the centre by a user
configurable value. See the renderer options class ezcGraphRendererOptions for
details.
There are several pie chart specific configuration options available. In the
eZ components options are always accessed via public properties. For a full
list of all available options have a look at the ezcGraphPieChartOption class.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $graph = new ezcGraphPieChart();
6. $graph->title = 'Elections 2005 Germany';
7.
8. $graph->data['2005'] = new ezcGraphArrayDataSet( array(
9. 'CDU' => 35.2,
10. 'SPD' => 34.2,
11. 'FDP' => 9.8,
12. 'Die Gruenen' => 8.1,
13. 'PDS' => 8.7,
14. 'NDP' => 1.6,
15. 'REP' => 0.6,
16. ) );
17.
18. $graph->options->label = '%3$.1f%%';
19. $graph->options->sum = 100;
20. $graph->options->percentThreshold = 0.02;
21. $graph->options->summarizeCaption = 'Others';
22.
23. $graph->render( 400, 150, 'tutorial_example_02.svg' );
24.
25. ?>
In line 16 a sprintf format string is set, which defines how the labels are
formatted. Instead of a sprintf format string you could also set a callback
function which takes care of the label formatting.
In this example we set a custom sum to force the pie chart to show the
complete 100%. The percentTreshHold lets the chart collect all data which has
less percent to be aggregated in one data point. We also could define a absolute
threshold, so that all data below a certain value would be aggregated in one
data point, and the summarizeCaption defines the caption for this aggregated
dataset.
Line charts are created the exactly same way as pie charts, only that they
accept more then one dataset. We are using default driver, palette and
renderer again in this example.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4. $wikidata = include 'tutorial_wikipedia_data.php';
5.
6. $graph = new ezcGraphLineChart();
7. $graph->title = 'Wikipedia articles';
8.
9. // Add data
10. foreach ( $wikidata as $language => $data )
11. {
12. $graph->data[$language] = new ezcGraphArrayDataSet( $data );
13. }
14.
15. $graph->render( 400, 150, 'tutorial_example_03.svg' );
16.
17. ?>
There are only two differences compared to the last example. In line we
instantiate an ezcGraphLineChart object instead of a PieChart and beginning in
line 10 we assign multiple datasets from an array we included earlier in the
script. The array in the file tutorial_wikipedia_data.php is build like this:
1. <?php
2. return array(
3. 'English' => array(
4. 'Jan 2006' => 965,
5. 'Feb 2006' => 1000,
6. ...
7. ),
8. ...
9. );
10. ?>
The result is the simple default line chart.
Bar charts are very similar to line charts. They accept the same datasets and
only define another default dataset display type and axis label renderer.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4. $wikidata = include 'tutorial_wikipedia_data.php';
5.
6. $graph = new ezcGraphBarChart();
7. $graph->title = 'Wikipedia articles';
8.
9. // Add data
10. foreach ( $wikidata as $language => $data )
11. {
12. $graph->data[$language] = new ezcGraphArrayDataSet( $data );
13. }
14.
15. $graph->render( 400, 150, 'tutorial_example_04.svg' );
16.
17. ?>
As you can see in line 6 we only change the chart constructor again, and the
other default values are applied.
The only difference between bar and line charts is the display type of the
dataset and the axis label renderer of the x axis. You can use one of those
constructors and modify you chart to display one or more datasets mack to the
other display type. The axis label renderer are described later in this
tutorial.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4. $wikidata = include 'tutorial_wikipedia_data.php';
5.
6. $graph = new ezcGraphBarChart();
7. $graph->title = 'Wikipedia articles';
8.
9. // Add data
10. foreach ( $wikidata as $language => $data )
11. {
12. $graph->data[$language] = new ezcGraphArrayDataSet( $data );
13. }
14. $graph->data['German']->displayType = ezcGraph::LINE;
15.
16. $graph->options->fillLines = 210;
17.
18. $graph->render( 400, 150, 'tutorial_example_05.svg' );
19.
20. ?>
After creating the datasets we modify one of the datasets in line 14 to change
the default display type to ezcGraph::LINE. To be able to recognize the line
we set one graph option in line 16. The options are accessed like public
properties and in this case we set an option for the graph called "fillLines",
which indicates what transparency value is used tu fill up the space between
data lines and the axis.
There are some more options available for line and bar charts, which configure
how the highlighting of data sets is applied to this chart type.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4. $wikidata = include 'tutorial_wikipedia_data.php';
5.
6. $graph = new ezcGraphBarChart();
7. $graph->title = 'Wikipedia articles';
8.
9. // Add data
10. foreach ( $wikidata as $language => $data )
11. {
12. $graph->data[$language] = new ezcGraphArrayDataSet( $data );
13. }
14. $graph->data['German']->displayType = ezcGraph::LINE;
15. $graph->data['German']->highlight = true;
16. $graph->data['German']->highlight['Mar 2006'] = false;
17.
18. $graph->options->fillLines = 210;
19.
20. $graph->options->highlightSize = 12;
21.
22. $graph->options->highlightFont->background = '#EEEEEC88';
23. $graph->options->highlightFont->border = '#000000';
24. $graph->options->highlightFont->borderWidth = 1;
25.
26. $graph->render( 400, 150, 'tutorial_example_06.svg' );
27.
28. ?>
In line 20 the size of the highlight boxes is configured and the lines 22 to
24 change the font configuration for the highlight boxes. The highlighting
basically works the same way as for pie charts, but in line and bar charts it
makes sense to highlight a complete data set and not only one single data
point, due to the fact that there usually more then one data set in line and
bar charts.
ezcGraph offers palettes for the graphs which define the style properties of
the charts elements. The style properties are similar to the ones you know
from CSS:
- color
- background color
- border color
- border width
- padding
- margin
- data set symbols
There are several predefined palettes delivered with ezcGraph and you can
easily modify them or create a palette by yourself.
You can assign each class class extending ezcGraphPalette to the palette
property of your graph. You should do this before adding data sets, because the
datasets request there colors from the palette. If you set the palette after
creating the data sets, the data sets will still use the colors from the
default palette.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4. $wikidata = include 'tutorial_wikipedia_data.php';
5.
6. $graph = new ezcGraphBarChart();
7. $graph->palette = new ezcGraphPaletteBlack();
8. $graph->title = 'Wikipedia articles';
9.
10. // Add data
11. foreach ( $wikidata as $language => $data )
12. {
13. $graph->data[$language] = new ezcGraphArrayDataSet( $data );
14. }
15. $graph->data['German']->displayType = ezcGraph::LINE;
16.
17. $graph->options->fillLines = 210;
18.
19. $graph->render( 400, 150, 'tutorial_example_07.svg' );
20.
21. ?>
The generated output differs quite a lot from the output using the default
tango palette. The palette of course changes the colors of background, data
sets and fonts. Additionally it sets a color for the major and minor grid, and
defines a border width and color for the charts elements, defaults to a serif
font and increases the margin between the chart elements.
You can find a complete list of the available palettes in the class tree.
In the last example we assigned a palette object to the palette property of
the graph. You can of course create and modify the object before assigning it.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4. require_once 'tutorial_custom_palette.php';
5. $wikidata = include 'tutorial_wikipedia_data.php';
6.
7. $graph = new ezcGraphBarChart();
8. $graph->palette = new tutorialCustomPalette();
9. $graph->title = 'Wikipedia articles';
10.
11. // Add data
12. foreach ( $wikidata as $language => $data )
13. {
14. $graph->data[$language] = new ezcGraphArrayDataSet( $data );
15. }
16. $graph->data['German']->displayType = ezcGraph::LINE;
17.
18. $graph->options->fillLines = 210;
19.
20. $graph->render( 400, 150, 'tutorial_example_08.svg' );
21.
22. ?>
The palette object is created in line 6 and we overwrite some of its
properties. An see an overview on all available properties in the class
documentation for the abstract class ezcGraphPalette. In this example we just
set two colors for the automatic colorization of the datasets and three
symbols for data sets.
Since we assign more then two datasets the first assigned color will be reused
for the third data set. You can see the usage of the symbols in the legend and
on the line chart. The line chart displays a symbol for each data point if the
symbol is set to something different then ezcGraph::NO_SYMBOL which was the
default data set symbol in the default palette.
To fit the graphs easily in your corporate identity the easiest way will be
to create your own palette and use it for the graphs you create. To create a
custom palette you can either extend one of the predefined palettes and
overwrite the properties or extend the abstract palette class.
1. <?php
2.
3. class tutorialCustomPalette extends ezcGraphPalette
4. {
5. protected $axisColor = '#000000';
6.
7. protected $majorGridColor = '#000000BB';
8.
9. protected $dataSetColor = array(
10. '#4E9A0688',
11. '#3465A4',
12. '#F57900'
13. );
14.
15. protected $dataSetSymbol = array(
16. ezcGraph::BULLET,
17. );
18.
19. protected $fontName = 'sans-serif';
20.
21. protected $fontColor = '#555753';
22. }
23.
24. ?>
Each color you do not define defaults to a complete transparent white. As you
can see in the example definition you can define a alpha value besides the
normal RGB values for the colors. After creating a custom palette you can use
it like every other palette before.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4. require_once 'tutorial_custom_palette.php';
5. $wikidata = include 'tutorial_wikipedia_data.php';
6.
7. $graph = new ezcGraphBarChart();
8. $graph->palette = new tutorialCustomPalette();
9. $graph->title = 'Wikipedia articles';
10.
11. // Add data
12. foreach ( $wikidata as $language => $data )
13. {
14. $graph->data[$language] = new ezcGraphArrayDataSet( $data );
15. }
16. $graph->data['German']->displayType = ezcGraph::LINE;
17.
18. $graph->options->fillLines = 210;
19.
20. $graph->render( 400, 150, 'tutorial_example_09.svg' );
21.
22. ?>
The example now uses your palette to format the output.
Internally each chart consists of several chart elements which all extends
ezcGraphChartElement. Each of this elements can be configured independently
and represents one of the rendered chart elements. A default chart consists of
the following elements:
- title
- background
- legend
- xAxis
- yAxis
The palette defines the default formatting of the elements. But you cannot
only set foreground and background colors for all the elements, but also
define their position in the chart, or prevent them from being rendered at
all.
Regarding the font configuration we try to fulfill two goals. We want to give
a single point where you should be able to configure the fonts used for the
texts in the chart. On the other hand it should be possible to configure the
used fonts independently for each chart element.
The used solution is, that you can modify the global font configuration by
accessing $graph->options->font, and this takes effect on all chart elements
until you intentionally access the font configuration of one chart element.
The following section shows an example for this.
The chart title element will only be rendered if you manually assign a title.
It can be placed on top or at the bottom of the chart.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $graph = new ezcGraphPieChart();
6. $graph->palette = new ezcGraphPaletteEzBlue();
7. $graph->title = 'Access statistics';
8.
9. $graph->options->font->name = 'serif';
10.
11. $graph->title->background = '#EEEEEC';
12. $graph->title->font->name = 'sans-serif';
13.
14. $graph->options->font->maxFontSize = 8;
15.
16. $graph->data['Access statistics'] = new ezcGraphArrayDataSet( array(
17. 'Mozilla' => 19113,
18. 'Explorer' => 10917,
19. 'Opera' => 1464,
20. 'Safari' => 652,
21. 'Konqueror' => 474,
22. ) );
23.
24. $graph->render( 400, 150, 'tutorial_example_10.svg' );
25.
26. ?>
The chart title is simplest element, so that I can show you some more things
here. In line 9 we change the global font configuration to use a serif font.
In the SCG renderer only the font name is relevant, because it is up to the
client to actually render the bitmap from the defined vector definitions.
In line 11 we access the font configuration of the title element and change it
back to use a sans-serif font. From now on no change on the global font
configuration will affect the titles font configuration. In line 14 we set a
maximum font size, which now only affects the legend and the pie chart
captions.
Besides the font configuration we set an option available for all chart
elements in line 11 - the background color of the current element. This
results in a gray background only for the title element.
With all drivers except the ming (flash) driver you can set background images
with the option to repeat them in the same way you know from CSS definitions.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $graph = new ezcGraphPieChart();
6. $graph->palette = new ezcGraphPaletteEzRed();
7. $graph->title = 'Access statistics';
8.
9. $graph->data['Access statistics'] = new ezcGraphArrayDataSet( array(
10. 'Mozilla' => 19113,
11. 'Explorer' => 10917,
12. 'Opera' => 1464,
13. 'Safari' => 652,
14. 'Konqueror' => 474,
15. ) );
16.
17. $graph->background->image = 'ez.png';
18. $graph->background->position = ezcGraph::BOTTOM | ezcGraph::RIGHT;
19. $graph->background->repeat = ezcGraph::NO_REPEAT;
20.
21. $graph->render( 400, 150, 'tutorial_example_11.svg' );
22.
23. ?>
In line 17 we set a background image, and define it's position in line 18. You
can use every combination of bottom / center / top with left / middle / right
here, and it defaults to center | middle. In line 19 you finally set the type
of repetition of the background image. This can be ezcGraph::NO_REPEAT or a
combination of ezcGraph::HORIZONTAL and ezcGraph::VERTICAL. In this case we
just want a logo to be placed at the bottom right corner of the image.
In the SVG driver the image is in lined using a data URL with the base64
encoded content of the binary image file, so that you do not need to worry
about the locations of your referenced images, when you are using the SVG
output driver.
In the GD driver super sampling is not applied to the images, because they
would look blurred, what you certainly do not want for included bitmap
images.
Of course you could also apply settings on background color, borders, margin
and padding on the background element, too.
The legend is shown by default and automatically generated from the assigned
data. If you want to disable the legend you can do this by just setting it to
false (line 9).
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $graph = new ezcGraphPieChart();
6. $graph->palette = new ezcGraphPaletteEzGreen();
7. $graph->title = 'Access statistics';
8.
9. $graph->legend = false;
10.
11. $graph->data['Access statistics'] = new ezcGraphArrayDataSet( array(
12. 'Mozilla' => 19113,
13. 'Explorer' => 10917,
14. 'Opera' => 1464,
15. 'Safari' => 652,
16. 'Konqueror' => 474,
17. ) );
18.
19. $graph->render( 400, 150, 'tutorial_example_12.svg' );
20.
21. ?>
Besides just hiding the legend you have some more configurations options. It
can be placed at the bottom, left or top in the chart, you can assign a title
for the legend and change the symbol sizes, or the legends size.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $graph = new ezcGraphPieChart();
6. $graph->palette = new ezcGraphPaletteEz();
7. $graph->title = 'Access statistics';
8.
9. $graph->data['Access statistics'] = new ezcGraphArrayDataSet( array(
10. 'Mozilla' => 19113,
11. 'Explorer' => 10917,
12. 'Opera' => 1464,
13. 'Safari' => 652,
14. 'Konqueror' => 474,
15. ) );
16.
17. $graph->legend->position = ezcGraph::BOTTOM;
18. $graph->legend->landscapeSize = .3;
19. $graph->legend->title = 'Legend';
20.
21. $graph->render( 400, 150, 'tutorial_example_13.svg' );
22.
23. ?>
To place the legend at another position of the graph you may set the position
property of the legend like in line 17. If the legend is placed on top or
bottom it will automatically use a landscape format. The space consumed by the
legend is configured by the landscapeSize for the legends in landscape format
and and portraitSize otherwise. The assigned value is the percent of space of
the graph size consumed by the legend. The legend only displays a title if you
manually set it like in line 19.
The axis defines the unit scale in line and bar charts. There are always two
axis - the x-axis and the y-axis, which value ranges are automatically
received from the datasets and are automatically scaled to display adequate
values.
There are different types of values to display - for both, the x-axis and the
y-axis. ezcGraph supports different axis types for different data you give the
graph to render. For normal string keys usually the standard labeled axis is
the right choice. The numeric axis is predestined to display numeric data,
and the date time axis for data associated to dates or times. All of the axis
types can be assigned to each axis.
The labeled axis is default for the x-axis in both - bar and line charts. It
is written to display string labels of datasets and uses the centered label
renderer as default. You saw it already in all the earlier examples with bar
and line charts, but of course it can be used for both axis, too.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4. $wikidata = include 'tutorial_wikipedia_data.php';
5.
6. $graph = new ezcGraphLineChart();
7. $graph->options->fillLines = 210;
8. $graph->options->font->maxFontSize = 10;
9. $graph->title = 'Error level colors';
10. $graph->legend = false;
11.
12. $graph->yAxis = new ezcGraphChartElementLabeledAxis();
13. $graph->yAxis->axisLabelRenderer->showZeroValue = true;
14.
15. $graph->yAxis->label = 'Color';
16. $graph->xAxis->label = 'Error level';
17.
18. // Add data
19. $graph->data['colors'] = new ezcGraphArrayDataSet(
20. array(
21. 'info' => 'blue',
22. 'notice' => 'green',
23. 'warning' => 'orange',
24. 'error' => 'red',
25. 'fatal' => 'red',
26. )
27. );
28.
29. $graph->render( 400, 150, 'tutorial_example_14.svg' );
30.
31. ?>
You could argue, if such a chart is really useful - but it works. Instead of
using numeric values colors are assigned, when creating the data set. The
labeled axis uses the values in the order they are assigned. In line 11 it is
the first time we actually touch the axis label renderer. The axis label
renderer describe how the labels are placed on the axis - the labeled axis
uses the centered axis label renderer by default, which places the labels
centered next to the steps on the axis. The setting in line 11 forces the
renderer to show the zero value, even though it interferes with the axis.
The numeric axis is the default for the y axis. It was build to display
numeric data and find automatically a good scaling for all values you may
assign. Of course you can configure all scaling parameters manually.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4. $wikidata = include 'tutorial_wikipedia_data.php';
5.
6. $graph = new ezcGraphLineChart();
7. $graph->title = 'Some random data';
8. $graph->legend = false;
9.
10. $graph->xAxis = new ezcGraphChartElementNumericAxis();
11.
12. $graph->xAxis->min = -15;
13. $graph->xAxis->max = 15;
14. $graph->xAxis->majorStep = 5;
15.
16. $data = array(
17. array(),
18. array()
19. );
20. for ( $i = -10; $i <= 10; $i++ )
21. {
22. $data[0][$i] = mt_rand( -23, 59 );
23. $data[1][$i] = mt_rand( -23, 59 );
24. }
25.
26. // Add data
27. $graph->data['random blue'] = new ezcGraphArrayDataSet( $data[0] );
28. $graph->data['random green'] = new ezcGraphArrayDataSet( $data[1] );
29.
30. $graph->render( 400, 150, 'tutorial_example_15.svg' );
31.
32. ?>
In this example we force both axis to be a numeric axis in line 10 of the
example. In the lines 12 to 15 we manually set the scaling options for the x
axis. We don't set a minorStep size here, so it will be automatically
calculated from the other values, as all the values are automatically
calculated for the y axis. The we create some random data and create two
datasets from it as usual.
The example shows one advantage of numeric axis over labeled axis for numeric
data. The axis are moved away from the charts border to display the negative
values below / left of the axis.
Earlier in this tutorial we used a labeled axis for date time data on the x
axis in the wikipedia examples. This works fine for evenly distributed
time spans. For other data you should use the date time axis.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $graph = new ezcGraphLineChart();
6. $graph->options->fillLines = 210;
7. $graph->title = 'Concurrent requests';
8. $graph->legend = false;
9.
10. $graph->xAxis = new ezcGraphChartElementDateAxis();
11.
12. // Add data
13. $graph->data['Machine 1'] = new ezcGraphArrayDataSet( array(
14. '8:00' => 3241,
15. '8:13' => 934,
16. '8:24' => 1201,
17. '8:27' => 1752,
18. '8:51' => 123,
19. ) );
20. $graph->data['Machine 2'] = new ezcGraphArrayDataSet( array(
21. '8:05' => 623,
22. '8:12' => 2103,
23. '8:33' => 543,
24. '8:43' => 2034,
25. '8:59' => 3410,
26. ) );
27.
28. $graph->data['Machine 1']->symbol = ezcGraph::BULLET;
29. $graph->data['Machine 2']->symbol = ezcGraph::BULLET;
30.
31. $graph->render( 400, 150, 'tutorial_example_16.svg' );
32.
33. ?>
You can use timestamps or date time strings as data set keys. The strings will
be converted using PHPs strtotime() function.
As mentioned earlier in this tutorial the axis label renderer defines where a
label is drawn relatively to the step on the axis. You already saw examples
for all available axis label renderers, but I want to mention them again:
ezcGraphAxisExactLabelRenderer
This is the default renderer for the numeric axis. The labels are drawn
directly below the axis step. This may look strange some times, because it
is of course not possible to always draw all labels of one axis on one side
of the step, because the last or first label would exceed the available
space for the axis, so that it is rendered on the other side.
ezcGraphAxisCenteredLabelRenderer
This renderer is default for the labeled axis in line charts and draws the
label centered next to the step. Therefor this renderer omits the label for
initial step on the axis (0, 0), but this can be forced as shown in example
14. The label is omitted, because it would interfere with the axis or the
labels of the other axis, and probably not really readable.
ezcGraphAxisBoxedLabelRenderer
This is the default renderer for the labeled axis in bar charts. The steps
on the axis and the grid is not drawn at the position of the label, but
between two labels. This helps to recognize which bars belongs together.
The label is rendered centered between two of those steps on the axis.
The data sets receive the data from the user and offer a defined interface for
ezcGraph to access the users data.
The array data set was used for all examples until now, because it is the
simplest way to provide data for ezcGraph.
The average dataset uses an existing dataset with numeric keys and build a
polynomial which interpolates the data points in the given data set using the
least squares algorithm.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4.
5. $graph = new ezcGraphLineChart();
6. $graph->title = 'Some random data';
7. $graph->legend->position = ezcGraph::BOTTOM;
8.
9. $graph->xAxis = new ezcGraphChartElementNumericAxis();
10.
11. $data = array();
12. for ( $i = 0; $i <= 10; $i++ )
13. {
14. $data[$i] = mt_rand( -5, 5 );
15. }
16.
17. // Add data
18. $graph->data['random data'] = $dataset = new ezcGraphArrayDataSet( $data );
19.
20. $average = new ezcGraphDataSetAveragePolynom( $dataset, 3 );
21. $graph->data[(string) $average->getPolynom()] = $average;
22.
23. $graph->render( 400, 150, 'tutorial_example_17.svg' );
24.
25. ?>
We use again two numeric axis, because we only display numeric data in this
example. First we create a normal array dataset from some random generated
data in line 14. We assign this data set to the chart , to see how well the
polynomial fits the random data points.
In line 20 we create a ezcGraphDataSetAveragePolynom from the random data with
a maximum degree of the polynomial of 3 (which is also the default value). You
can directly access the polynomial, what we use to set the name of the data set
to the data sets polynomial, when we add the data set to the graph.
For the computation of the polynomial a equitation has to be solved where the
size of the matrix is equal to the point count of the used dataset. Be
careful with data sets with large point count. Because this could mean that
ezcGraph will consume a lot of memory and processing power.
The renderer transform chart primitives into image primitives, that means that
things like a pie segment including labels, highlight etc. will be transformed
into some text strings, circle sectors and symbols to link the text to the
according circle sector.
ezcGraph comes with the default 2D renderer used in all of the above examples
and a 3D renderer which renders the chart elements in a pseudo 3D isometric
manner.
All examples until now used the default 2D renderer. But there are several
renderer specific options, which were not yet shown.
All the options specially available for bar charts are available for all
current renderers.
1. <?php
2.
3. require_once 'tutorial_autoload.php';
4. $wikidata = include 'tutorial_wikipedia_data.php';
5.
6. $graph = new ezcGraphBarChart();
7. $graph->title = 'Wikipedia articles';
8.
9. // Add data
10. foreach ( $wikidata as $language => $data )
11. {
12. $graph->data[$language] = new ezcGraphArrayDataSet( $data );
13. }
14. $graph->data['German']->displayType = ezcGraph::LINE;
15. $graph->data['German']->highlight = true;
16. $graph->data['German']->highlight['Mar 2006'] = false;
17.
18. $graph->options->fillLines = 210;
19.
20. $graph->options->highlightSize = 12;
21.
22. $graph->options->highlightFont->background = '#EEEEEC88';
23. $graph->options->highlightFont->border = '#000000';
24. $graph->options->highlightFont->borderWidth = 1;
25.
26. // $graph->renderer = new ezcGraphRenderer2d();
27.
28. $graph->renderer->options->barMargin = .2;
29. $graph->renderer->options->barPadding = .2;
30.
31. $graph->renderer->options->dataBorder = 0;
32.
33. $graph->render(