]> source.dussan.org Git - poi.git/commitdiff
more XDDF improvements based on SO examples
authorAlain Béarez <abearez@apache.org>
Fri, 5 Oct 2018 09:59:32 +0000 (09:59 +0000)
committerAlain Béarez <abearez@apache.org>
Fri, 5 Oct 2018 09:59:32 +0000 (09:59 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842876 13f79535-47bb-0310-9956-ffa450edef68

13 files changed:
src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/BarChart.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java
src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExample.java
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/XDDFTitle.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFValueAxis.java
src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFParagraphProperties.java
src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextBody.java
src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextParagraph.java

index bd26f8c731c5b59d0096840ab4d1a811d9ce4a4e..3b96eb0bda0c812e24b6820ecfffd57289d2e605 100644 (file)
@@ -115,6 +115,8 @@ public class BarChartDemo {
         series2.setTitle(series[1], chart.setSheetTitle(series[1], 1));
 
         chart.plot(bar);
+        chart.setTitleText(chartTitle); // https://stackoverflow.com/questions/30532612
+        // chart.setTitleOverlay(overlay);
     }
 
     private static void setColumnData(XSLFChart chart, String chartTitle) {
index c9e73efaba972e9a19c4f44eb171312839e227f2..3e58b912e8f3623f621f1e93649da56d99ac9798 100644 (file)
@@ -76,7 +76,9 @@ public class BarChart {
 
             // Use a category axis for the bottom axis.
             XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
+            bottomAxis.setTitle("x"); // https://stackoverflow.com/questions/32010765
             XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
+            leftAxis.setTitle("f(x)");
             leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
 
             XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
index 9a48f4757a4e2ee90a0b2661bf847be48e5beca7..9d566567958f54a95ab2129bcc5140ad6b3c0221 100644 (file)
@@ -77,7 +77,9 @@ public class LineChart {
 
             // Use a category axis for the bottom axis.
             XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
+            bottomAxis.setTitle("x"); // https://stackoverflow.com/questions/32010765
             XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
+            leftAxis.setTitle("f(x)");
             leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
 
             XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
index 8c62436e5afc207b8446a6c624bbc8c5cd62c562..2cd818a36b070c825cb076eef83eda2babef84f7 100644 (file)
@@ -77,7 +77,9 @@ public class ScatterChart {
             legend.setPosition(LegendPosition.TOP_RIGHT);
 
             XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
+            bottomAxis.setTitle("x"); // https://stackoverflow.com/questions/32010765
             XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
+            leftAxis.setTitle("f(x)");
             leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
 
             XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
index 55185cbf8117dc628cfe67d0fc4144aae7555a65..9950ffebd7f958f89812c6dac82f0a27bf696f8d 100644 (file)
@@ -117,6 +117,8 @@ public class BarChartExample {
         series2.setTitle(series[1], chart.setSheetTitle(series[1], 1));
 
         chart.plot(bar);
+        chart.setTitleText(chartTitle); // https://stackoverflow.com/questions/30532612
+        // chart.setTitleOverlay(overlay);
     }
 
     private static void setColumnData(XWPFChart chart, String chartTitle) {
index 52fe4223f3286c01bdbbfd9b08f5ce6d05d93f3f..3404725c79908839fe4f5670ae98702058d0ae5e 100644 (file)
@@ -79,6 +79,19 @@ public class XDDFCategoryAxis extends XDDFChartAxis {
         return new XDDFShapeProperties(properties);
     }
 
+    /**
+     * @since 4.0.1
+     */
+    @Override
+    public void setTitle(String text) {
+        if (!ctCatAx.isSetTitle()) {
+            ctCatAx.addNewTitle();
+        }
+        XDDFTitle title = new XDDFTitle(null, ctCatAx.getTitle());
+        title.setOverlay(false);
+        title.setText(text);
+    }
+
     @Override
     public boolean isSetMinorUnit() {
         return false;
index 7357381e78e217ee0ecdb3f2b42c7ee663a731b3..5022a75a2ca4ac047d0d1d02232588b864614696 100644 (file)
@@ -70,7 +70,6 @@ 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.CTTitle;
-import org.openxmlformats.schemas.drawingml.x2006.chart.CTTx;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
 import org.openxmlformats.schemas.drawingml.x2006.chart.ChartSpaceDocument;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
@@ -252,11 +251,27 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
         if (!chart.isSetTitle()) {
             chart.addNewTitle();
         }
-        CTTitle title = chart.getTitle();
-        if (title.isSetOverlay()) {
-            title.getOverlay().setVal(overlay);
+        new XDDFTitle(this, chart.getTitle()).setOverlay(overlay);
+    }
+
+    /**
+     * @since 4.0.1
+     */
+    public void setTitleText(String text) {
+        if (!chart.isSetTitle()) {
+            chart.addNewTitle();
+        }
+        new XDDFTitle(this, chart.getTitle()).setText(text);
+    }
+
+    /**
+     * @since 4.0.1
+     */
+    public XDDFTitle getTitle() {
+        if (chart.isSetTitle()) {
+            return new XDDFTitle(this, chart.getTitle());
         } else {
-            title.addNewOverlay().setVal(overlay);
+            return null;
         }
     }
 
@@ -271,15 +286,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
         if (!chart.isSetTitle()) {
             return null;
         }
-        CTTitle title = chart.getTitle();
-        if (!title.isSetTx()) {
-            return null;
-        }
-        CTTx tx = title.getTx();
-        if (!tx.isSetRich()) {
-            return null;
-        }
-        return new XDDFTextBody(this, tx.getRich());
+        return new XDDFTitle(this, chart.getTitle()).getBody();
     }
 
     @Override
index b08b2fb47a651940f4c454d6d4fbf1fc93bdd294..1a1f87ab17b0e5360138c2b0b9cb1070be81cd60 100644 (file)
@@ -56,6 +56,11 @@ public abstract class XDDFChartAxis implements HasShapeProperties {
 
     public abstract XDDFShapeProperties getOrAddMinorGridProperties();
 
+    /**
+     * @since 4.0.1
+     */
+    public abstract void setTitle(String text);
+
     /**
      * @return true if minor unit value is defined, false otherwise
      */
diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFTitle.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFTitle.java
new file mode 100644 (file)
index 0000000..bca40fc
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ *  ====================================================================
+ *    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.text.TextContainer;
+import org.apache.poi.xddf.usermodel.text.XDDFTextBody;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTTx;
+
+/**
+ * @since 4.0.1
+ */
+@Beta
+public class XDDFTitle {
+    private final CTTitle title;
+    private final TextContainer parent;
+
+    public XDDFTitle(TextContainer parent, CTTitle title) {
+        this.parent = parent;
+        this.title = title;
+    }
+
+    public XDDFTextBody getBody() {
+        XDDFTextBody body;
+        if (title.isSetTxPr()) {
+            body = new XDDFTextBody(parent, title.getTxPr());
+        } else {
+            if (!title.isSetTx()) {
+                title.addNewTx();
+            }
+            CTTx tx = title.getTx();
+            if (!tx.isSetRich()) {
+                tx.addNewRich();
+            }
+            body = new XDDFTextBody(parent, tx.getRich());
+        }
+        return body;
+    }
+
+    public void setText(String text) {
+        if (!title.isSetLayout()) {
+            title.addNewLayout();
+        }
+        getBody().setText(text);
+    }
+
+    public void setOverlay(Boolean overlay) {
+        if (overlay == null) {
+            if (title.isSetOverlay()) {
+                title.unsetOverlay();
+            }
+        } else {
+            if (title.isSetOverlay()) {
+                title.getOverlay().setVal(overlay);
+            } else {
+                title.addNewOverlay().setVal(overlay);
+            }
+        }
+    }
+
+}
index d25f8550b6221ab44dc404becf08b187201c2ac3..62c24b4c5da16eeee43e6fc451e5e8d7d24b4876 100644 (file)
@@ -78,6 +78,19 @@ public class XDDFValueAxis extends XDDFChartAxis {
         return new XDDFShapeProperties(properties);
     }
 
+    /**
+     * @since 4.0.1
+     */
+    @Override
+    public void setTitle(String text) {
+        if (!ctValAx.isSetTitle()) {
+            ctValAx.addNewTitle();
+        }
+        XDDFTitle title = new XDDFTitle(null, ctValAx.getTitle());
+        title.setOverlay(false);
+        title.setText(text);
+    }
+
     @Override
     public boolean isSetMinorUnit() {
         return ctValAx.isSetMinorUnit();
index 1ed61533abfc3b8577531e541f160e5ff29e84bf..456e1d54d9433759a555f3e757276eb5fd3437c0 100644 (file)
@@ -67,6 +67,16 @@ public class XDDFParagraphProperties {
         }
     }
 
+    /**
+     * @since 4.0.1
+     */
+    public XDDFRunProperties addDefaultRunProperties() {
+        if (!props.isSetDefRPr()) {
+            props.addNewDefRPr();
+        }
+        return getDefaultRunProperties();
+    }
+
     public XDDFRunProperties getDefaultRunProperties() {
         if (props.isSetDefRPr()) {
             return new XDDFRunProperties(props.getDefRPr());
index b0ee5d8e4d57753ec1c7bc5fe1467fe92fd2d99b..19555cd88424de9a789a8f1d9b121826856f1f26 100644 (file)
@@ -70,6 +70,19 @@ public class XDDFTextBody {
         return p;
     }
 
+    public void setText(String text) {
+        if (_body.sizeOfPArray() > 0) {
+            // remove all but first paragraph
+            for (int i = _body.sizeOfPArray() - 1; i > 0; i--) {
+                _body.removeP(i);
+            }
+            getParagraph(0).setText(text);
+        } else {
+            // as there were no paragraphs yet, initialize the text body
+            initialize().setText(text);
+        }
+    }
+
     public XDDFTextParagraph addNewParagraph() {
         return new XDDFTextParagraph(_body.addNewP(), this);
     }
index 46815ea3a4f5aeb68c7115d852bb9b5501d0d636..fffa2da81af2161c1601b84cbfe43996de435394 100644 (file)
@@ -69,6 +69,24 @@ public class XDDFTextParagraph {
                 _runs.add(new XDDFTextRun((CTRegularTextRun) xo, this));
             }
         }
+
+        addDefaultRunProperties();
+        addAfterLastRunProperties();
+    }
+
+    public void setText(String text) {
+        // remove all runs
+        for (int i = _p.sizeOfBrArray() - 1; i >= 0; i--) {
+            _p.removeBr(i);
+        }
+        for (int i = _p.sizeOfFldArray() - 1; i >= 0; i--) {
+            _p.removeFld(i);
+        }
+        for (int i = _p.sizeOfRArray() - 1; i >= 0; i--) {
+            _p.removeR(i);
+        }
+        _runs.clear();
+        appendRegularRun(text);
     }
 
     public String getText() {
@@ -662,6 +680,13 @@ public class XDDFTextParagraph {
         }
     }
 
+    /**
+     * @since 4.0.1
+     */
+    public XDDFRunProperties addDefaultRunProperties() {
+        return getOrCreateProperties().addDefaultRunProperties();
+    }
+
     public XDDFRunProperties getDefaultRunProperties() {
         if (_p.isSetPPr()) {
             return getProperties().getDefaultRunProperties();