From 3fbdd256083d8a28ada5e012e2bc984ee12b87d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alain=20B=C3=A9arez?= Date: Sat, 30 Nov 2019 01:20:23 +0000 Subject: [PATCH] Bug 63889: BarChartDemo now produces a valid file git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870604 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xslf/usermodel/BarChartDemo.java | 24 +++++--- .../poi/xslf/usermodel/ChartFromScratch.java | 22 +++++--- .../poi/xddf/usermodel/chart/XDDFChart.java | 55 ++++--------------- .../poi/xddf/usermodel/chart/XDDFTitle.java | 42 ++++++++++++++ 4 files changed, 81 insertions(+), 62 deletions(-) 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 641b3d369c..bc8b6fb900 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java @@ -100,23 +100,27 @@ public class BarChartDemo { final XDDFBarChartData bar = (XDDFBarChartData) data.get(0); final int numOfPoints = categories.length; - final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0)); - final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1)); - final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2)); - final XDDFDataSource categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0); - final XDDFNumericalDataSource valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, 1); + final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnLanguages, columnLanguages)); + final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnCountries, columnCountries)); + final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnSpeakers, columnSpeakers)); + final XDDFDataSource categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, columnLanguages); + final XDDFNumericalDataSource valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, columnCountries); values1[6] = 16.0; // if you ever want to change the underlying data - final XDDFNumericalDataSource valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, 2); + final XDDFNumericalDataSource valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers); XDDFChartData.Series series1 = bar.getSeries(0); series1.replaceData(categoriesData, valuesData); - series1.setTitle(series[0], chart.setSheetTitle(series[0], 0)); + series1.setTitle(series[0], chart.setSheetTitle(series[0], columnCountries)); XDDFChartData.Series series2 = bar.addSeries(categoriesData, valuesData2); - series2.setTitle(series[1], chart.setSheetTitle(series[1], 1)); + series2.setTitle(series[1], chart.setSheetTitle(series[1], columnSpeakers)); chart.plot(bar); chart.setTitleText(chartTitle); // https://stackoverflow.com/questions/30532612 // chart.setTitleOverlay(overlay); + + // adjust font size for readability + bar.getCategoryAxis().getOrAddTextProperties().setFontSize(11.5); + chart.getTitle().getOrAddTextProperties().setFontSize(18.2); } private static void setColumnData(XSLFChart chart, String chartTitle) { @@ -150,4 +154,8 @@ public class BarChartDemo { } return chart; } + + private static final int columnLanguages = 0; + private static final int columnCountries = 1; + private static final int columnSpeakers = 2; } diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java b/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java index f30f03efbe..2c941e868c 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java @@ -93,7 +93,7 @@ public class ChartFromScratch { slide.addChart(chart, rect2D); setBarData(chart, chartTitle, series, categories, values1, values2); // save the result - try (OutputStream out = new FileOutputStream("bar-chart-demo-output.pptx")) { + try (OutputStream out = new FileOutputStream("chart-from-scratch.pptx")) { ppt.write(out); } } @@ -114,21 +114,21 @@ public class ChartFromScratch { leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); final int numOfPoints = categories.length; - final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0)); - final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1)); - final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2)); - final XDDFDataSource categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0); - final XDDFNumericalDataSource valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, 1); + final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnLanguages, columnLanguages)); + final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnCountries, columnCountries)); + final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnSpeakers, columnSpeakers)); + final XDDFDataSource categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, columnLanguages); + final XDDFNumericalDataSource valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, columnCountries); values1[6] = 16.0; // if you ever want to change the underlying data - final XDDFNumericalDataSource valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, 2); + final XDDFNumericalDataSource valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers); XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis); XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData); - series1.setTitle(series[0], chart.setSheetTitle(series[0], 1)); + series1.setTitle(series[0], chart.setSheetTitle(series[0], columnCountries)); XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData2); - series2.setTitle(series[1], chart.setSheetTitle(series[1], 2)); + series2.setTitle(series[1], chart.setSheetTitle(series[1], columnSpeakers)); bar.setVaryColors(true); bar.setBarDirection(BarDirection.COL); @@ -141,5 +141,9 @@ public class ChartFromScratch { chart.setTitleText(chartTitle); chart.setTitleOverlay(false); } + + private static final int columnLanguages = 0; + private static final int columnCountries = 1; + private static final int columnSpeakers = 2; } 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 41feef8f1a..f3c0d96a4d 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 @@ -53,7 +53,6 @@ import org.apache.poi.xddf.usermodel.text.XDDFTextBody; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.apache.poi.xssf.usermodel.XSSFTable; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlOptions; @@ -84,9 +83,6 @@ import org.openxmlformats.schemas.drawingml.x2006.chart.ChartSpaceDocument; import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns; @Beta public abstract class XDDFChart extends POIXMLDocumentPart implements TextContainer { @@ -470,6 +466,8 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai series.add(new XDDFSurface3DChartData(this, surfaceChart, categories, values)); } // TODO repeat above code for missing charts: Bubble, Doughnut, OfPie and Stock + + seriesCount = series.size(); return series; } @@ -501,6 +499,8 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai XDDFChartAxis axis = axes.get(0); axis.crossAxis(valueAxis); valueAxis.crossAxis(axis); + axis.setCrosses(AxisCrosses.AUTO_ZERO); + valueAxis.setCrosses(AxisCrosses.AUTO_ZERO); } axes.add(valueAxis); return valueAxis; @@ -518,6 +518,8 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai XDDFChartAxis axis = axes.get(0); axis.crossAxis(seriesAxis); seriesAxis.crossAxis(axis); + axis.setCrosses(AxisCrosses.AUTO_ZERO); + seriesAxis.setCrosses(AxisCrosses.AUTO_ZERO); } axes.add(seriesAxis); return seriesAxis; @@ -529,6 +531,8 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai XDDFChartAxis axis = axes.get(0); axis.crossAxis(categoryAxis); categoryAxis.crossAxis(axis); + axis.setCrosses(AxisCrosses.AUTO_ZERO); + categoryAxis.setCrosses(AxisCrosses.AUTO_ZERO); } axes.add(categoryAxis); return categoryAxis; @@ -540,6 +544,8 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai XDDFChartAxis axis = axes.get(0); axis.crossAxis(dateAxis); dateAxis.crossAxis(axis); + axis.setCrosses(AxisCrosses.AUTO_ZERO); + dateAxis.setCrosses(AxisCrosses.AUTO_ZERO); } axes.add(dateAxis); return dateAxis; @@ -853,50 +859,9 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai XSSFCell cell = this.getCell(row, column); cell.setCellValue(title); - CTTable ctTable = this.getSheetTable(sheet); - - this.updateSheetTable(ctTable, title, column); return new CellReference(sheet.getSheetName(), 0, column, true, true); } - /** - * this method will check whether sheet have table - * in case table size zero then create new table and add table columns element - * @param sheet - * @return table object - */ - private CTTable getSheetTable(XSSFSheet sheet) { - if(sheet.getTables().size() == 0) - { - XSSFTable newTable = sheet.createTable(null); - newTable.getCTTable().addNewTableColumns(); - sheet.getTables().add(newTable); - } - return sheet.getTables().get(0).getCTTable(); - } - - /** - * this method update column header of sheet into table - * - * @param ctTable - * xssf table object - * @param title - * title of column - * @param index - * index of column - */ - private void updateSheetTable(CTTable ctTable, String title, int index) { - CTTableColumns tableColumnList = ctTable.getTableColumns(); - CTTableColumn column = null; - int columnCount = tableColumnList.getTableColumnList().size()-1; - for( int i = columnCount; i < index; i++) { - column = tableColumnList.addNewTableColumn(); - column.setId(i); - } - column = tableColumnList.getTableColumnArray(index); - column.setName(title); - } - /** * @param range * @return 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 index 8df7b9a713..e53415c1ce 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFTitle.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFTitle.java @@ -21,9 +21,14 @@ 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.XDDFRunProperties; import org.apache.poi.xddf.usermodel.text.XDDFTextBody; import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle; import org.openxmlformats.schemas.drawingml.x2006.chart.CTTx; +import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody; +import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties; +import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph; +import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties; /** * @since 4.0.1 @@ -73,4 +78,41 @@ public class XDDFTitle { } } + /** + * @since POI 4.1.2 + */ + public XDDFRunProperties getOrAddTextProperties() { + CTTextBody text; + if (title.isSetTxPr()) { + text = title.getTxPr(); + } else { + text = title.addNewTxPr(); + } + return new XDDFRunProperties(getOrAddTextProperties(text)); + } + + private CTTextCharacterProperties getOrAddTextProperties(CTTextBody body) { + CTTextCharacterProperties properties; + if (body.getBodyPr() == null) { + body.addNewBodyPr(); + } + CTTextParagraph paragraph; + if (body.sizeOfPArray() > 0) { + paragraph = body.getPArray(0); + } else { + paragraph = body.addNewP(); + } + CTTextParagraphProperties paraprops; + if (paragraph.isSetPPr()) { + paraprops = paragraph.getPPr(); + } else { + paraprops = paragraph.addNewPPr(); + } + if (paraprops.isSetDefRPr()) { + properties = paraprops.getDefRPr(); + } else { + properties = paraprops.addNewDefRPr(); + } + return properties; + } } -- 2.39.5