简体   繁体   中英

How do I remove an Excel Chart Legend Entry with C#?

I want to remove the legend entry for some, but not all, series in my Excel chart. From my experience it seems as if SeriesCollection.Item(index) and LegendEntries.Item(index) are not related. Given a series n how can I remove only the legend for that series?

I'm using Office Interop 2010 with Visual Studio 2010. This is easily accomplished via the GUI by selecting the legend entry, then right clicking and choosing "delete".

To delete a legend entry you have to know the index of the legend you want to delete. Unfortunately, there doesn't seem to be a relationship available through the interop api that exposes the relationship between legend and series. There is one hokey workaround however. To remove a legend for a specific series the approach that worked for me was to remove the legend immediately after adding the series. This is the only time that the legend index is known.

// .
// . code to add series
// .
// remove the legend entry for the newly added series
chart.Legend.LegendEntries(chart.Legend.LegendEntries().Count).Delete();

I know the original question was about Office Interop 2010, but this seems to be a good reference point for dynamically showing and hiding series in a chart too, so I'll add the following top in case it helps others.

If you want to show/hide a series on a chartobject on a worksheet in Office 2013 (in VBA at least; not sure about interop), you can do the following:

Worksheets("MySheetName").ChartObjects("MyChartName").Chart.FullSeriesCollection("MyLedendSeriesName").IsFiltered = false

This hides the series without removing it. Set it to true to show the series again.

I needed to remove the last two legend entries from a chart because they were fake series added to create a cross-hairs effect. One series made the vertical line and the other horizontal. After the legend entry was removed, the series remained on the chart, which is what I wanted. I used the code below:

Microsoft.Office.Interop.Excel.ChartObject chartObj = null;
Microsoft.Office.Interop.Excel.Chart chart = null;
Microsoft.Office.Interop.Excel.Legend legend = null;
Microsoft.Office.Interop.Excel.LegendEntries legendEntries = null;
Microsoft.Office.Interop.Excel.LegendEntry legendItem;

int legendEntryCount = 0;

chartObj = (Microsoft.Office.Interop.Excel.ChartObject) xlws.ChartObjects("Chart 1");
chart = chartObj.Chart;
legend = chart.Legend;
legendEntries = (Microsoft.Office.Interop.Excel.LegendEntries) chart.Legend.LegendEntries();
legendEntryCount = legendEntries.Count;
if (legendEntryCount > 2)
{
    legendItem = (Microsoft.Office.Interop.Excel.LegendEntry) legend.LegendEntries(legendEntryCount);
    legendItem.Delete();
    legendItem = (Microsoft.Office.Interop.Excel.LegendEntry) legend.LegendEntries(legendEntryCount - 1);
    legendItem.Delete();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM