more XDDF improvements based on SO examples

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842876 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alain Béarez 2018-10-05 09:59:32 +00:00
parent 01ad2f9d1a
commit 01b780666b
13 changed files with 189 additions and 14 deletions

View File

@ -115,6 +115,8 @@ public class BarChartDemo {
series2.setTitle(series[1], chart.setSheetTitle(series[1], 1)); series2.setTitle(series[1], chart.setSheetTitle(series[1], 1));
chart.plot(bar); chart.plot(bar);
chart.setTitleText(chartTitle); // https://stackoverflow.com/questions/30532612
// chart.setTitleOverlay(overlay);
} }
private static void setColumnData(XSLFChart chart, String chartTitle) { private static void setColumnData(XSLFChart chart, String chartTitle) {

View File

@ -76,7 +76,9 @@ public class BarChart {
// Use a category axis for the bottom axis. // Use a category axis for the bottom axis.
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("x"); // https://stackoverflow.com/questions/32010765
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("f(x)");
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));

View File

@ -77,7 +77,9 @@ public class LineChart {
// Use a category axis for the bottom axis. // Use a category axis for the bottom axis.
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("x"); // https://stackoverflow.com/questions/32010765
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("f(x)");
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));

View File

@ -77,7 +77,9 @@ public class ScatterChart {
legend.setPosition(LegendPosition.TOP_RIGHT); legend.setPosition(LegendPosition.TOP_RIGHT);
XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM); XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("x"); // https://stackoverflow.com/questions/32010765
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("f(x)");
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));

View File

@ -117,6 +117,8 @@ public class BarChartExample {
series2.setTitle(series[1], chart.setSheetTitle(series[1], 1)); series2.setTitle(series[1], chart.setSheetTitle(series[1], 1));
chart.plot(bar); chart.plot(bar);
chart.setTitleText(chartTitle); // https://stackoverflow.com/questions/30532612
// chart.setTitleOverlay(overlay);
} }
private static void setColumnData(XWPFChart chart, String chartTitle) { private static void setColumnData(XWPFChart chart, String chartTitle) {

View File

@ -79,6 +79,19 @@ public class XDDFCategoryAxis extends XDDFChartAxis {
return new XDDFShapeProperties(properties); 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 @Override
public boolean isSetMinorUnit() { public boolean isSetMinorUnit() {
return false; return false;

View 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.CTSerAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTSurface; import org.openxmlformats.schemas.drawingml.x2006.chart.CTSurface;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle; 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.CTValAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.ChartSpaceDocument; import org.openxmlformats.schemas.drawingml.x2006.chart.ChartSpaceDocument;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
@ -252,11 +251,27 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
if (!chart.isSetTitle()) { if (!chart.isSetTitle()) {
chart.addNewTitle(); chart.addNewTitle();
} }
CTTitle title = chart.getTitle(); new XDDFTitle(this, chart.getTitle()).setOverlay(overlay);
if (title.isSetOverlay()) { }
title.getOverlay().setVal(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 { } else {
title.addNewOverlay().setVal(overlay); return null;
} }
} }
@ -271,15 +286,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
if (!chart.isSetTitle()) { if (!chart.isSetTitle()) {
return null; return null;
} }
CTTitle title = chart.getTitle(); return new XDDFTitle(this, chart.getTitle()).getBody();
if (!title.isSetTx()) {
return null;
}
CTTx tx = title.getTx();
if (!tx.isSetRich()) {
return null;
}
return new XDDFTextBody(this, tx.getRich());
} }
@Override @Override

View File

@ -56,6 +56,11 @@ public abstract class XDDFChartAxis implements HasShapeProperties {
public abstract XDDFShapeProperties getOrAddMinorGridProperties(); public abstract XDDFShapeProperties getOrAddMinorGridProperties();
/**
* @since 4.0.1
*/
public abstract void setTitle(String text);
/** /**
* @return true if minor unit value is defined, false otherwise * @return true if minor unit value is defined, false otherwise
*/ */

View File

@ -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);
}
}
}
}

View File

@ -78,6 +78,19 @@ public class XDDFValueAxis extends XDDFChartAxis {
return new XDDFShapeProperties(properties); 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 @Override
public boolean isSetMinorUnit() { public boolean isSetMinorUnit() {
return ctValAx.isSetMinorUnit(); return ctValAx.isSetMinorUnit();

View 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() { public XDDFRunProperties getDefaultRunProperties() {
if (props.isSetDefRPr()) { if (props.isSetDefRPr()) {
return new XDDFRunProperties(props.getDefRPr()); return new XDDFRunProperties(props.getDefRPr());

View File

@ -70,6 +70,19 @@ public class XDDFTextBody {
return p; 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() { public XDDFTextParagraph addNewParagraph() {
return new XDDFTextParagraph(_body.addNewP(), this); return new XDDFTextParagraph(_body.addNewP(), this);
} }

View File

@ -69,6 +69,24 @@ public class XDDFTextParagraph {
_runs.add(new XDDFTextRun((CTRegularTextRun) xo, this)); _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() { public String getText() {
@ -662,6 +680,13 @@ public class XDDFTextParagraph {
} }
} }
/**
* @since 4.0.1
*/
public XDDFRunProperties addDefaultRunProperties() {
return getOrCreateProperties().addDefaultRunProperties();
}
public XDDFRunProperties getDefaultRunProperties() { public XDDFRunProperties getDefaultRunProperties() {
if (_p.isSetPPr()) { if (_p.isSetPPr()) {
return getProperties().getDefaultRunProperties(); return getProperties().getDefaultRunProperties();