Using Legends
There are 2 main ways to display legends using GraPHPite.
As a box, overlayed on the plotarea
As a box, displayed somewhere besides/above/below the plotarea
Overlayed Legends
The 1st way will display a box that is placed (by default in the upper-right corner) of the plotarea. This is the
easiest way to add the legend:
- ...
- $PlotArea->add(new Legend());
- ...
Note that a Legend is added to a PlotArea.
By displaying the legend in the upper-right corner it could overlap your plot hiding information. However you have
the possibility to display the legend in another "corner" of the plot.
- ...
- $Legend->setAlignment(ALIGN_BOTTOM + ALIGN_LEFT);
- ...
This will make it appear in the bottom-left corner of the plot. Use ALIGN_BOTTOM, ALIGN_TOP, ALIGN_LEFT and ALIGN_RIGHT
for corner positions.
Layouted Legends
The 2nd way is (slightly) more complex, but will allow you to,display legends "outside" the plot, thereby not overlapping.
This is done in the same using layouts (Plot Layout).
- ...
- $Graph->add(
- new VerticalLayout(
- $PlotArea = new PlotArea(),
- $Legend = new Legend(),
- 95
- )
- );
- $Legend->setPlotArea($PlotArea);
- ...
Note that you have to link the Legend to the PlotArea explicitly. In the 1st option, this is done
implicitly, since you add the legend to the plotarea. Using this method you add the legend to a (in this case)
VerticalLayout which is added to a GraPHP, leaving the legend with "no idea" of what plot to show
legend for. So you need to have the $Legend->setPlotArea($PlotArea); for things to work..
This will also give the possibility to place the legend wherever you want, fx:
- ...
- $Graph->add(
- new VerticalLayout(
- $PlotArea = new PlotArea(),
- new HorizontalLayout(
- new PlotArea(),
- new VerticalLayout(
- new PlotArea(),
- $Legend = new Legend()
- )
- )
- )
- );
- $Legend->setPlotArea($PlotArea);
- ...
Which will cause the Legend to be displayed in the lower-right eigth' of the graph, where the linked PlotArea is displayed in the upper half