简体   繁体   English

删除特定图例 apache poi excel 图 XDDFChartLegend

[英]remove specific legend apache poi excel graph XDDFChartLegend

i am using apache poi 5.0.0 version i have generated a graph in excel using the data and i am able to see the legends in the bottom of the graph.我正在使用 apache poi 5.0.0 版本我已经使用数据在 excel 中生成了一个图表,我能够在图表底部看到图例。 There are six legends shown.显示了六个图例。 Now i want to remove two specific legends without removing them from the graph.现在我想删除两个特定的图例而不将它们从图表中删除。

在此处输入图像描述

There seems to be no functions available in XDDFChartLegend which really works. XDDFChartLegend 中似乎没有真正有效的功能。 say for example比如说

XDDFChartLegend  legend = chart.getOrAddLegend();
legend.getEntries().remove(4);

doesnt work不起作用

Any help would be appreciated.任何帮助,将不胜感激。

A chart legend entry cannot be removed.无法删除图表图例条目。 It only can be marked deleted, so it will not be shown.它只能被标记为已删除,因此不会显示。

XDDFLegendEntry provides method setDelete to do so. XDDFLegendEntry提供了方法setDelete来做到这一点。 But the problem is how to get the XDDFLegendEntry .但问题是如何获得XDDFLegendEntry

XDDFChart.getOrAddLegend only adds an empty legend marker which uses defaults for showing the legend. XDDFChart.getOrAddLegend只添加一个空的图例标记,它使用默认值来显示图例。 There are no legend entries by default as those only are needed to set special properties which are not default.默认情况下没有图例条目,因为只需要设置非默认的特殊属性。 So we would need someting like XDDFChartLegend.getOrAddLegendEntry to get or add a legend entry.所以我们需要像XDDFChartLegend.getOrAddLegendEntry这样的东西来获取或添加图例条目。 This does not exist until now.这直到现在才存在。

Following method gets or adds a legend entry to a given XDDFChartLegend for a given index.以下方法获取或添加图例条目到给定索引的给定XDDFChartLegend

 XDDFLegendEntry getOrAddLegendEntry(XDDFChartLegend legend, long index) {
  XDDFLegendEntry legendEntry = null;
  for (XDDFLegendEntry storedLegendEntry : legend.getEntries()) {
   if (storedLegendEntry.getIndex() == index) {
    legendEntry = storedLegendEntry;
    break;
   }   
  }
  if (legendEntry == null) {
   legendEntry = legend.addEntry();
   legendEntry.setIndex(index); 
  }
  return legendEntry;
 } 

Note, I do not use XDDFChartLegend.getEntry as this returns the entry at position index rather than the entry having the index index .请注意,我不使用XDDFChartLegend.getEntry ,因为它返回position index处的条目,而不是具有索引index的条目。 But we need the entry having the index index .但是我们需要具有索引index的条目。 So I loop over all entries and do check whether there is one having the index already.所以我遍历所有条目并检查是否已经有索引。

Usage to mark legend entry as deleted could be as so:将图例条目标记为已删除的用法如下:

...
   XDDFChartLegend legend = chart.getOrAddLegend();
   //...
   XDDFLegendEntry legendEntry = getOrAddLegendEntry(legend, 4);
   legendEntry.setDelete(true);
...

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

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