]> source.dussan.org Git - poi.git/commitdiff
Error bar minus and plus as numerical data sources
authorAlain Béarez <abearez@apache.org>
Mon, 13 Jan 2020 00:27:06 +0000 (00:27 +0000)
committerAlain Béarez <abearez@apache.org>
Mon, 13 Jan 2020 00:27:06 +0000 (00:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1872692 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFErrorBars.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFNumericalDataSource.java

index 0034862eeb90564f590e4d754c6a355d04365348..1066e6da81212ad430c2f677e7ccf2904e4cd209 100644 (file)
@@ -33,11 +33,9 @@ import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;
-import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;
-import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTUnsignedInt;
 
 /**
@@ -211,16 +209,15 @@ public abstract class XDDFChartData {
         }
 
         public void plot() {
-            int numOfPoints = categoryData.getPointCount();
             if (categoryData.isNumeric()) {
                 CTNumData cache = retrieveNumCache(getAxDS(), categoryData);
-                fillNumCache(cache, numOfPoints, (XDDFNumericalDataSource<?>) categoryData);
+                ((XDDFNumericalDataSource<?>) categoryData).fillNumericalCache(cache);
             } else {
                 CTStrData cache = retrieveStrCache(getAxDS(), categoryData);
-                fillStringCache(cache, numOfPoints, categoryData);
+                categoryData.fillStringCache(cache);
             }
             CTNumData cache = retrieveNumCache(getNumDS(), valuesData);
-            fillNumCache(cache, numOfPoints, valuesData);
+            valuesData.fillNumericalCache(cache);
         }
 
         /**
@@ -343,61 +340,5 @@ public abstract class XDDFChartData {
             }
             return numCache;
         }
-
-        private void fillStringCache(CTStrData cache, int numOfPoints, XDDFDataSource<?> data) {
-            cache.setPtArray(null); // unset old values
-            int effectiveNumOfPoints = 0;
-            for (int i = 0; i < numOfPoints; ++i) {
-                Object value = data.getPointAt(i);
-                if (value != null) {
-                    CTStrVal ctStrVal = cache.addNewPt();
-                    ctStrVal.setIdx(i);
-                    ctStrVal.setV(value.toString());
-                    effectiveNumOfPoints++;
-                }
-            }
-            if (effectiveNumOfPoints == 0) {
-                if (cache.isSetPtCount()) {
-                    cache.unsetPtCount();
-                }
-            } else {
-                if (cache.isSetPtCount()) {
-                    cache.getPtCount().setVal(numOfPoints);
-                } else {
-                    cache.addNewPtCount().setVal(numOfPoints);
-                }
-            }
-        }
-
-        private void fillNumCache(CTNumData cache, int numOfPoints, XDDFNumericalDataSource<?> data) {
-            String formatCode = data.getFormatCode();
-            if (formatCode == null) {
-                if (cache.isSetFormatCode()) {
-                    cache.unsetFormatCode();
-                }
-            } else {
-                cache.setFormatCode(formatCode);
-            }
-            cache.setPtArray(null); // unset old values
-            int effectiveNumOfPoints = 0;
-            for (int i = 0; i < numOfPoints; ++i) {
-                Object value = data.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);
-                }
-            }
-        }
     }
 }
index 8d941bf11c2197ddf9a48ad8263160e23c678054..2a51f32bd376aacb7c1d71e2d97b718c57ab4dc4 100644 (file)
@@ -20,6 +20,9 @@
 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.CTStrData;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal;
 
 @Beta
 public interface XDDFDataSource<T> {
@@ -46,4 +49,34 @@ public interface XDDFDataSource<T> {
     String getDataRangeReference();
 
     String getFormula();
+
+    /**
+     * @since POI 4.1.2
+     */
+    @Internal
+    default void fillStringCache(CTStrData cache) {
+        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) {
+                CTStrVal ctStrVal = cache.addNewPt();
+                ctStrVal.setIdx(i);
+                ctStrVal.setV(value.toString());
+                effectiveNumOfPoints++;
+            }
+        }
+        if (effectiveNumOfPoints == 0) {
+            if (cache.isSetPtCount()) {
+                cache.unsetPtCount();
+            }
+        } else {
+            if (cache.isSetPtCount()) {
+                cache.getPtCount().setVal(numOfPoints);
+            } else {
+                cache.addNewPtCount().setVal(numOfPoints);
+            }
+        }
+    }
 }
index e3c70a675759b408a8ffc1d2d1a911f84488e05d..807afc080a536bb40ce14b375caf6da1aff9dbac 100644 (file)
@@ -22,6 +22,9 @@ import org.apache.poi.util.Internal;
 import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
 import org.apache.xmlbeans.XmlObject;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTErrBars;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;
 
 /**
  * @since POI 4.1.2
@@ -166,5 +169,61 @@ public class XDDFErrorBars {
         }
     }
 
-    // TODO handle minus and plus as numerical data sources
+    public XDDFNumericalDataSource<Double> getMinus() {
+        if (bars.isSetMinus()) {
+            return XDDFDataSourcesFactory.fromDataSource(bars.getMinus());
+        } else {
+            return null;
+        }
+    }
+
+    public void setMinus(XDDFNumericalDataSource<Double> ds) {
+        if (ds == null) {
+            if (bars.isSetMinus()) {
+                bars.unsetMinus();
+            }
+        } else {
+            if (bars.isSetMinus()) {
+                ds.fillNumericalCache(retrieveCache(bars.getMinus(), ds.getDataRangeReference()));
+            } else {
+                CTNumDataSource ctDS = bars.addNewMinus();
+                ctDS.addNewNumLit();
+                ds.fillNumericalCache(retrieveCache(ctDS, ds.getDataRangeReference()));
+            }
+        }
+    }
+
+    public XDDFNumericalDataSource<Double> getPlus() {
+        if (bars.isSetPlus()) {
+            return XDDFDataSourcesFactory.fromDataSource(bars.getPlus());
+        } else {
+            return null;
+        }
+    }
+
+    public void setPlus(XDDFNumericalDataSource<Double> ds) {
+        if (ds == null) {
+            if (bars.isSetPlus()) {
+                bars.unsetPlus();
+            }
+        } else {
+            if (bars.isSetPlus()) {
+                ds.fillNumericalCache(retrieveCache(bars.getPlus(), ds.getDataRangeReference()));
+            } else {
+                CTNumDataSource ctDS = bars.addNewPlus();
+                ctDS.addNewNumLit();
+                ds.fillNumericalCache(retrieveCache(ctDS, ds.getDataRangeReference()));
+            }
+        }
+    }
+
+    private CTNumData retrieveCache(CTNumDataSource ds, String dataRangeReference) {
+        if (ds.isSetNumRef()) {
+            CTNumRef numRef = ds.getNumRef();
+            numRef.setF(dataRangeReference);
+            return numRef.getNumCache();
+        } else {
+            return ds.getNumLit();
+        }
+    }
 }
index add2f3ccbf90bf4ac19497f30a0c0abbc41f452c..74e1fb33649fead34877fb482d89d2a69365ba52 100644 (file)
@@ -20,6 +20,9 @@
 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;
 
 @Beta
 public interface XDDFNumericalDataSource<T extends Number> extends XDDFDataSource<T> {
@@ -31,4 +34,40 @@ public interface XDDFNumericalDataSource<T extends Number> extends XDDFDataSourc
     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);
+            }
+        }
+    }
 }