From 2603eccc96d9895317164ae832eed73daf208f1d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alain=20B=C3=A9arez?= Date: Mon, 12 Feb 2018 12:59:38 +0000 Subject: [PATCH] [bug-57369] support for major and minor unit on XDDFChart axes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1823963 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/chart/XDDFCategoryAxis.java | 30 +++ .../poi/xddf/usermodel/chart/XDDFChart.java | 32 ++- .../xddf/usermodel/chart/XDDFChartAxis.java | 75 +++++-- .../xddf/usermodel/chart/XDDFDateAxis.java | 58 ++++++ .../xddf/usermodel/chart/XDDFSeriesAxis.java | 190 ++++++++++++++++++ .../xddf/usermodel/chart/XDDFValueAxis.java | 58 ++++++ 6 files changed, 426 insertions(+), 17 deletions(-) create mode 100644 src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSeriesAxis.java diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFCategoryAxis.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFCategoryAxis.java index 4a673f7d33..52fe4223f3 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFCategoryAxis.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFCategoryAxis.java @@ -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()); 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 f86d217470..99a117fd36 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 @@ -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 diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartAxis.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartAxis.java index cd1c6f01ab..b08b2fb47a 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartAxis.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartAxis.java @@ -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; } } diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDateAxis.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDateAxis.java index 95f7d74f4c..bde26ffb7e 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDateAxis.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDateAxis.java @@ -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 index 0000000000..37ec0ddab3 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSeriesAxis.java @@ -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); + } +} diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFValueAxis.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFValueAxis.java index 9720000d16..d25f8550b6 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFValueAxis.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFValueAxis.java @@ -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()); -- 2.39.5