Skip to content

howto change legend labels

Erich Seifert edited this page Mar 14, 2016 · 1 revision

How can I change the labels of a legend?

GRAL plots provide different types of legends with different properties. For example, pie plots provide legends of the type ValueLegend which show items for each value in the data set. The values are used as labels by default. To set which labels are used by ValueLegend you can change its labelColumn property.

How to set a custom column that contains labels?

Given we have a pie plot with the following data:

7 "Spam"
42 "Ham"

We can then tell the legend to use the second column to render the labels:

ValueLegend legend = (ValueLegend) plot.getLegend();
legend.setLabelColumn(1);

or as a one-liner:

((ValueLegend) plot.getLegend()).setLabelColumn(1);

How to format values for legend labels?

The labelFormat property of ValueLegend allows a custom formatting of label values. This could be used to display date or time values, or to change the format of numbers. labelFormat stores an object of type java.text.Format which specifies how to format the labels.

This example uses java.text.DateFormat to format date values:

DateFormat customFormat = DateFormat.getDateInstance();
((ValueLegend) plot.getLegend()).setLabelFormat(customFormat);

See also

API docs for ​ValueLegend