From: Alain BĂ©arez Date: Fri, 5 Oct 2018 09:59:32 +0000 (+0000) Subject: more XDDF improvements based on SO examples X-Git-Tag: REL_4_0_1~67 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f446df6f6647e765970e5074b8429856cf5cbffe;p=poi.git more XDDF improvements based on SO examples git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842876 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java b/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java index bd26f8c731..3b96eb0bda 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java @@ -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) { diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/BarChart.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/BarChart.java index c9e73efaba..3e58b912e8 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/BarChart.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/BarChart.java @@ -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 xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java index 9a48f4757a..9d56656795 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java @@ -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 xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java index 8c62436e5a..2cd818a36b 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java @@ -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 xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExample.java b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExample.java index 55185cbf81..9950ffebd7 100644 --- a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExample.java +++ b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExample.java @@ -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) { 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 52fe4223f3..3404725c79 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,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; 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 7357381e78..5022a75a2c 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 @@ -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 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 b08b2fb47a..1a1f87ab17 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 @@ -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 index 0000000000..bca40fc8b0 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFTitle.java @@ -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); + } + } + } + +} 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 d25f8550b6..62c24b4c5d 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,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(); diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFParagraphProperties.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFParagraphProperties.java index 1ed61533ab..456e1d54d9 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFParagraphProperties.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFParagraphProperties.java @@ -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()); diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextBody.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextBody.java index b0ee5d8e4d..19555cd884 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextBody.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextBody.java @@ -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); } diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextParagraph.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextParagraph.java index 46815ea3a4..fffa2da81a 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextParagraph.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFTextParagraph.java @@ -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();