ChartLegend

Inheritance: java.lang.Object

All Implemented Interfaces: java.lang.Cloneable

public class ChartLegend implements Cloneable

Represents chart legend properties.

To learn more, visit the Working with Charts documentation article.

Examples:

Shows how to edit the appearance of a chart’s legend.


 Document doc = new Document();
 DocumentBuilder builder = new DocumentBuilder(doc);

 Shape shape = builder.insertChart(ChartType.LINE, 450.0, 300.0);
 Chart chart = shape.getChart();

 Assert.assertEquals(3, chart.getSeries().getCount());
 Assert.assertEquals("Series 1", chart.getSeries().get(0).getName());
 Assert.assertEquals("Series 2", chart.getSeries().get(1).getName());
 Assert.assertEquals("Series 3", chart.getSeries().get(2).getName());

 // Move the chart's legend to the top right corner.
 ChartLegend legend = chart.getLegend();
 legend.setPosition(LegendPosition.TOP_RIGHT);

 // Give other chart elements, such as the graph, more room by allowing them to overlap the legend.
 legend.setOverlay(true);

 doc.save(getArtifactsDir() + "Charts.ChartLegend.docx");
 

Methods

MethodDescription
getFont()Provides access to the default font formatting of legend entries.
getFormat()Provides access to fill and line formatting of the legend.
getLegendEntries()Returns a collection of legend entries for all series and trendlines of the parent chart.
getOverlay()Determines whether other chart elements shall be allowed to overlap legend.
getPosition()Specifies the position of the legend on a chart.
getShapeType()
isFillSupported()
isFormatDefined()
materializeSpPr()
setOverlay(boolean value)Determines whether other chart elements shall be allowed to overlap legend.
setPosition(int value)Specifies the position of the legend on a chart.
setShapeType(int value)

getFont()

public Font getFont()

Provides access to the default font formatting of legend entries. To override the font formatting for a specific legend entry, use the ChartLegendEntry.getFont() property.

Examples:

Shows how to work with a legend font.


 Document doc = new Document(getMyDir() + "Reporting engine template - Chart series (Java).docx");
 Chart chart = ((Shape)doc.getChild(NodeType.SHAPE, 0, true)).getChart();

 ChartLegend chartLegend = chart.getLegend();
 // Set default font size all legend entries.
 chartLegend.getFont().setSize(14.0);
 // Change font for specific legend entry.
 chartLegend.getLegendEntries().get(1).getFont().setItalic(true);
 chartLegend.getLegendEntries().get(1).getFont().setSize(12.0);

 doc.save(getArtifactsDir() + "Charts.LegendFont.docx");
 

Returns: Font - The corresponding Font value.

getFormat()

public ChartFormat getFormat()

Provides access to fill and line formatting of the legend.

Examples:

Shows how to use chart formating.


 Document doc = new Document();
 DocumentBuilder builder = new DocumentBuilder(doc);

 Shape shape = builder.insertChart(ChartType.COLUMN, 432.0, 252.0);
 Chart chart = shape.getChart();

 // Delete series generated by default.
 ChartSeriesCollection series = chart.getSeries();
 series.clear();

 String[] categories = new String[] { "Category 1", "Category 2" };
 series.add("Series 1", categories, new double[] { 1.0, 2.0 });
 series.add("Series 2", categories, new double[] { 3.0, 4.0 });

 // Format chart background.
 chart.getFormat().getFill().solid(Color.darkGray);

 // Hide axis tick labels.
 chart.getAxisX().getTickLabels().setPosition(AxisTickLabelPosition.NONE);
 chart.getAxisY().getTickLabels().setPosition(AxisTickLabelPosition.NONE);

 // Format chart title.
 chart.getTitle().getFormat().getFill().solid(Color.yellow);

 // Format axis title.
 chart.getAxisX().getTitle().setShow(true);
 chart.getAxisX().getTitle().getFormat().getFill().solid(Color.yellow);

 // Format legend.
 chart.getLegend().getFormat().getFill().solid(Color.yellow);

 doc.save(getArtifactsDir() + "Charts.ChartFormat.docx");
 

Returns: ChartFormat - The corresponding ChartFormat value.

getLegendEntries()

public ChartLegendEntryCollection getLegendEntries()

Returns a collection of legend entries for all series and trendlines of the parent chart.

Examples:

Shows how to work with a legend entry for chart series.


 Document doc = new Document();
 DocumentBuilder builder = new DocumentBuilder(doc);

 Shape shape = builder.insertChart(ChartType.COLUMN, 432.0, 252.0);

 Chart chart = shape.getChart();
 ChartSeriesCollection series = chart.getSeries();
 series.clear();

 String[] categories = new String[] { "AW Category 1", "AW Category 2" };

 ChartSeries series1 = series.add("Series 1", categories, new double[] { 1.0, 2.0 });
 series.add("Series 2", categories, new double[] { 3.0, 4.0 });
 series.add("Series 3", categories, new double[] { 5.0, 6.0 });
 series.add("Series 4", categories, new double[] { 0.0, 0.0 });

 ChartLegendEntryCollection legendEntries = chart.getLegend().getLegendEntries();
 legendEntries.get(3).isHidden(true);

 doc.save(getArtifactsDir() + "Charts.LegendEntries.docx");
 

Returns: ChartLegendEntryCollection - A collection of legend entries for all series and trendlines of the parent chart.

getOverlay()

public boolean getOverlay()

Determines whether other chart elements shall be allowed to overlap legend. Default value is false .

Examples:

Shows how to edit the appearance of a chart’s legend.


 Document doc = new Document();
 DocumentBuilder builder = new DocumentBuilder(doc);

 Shape shape = builder.insertChart(ChartType.LINE, 450.0, 300.0);
 Chart chart = shape.getChart();

 Assert.assertEquals(3, chart.getSeries().getCount());
 Assert.assertEquals("Series 1", chart.getSeries().get(0).getName());
 Assert.assertEquals("Series 2", chart.getSeries().get(1).getName());
 Assert.assertEquals("Series 3", chart.getSeries().get(2).getName());

 // Move the chart's legend to the top right corner.
 ChartLegend legend = chart.getLegend();
 legend.setPosition(LegendPosition.TOP_RIGHT);

 // Give other chart elements, such as the graph, more room by allowing them to overlap the legend.
 legend.setOverlay(true);

 doc.save(getArtifactsDir() + "Charts.ChartLegend.docx");
 

Returns: boolean - The corresponding boolean value.

getPosition()

public int getPosition()

Specifies the position of the legend on a chart.

Remarks:

The default value is LegendPosition.RIGHT for pre-Word 2016 charts and LegendPosition.TOP for Word 2016 charts.

Examples:

Shows how to edit the appearance of a chart’s legend.


 Document doc = new Document();
 DocumentBuilder builder = new DocumentBuilder(doc);

 Shape shape = builder.insertChart(ChartType.LINE, 450.0, 300.0);
 Chart chart = shape.getChart();

 Assert.assertEquals(3, chart.getSeries().getCount());
 Assert.assertEquals("Series 1", chart.getSeries().get(0).getName());
 Assert.assertEquals("Series 2", chart.getSeries().get(1).getName());
 Assert.assertEquals("Series 3", chart.getSeries().get(2).getName());

 // Move the chart's legend to the top right corner.
 ChartLegend legend = chart.getLegend();
 legend.setPosition(LegendPosition.TOP_RIGHT);

 // Give other chart elements, such as the graph, more room by allowing them to overlap the legend.
 legend.setOverlay(true);

 doc.save(getArtifactsDir() + "Charts.ChartLegend.docx");
 

Returns: int - The corresponding int value. The returned value is one of LegendPosition constants.

getShapeType()

public int getShapeType()

Returns: int

isFillSupported()

public boolean isFillSupported()

Returns: boolean

isFormatDefined()

public boolean isFormatDefined()

Returns: boolean

materializeSpPr()

public void materializeSpPr()

setOverlay(boolean value)

public void setOverlay(boolean value)

Determines whether other chart elements shall be allowed to overlap legend. Default value is false .

Examples:

Shows how to edit the appearance of a chart’s legend.


 Document doc = new Document();
 DocumentBuilder builder = new DocumentBuilder(doc);

 Shape shape = builder.insertChart(ChartType.LINE, 450.0, 300.0);
 Chart chart = shape.getChart();

 Assert.assertEquals(3, chart.getSeries().getCount());
 Assert.assertEquals("Series 1", chart.getSeries().get(0).getName());
 Assert.assertEquals("Series 2", chart.getSeries().get(1).getName());
 Assert.assertEquals("Series 3", chart.getSeries().get(2).getName());

 // Move the chart's legend to the top right corner.
 ChartLegend legend = chart.getLegend();
 legend.setPosition(LegendPosition.TOP_RIGHT);

 // Give other chart elements, such as the graph, more room by allowing them to overlap the legend.
 legend.setOverlay(true);

 doc.save(getArtifactsDir() + "Charts.ChartLegend.docx");
 

Parameters:

ParameterTypeDescription
valuebooleanThe corresponding boolean value.

setPosition(int value)

public void setPosition(int value)

Specifies the position of the legend on a chart.

Remarks:

The default value is LegendPosition.RIGHT for pre-Word 2016 charts and LegendPosition.TOP for Word 2016 charts.

Examples:

Shows how to edit the appearance of a chart’s legend.


 Document doc = new Document();
 DocumentBuilder builder = new DocumentBuilder(doc);

 Shape shape = builder.insertChart(ChartType.LINE, 450.0, 300.0);
 Chart chart = shape.getChart();

 Assert.assertEquals(3, chart.getSeries().getCount());
 Assert.assertEquals("Series 1", chart.getSeries().get(0).getName());
 Assert.assertEquals("Series 2", chart.getSeries().get(1).getName());
 Assert.assertEquals("Series 3", chart.getSeries().get(2).getName());

 // Move the chart's legend to the top right corner.
 ChartLegend legend = chart.getLegend();
 legend.setPosition(LegendPosition.TOP_RIGHT);

 // Give other chart elements, such as the graph, more room by allowing them to overlap the legend.
 legend.setOverlay(true);

 doc.save(getArtifactsDir() + "Charts.ChartLegend.docx");
 

Parameters:

ParameterTypeDescription
valueintThe corresponding int value. The value must be one of LegendPosition constants.

setShapeType(int value)

public void setShapeType(int value)

Parameters:

ParameterTypeDescription
valueint