From 8e00a427671a7eb0048e1c359a8e086d70e61c02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alain=20B=C3=A9arez?= Date: Wed, 1 Apr 2020 01:40:52 +0000 Subject: [PATCH] Remove chart title git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1875978 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xddf/usermodel/chart/XDDFChart.java | 13 ++++++++- .../usermodel/charts/TestXSSFChartTitle.java | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java index 77dbbceeba..828669a892 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java @@ -253,6 +253,9 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai chart.setAutoTitleDeleted(CTBoolean.Factory.newInstance()); } chart.getAutoTitleDeleted().setVal(deleted); + if (deleted && chart.isSetTitle()) { + chart.unsetTitle(); + } } /** @@ -321,7 +324,15 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai } } - /** + /** + * Remove the chart title. + * @since POI 4.1.3 + */ + public void removeTitle() { + setAutoTitleDeleted(true); + } + + /** * Get or Add chart 3D view into chart * * @return this method will add 3D view diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestXSSFChartTitle.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestXSSFChartTitle.java index d0388175e1..a5608f447b 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestXSSFChartTitle.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestXSSFChartTitle.java @@ -137,6 +137,7 @@ public class TestXSSFChartTitle { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_withTitle.xlsx"); XSSFChart chart = getChartFromWorkbook(wb, "Sheet1"); assertNotNull(chart); + assertNotNull(chart.getTitle()); XSSFRichTextString originalTitle = chart.getTitleText(); assertNotNull(originalTitle); final String myTitle = "My chart title"; @@ -154,6 +155,7 @@ public class TestXSSFChartTitle { XSSFChart chart = getChartFromWorkbook(wb, "Sheet1"); assertNotNull(chart); assertNull(chart.getTitleText()); + assertNull(chart.getTitle()); final String myTitle = "My chart title"; chart.setTitleText(myTitle); XSSFRichTextString queryTitle = chart.getTitleText(); @@ -176,4 +178,30 @@ public class TestXSSFChartTitle { wb.close(); } + @Test + public void testRemovingFromExistingChartNoTitle() throws IOException { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_noTitle.xlsx"); + XSSFChart chart = getChartFromWorkbook(wb, "Sheet1"); + assertNotNull(chart); + assertNull(chart.getTitleText()); + assertNull(chart.getTitle()); + chart.removeTitle(); + assertNull(chart.getTitleText()); + assertNull(chart.getTitle()); + wb.close(); + } + + @Test + public void testRemovingFromExistingChartWithTitle() throws IOException { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_withTitle.xlsx"); + XSSFChart chart = getChartFromWorkbook(wb, "Sheet1"); + assertNotNull(chart); + assertNotNull(chart.getTitleText()); + assertNotNull(chart.getTitle()); + chart.removeTitle(); + assertNull(chart.getTitleText()); + assertNull(chart.getTitle()); + wb.close(); + } + }