[]
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);
voiddelete()getFont()IFontFormat object that represents the font of the legend.booleanIChart that contains this legend.voidsetIncludeInLayout(boolean value) voidsetPosition(LegendPosition value) 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());
IFontFormat object that represents the font 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());
IChartFormat object that represents the legend formatting.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();
IChart of the legend.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();
true if the legend occupies chart layout space when the chart layout is determined; otherwise, false.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);
value - true if the legend occupies chart layout space when the chart layout is determined; otherwise, false.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();
LegendPosition that specifies the legend position 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);
value - The legend position.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();
ILegendEntries collection that represents the legend entries in this legend.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();