]> source.dussan.org Git - poi.git/commitdiff
[bug-57369] support for major and minor unit on XDDFChart axes
authorAlain Béarez <abearez@apache.org>
Mon, 12 Feb 2018 12:59:38 +0000 (12:59 +0000)
committerAlain Béarez <abearez@apache.org>
Mon, 12 Feb 2018 12:59:38 +0000 (12:59 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1823963 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFCategoryAxis.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartAxis.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDateAxis.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSeriesAxis.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFValueAxis.java

index 4a673f7d33328974d07691bdedce790138691504..52fe4223f3286c01bdbbfd9b08f5ce6d05d93f3f 100644 (file)
@@ -79,6 +79,36 @@ public class XDDFCategoryAxis extends XDDFChartAxis {
         return new XDDFShapeProperties(properties);
     }
 
+    @Override
+    public boolean isSetMinorUnit() {
+        return false;
+    }
+
+    @Override
+    public void setMinorUnit(double minor) {
+        // nothing
+    }
+
+    @Override
+    public double getMinorUnit() {
+        return Double.NaN;
+    }
+
+    @Override
+    public boolean isSetMajorUnit() {
+        return false;
+    }
+
+    @Override
+    public void setMajorUnit(double major) {
+        // nothing
+    }
+
+    @Override
+    public double getMajorUnit() {
+        return Double.NaN;
+    }
+
     @Override
     public void crossAxis(XDDFChartAxis axis) {
         ctCatAx.getCrossAx().setVal(axis.getId());
index f86d2174700408bb81edfbe39c99388e7a7213ba..99a117fd36a98d7764a06afb3bea56eadf89af58 100644 (file)
@@ -62,6 +62,7 @@ import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTRadarChart;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterChart;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerAx;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTSurface;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
 import org.openxmlformats.schemas.drawingml.x2006.chart.ChartSpaceDocument;
@@ -359,18 +360,47 @@ public abstract class XDDFChart extends POIXMLDocumentPart {
     }
 
     private void parseAxes() {
-        // TODO: add other axis types
         for (CTCatAx catAx : chart.getPlotArea().getCatAxArray()) {
             axes.add(new XDDFCategoryAxis(catAx));
         }
         for (CTDateAx dateAx : chart.getPlotArea().getDateAxArray()) {
             axes.add(new XDDFDateAxis(dateAx));
         }
+        for (CTSerAx serAx : chart.getPlotArea().getSerAxArray()) {
+            axes.add(new XDDFSeriesAxis(serAx));
+        }
         for (CTValAx valAx : chart.getPlotArea().getValAxArray()) {
             axes.add(new XDDFValueAxis(valAx));
         }
     }
 
+    /**
+     * Set value range (basic Axis Options)
+     * @param axisIndex 0 - primary axis, 1 - secondary axis
+     * @param minimum minimum value; Double.NaN - automatic; null - no change
+     * @param maximum maximum value; Double.NaN - automatic; null - no change
+     * @param majorUnit major unit value; Double.NaN - automatic; null - no change
+     * @param minorUnit minor unit value; Double.NaN - automatic; null - no change
+     */
+    public void setValueRange(int axisIndex, Double minimum, Double maximum, Double majorUnit, Double minorUnit) {
+        XDDFChartAxis axis = getAxes().get(axisIndex);
+        if (axis == null) {
+            return;
+        }
+        if (minimum != null) {
+            axis.setMinimum(minimum);
+        }
+        if (maximum != null) {
+            axis.setMaximum(maximum);
+        }
+        if (majorUnit != null) {
+            axis.setMajorUnit(majorUnit);
+        }
+        if (minorUnit != null) {
+            axis.setMinorUnit(minorUnit);
+        }
+    }
+
     /**
      * method to create relationship with embedded part
      * for example writing xlsx file stream into output stream
index cd1c6f01ab08b37a8a01015e4a52cd45521c7ef6..b08b2fb47a651940f4c454d6d4fbf1fc93bdd294 100644 (file)
@@ -24,7 +24,6 @@ import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartLines;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
-import org.openxmlformats.schemas.drawingml.x2006.chart.CTLogBase;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
@@ -57,6 +56,38 @@ public abstract class XDDFChartAxis implements HasShapeProperties {
 
     public abstract XDDFShapeProperties getOrAddMinorGridProperties();
 
+    /**
+     * @return true if minor unit value is defined, false otherwise
+     */
+    public abstract boolean isSetMinorUnit();
+
+    /**
+     * @param minor
+     *            axis minor unit
+     */
+    public abstract void setMinorUnit(double minor);
+
+    /**
+     * @return axis minor unit or NaN if not set
+     */
+    public abstract double getMinorUnit();
+
+    /**
+     * @return true if major unit value is defined, false otherwise
+     */
+    public abstract boolean isSetMajorUnit();
+
+    /**
+     * @param major
+     *            axis major unit
+     */
+    public abstract void setMajorUnit(double major);
+
+    /**
+     * @return axis major unit or NaN if not set
+     */
+    public abstract double getMajorUnit();
+
     /**
      * @return axis id
      */
@@ -132,14 +163,14 @@ public abstract class XDDFChartAxis implements HasShapeProperties {
     }
 
     /**
-     * @return axis log base or 0.0 if not set
+     * @return axis log base or NaN if not set
      */
     public double getLogBase() {
-        CTLogBase logBase = getCTScaling().getLogBase();
-        if (logBase != null) {
-            return logBase.getVal();
+        CTScaling scaling = getCTScaling();
+        if (scaling.isSetLogBase()) {
+            return scaling.getLogBase().getVal();
         }
-        return 0.0;
+        return Double.NaN;
     }
 
     /**
@@ -155,22 +186,28 @@ public abstract class XDDFChartAxis implements HasShapeProperties {
      */
     public void setMinimum(double min) {
         CTScaling scaling = getCTScaling();
-        if (scaling.isSetMin()) {
-            scaling.getMin().setVal(min);
+        if (Double.isNaN(min)) {
+            if (scaling.isSetMin()) {
+                scaling.unsetMin();
+            }
         } else {
-            scaling.addNewMin().setVal(min);
+            if (scaling.isSetMin()) {
+                scaling.getMin().setVal(min);
+            } else {
+                scaling.addNewMin().setVal(min);
+            }
         }
     }
 
     /**
-     * @return axis minimum or 0.0 if not set
+     * @return axis minimum or NaN if not set
      */
     public double getMinimum() {
         CTScaling scaling = getCTScaling();
         if (scaling.isSetMin()) {
             return scaling.getMin().getVal();
         } else {
-            return 0.0;
+            return Double.NaN;
         }
     }
 
@@ -187,22 +224,28 @@ public abstract class XDDFChartAxis implements HasShapeProperties {
      */
     public void setMaximum(double max) {
         CTScaling scaling = getCTScaling();
-        if (scaling.isSetMax()) {
-            scaling.getMax().setVal(max);
+        if (Double.isNaN(max)) {
+            if (scaling.isSetMax()) {
+                scaling.unsetMax();
+            }
         } else {
-            scaling.addNewMax().setVal(max);
+            if (scaling.isSetMax()) {
+                scaling.getMax().setVal(max);
+            } else {
+                scaling.addNewMax().setVal(max);
+            }
         }
     }
 
     /**
-     * @return axis maximum or 0.0 if not set
+     * @return axis maximum or NaN if not set
      */
     public double getMaximum() {
         CTScaling scaling = getCTScaling();
         if (scaling.isSetMax()) {
             return scaling.getMax().getVal();
         } else {
-            return 0.0;
+            return Double.NaN;
         }
     }
 
index 95f7d74f4cb327060817b682a77cf4622bd1a0f2..bde26ffb7e39e61ade2f5bf71ecd9ddf1ecdf71a 100644 (file)
@@ -82,6 +82,64 @@ public class XDDFDateAxis extends XDDFChartAxis {
         return new XDDFShapeProperties(properties);
     }
 
+    @Override
+    public boolean isSetMinorUnit() {
+        return ctDateAx.isSetMinorUnit();
+    }
+
+    @Override
+    public void setMinorUnit(double minor) {
+        if (Double.isNaN(minor)) {
+            if (ctDateAx.isSetMinorUnit()) {
+                ctDateAx.unsetMinorUnit();
+            }
+        } else {
+            if (ctDateAx.isSetMinorUnit()) {
+                ctDateAx.getMinorUnit().setVal(minor);
+            } else {
+                ctDateAx.addNewMinorUnit().setVal(minor);
+            }
+        }
+    }
+
+    @Override
+    public double getMinorUnit() {
+        if (ctDateAx.isSetMinorUnit()) {
+            return ctDateAx.getMinorUnit().getVal();
+        } else {
+            return Double.NaN;
+        }
+    }
+
+    @Override
+    public boolean isSetMajorUnit() {
+        return ctDateAx.isSetMajorUnit();
+    }
+
+    @Override
+    public void setMajorUnit(double major) {
+        if (Double.isNaN(major)) {
+            if (ctDateAx.isSetMajorUnit()) {
+                ctDateAx.unsetMajorUnit();
+            }
+        } else {
+            if (ctDateAx.isSetMajorUnit()) {
+                ctDateAx.getMajorUnit().setVal(major);
+            } else {
+                ctDateAx.addNewMajorUnit().setVal(major);
+            }
+        }
+    }
+
+    @Override
+    public double getMajorUnit() {
+        if (ctDateAx.isSetMajorUnit()) {
+            return ctDateAx.getMajorUnit().getVal();
+        } else {
+            return Double.NaN;
+        }
+    }
+
     @Override
     public void crossAxis(XDDFChartAxis axis) {
         ctDateAx.getCrossAx().setVal(axis.getId());
diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSeriesAxis.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSeriesAxis.java
new file mode 100644 (file)
index 0000000..37ec0dd
--- /dev/null
@@ -0,0 +1,190 @@
+/* ====================================================================
+   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.xddf.usermodel.chart;
+
+import org.apache.poi.util.Beta;
+import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartLines;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerAx;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickMark;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTUnsignedInt;
+import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
+
+@Beta
+public class XDDFSeriesAxis extends XDDFChartAxis {
+
+    private CTSerAx ctSerAx;
+
+    public XDDFSeriesAxis(CTPlotArea plotArea, AxisPosition position) {
+        initializeAxis(plotArea, position);
+    }
+
+    public XDDFSeriesAxis(CTSerAx ctSerAx) {
+        this.ctSerAx = ctSerAx;
+    }
+
+    @Override
+    public XDDFShapeProperties getOrAddMajorGridProperties() {
+        CTChartLines majorGridlines;
+        if (ctSerAx.isSetMajorGridlines()) {
+            majorGridlines = ctSerAx.getMajorGridlines();
+        } else {
+            majorGridlines = ctSerAx.addNewMajorGridlines();
+        }
+        return new XDDFShapeProperties(getOrAddLinesProperties(majorGridlines));
+    }
+
+    @Override
+    public XDDFShapeProperties getOrAddMinorGridProperties() {
+        CTChartLines minorGridlines;
+        if (ctSerAx.isSetMinorGridlines()) {
+            minorGridlines = ctSerAx.getMinorGridlines();
+        } else {
+            minorGridlines = ctSerAx.addNewMinorGridlines();
+        }
+        return new XDDFShapeProperties(getOrAddLinesProperties(minorGridlines));
+    }
+
+    @Override
+    public XDDFShapeProperties getOrAddShapeProperties() {
+        CTShapeProperties properties;
+        if (ctSerAx.isSetSpPr()) {
+            properties = ctSerAx.getSpPr();
+        } else {
+            properties = ctSerAx.addNewSpPr();
+        }
+
+        return new XDDFShapeProperties(properties);
+    }
+
+    @Override
+    public boolean isSetMinorUnit() {
+        return false;
+    }
+
+    @Override
+    public void setMinorUnit(double minor) {
+        // nothing
+    }
+
+    @Override
+    public double getMinorUnit() {
+        return Double.NaN;
+    }
+
+    @Override
+    public boolean isSetMajorUnit() {
+        return false;
+    }
+
+    @Override
+    public void setMajorUnit(double major) {
+        // nothing
+    }
+
+    @Override
+    public double getMajorUnit() {
+        return Double.NaN;
+    }
+
+    @Override
+    public void crossAxis(XDDFChartAxis axis) {
+        ctSerAx.getCrossAx().setVal(axis.getId());
+    }
+
+    @Override
+    protected CTUnsignedInt getCTAxId() {
+        return ctSerAx.getAxId();
+    }
+
+    @Override
+    protected CTAxPos getCTAxPos() {
+        return ctSerAx.getAxPos();
+    }
+
+    @Override
+    public boolean hasNumberFormat() {
+        return ctSerAx.isSetNumFmt();
+    }
+
+    @Override
+    protected CTNumFmt getCTNumFmt() {
+        if (ctSerAx.isSetNumFmt()) {
+            return ctSerAx.getNumFmt();
+        }
+        return ctSerAx.addNewNumFmt();
+    }
+
+    @Override
+    protected CTScaling getCTScaling() {
+        return ctSerAx.getScaling();
+    }
+
+    @Override
+    protected CTCrosses getCTCrosses() {
+        CTCrosses crosses = ctSerAx.getCrosses();
+        if (crosses == null) {
+            return ctSerAx.addNewCrosses();
+        } else {
+            return crosses;
+        }
+    }
+
+    @Override
+    protected CTBoolean getDelete() {
+        return ctSerAx.getDelete();
+    }
+
+    @Override
+    protected CTTickMark getMajorCTTickMark() {
+        return ctSerAx.getMajorTickMark();
+    }
+
+    @Override
+    protected CTTickMark getMinorCTTickMark() {
+        return ctSerAx.getMinorTickMark();
+    }
+
+    private void initializeAxis(CTPlotArea plotArea, AxisPosition position) {
+        final long id = getNextAxId(plotArea);
+        ctSerAx = plotArea.addNewSerAx();
+        ctSerAx.addNewAxId().setVal(id);
+        ctSerAx.addNewAxPos();
+        ctSerAx.addNewScaling();
+        ctSerAx.addNewCrosses();
+        ctSerAx.addNewCrossAx();
+        ctSerAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);
+        ctSerAx.addNewDelete();
+        ctSerAx.addNewMajorTickMark();
+        ctSerAx.addNewMinorTickMark();
+
+        setPosition(position);
+        setOrientation(AxisOrientation.MIN_MAX);
+        setCrosses(AxisCrosses.AUTO_ZERO);
+        setVisible(true);
+        setMajorTickMark(AxisTickMark.CROSS);
+        setMinorTickMark(AxisTickMark.NONE);
+    }
+}
index 9720000d16ccf5e6bdbdcfb147e9bf713f3ff3ab..d25f8550b6221ab44dc404becf08b187201c2ac3 100644 (file)
@@ -78,6 +78,64 @@ public class XDDFValueAxis extends XDDFChartAxis {
         return new XDDFShapeProperties(properties);
     }
 
+    @Override
+    public boolean isSetMinorUnit() {
+        return ctValAx.isSetMinorUnit();
+    }
+
+    @Override
+    public void setMinorUnit(double minor) {
+        if (Double.isNaN(minor)) {
+            if (ctValAx.isSetMinorUnit()) {
+                ctValAx.unsetMinorUnit();
+            }
+        } else {
+            if (ctValAx.isSetMinorUnit()) {
+                ctValAx.getMinorUnit().setVal(minor);
+            } else {
+                ctValAx.addNewMinorUnit().setVal(minor);
+            }
+        }
+    }
+
+    @Override
+    public double getMinorUnit() {
+        if (ctValAx.isSetMinorUnit()) {
+            return ctValAx.getMinorUnit().getVal();
+        } else {
+            return Double.NaN;
+        }
+    }
+
+    @Override
+    public boolean isSetMajorUnit() {
+        return ctValAx.isSetMajorUnit();
+    }
+
+    @Override
+    public void setMajorUnit(double major) {
+        if (Double.isNaN(major)) {
+            if (ctValAx.isSetMajorUnit()) {
+                ctValAx.unsetMajorUnit();
+            }
+        } else {
+            if (ctValAx.isSetMajorUnit()) {
+                ctValAx.getMajorUnit().setVal(major);
+            } else {
+                ctValAx.addNewMajorUnit().setVal(major);
+            }
+        }
+    }
+
+    @Override
+    public double getMajorUnit() {
+        if (ctValAx.isSetMajorUnit()) {
+            return ctValAx.getMajorUnit().getVal();
+        } else {
+            return Double.NaN;
+        }
+    }
+
     @Override
     public void crossAxis(XDDFChartAxis axis) {
         ctValAx.getCrossAx().setVal(axis.getId());