Standardize some more common chart axis properties.

Implement the ChartAxis ss interface for date axes, which are pretty much a category axis with one extra property, a date granularity (not currently passed through, get it still from the CT* class).  Created the class and test from the category axis versions.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1798414 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Greg Woolsey 2017-06-12 05:38:41 +00:00
parent 7e79cadbdc
commit aca91b7888
8 changed files with 257 additions and 18 deletions

View File

@ -153,4 +153,10 @@ public interface ChartAxis {
* @param tickMark minor tick mark type.
*/
void setMinorTickMark(AxisTickMark tickMark);
/**
* Use this to check before retrieving a number format, as calling {@link #getNumberFormat()} may create a default one if none exists.
* @return true if a number format element is defined, false if not
*/
boolean hasNumberFormat();
}

View File

@ -29,13 +29,21 @@ import org.apache.poi.util.Beta;
public interface ChartAxisFactory {
/**
* @return new value axis
* @param pos
* @return new value axis at the end of the list at the specified chart position
*/
ValueAxis createValueAxis(AxisPosition pos);
/**
* @return new category axis.
* @param pos
* @return new category axis at the end of the list at the specified chart position
*/
ChartAxis createCategoryAxis(AxisPosition pos);
/**
* @param pos
* @return new date category axis at the end of the list at the specified chart position
*/
ChartAxis createDateAxis(AxisPosition pos);
}

View File

@ -39,6 +39,7 @@ import org.apache.poi.xssf.usermodel.charts.XSSFCategoryAxis;
import org.apache.poi.xssf.usermodel.charts.XSSFChartAxis;
import org.apache.poi.xssf.usermodel.charts.XSSFChartDataFactory;
import org.apache.poi.xssf.usermodel.charts.XSSFChartLegend;
import org.apache.poi.xssf.usermodel.charts.XSSFDateAxis;
import org.apache.poi.xssf.usermodel.charts.XSSFManualLayout;
import org.apache.poi.xssf.usermodel.charts.XSSFValueAxis;
import org.apache.xmlbeans.XmlException;
@ -47,6 +48,7 @@ import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartSpace;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDateAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPageMargins;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPrintSettings;
@ -224,6 +226,18 @@ public final class XSSFChart extends POIXMLDocumentPart implements Chart, ChartA
return categoryAxis;
}
public XSSFDateAxis createDateAxis(AxisPosition pos) {
long id = axis.size() + 1;
XSSFDateAxis dateAxis = new XSSFDateAxis(this, id, pos);
if (axis.size() == 1) {
ChartAxis ax = axis.get(0);
ax.crossAxis(dateAxis);
dateAxis.crossAxis(ax);
}
axis.add(dateAxis);
return dateAxis;
}
public List<? extends XSSFChartAxis> getAxis() {
if (axis.isEmpty() && hasAxis()) {
parseAxis();
@ -438,6 +452,7 @@ public final class XSSFChart extends POIXMLDocumentPart implements Chart, ChartA
private void parseAxis() {
// TODO: add other axis types
parseCategoryAxis();
parseDateAxis();
parseValueAxis();
}
@ -447,6 +462,12 @@ public final class XSSFChart extends POIXMLDocumentPart implements Chart, ChartA
}
}
private void parseDateAxis() {
for (CTDateAx dateAx : chart.getPlotArea().getDateAxArray()) {
axis.add(new XSSFDateAxis(this, dateAx));
}
}
private void parseValueAxis() {
for (CTValAx valAx : chart.getPlotArea().getValAxArray()) {
axis.add(new XSSFValueAxis(this, valAx));

View File

@ -17,10 +17,22 @@
package org.apache.poi.xssf.usermodel.charts;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.ss.usermodel.charts.AxisCrosses;
import org.apache.poi.ss.usermodel.charts.AxisOrientation;
import org.apache.poi.ss.usermodel.charts.AxisPosition;
import org.apache.poi.ss.usermodel.charts.AxisTickMark;
import org.apache.poi.ss.usermodel.charts.ChartAxis;
import org.apache.poi.util.Beta;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.*;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx;
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.CTScaling;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickMark;
import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;
/**
* Category axis type.
@ -80,6 +92,10 @@ public class XSSFCategoryAxis extends XSSFChartAxis {
return ctCatAx.getMinorTickMark();
}
public CTChartLines getMajorGridLines() {
return ctCatAx.getMajorGridlines();
}
public void crossAxis(ChartAxis axis) {
ctCatAx.getCrossAx().setVal(axis.getId());
}
@ -103,4 +119,8 @@ public class XSSFCategoryAxis extends XSSFChartAxis {
setMajorTickMark(AxisTickMark.CROSS);
setMinorTickMark(AxisTickMark.NONE);
}
public boolean hasNumberFormat() {
return ctCatAx.isSetNumFmt();
}
}

View File

@ -17,24 +17,25 @@
package org.apache.poi.xssf.usermodel.charts;
import org.apache.poi.ss.usermodel.charts.ChartAxis;
import org.apache.poi.ss.usermodel.charts.AxisPosition;
import org.apache.poi.ss.usermodel.charts.AxisOrientation;
import org.apache.poi.ss.usermodel.charts.AxisCrosses;
import org.apache.poi.ss.usermodel.charts.AxisOrientation;
import org.apache.poi.ss.usermodel.charts.AxisPosition;
import org.apache.poi.ss.usermodel.charts.AxisTickMark;
import org.apache.poi.ss.usermodel.charts.ChartAxis;
import org.apache.poi.util.Beta;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartLines;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTOrientation;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLogBase;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTOrientation;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickMark;
import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation;
import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrosses;
import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation;
import org.openxmlformats.schemas.drawingml.x2006.chart.STTickMark;
/**
@ -193,7 +194,8 @@ public abstract class XSSFChartAxis implements ChartAxis {
protected abstract CTBoolean getDelete();
protected abstract CTTickMark getMajorCTTickMark();
protected abstract CTTickMark getMinorCTTickMark();
public abstract CTChartLines getMajorGridLines();
private static STOrientation.Enum fromAxisOrientation(AxisOrientation orientation) {
switch (orientation) {
case MIN_MAX: return STOrientation.MIN_MAX;

View File

@ -0,0 +1,134 @@
/* ====================================================================
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.xssf.usermodel.charts;
import org.apache.poi.ss.usermodel.charts.AxisCrosses;
import org.apache.poi.ss.usermodel.charts.AxisOrientation;
import org.apache.poi.ss.usermodel.charts.AxisPosition;
import org.apache.poi.ss.usermodel.charts.AxisTickMark;
import org.apache.poi.ss.usermodel.charts.ChartAxis;
import org.apache.poi.util.Beta;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartLines;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDateAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickMark;
import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;
/**
* Date axis type. Currently only implements the same values as {@link XSSFCategoryAxis}, since the two are nearly identical.
*/
@Beta
public class XSSFDateAxis extends XSSFChartAxis {
private CTDateAx ctDateAx;
/**
* @param chart
* @param id
* @param pos
*/
public XSSFDateAxis(XSSFChart chart, long id, AxisPosition pos) {
super(chart);
createAxis(id, pos);
}
/**
* @param chart
* @param ctDateAx
*/
public XSSFDateAxis(XSSFChart chart, CTDateAx ctDateAx) {
super(chart);
this.ctDateAx = ctDateAx;
}
public long getId() {
return ctDateAx.getAxId().getVal();
}
protected CTAxPos getCTAxPos() {
return ctDateAx.getAxPos();
}
protected CTNumFmt getCTNumFmt() {
if (ctDateAx.isSetNumFmt()) {
return ctDateAx.getNumFmt();
}
return ctDateAx.addNewNumFmt();
}
protected CTScaling getCTScaling() {
return ctDateAx.getScaling();
}
protected CTCrosses getCTCrosses() {
return ctDateAx.getCrosses();
}
@Override
protected CTBoolean getDelete() {
return ctDateAx.getDelete();
}
@Override
protected CTTickMark getMajorCTTickMark() {
return ctDateAx.getMajorTickMark();
}
@Override
protected CTTickMark getMinorCTTickMark() {
return ctDateAx.getMinorTickMark();
}
public CTChartLines getMajorGridLines() {
return ctDateAx.getMajorGridlines();
}
public void crossAxis(ChartAxis axis) {
ctDateAx.getCrossAx().setVal(axis.getId());
}
private void createAxis(long id, AxisPosition pos) {
ctDateAx = chart.getCTChart().getPlotArea().addNewDateAx();
ctDateAx.addNewAxId().setVal(id);
ctDateAx.addNewAxPos();
ctDateAx.addNewScaling();
ctDateAx.addNewCrosses();
ctDateAx.addNewCrossAx();
ctDateAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);
ctDateAx.addNewDelete();
ctDateAx.addNewMajorTickMark();
ctDateAx.addNewMinorTickMark();
setPosition(pos);
setOrientation(AxisOrientation.MIN_MAX);
setCrosses(AxisCrosses.AUTO_ZERO);
setVisible(true);
setMajorTickMark(AxisTickMark.CROSS);
setMinorTickMark(AxisTickMark.NONE);
}
public boolean hasNumberFormat() {
return ctDateAx.isSetNumFmt();
}
}

View File

@ -17,23 +17,23 @@
package org.apache.poi.xssf.usermodel.charts;
import org.apache.poi.ss.usermodel.charts.ChartAxis;
import org.apache.poi.ss.usermodel.charts.ValueAxis;
import org.apache.poi.ss.usermodel.charts.AxisPosition;
import org.apache.poi.ss.usermodel.charts.AxisOrientation;
import org.apache.poi.ss.usermodel.charts.AxisCrossBetween;
import org.apache.poi.ss.usermodel.charts.AxisCrosses;
import org.apache.poi.ss.usermodel.charts.AxisOrientation;
import org.apache.poi.ss.usermodel.charts.AxisPosition;
import org.apache.poi.ss.usermodel.charts.AxisTickMark;
import org.apache.poi.ss.usermodel.charts.ChartAxis;
import org.apache.poi.ss.usermodel.charts.ValueAxis;
import org.apache.poi.util.Beta;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
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.CTScaling;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickMark;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrossBetween;
import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;
@ -107,6 +107,10 @@ public class XSSFValueAxis extends XSSFChartAxis implements ValueAxis {
return ctValAx.getMinorTickMark();
}
public CTChartLines getMajorGridLines() {
return ctValAx.getMajorGridlines();
}
public void crossAxis(ChartAxis axis) {
ctValAx.getCrossAx().setVal(axis.getId());
}
@ -150,4 +154,8 @@ public class XSSFValueAxis extends XSSFChartAxis implements ValueAxis {
throw new IllegalArgumentException();
}
}
public boolean hasNumberFormat() {
return ctValAx.isSetNumFmt();
}
}

View File

@ -0,0 +1,40 @@
/* ====================================================================
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.xssf.usermodel.charts;
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.xssf.usermodel.*;
public final class TestXSSFDateAxis extends TestCase {
public void testAccessMethods() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 10, 30);
XSSFChart chart = drawing.createChart(anchor);
XSSFDateAxis axis = chart.getChartAxisFactory().createDateAxis(AxisPosition.BOTTOM);
axis.setCrosses(AxisCrosses.AUTO_ZERO);
assertEquals(axis.getCrosses(), AxisCrosses.AUTO_ZERO);
assertEquals(chart.getAxis().size(), 1);
}
}