[]
        
(Showing Draft Content)

ILegend

Interface ILegend


public interface ILegend
Represents the legend in a chart.

A legend identifies the data series or points plotted in a chart and can be used to control legend position, layout participation, formatting, and legend entries. Use IChart.getLegend() to access the legend for a chart, then customize it through members such as getFont(), getFormat(), getLegendEntries(), getPosition(), and setPosition(LegendPosition).


 worksheet.getRange("A1:C3").setValue(new Object[][] {
     {"Item", "Sales", "Cost"},
     {"North", 100, 80},
     {"South", 120, 90}
 });
 IShape shape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 20, 20, 320, 220);
 shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:C3"), RowCol.Columns, true, true);
 shape.getChart().setHasLegend(true);
 ILegend legend = shape.getChart().getLegend();
 legend.setPosition(LegendPosition.Left);
 
  • Method Details

    • getFont

      IFontFormat getFont()
      Gets the IFontFormat object that represents the font of the legend.

      Use the returned object to access and modify font settings for the legend text, such as color, size, and italic style.

      
       IChart chart = worksheet.getShapes().addChart(ChartType.ColumnClustered, 20, 20, 300, 200).getChart();
       ILegend legend = chart.getLegend();
       IFontFormat font = legend.getFont();
       font.getColor().setRGB(Color.GetRed());
       
      Returns:
      The IFontFormat object that represents the font of the legend.
    • getFormat

      IChartFormat getFormat()
      Gets the chart format of the legend.

      The returned IChartFormat object provides access to the legend's fill and line formatting.

      
       IChart chart = worksheet.getShapes().addChart(ChartType.ColumnClustered, 20, 20, 300, 200).getChart();
       ILegend legend = chart.getLegend();
       IChartFormat format = legend.getFormat();
       format.getFill().getColor().setRGB(Color.GetRed());
       
      Returns:
      The IChartFormat object that represents the legend formatting.
    • getParent

      IChart getParent()
      Gets the parent IChart that contains this legend.

      Use this method to access the chart that owns the current legend.

      
       IChart chart = worksheet.getShapes().addChart(ChartType.ColumnClustered, 20, 20, 300, 200).getChart();
       ILegend legend = chart.getLegend();
       IChart parentChart = legend.getParent();
       
      Returns:
      The parent IChart of the legend.
    • getIncludeInLayout

      boolean getIncludeInLayout()
      Gets whether the legend is included in the chart layout.

      If this property is true, the legend occupies chart layout space when the chart layout is determined. The default value is true.

      
       IChart chart = worksheet.getShapes().addChart(ChartType.ColumnClustered, 20, 20, 300, 200).getChart();
       ILegend legend = chart.getLegend();
       legend.setIncludeInLayout(false);
       boolean includeInLayout = legend.getIncludeInLayout();
       
      Returns:
      true if the legend occupies chart layout space when the chart layout is determined; otherwise, false.
    • setIncludeInLayout

      void setIncludeInLayout(boolean value)
      Sets whether the legend is included in the chart layout.

      If this property is true, the legend occupies chart layout space when the chart layout is determined. The default value is true.

      
       IChart chart = worksheet.getShapes().addChart(ChartType.ColumnClustered, 20, 20, 300, 200).getChart();
       ILegend legend = chart.getLegend();
       legend.setIncludeInLayout(false);
       
      Parameters:
      value - true if the legend occupies chart layout space when the chart layout is determined; otherwise, false.
    • getPosition

      LegendPosition getPosition()
      Gets the position of the legend on the chart.

      Use the returned LegendPosition value to determine whether the legend is displayed above, below, to the left, or to the right of the chart, or uses a custom position.

      
       worksheet.getRange("A1:B3").setValue(new Object[][] {
           {"Month", "Sales"},
           {"Jan", 100},
           {"Feb", 120}
       });
       IChart chart = worksheet.getShapes().addChart(ChartType.ColumnClustered, 20, 20, 300, 200).getChart();
       chart.setSourceData(worksheet.getRange("A1:B3"));
       ILegend legend = chart.getLegend();
       LegendPosition position = legend.getPosition();
       
      Returns:
      The LegendPosition that specifies the legend position on the chart.
    • setPosition

      void setPosition(LegendPosition value)
      Sets the position of the legend on the chart.

      Use a LegendPosition value to place the legend above, below, to the left, or to the right of the chart, or to use a custom position.

      
       worksheet.getRange("A1:B3").setValue(new Object[][] {
           {"Month", "Sales"},
           {"Jan", 100},
           {"Feb", 120}
       });
       IChart chart = worksheet.getShapes().addChart(ChartType.ColumnClustered, 20, 20, 300, 200).getChart();
       chart.setSourceData(worksheet.getRange("A1:B3"));
       ILegend legend = chart.getLegend();
       legend.setPosition(LegendPosition.Right);
       
      Parameters:
      value - The legend position.
    • getLegendEntries

      ILegendEntries getLegendEntries()
      Gets the collection of legend entries in this legend.

      Use the returned ILegendEntries object to access individual ILegendEntry objects or to retrieve the number of entries in the legend.

      
       worksheet.getRange("A1:B3").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 10},
           {"B", 20}
       });
       IChart chart = worksheet.getShapes().addChart(ChartType.ColumnClustered, 20, 20, 300, 200).getChart();
       chart.setSourceData(worksheet.getRange("A1:B3"));
       ILegendEntries legendEntries = chart.getLegend().getLegendEntries();
       
      Returns:
      The ILegendEntries collection that represents the legend entries in this legend.
    • delete

      void delete()
      Deletes the legend from its parent chart.

      After this method is called, the chart no longer displays this legend.

      
       IChart chart = worksheet.getShapes().addChart(ChartType.ColumnClustered, 20, 20, 300, 200).getChart();
       ILegend legend = chart.getLegend();
       legend.delete();