diff --git src/java/org/apache/poi/ss/usermodel/charts/ChartSerie.java src/java/org/apache/poi/ss/usermodel/charts/ChartSerie.java new file mode 100644 index 0000000..a921109 --- /dev/null +++ src/java/org/apache/poi/ss/usermodel/charts/ChartSerie.java @@ -0,0 +1,57 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.usermodel.charts; + +import org.apache.poi.ss.util.CellReference; + +/** + * Basic settings for all chart series. + * + * @author Martin Andersson + */ +public interface ChartSerie { + + /** + * Sets the title of the series as a string literal. + * + * @param title + */ + void setTitle(String title); + + /** + * Sets the title of the series as a cell reference. + * + * @param titleReference + */ + void setTitle(CellReference titleReference); + + /** + * @return title as string literal. + */ + String getTitleString(); + + /** + * @return title as cell reference. + */ + CellReference getTitleCellReference(); + + /** + * @return title type. + */ + TitleType getTitleType(); +} diff --git src/java/org/apache/poi/ss/usermodel/charts/LineChartSerie.java src/java/org/apache/poi/ss/usermodel/charts/LineChartSerie.java index 7d34e4e..11017f4 100644 --- src/java/org/apache/poi/ss/usermodel/charts/LineChartSerie.java +++ src/java/org/apache/poi/ss/usermodel/charts/LineChartSerie.java @@ -26,7 +26,7 @@ import org.apache.poi.util.Beta; * @author Martin Andersson */ @Beta -public interface LineChartSerie { +public interface LineChartSerie extends ChartSerie { /** * @return data source used for category axis data. diff --git src/java/org/apache/poi/ss/usermodel/charts/ScatterChartSerie.java src/java/org/apache/poi/ss/usermodel/charts/ScatterChartSerie.java index c968f79..ab84a56 100644 --- src/java/org/apache/poi/ss/usermodel/charts/ScatterChartSerie.java +++ src/java/org/apache/poi/ss/usermodel/charts/ScatterChartSerie.java @@ -25,7 +25,7 @@ import org.apache.poi.util.Beta; * @author Roman Kashitsyn */ @Beta -public interface ScatterChartSerie { +public interface ScatterChartSerie extends ChartSerie { /** * @return data source used for X axis values diff --git src/java/org/apache/poi/ss/usermodel/charts/TitleType.java src/java/org/apache/poi/ss/usermodel/charts/TitleType.java new file mode 100644 index 0000000..4a3d497 --- /dev/null +++ src/java/org/apache/poi/ss/usermodel/charts/TitleType.java @@ -0,0 +1,28 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.usermodel.charts; + +/** + * Title types for charts. + * + * @author Martin Andersson + */ +public enum TitleType { + STRING, + CELL_REFERENCE; +} diff --git src/ooxml/java/org/apache/poi/xssf/usermodel/charts/AbstractXSSFChartSerie.java src/ooxml/java/org/apache/poi/xssf/usermodel/charts/AbstractXSSFChartSerie.java new file mode 100644 index 0000000..88763f4 --- /dev/null +++ src/ooxml/java/org/apache/poi/xssf/usermodel/charts/AbstractXSSFChartSerie.java @@ -0,0 +1,80 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.xssf.usermodel.charts; + +import org.apache.poi.ss.usermodel.charts.ChartSerie; +import org.apache.poi.ss.usermodel.charts.TitleType; +import org.apache.poi.ss.util.CellReference; +import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx; + +/** + * + * @author Martin Andersson + */ +public abstract class AbstractXSSFChartSerie implements ChartSerie { + + private String titleValue; + private CellReference titleRef; + private TitleType titleType; + + public void setTitle(CellReference titleReference) { + titleType = TitleType.CELL_REFERENCE; + titleRef = titleReference; + } + + public void setTitle(String title) { + titleType = TitleType.STRING; + titleValue = title; + } + + public CellReference getTitleCellReference() { + if (TitleType.CELL_REFERENCE.equals(titleType)) { + return titleRef; + } + throw new IllegalStateException("Title type is not CellReference."); + } + + public String getTitleString() { + if (TitleType.STRING.equals(titleType)) { + return titleValue; + } + throw new IllegalStateException("Title type is not String."); + } + + public TitleType getTitleType() { + return titleType; + } + + protected boolean isTitleSet() { + return titleType != null; + } + + protected CTSerTx getCTSerTx() { + CTSerTx tx = CTSerTx.Factory.newInstance(); + switch (titleType) { + case CELL_REFERENCE: + tx.addNewStrRef().setF(titleRef.formatAsString()); + return tx; + case STRING: + tx.setV(titleValue); + return tx; + default: + throw new IllegalStateException("Unkown title type: " + titleType); + } + } +} diff --git src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFLineChartData.java src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFLineChartData.java index 1fb8b56..06ebab9 100644 --- src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFLineChartData.java +++ src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFLineChartData.java @@ -50,7 +50,7 @@ public class XSSFLineChartData implements LineChartData { series = new ArrayList(); } - static class Serie implements LineChartSerie { + static class Serie extends AbstractXSSFChartSerie implements LineChartSerie { private int id; private int order; private ChartDataSource categories; @@ -85,6 +85,10 @@ public class XSSFLineChartData implements LineChartData { XSSFChartUtil.buildAxDataSource(catDS, categories); CTNumDataSource valueDS = ctLineSer.addNewVal(); XSSFChartUtil.buildNumDataSource(valueDS, values); + + if (isTitleSet()) { + ctLineSer.setTx(getCTSerTx()); + } } } diff --git src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFScatterChartData.java src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFScatterChartData.java index 2387627..088e6fc 100644 --- src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFScatterChartData.java +++ src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFScatterChartData.java @@ -50,7 +50,7 @@ public class XSSFScatterChartData implements ScatterChartData { /** * Package private ScatterChartSerie implementation. */ - static class Serie implements ScatterChartSerie { + static class Serie extends AbstractXSSFChartSerie implements ScatterChartSerie { private int id; private int order; private ChartDataSource xs; @@ -92,6 +92,10 @@ public class XSSFScatterChartData implements ScatterChartData { CTNumDataSource yVal = scatterSer.addNewYVal(); XSSFChartUtil.buildNumDataSource(yVal, ys); + + if (isTitleSet()) { + scatterSer.setTx(getCTSerTx()); + } } } diff --git src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestAbstractXSSFChartSerie.java src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestAbstractXSSFChartSerie.java new file mode 100644 index 0000000..e9276ed --- /dev/null +++ src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestAbstractXSSFChartSerie.java @@ -0,0 +1,49 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.xssf.usermodel.charts; + +import junit.framework.TestCase; + +import org.apache.poi.ss.usermodel.charts.TitleType; +import org.apache.poi.ss.util.CellReference; + +/** + * + * @author Martin Andersson + */ +public class TestAbstractXSSFChartSerie extends TestCase { + + public void testTitleAccessorMethods() { + AbstractXSSFChartSerie serie = new AbstractXSSFChartSerie() {}; + + assertFalse(serie.isTitleSet()); + + serie.setTitle("title"); + assertTrue(serie.isTitleSet()); + assertNotNull(serie.getCTSerTx()); + assertEquals(TitleType.STRING, serie.getTitleType()); + assertEquals("title", serie.getTitleString()); + + CellReference cellRef = new CellReference("Sheet1!A1"); + serie.setTitle(cellRef); + assertTrue(serie.isTitleSet()); + assertNotNull(serie.getCTSerTx()); + assertEquals(TitleType.CELL_REFERENCE, serie.getTitleType()); + assertEquals(cellRef, serie.getTitleCellReference()); + } +}