Skip to main content

How to: Show or Hide the Chart Legend or the Legend Entry

  • 3 minutes to read

After you create a chart, its legend appears. A chart legend is a box that identifies data series displayed on a chart. In most cases, the legend displays series names, but in a pie or doughnut chart it shows data points of a single series. The legend also adds a sample of the line style, color, and fill pattern used to draw the series on the chart.

SpreadsheetChart_ChartLegend

The Legend object defines a chart legend. Use the ChartObject.Legend property to retrieve the Legend object. To specify the legend placement, use the Legend.Position property. Set the Legend.Overlay property to true to save space in the chart and enable the legend overlay. To remove the legend, set the Legend.Visible property to false.

View Example

Worksheet worksheet = workbook.Worksheets["chartTask3"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Create a chart and specify its location.
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered, worksheet["B2:F6"]);
chart.TopLeftCell = worksheet.Cells["H2"];
chart.BottomRightCell = worksheet.Cells["N14"];

// Specify the position of the legend.
chart.Legend.Position = LegendPosition.Bottom;

Change a Legend Entry

The Legend.CustomEntries property obtains the collection of customized legend entries (LegendEntryCollection). You can use this collection to modify the individual legend entries.

To hide a legend entry, add a LegendEntry instance to the collection with the index set to the index of the selected entry. Set the LegendEntry.Hidden property to true. The LegendEntry.Font property allows you to change the font attributes of an individual entry.

Note

The SpreadsheetControl does not display an individual font for each legend entry specified by the LegendEntry.Font property. The font specified by the Legend.Font property is applied to all entries. However, the LegendEntry.Font property can be retrieved in code, exported in supported formats, and visualized in Microsoft Excel.

If the Legend.Font property is not specified, but one or multiple of the legend entry has the Font property specified, the font is retrieved from the first entry in the collection.

The example below removes entries from the chart legend.

View Example

Worksheet worksheet = workbook.Worksheets["chartTask3"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Create a chart and specify its location.
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered, worksheet["B2:F6"]);
chart.TopLeftCell = worksheet.Cells["H2"];
chart.BottomRightCell = worksheet.Cells["N14"];

// Exclude entries from the legend.
chart.Legend.CustomEntries.Add(2).Hidden = true;
chart.Legend.CustomEntries.Add(3).Hidden = true;
See Also