The palette defines the default formatting of the elements. No only can you set
foreground and background colors for all the elements, but you can also
define their position in the chart or prevent them from being rendered at
all.
We try to fulfill two goals regarding font configuration. First, there should
be a single point to configure the fonts used for the text areas in the chart.
On the other hand, it should be possible to configure the fonts independently
for each chart element.
The solution is that you can modify the global font configuration by
accessing $graph->options->font. This takes effect on all chart elements
unless you intentionally access the font configuration of an individual chart
element. The following section shows an example of 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_chart_title.svg' );
25.
26. ?>
The chart title is the simplest element. In line 9, we change the global font
configuration to use a serif font. In the SVG 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 title's font configuration. In line 14, we set a
maximum font size, which now only affects the legend and the pie chart
captions.
Aside from the font configuration, we set an option 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 as in 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_chart_background.svg' );
22.
23. ?>
In line 17, we set a background image, and define its 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 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.
With the SVG driver, the image is inlined using a data URL with the base64
encoded content of the binary image file. Using this driver, you do not need to
worry about the locations of your referenced images.
With the GD driver, super sampling is not applied to the images, as this would
make them blurry.
Of course, you could also apply the following settings to the background
element: background color, borders, margin and padding.
The legend is shown by default and is automatically generated from the assigned
data. If you want to disable the legend, you can do this by 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_chart_legend.svg' );
20.
21. ?>
Other than hiding the legend, you can also place it at the bottom, left or top
in the chart; you can assign a title for the legend and change the symbol
sizes; you can additionally assign the legend 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_legend_options.svg' );
22.
23. ?>
To place the legend at another position on the graph, set the position
property of the legend, as shown in line 17. If the legend is placed at the top or
bottom, it will automatically use a landscape format. The space consumed by the
legend is configured by the landscapeSize setting for landscape-oriented legends
and the portraitSize setting otherwise. The assigned value is the percent
portion of space taken up by the legend, relative to the size of the chart. The
legend only displays a title if you manually set it, as shown in line 19.
The axis defines the unit scale in line and bar charts. There are always two
axes - the x-axis and the y-axis, whose ranges are automatically
received from the datasets and scaled to display appropriate 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 types of data. For
normal string keys, the standard labeled axis is usually the right choice. The
numeric axis is predestined to display numeric data, and the date time axis is
for data associated with dates or times. All of the axis types can be assigned
to either axis.
The labeled axis is default for the x-axis in both bar and line charts. It
is intended to display string labels of datasets and uses the centered label
renderer by default. You saw it in all the earlier examples with bar
and line charts, but it can be used for both axes as well.
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_axis_labeled.svg' );
30.
31. ?>
You could argue whether such a chart is really useful - but it works. Instead of
using numeric values, colors are assigned when creating the dataset. The
labeled axis uses the values in the order they are assigned. Line 11 is
the first time we actually configure 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 displays
numeric data and automatically determines appropriate scaling for the assigned
values. However, you can also 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_axis_numeric.svg' );
31.
32. ?>
In this example, we force both axes to be numeric axes in line 10. In lines
12 to 15, we manually set the scaling options for the x-axis. We do not set a
minorStep size here, so it will be automatically calculated from the other
values, as will the settings for the y-axis. Then, we create some random data
and create two datasets from it as usual.
The example shows one advantage of a numeric axis over a labeled axis for numeric
data. The axes are moved away from the chart's border to display the negative
values below and 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_axis_datetime.svg' );
32.
33. ?>
You can use timestamps or date time strings as dataset keys. The strings will
be converted using PHP's strtotime() function.
As mentioned earlier in this tutorial, the axis label renderer defines where a
label is drawn relative to the step on the axis. You already saw examples
for all available axis label renderers, but here is an overview:
ezcGraphAxisExactLabelRenderer
This is the default renderer for the numeric axis. The labels are drawn
directly below the axis step. This may look strange sometimes, because it
is not always possible to draw all labels of one axis on one side
of the step; the last or first label would exceed the available
space for the axis, and be rendered on the other side.
ezcGraphAxisCenteredLabelRenderer
This renderer is the default for the labeled axis in line charts and draws the
label centered next to the step. Therefore, this renderer omits the label for
the initial step on the axis (0, 0) by default. However, 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 thus be difficult to read.
ezcGraphAxisBoxedLabelRenderer
This is the default renderer for the labeled axis in bar charts. The steps
on the axis and the grid are not drawn at the position of the label, but
between two labels. This helps to recognize which bars belong together.
Labels are rendered centered between two steps on the axis.
There is one more new axis label renderer since version 1.1 of ezcGraph - the
ezcGraphAxisRotatedLabelRenderer which enables you to render rotated labels on
an axis.
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. $graph->xAxis->axisLabelRenderer = new ezcGraphAxisRotatedLabelRenderer();
10. $graph->xAxis->axisLabelRenderer->angle = 45;
11. $graph->xAxis->axisSpace = .2;
12.
13. // Add data
14. foreach ( $wikidata as $language => $data )
15. {
16. $graph->data[$language] = new ezcGraphArrayDataSet( $data );
17. }
18. $graph->data['German']->displayType = ezcGraph::LINE;
19.
20. $graph->options->fillLines = 210;
21.
22. $graph->render( 400, 150, 'tutorial_rotated_labels.svg' );
23.
24. ?>
In line 9 a custom renderer is defined used for the labeled x axis. You can
assign custom axis label renderers on the property $axisLabelRenderer for
ezcGraphChartAxisElement objects.
The renderer used in this example has custom properties like the rotation of
the labels, which is set in degree, while the rotation direction depends on
the direction of the axis.
It makes sense to define more vertical space below the the axis for the
rotated labels like done in line 11 of the above example.
The results conatins rotated labels, which enables you to pack a lot more
labels on one axis.