diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java index 1066e6da81..937ed1bbb6 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java @@ -211,7 +211,7 @@ public abstract class XDDFChartData { public void plot() { if (categoryData.isNumeric()) { CTNumData cache = retrieveNumCache(getAxDS(), categoryData); - ((XDDFNumericalDataSource) categoryData).fillNumericalCache(cache); + categoryData.fillNumericalCache(cache); } else { CTStrData cache = retrieveStrCache(getAxDS(), categoryData); categoryData.fillStringCache(cache); diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java index 2a51f32bd3..3bfd18bbe8 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java @@ -21,6 +21,8 @@ package org.apache.poi.xddf.usermodel.chart; import org.apache.poi.util.Beta; import org.apache.poi.util.Internal; +import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData; +import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal; import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData; import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal; @@ -50,6 +52,44 @@ public interface XDDFDataSource { String getFormula(); + String getFormatCode(); + + /** + * @since POI 4.1.3 + */ + @Internal + default void fillNumericalCache(CTNumData cache) { + String formatCode = getFormatCode(); + if (formatCode == null) { + if (cache.isSetFormatCode()) { + cache.unsetFormatCode(); + } + } else { + cache.setFormatCode(formatCode); + } + cache.setPtArray(null); // unset old values + final int numOfPoints = getPointCount(); + int effectiveNumOfPoints = 0; + for (int i = 0; i < numOfPoints; ++i) { + Object value = getPointAt(i); + if (value != null) { + CTNumVal ctNumVal = cache.addNewPt(); + ctNumVal.setIdx(i); + ctNumVal.setV(value.toString()); + effectiveNumOfPoints++; + } + } + if (effectiveNumOfPoints == 0) { + cache.unsetPtCount(); + } else { + if (cache.isSetPtCount()) { + cache.getPtCount().setVal(numOfPoints); + } else { + cache.addNewPtCount().setVal(numOfPoints); + } + } + } + /** * @since POI 4.1.2 */ diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java index b2bf4f6806..c45fbaa6a7 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java @@ -45,6 +45,7 @@ public class XDDFDataSourcesFactory { if (categoryDS.getStrRef() == null) { return new XDDFCategoryDataSource() { private CTNumData category = (CTNumData) categoryDS.getNumRef().getNumCache().copy(); + private String formatCode = category.isSetFormatCode() ? category.getFormatCode() : null; @Override public boolean isCellRange() { @@ -70,6 +71,9 @@ public class XDDFDataSourcesFactory { public String getPointAt(int index) { return category.getPtArray(index).getV(); } + + @Override + public String getFormatCode() { return formatCode; } }; } else { return new XDDFCategoryDataSource() { @@ -94,6 +98,9 @@ public class XDDFDataSourcesFactory { public String getPointAt(int index) { return category.getPtArray(index).getV(); } + + @Override + public String getFormatCode() { return null; } }; } } @@ -287,6 +294,9 @@ public class XDDFDataSourcesFactory { public String getFormula() { return getDataRangeReference(); } + + @Override + public String getFormatCode() { return null; } } private static class LiteralNumericalArrayDataSource extends NumericalArrayDataSource { @@ -430,5 +440,8 @@ public class XDDFDataSourcesFactory { public boolean isNumeric() { return false; } + + @Override + public String getFormatCode() { return null; } } } diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFNumericalDataSource.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFNumericalDataSource.java index 74e1fb3364..95ef676462 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFNumericalDataSource.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFNumericalDataSource.java @@ -26,48 +26,10 @@ import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal; @Beta public interface XDDFNumericalDataSource extends XDDFDataSource { - String getFormatCode(); - void setFormatCode(String formatCode); @Override default boolean isLiteral() { return false; } - - /** - * @since POI 4.1.2 - */ - @Internal - default void fillNumericalCache(CTNumData cache) { - String formatCode = getFormatCode(); - if (formatCode == null) { - if (cache.isSetFormatCode()) { - cache.unsetFormatCode(); - } - } else { - cache.setFormatCode(formatCode); - } - cache.setPtArray(null); // unset old values - final int numOfPoints = getPointCount(); - int effectiveNumOfPoints = 0; - for (int i = 0; i < numOfPoints; ++i) { - Object value = getPointAt(i); - if (value != null) { - CTNumVal ctNumVal = cache.addNewPt(); - ctNumVal.setIdx(i); - ctNumVal.setV(value.toString()); - effectiveNumOfPoints++; - } - } - if (effectiveNumOfPoints == 0) { - cache.unsetPtCount(); - } else { - if (cache.isSetPtCount()) { - cache.getPtCount().setVal(numOfPoints); - } else { - cache.addNewPtCount().setVal(numOfPoints); - } - } - } }