There are two ways that you can manage the font size of the text components in a graph. You can treat the font size as an absolute size, or you can have the graph use its virtual coordinate system to keep the font size proportionate to the size of the graph.
By default, you can set the font size, either through the GraphFont
object or
by using the standard java.awt.Font
class. The following example shows two ways to
set the font size for the title of the graph.
//You do not normally need to make this call, because true is the default graph.setFontSizeAbsolute(true); //using the GraphFont to set the size graph.getTitle().getGraphFont().setGraphFontSize(12); //using awt FontFont f = new Font("Arial", PLAIN, 12); graph.getTitle().setFont(f);
For more information about the differences between the java.awt.Font
and the
GraphFont
, see Comparing the
GraphFont and Font Objects.
When you set the FontSizeAbsolute
property to false
, then the
graph uses its virtual coordinate system to maintain proportionate font sizes. When the size of
the graph changes, then the font sizes change as well.
When the FontSizeAbsolute
property is set to false
, then you can
change the proportional size of a font by setting the GraphProportionalFontSize
property of the GraphFont
object. The following line of code shows how to set a
proportional font size.
graph.setFontSizeAbsolute(false); // 3200 is one-tenth the size of the graph graph.getTitle().getGraphFont().setGraphProportionalFontSize(3200);
The amount of data that you display in a graph has a great effect on the display of tick labels along the O1Axis and on the display of series labels in the legend. For example, if you have five series of data in a line graph, then the tick labels probably fit well along the ordinal axis of the graph, and the legend text probably fits well in the legend area. However, if you add 15 more series to the graph, then the tick labels are likely to become more crowded, as is the legend text.
By default, the graph automatically calculates the amount of space required for these labels, and it lays out graph components as best it can to accommodate the labels. Automatic graph layout does not change the font size of the labels. Instead, it allots more space for the legend and for the ordinal tick labels. However, if you disable the automatic graph layout, then you can have the graph adjust the font size of these labels as the data changes, without resizing the plot area or the legend. You can also limit this automatic sizing, so that the sizing changes within a range of acceptable sizes.
The following code shows how to tell the graph to fit the legend text and the tick labels of the ordinal axis automatically in the space that is available for them. The code limits the range of sizes between one one-hundredth and one tenth the size of the graph.
// AutoLayout must be disabled graph.setAutoLayout(Graph.AL_NEVER); // FontSizeAbsolute must be false graph.setFontSizeAbsolute(false); // Autofit legend text, within limits LegendText legendText = graph.getLegendText(); legendText.setTextFittingAutomatic(true); legendText.setMaxAutoFontSize(1600); legendText.setMinAutoFontSize(160); // Autofit tick labels, within limits graph.getO1TickLabel().setTextFittingAutomatic(true); graph.setMaxTickLabelAutoFontSize(1600); graph.setMinTickLabelAutoFontSize(160);