com.aspose.words
Class ChartLegend

java.lang.Object
    extended by com.aspose.words.ChartLegend
All Implemented Interfaces:
java.lang.Cloneable

public class ChartLegend 
extends java.lang.Object

Represents chart legend properties.

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

Example:

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");

Property Getters/Setters Summary
FontgetFont()
           Provides access to the default font formatting of legend entries. To override the font formatting for a specific legend entry, use theChartLegendEntry.Font property.
ChartFormatgetFormat()
           Provides access to fill and line formatting of the legend.
ChartLegendEntryCollectiongetLegendEntries()
           Returns a collection of legend entries for all series and trendlines of the parent chart.
booleangetOverlay()
voidsetOverlay(boolean value)
           Determines whether other chart elements shall be allowed to overlap legend. Default value is false.
intgetPosition()
voidsetPosition(int value)
           Specifies the position of the legend on a chart. The value of the property is LegendPosition integer constant.
 

Property Getters/Setters Detail

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 theChartLegendEntry.Font property.

Example:

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");

getFormat

public ChartFormat getFormat()
Provides access to fill and line formatting of the legend.

Example:

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");

getLegendEntries

public ChartLegendEntryCollection getLegendEntries()
Returns a collection of legend entries for all series and trendlines of the parent chart.

Example:

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");

getOverlay/setOverlay

public boolean getOverlay() / public void setOverlay(boolean value)
Determines whether other chart elements shall be allowed to overlap legend. Default value is false.

Example:

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");

getPosition/setPosition

public int getPosition() / public void setPosition(int value)
Specifies the position of the legend on a chart. The value of the property is LegendPosition integer constant. The default value is LegendPosition.RIGHT for pre-Word 2016 charts and LegendPosition.TOP for Word 2016 charts.

Example:

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");

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.