Customize Chart Legends — Presentation API for Java
- 7 minutes to read
The Presentation API allows you to display, hide, position, and format chart legends. A legend contains an entry for each chart series. Each entry displays a legend key (the series color) and the series name. If a chart view varies colors by data point (for example, a pie chart), the legend contains an entry for each data point.
Access or Hide a Chart Legend
A new chart displays a legend on the right side of the chart. Call the getLegend() method of a chart to access the legend. Call the setLegend(value) method to assign a new legend object to the chart or hide the legend.
| Chart Type | Get the Legend | Set the Legend | Legend Class |
|---|---|---|---|
| Standard | Chart.getLegend() | Chart.setLegend(value) | Legend |
| Microsoft Excel 2016 | ChartEx.getLegend() | ChartEx.setLegend(value) | LegendEx |
The following code snippet displays a legend with default settings:
chart.setLegend(new Legend());
To hide the legend, call the setLegend(null) method:
chart.setLegend(null);
Specify the Legend Position
Call the Legend.setPosition(value) method to specify the legend position:
| Value | Description |
|---|---|
| BOTTOM | Indicates that the legend is at the bottom of the chart. |
| TOP_RIGHT | Indicates that the legend is in the upper-right corner of the chart. |
| LEFT | Indicates that the legend is on the left side of the chart. |
| RIGHT | Indicates that the legend is on the right side of the chart. |
| TOP | Indicates that the legend is at the top of the chart. |
The chart reduces the plot area to fit the legend. To display the legend over the plot area, call the Legend.setAllowOverlapChart(true) method.
The following code snippet creates a column chart and displays the legend above the plot area:
import java.io.*;
import java.nio.file.*;
import java.util.List;
import com.devexpress.docs.office.*;
import com.devexpress.docs.presentation.*;
public class Main {
public static void main(String[] args) throws Exception {
try (Presentation presentation = new Presentation()) {
// Add a blank slide.
presentation.getSlides().clear();
Slide slide = new Slide(SlideLayoutType.BLANK);
presentation.getSlides().add(slide);
// Insert a chart that fills the slide.
Chart chart = new Chart();
chart.setWidth(presentation.getSlideSize().getWidth());
chart.setHeight(presentation.getSlideSize().getHeight());
slide.getShapes().add(chart);
// Add three column series.
List<String> quarters = List.of("Q1", "Q2", "Q3", "Q4");
BarSeries north = new BarSeries();
north.setText("North");
north.setArguments(new ChartStringData(quarters));
north.setValues(new ChartNumericData(List.of(42.0, 51.5, 47.8, 63.2)));
chart.getSeries().add(north);
BarSeries south = new BarSeries();
south.setText("South");
south.setArguments(new ChartStringData(quarters));
south.setValues(new ChartNumericData(List.of(35.4, 39.0, 44.6, 52.1)));
chart.getSeries().add(south);
BarSeries west = new BarSeries();
west.setText("West");
west.setArguments(new ChartStringData(quarters));
west.setValues(new ChartNumericData(List.of(28.7, 33.2, 41.5, 46.9)));
chart.getSeries().add(west);
// Display the legend above the plot area.
Legend legend = chart.getLegend();
legend.setPosition(LegendPositionType.TOP);
TextProperties legendText = new TextProperties();
legendText.setFontSize(18f);
legend.setTextProperties(legendText);
// Save the presentation.
Files.createDirectories(Path.of("output"));
try (OutputStream output = Files.newOutputStream(Path.of("output", "Chart.pptx"))) {
presentation.save(output);
}
}
}
}
The following screenshot illustrates the result:

Position a Legend Manually
To specify the legend position and size manually, pass a LayoutOptions object to the Legend.setLayout(value) method. In this instance, the chart ignores the legend position specified by the setPosition(value) method.
Call the following methods to specify the legend position and size:
| Method | Description |
|---|---|
| setLeft(@Nullable LayoutPosition value) | Sets the left position of the chart element. |
| setTop(@Nullable LayoutPosition value) | Sets the top position of the chart element. |
| setWidth(@Nullable LayoutPosition value) | Sets the width or right edge position of the chart element. |
| setHeight(@Nullable LayoutPosition value) | Sets the height or bottom edge position of the chart element. |
Each method accepts a LayoutPosition object that contains a layout mode and a numeric value. Values are fractions of the chart width or height (for example, 0.05 corresponds to 5%). The layout mode specifies how the chart interprets the value:
| Mode | Description |
|---|---|
| EDGE | Left and top values specify the element position relative to the chart edges. Width and height values specify positions of the right and bottom element edges. |
| FACTOR | Left and top values specify offsets from the default element position. Width and height values specify the element size. |
The following code snippet displays the legend in the top-left corner of the plot area:
Legend legend = chart.getLegend();
legend.setAllowOverlapChart(true);
TextProperties legendText = new TextProperties();
legendText.setFontSize(18f);
legend.setTextProperties(legendText);
// Place the legend in the top-left corner of the plot area.
LayoutOptions layout = new LayoutOptions();
layout.setLeft(new LayoutPosition(LayoutModeType.EDGE, 0.08));
layout.setTop(new LayoutPosition(LayoutModeType.EDGE, 0.05));
layout.setWidth(new LayoutPosition(LayoutModeType.FACTOR, 0.12));
layout.setHeight(new LayoutPosition(LayoutModeType.FACTOR, 0.25));
legend.setLayout(layout);
The following screenshot illustrates the result:

Customize Legend Appearance
Call the following methods to customize legend appearance:
| Method | Description |
|---|---|
| Legend.setTextProperties(value) | Sets text formatting settings of the legend. |
| Legend.setFill(value) | Sets fill settings of the chart element. |
| Legend.setOutlineStyle(value) | Sets outline settings of the chart element. |
| Legend.setEffects(value) | Sets shape effect settings of the chart element. |
The following code snippet displays the legend below the plot area, applies bold dark blue text formatting, and adds a light yellow fill and a gray outline to the legend:
Legend legend = chart.getLegend();
legend.setPosition(LegendPositionType.BOTTOM);
// Apply bold dark blue text formatting.
TextProperties legendText = new TextProperties();
legendText.setFontSize(20f);
legendText.setBold(true);
legendText.setFill(new SolidFill(Color.getDarkBlue()));
legend.setTextProperties(legendText);
// Fill the legend with light yellow and add a gray outline.
OutlineStyle outline = new OutlineStyle();
outline.setFill(new SolidFill(Color.getGray()));
legend.setFill(new SolidFill(Color.getLightYellow()));
legend.setOutlineStyle(outline);
The following screenshot illustrates the result:

Customize Individual Legend Entries
The Legend.getCustomEntries() method returns a map of individual legend entry settings (Map<Integer, LegendEntry>). Map keys are zero-based legend entry indexes. Legend entries have the same order as chart series (or data points, if the chart view varies colors by data point).
To hide or format an individual legend entry, create a LegendEntry object and call the put(key, value) method to add the object to the map. To remove individual entry settings, call the remove(key) method.
Use the following methods to customize an individual legend entry:
| Method | Description |
|---|---|
| setHidden(boolean value) | Sets whether to hide the legend entry. |
| setTextProperties(TextProperties value) | Sets text formatting settings of the legend entry. |
The following code snippet creates a pie chart, applies bold red text formatting to the first legend entry, and hides the fourth legend entry:
import java.io.*;
import java.nio.file.*;
import java.util.List;
import com.devexpress.docs.office.*;
import com.devexpress.docs.presentation.*;
import com.devexpress.system.drawing.Color;
public class Main {
public static void main(String[] args) throws Exception {
try (Presentation presentation = new Presentation()) {
// Add a blank slide.
presentation.getSlides().clear();
Slide slide = new Slide(SlideLayoutType.BLANK);
presentation.getSlides().add(slide);
// Insert a chart that fills the slide.
Chart chart = new Chart();
chart.setWidth(presentation.getSlideSize().getWidth());
chart.setHeight(presentation.getSlideSize().getHeight());
slide.getShapes().add(chart);
// Add a pie series and paint each slice in a different color.
PieSeries series = new PieSeries();
series.setArguments(new ChartStringData(List.of("Online", "Retail", "Wholesale", "Other")));
series.setValues(new ChartNumericData(List.of(4200.0, 3100.0, 1800.0, 400.0)));
chart.getSeries().add(series);
((PieChartView) chart.getViews().get(0)).setVaryColors(true);
// Specify the legend font size.
Legend legend = chart.getLegend();
TextProperties legendText = new TextProperties();
legendText.setFontSize(18f);
legend.setTextProperties(legendText);
// Apply bold red text formatting to the first legend entry.
TextProperties highlightText = new TextProperties();
highlightText.setFontSize(18f);
highlightText.setBold(true);
highlightText.setFill(new SolidFill(Color.getRed()));
LegendEntry onlineEntry = new LegendEntry();
onlineEntry.setTextProperties(highlightText);
legend.getCustomEntries().put(0, onlineEntry);
// Hide the fourth legend entry.
LegendEntry otherEntry = new LegendEntry();
otherEntry.setHidden(true);
legend.getCustomEntries().put(3, otherEntry);
// Save the presentation.
Files.createDirectories(Path.of("output"));
try (OutputStream output = Files.newOutputStream(Path.of("output", "Chart.pptx"))) {
presentation.save(output);
}
}
}
}
The following screenshot illustrates the result:

Legends in Chart Types Introduced in Microsoft Excel 2016
The ChartEx.getLegend() method returns a LegendEx object. Call LegendEx methods to specify the legend position, overlap, text formatting, fill, outline, and effects in the same way as you do for a standard chart legend. To hide the legend, call the ChartEx.setLegend(null) method.
Note
LegendEx objects do not have manual layout settings or individual legend entry settings.
The following code snippet displays the legend of a waterfall chart below the plot area:
LegendEx legend = chart.getLegend();
legend.setPosition(LegendPositionType.BOTTOM);
TextProperties legendText = new TextProperties();
legendText.setFontSize(18f);
legend.setTextProperties(legendText);
Refer to the following help topic for additional information: Create Waterfall, Funnel, Treemap, and Other Office 2016 Charts.