Skip to main content

Legend Class

A legend in a standard chart.

Declaration

public class Legend extends LegendBase

Remarks

Call the Chart.getLegend() method to access the legend of a chart. A new chart displays a legend. To hide the legend, call the Chart.setLegend(null) method.

The Legend class inherits position, overlap, and text formatting settings from the LegendBase class.

Refer to the following help topic for additional information: Customize Chart Legends — Presentation API for Java.

Example

Position a Legend and Hide a Legend Entry

The following code snippet places the chart legend in the top-left corner of the chart and hides the legend entry for the first series:

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 two bar series.
            String[] names = { "2024", "2025" };
            double[][] values = { { 12, 18, 15 }, { 14, 20, 19 } };
            for (int i = 0; i < names.length; i++) {
                BarSeries series = new BarSeries();
                series.setText(names[i]);
                series.setArguments(new ChartStringData(List.of("Q1", "Q2", "Q3")));
                series.setValues(new ChartNumericData(List.of(values[i][0], values[i][1], values[i][2])));
                chart.getSeries().add(series);
            }

            // Place the legend in the top-left corner of the chart.
            Legend legend = new Legend();
            LayoutOptions layout = new LayoutOptions();
            layout.setLeft(new LayoutPosition(LayoutModeType.EDGE, 0.05));
            layout.setTop(new LayoutPosition(LayoutModeType.EDGE, 0.05));
            layout.setWidth(new LayoutPosition(LayoutModeType.FACTOR, 0.15));
            layout.setHeight(new LayoutPosition(LayoutModeType.FACTOR, 0.15));
            legend.setLayout(layout);

            // Hide the legend entry for the first series.
            LegendEntry entry = new LegendEntry();
            entry.setHidden(true);
            legend.getCustomEntries().put(0, entry);
            chart.setLegend(legend);

            // Save the presentation.
            Files.createDirectories(Path.of("output"));
            try (OutputStream output = Files.newOutputStream(Path.of("output", "Chart.pptx"))) {
                presentation.save(output);
            }
        }
    }
}

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

See Also

Legend()

Initializes a new instance of the Legend class.

Declaration

public Legend()

Methods

deepClone() Method

Creates a copy of the legend.

Declaration

public Legend deepClone()

Returns

Type Description
Legend

A copy of the legend.

getCustomEntries() Method

Returns settings for individual legend entries.

Declaration

public ILegendEntryDictionary getCustomEntries()

Returns

Type Description
ILegendEntryDictionary

A dictionary of legend entry settings. Keys are zero-based legend entry indexes.

Remarks

Add a LegendEntry object to the dictionary to hide or format an individual legend entry.

getLayout() Method

Returns manual layout settings for the legend.

Declaration

public LayoutOptions getLayout()

Returns

Type Description
LayoutOptions

Layout settings, or null if the chart positions the legend automatically.

setLayout(LayoutOptions value) Method

Sets manual layout settings for the legend.

Declaration

public void setLayout(LayoutOptions value)

Parameters

Name Type Description
value LayoutOptions

Layout settings, or null to position the legend automatically.

Remarks

If layout settings are null, the Legend.setPosition(value) method specifies the legend position.