From 1a99d1d58e3c735721b04a1f4bc675637627dfc6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alain=20B=C3=A9arez?= Date: Sat, 30 Nov 2019 01:20:20 +0000 Subject: [PATCH] Clean-up code for more recent chart types git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870603 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/chart/XDDFArea3DChartData.java | 2 +- .../usermodel/chart/XDDFAreaChartData.java | 2 +- .../usermodel/chart/XDDFBar3DChartData.java | 2 +- .../poi/xddf/usermodel/chart/XDDFChart.java | 38 +++++++-------- .../xddf/usermodel/chart/XDDFChartData.java | 46 +++++++++++-------- .../usermodel/chart/XDDFLine3DChartData.java | 2 +- .../usermodel/chart/XDDFPie3DChartData.java | 2 +- .../chart/XDDFSurface3DChartData.java | 2 +- .../usermodel/chart/XDDFSurfaceChartData.java | 2 +- 9 files changed, 55 insertions(+), 43 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFArea3DChartData.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFArea3DChartData.java index fda803f6aa..1c132a2830 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFArea3DChartData.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFArea3DChartData.java @@ -126,7 +126,7 @@ public class XDDFArea3DChartData extends XDDFChartData { @Override public XDDFChartData.Series addSeries(XDDFDataSource category, XDDFNumericalDataSource values) { - final int index = this.series.size(); + final long index = this.parent.incrementSeriesCount(); final CTAreaSer ctSer = this.chart.addNewSer(); ctSer.addNewCat(); ctSer.addNewVal(); diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFAreaChartData.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFAreaChartData.java index 0de052a727..1d67f10686 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFAreaChartData.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFAreaChartData.java @@ -104,7 +104,7 @@ public class XDDFAreaChartData extends XDDFChartData { @Override public XDDFChartData.Series addSeries(XDDFDataSource category, XDDFNumericalDataSource values) { - final int index = this.series.size(); + final long index = this.parent.incrementSeriesCount(); final CTAreaSer ctSer = this.chart.addNewSer(); ctSer.addNewCat(); ctSer.addNewVal(); diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFBar3DChartData.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFBar3DChartData.java index ff4d65f179..4adb94c950 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFBar3DChartData.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFBar3DChartData.java @@ -181,7 +181,7 @@ public class XDDFBar3DChartData extends XDDFChartData { @Override public XDDFChartData.Series addSeries(XDDFDataSource category, XDDFNumericalDataSource values) { - final int index = this.series.size(); + final long index = this.parent.incrementSeriesCount(); final CTBarSer ctSer = this.chart.addNewSer(); ctSer.addNewTx(); ctSer.addNewCat(); 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 3e2434618a..41feef8f1a 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 @@ -380,7 +380,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai } public XDDFManualLayout getOrAddManualLayout() { - return new XDDFManualLayout(chart.getPlotArea()); + return new XDDFManualLayout(getCTPlotArea()); } private long seriesCount = 0; @@ -390,7 +390,8 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai public void plot(XDDFChartData data) { XSSFSheet sheet = getSheet(); - for (XDDFChartData.Series series : data.getSeries()) { + for (int idx = 0; idx < data.getSeriesCount(); idx++) { + XDDFChartData.Series series = data.getSeries(idx); series.plot(); XDDFDataSource categoryDS = series.getCategoryData(); XDDFNumericalDataSource valuesDS = series.getValuesData(); @@ -468,7 +469,6 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai CTSurface3DChart surfaceChart = plotArea.getSurface3DChartArray(i); series.add(new XDDFSurface3DChartData(this, surfaceChart, categories, values)); } - // TODO repeat above code for missing charts: Bubble, Doughnut, OfPie and Stock return series; } @@ -496,7 +496,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai } public XDDFValueAxis createValueAxis(AxisPosition pos) { - XDDFValueAxis valueAxis = new XDDFValueAxis(chart.getPlotArea(), pos); + XDDFValueAxis valueAxis = new XDDFValueAxis(getCTPlotArea(), pos); if (axes.size() == 1) { XDDFChartAxis axis = axes.get(0); axis.crossAxis(valueAxis); @@ -513,7 +513,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai * @return series axis with specified position */ public XDDFSeriesAxis createSeriesAxis(AxisPosition pos) { - XDDFSeriesAxis seriesAxis = new XDDFSeriesAxis(chart.getPlotArea(), pos); + XDDFSeriesAxis seriesAxis = new XDDFSeriesAxis(getCTPlotArea(), pos); if (axes.size() == 1) { XDDFChartAxis axis = axes.get(0); axis.crossAxis(seriesAxis); @@ -524,7 +524,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai } public XDDFCategoryAxis createCategoryAxis(AxisPosition pos) { - XDDFCategoryAxis categoryAxis = new XDDFCategoryAxis(chart.getPlotArea(), pos); + XDDFCategoryAxis categoryAxis = new XDDFCategoryAxis(getCTPlotArea(), pos); if (axes.size() == 1) { XDDFChartAxis axis = axes.get(0); axis.crossAxis(categoryAxis); @@ -535,7 +535,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai } public XDDFDateAxis createDateAxis(AxisPosition pos) { - XDDFDateAxis dateAxis = new XDDFDateAxis(chart.getPlotArea(), pos); + XDDFDateAxis dateAxis = new XDDFDateAxis(getCTPlotArea(), pos); if (axes.size() == 1) { XDDFChartAxis axis = axes.get(0); axis.crossAxis(dateAxis); @@ -602,23 +602,23 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai } private boolean hasAxes() { - CTPlotArea ctPlotArea = chart.getPlotArea(); + CTPlotArea ctPlotArea = getCTPlotArea(); int totalAxisCount = ctPlotArea.sizeOfValAxArray() + ctPlotArea.sizeOfCatAxArray() + ctPlotArea .sizeOfDateAxArray() + ctPlotArea.sizeOfSerAxArray(); return totalAxisCount > 0; } private void parseAxes() { - for (CTCatAx catAx : chart.getPlotArea().getCatAxArray()) { + for (CTCatAx catAx : getCTPlotArea().getCatAxArray()) { axes.add(new XDDFCategoryAxis(catAx)); } - for (CTDateAx dateAx : chart.getPlotArea().getDateAxArray()) { + for (CTDateAx dateAx : getCTPlotArea().getDateAxArray()) { axes.add(new XDDFDateAxis(dateAx)); } - for (CTSerAx serAx : chart.getPlotArea().getSerAxArray()) { + for (CTSerAx serAx : getCTPlotArea().getSerAxArray()) { axes.add(new XDDFSeriesAxis(serAx)); } - for (CTValAx valAx : chart.getPlotArea().getValAxArray()) { + for (CTValAx valAx : getCTPlotArea().getValAxArray()) { axes.add(new XDDFValueAxis(valAx)); } } @@ -775,10 +775,11 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai * @since POI 4.0.0 */ private XSSFRow getRow(XSSFSheet sheet, int index) { - if (sheet.getRow(index) != null) { - return sheet.getRow(index); - } else { + XSSFRow row = sheet.getRow(index); + if (row == null) { return sheet.createRow(index); + } else { + return row; } } @@ -794,10 +795,11 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai * @since POI 4.0.0 */ private XSSFCell getCell(XSSFRow row, int index) { - if (row.getCell(index) != null) { - return row.getCell(index); - } else { + XSSFCell cell = row.getCell(index); + if (cell == null) { return row.createCell(index); + } else { + return cell; } } diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java index 618ac4e916..0034862eeb 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java @@ -346,20 +346,26 @@ public abstract class XDDFChartData { private void fillStringCache(CTStrData cache, int numOfPoints, XDDFDataSource data) { cache.setPtArray(null); // unset old values - if (data.getPointAt(0) != null) { // assuming no value for first is no values at all + int effectiveNumOfPoints = 0; + for (int i = 0; i < numOfPoints; ++i) { + Object value = data.getPointAt(i); + if (value != null) { + CTStrVal ctStrVal = cache.addNewPt(); + ctStrVal.setIdx(i); + ctStrVal.setV(value.toString()); + effectiveNumOfPoints++; + } + } + if (effectiveNumOfPoints == 0) { + if (cache.isSetPtCount()) { + cache.unsetPtCount(); + } + } else { if (cache.isSetPtCount()) { cache.getPtCount().setVal(numOfPoints); } else { cache.addNewPtCount().setVal(numOfPoints); } - for (int i = 0; i < numOfPoints; ++i) { - String value = data.getPointAt(i).toString(); - if (value != null) { - CTStrVal ctStrVal = cache.addNewPt(); - ctStrVal.setIdx(i); - ctStrVal.setV(value); - } - } } } @@ -373,20 +379,24 @@ public abstract class XDDFChartData { cache.setFormatCode(formatCode); } cache.setPtArray(null); // unset old values - if (data.getPointAt(0) != null) { // assuming no value for first is no values at all + int effectiveNumOfPoints = 0; + for (int i = 0; i < numOfPoints; ++i) { + Object value = data.getPointAt(i); + if (value != null) { + CTNumVal ctNumVal = cache.addNewPt(); + ctNumVal.setIdx(i); + ctNumVal.setV(value.toString()); + effectiveNumOfPoints++; + } + } + if (effectiveNumOfPoints == 0) { + cache.unsetPtCount(); + } else { if (cache.isSetPtCount()) { cache.getPtCount().setVal(numOfPoints); } else { cache.addNewPtCount().setVal(numOfPoints); } - for (int i = 0; i < numOfPoints; ++i) { - Object value = data.getPointAt(i); - if (value != null) { - CTNumVal ctNumVal = cache.addNewPt(); - ctNumVal.setIdx(i); - ctNumVal.setV(value.toString()); - } - } } } } diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFLine3DChartData.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFLine3DChartData.java index 362ca90985..d744d4d2ed 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFLine3DChartData.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFLine3DChartData.java @@ -117,7 +117,7 @@ public class XDDFLine3DChartData extends XDDFChartData { @Override public XDDFChartData.Series addSeries(XDDFDataSource category, XDDFNumericalDataSource values) { - final int index = this.series.size(); + final long index = this.parent.incrementSeriesCount(); final CTLineSer ctSer = this.chart.addNewSer(); ctSer.addNewCat(); ctSer.addNewVal(); diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFPie3DChartData.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFPie3DChartData.java index 5db479ea8a..b7a8092adb 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFPie3DChartData.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFPie3DChartData.java @@ -63,7 +63,7 @@ public class XDDFPie3DChartData extends XDDFChartData { @Override public XDDFChartData.Series addSeries(XDDFDataSource category, XDDFNumericalDataSource values) { - final int index = this.series.size(); + final long index = this.parent.incrementSeriesCount(); final CTPieSer ctSer = this.chart.addNewSer(); ctSer.addNewCat(); ctSer.addNewVal(); diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSurface3DChartData.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSurface3DChartData.java index e4e4559b80..819ef1e2a3 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSurface3DChartData.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSurface3DChartData.java @@ -101,7 +101,7 @@ public class XDDFSurface3DChartData extends XDDFChartData { @Override public XDDFChartData.Series addSeries(XDDFDataSource category, XDDFNumericalDataSource values) { - final int index = this.series.size(); + final long index = this.parent.incrementSeriesCount(); final CTSurfaceSer ctSer = this.chart.addNewSer(); ctSer.addNewCat(); ctSer.addNewVal(); diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSurfaceChartData.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSurfaceChartData.java index 096199d6b3..b3e98563f7 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSurfaceChartData.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFSurfaceChartData.java @@ -101,7 +101,7 @@ public class XDDFSurfaceChartData extends XDDFChartData { @Override public XDDFChartData.Series addSeries(XDDFDataSource category, XDDFNumericalDataSource values) { - final int index = this.series.size(); + final long index = this.parent.incrementSeriesCount(); final CTSurfaceSer ctSer = this.chart.addNewSer(); ctSer.addNewCat(); ctSer.addNewVal(); -- 2.39.5