ContentsBasic ChartingAxis labels
Previous: Add a grid | Next: Place a chart legend

Axis labels

Putting labels on charts is typically a pain for the author, but is an absolute necessity for proper interpretation. No chart should be without them, no matter how inane they may seem.

To provide flexibility, there are many parameters that can be tweaked to get what you want. Let's build up.

Let's start with a simple X axis label -> code / output

The only change was the added invocation of PdfChart.drawBottomLabels(). The first three parameters set the offset and orientation angle of the text. The string array is evenly distributed along the axis. The final parameter is the length of the extension line.

Even if you need to omit labels (e.g. at the ends of the axis), you still need placeholders in order to preserve the spacing. Use a null or empty string for this purpose. Notice that the axis labels and extension lines are not associated with either the spacing parameter or the grid lines... you can have one without the other.

Now we'll add a Y axis label -> code / output

In addition to the invocation of drawLeftLabels (at line 34), we also moved the origin and width of the chart by changing the xLoc and width parameters of drawMain() (at line 31).

Finally, let's programmatically bump up the number of X label extensions so that the angle shows up for every 30 degrees of the cycle -> code / output


ContentsBasic ChartingAxis labels
Previous: Add a grid | Next: Place a chart legend