简体   繁体   English

如何使用C#删除Excel图表图例条目?

[英]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. 我想删除我的Excel图表中某些(但不是全部)系列的图例条目。 From my experience it seems as if SeriesCollection.Item(index) and LegendEntries.Item(index) are not related. 根据我的经验,似乎SeriesCollection.Item(index)LegendEntries.Item(index)无关。 Given a series n how can I remove only the legend for that series? 给定一个系列n ,我如何只删除该系列的图例?

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". 我将Office Interop 2010与Visual Studio 2010一起使用。这可以通过GUI轻松完成,方法是选择图例条目,然后右键单击并选择“删除”。

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. 不幸的是,似乎没有可通过interop api获得的关系来揭示图例和系列之间的关系。 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. 我知道最初的问题是关于Office Interop 2010的,但这似乎也是在图表中动态显示和隐藏系列的一个很好的参考点,因此我将添加以下顶部,以防对他人有所帮助。

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: 如果要在Office 2013中的工作表上的图表对象上显示/隐藏系列(至少在VBA中;不确定互操作性),可以执行以下操作:

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. 将其设置为true可再次显示该系列。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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