From dd3279df30eb9656c3a72f4a6a2445f777c7dd5a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alain=20B=C3=A9arez?= Date: Mon, 2 Dec 2019 00:15:34 +0000 Subject: [PATCH] Fix examples to build chart from scratch git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870696 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xslf/usermodel/BarChartDemo.java | 2 +- .../poi/xslf/usermodel/ChartFromScratch.java | 24 +++++++++--- .../usermodel/examples/ChartFromScratch.java | 38 +++++++++++++------ .../poi/xddf/usermodel/chart/XDDFChart.java | 12 ++++-- .../xddf/usermodel/chart/XDDFDataSource.java | 5 +++ .../chart/XDDFDataSourcesFactory.java | 25 ++++++++++++ 6 files changed, 84 insertions(+), 22 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 bc8b6fb900..f9e8bac0c3 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java @@ -105,7 +105,7 @@ public class BarChartDemo { 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 + values1[6] = 16.0; // if you ever want to change the underlying data, it has to be done before building the data source final XDDFNumericalDataSource valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers); XDDFChartData.Series series1 = bar.getSeries(0); 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 3d1f7c7b9c..1e923b9aab 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java @@ -28,13 +28,16 @@ import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.util.Units; +import org.apache.poi.xddf.usermodel.chart.AxisCrossBetween; import org.apache.poi.xddf.usermodel.chart.AxisCrosses; import org.apache.poi.xddf.usermodel.chart.AxisPosition; +import org.apache.poi.xddf.usermodel.chart.AxisTickMark; import org.apache.poi.xddf.usermodel.chart.BarDirection; +import org.apache.poi.xddf.usermodel.chart.BarGrouping; import org.apache.poi.xddf.usermodel.chart.ChartTypes; import org.apache.poi.xddf.usermodel.chart.LegendPosition; import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData; -import org.apache.poi.xddf.usermodel.chart.XDDFChart; import org.apache.poi.xddf.usermodel.chart.XDDFChartAxis; import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend; import org.apache.poi.xddf.usermodel.chart.XDDFDataSource; @@ -47,7 +50,7 @@ import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis; */ public class ChartFromScratch { private static void usage(){ - System.out.println("Usage: BarChartExample "); + System.out.println("Usage: ChartFromScratch "); System.out.println(" bar-chart-data.txt the model to set. First line is chart title, " + "then go pairs {axis-label value}"); } @@ -86,8 +89,7 @@ public class ChartFromScratch { try (XMLSlideShow ppt = new XMLSlideShow()) { XSLFSlide slide = ppt.createSlide(); XSLFChart chart = ppt.createChart(); - Rectangle2D rect2D = new java.awt.Rectangle(XDDFChart.DEFAULT_X, XDDFChart.DEFAULT_Y, - XDDFChart.DEFAULT_WIDTH, XDDFChart.DEFAULT_HEIGHT); + Rectangle2D rect2D = new java.awt.Rectangle(fromCM(1.5), fromCM(4), fromCM(22), fromCM(14)); slide.addChart(chart, rect2D); setBarData(chart, chartTitle, series, categories, values1, values2); // save the result @@ -99,6 +101,10 @@ public class ChartFromScratch { System.out.println("Done"); } + private static int fromCM(double cm) { + return (int) (Math.rint(cm * Units.EMU_PER_CENTIMETER)); + } + private static void setBarData(XSLFChart chart, String chartTitle, String[] series, String[] categories, Double[] values1, Double[] values2) { // Use a category axis for the bottom axis. XDDFChartAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); @@ -106,6 +112,8 @@ public class ChartFromScratch { XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); leftAxis.setTitle(series[0]+","+series[1]); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); + leftAxis.setMajorTickMark(AxisTickMark.OUT); + leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN); final int numOfPoints = categories.length; final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, columnLanguages, columnLanguages)); @@ -113,11 +121,15 @@ public class ChartFromScratch { 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 + valuesData.setFormatCode("General"); + values1[6] = 16.0; // if you ever want to change the underlying data, it has to be done before building the data source final XDDFNumericalDataSource valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers); + valuesData2.setFormatCode("General"); XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis); + bar.setBarGrouping(BarGrouping.CLUSTERED); + XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData); series1.setTitle(series[0], chart.setSheetTitle(series[0], columnCountries)); @@ -134,10 +146,10 @@ public class ChartFromScratch { chart.setTitleText(chartTitle); chart.setTitleOverlay(false); + chart.setAutoTitleDeleted(false); } 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/xwpf/usermodel/examples/ChartFromScratch.java b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java index ee5624fd57..b1ebbdc01e 100644 --- a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java +++ b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java @@ -27,9 +27,12 @@ import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xddf.usermodel.chart.AxisCrossBetween; import org.apache.poi.xddf.usermodel.chart.AxisCrosses; import org.apache.poi.xddf.usermodel.chart.AxisPosition; +import org.apache.poi.xddf.usermodel.chart.AxisTickMark; import org.apache.poi.xddf.usermodel.chart.BarDirection; +import org.apache.poi.xddf.usermodel.chart.BarGrouping; import org.apache.poi.xddf.usermodel.chart.ChartTypes; import org.apache.poi.xddf.usermodel.chart.LegendPosition; import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData; @@ -48,7 +51,7 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument; */ public class ChartFromScratch { private static void usage(){ - System.out.println("Usage: BarChartExample "); + System.out.println("Usage: ChartFromScratch "); System.out.println(" bar-chart-data.txt the model to set. First line is chart title, " + "then go pairs {axis-label value}"); } @@ -85,10 +88,10 @@ public class ChartFromScratch { Double[] values2 = listSpeakers.toArray(new Double[0]); try (XWPFDocument doc = new XWPFDocument()) { - XWPFChart chart = doc.createChart(XDDFChart.DEFAULT_WIDTH, XDDFChart.DEFAULT_HEIGHT); + XWPFChart chart = doc.createChart(XDDFChart.DEFAULT_WIDTH * 10, XDDFChart.DEFAULT_HEIGHT * 15); setBarData(chart, chartTitle, series, categories, values1, values2); // save the result - try (OutputStream out = new FileOutputStream("bar-chart-demo-output.docx")) { + try (OutputStream out = new FileOutputStream("chart-from-scratch.docx")) { doc.write(out); } } @@ -107,23 +110,29 @@ public class ChartFromScratch { XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); leftAxis.setTitle(series[0]+","+series[1]); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); + leftAxis.setMajorTickMark(AxisTickMark.OUT); + leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN); 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); - values1[6] = 16.0; // if you ever want to change the underlying data - final XDDFNumericalDataSource valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, 2); + 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); + valuesData.setFormatCode("General"); + values1[6] = 16.0; // if you ever want to change the underlying data, it has to be done before building the data source + final XDDFNumericalDataSource valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers); + valuesData2.setFormatCode("General"); XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis); + bar.setBarGrouping(BarGrouping.CLUSTERED); + 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); @@ -135,6 +144,11 @@ public class ChartFromScratch { chart.setTitleText(chartTitle); chart.setTitleOverlay(false); + chart.setAutoTitleDeleted(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 aded9fdca5..e074bf7268 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 @@ -392,7 +392,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai series.plot(); XDDFDataSource categoryDS = series.getCategoryData(); XDDFNumericalDataSource valuesDS = series.getValuesData(); - if (categoryDS.isReference() || valuesDS.isReference() + if (categoryDS.isCellRange() || valuesDS.isCellRange() || categoryDS.isLiteral() || valuesDS.isLiteral()) { // let's assume the data is already in the sheet } else { @@ -773,8 +773,14 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai int numOfPoints = categoryData.getPointCount(); for (int i = 0; i < numOfPoints; i++) { XSSFRow row = this.getRow(sheet, i + 1); // first row is for title - this.getCell(row, categoryData.getColIndex()).setCellValue(categoryData.getPointAt(i).toString()); - this.getCell(row, valuesData.getColIndex()).setCellValue(valuesData.getPointAt(i).doubleValue()); + Object category = categoryData.getPointAt(i); + if (category != null) { + this.getCell(row, categoryData.getColIndex()).setCellValue(category.toString()); + } + Number value = valuesData.getPointAt(i); + if (value != null) { + this.getCell(row, valuesData.getColIndex()).setCellValue(value.doubleValue()); + } } } diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java index f071b15478..8d941bf11c 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java @@ -32,6 +32,11 @@ public interface XDDFDataSource { */ boolean isLiteral(); + /** + * @since POI 4.1.2 + */ + boolean isCellRange(); + boolean isReference(); boolean isNumeric(); diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java index f45e61eb92..b2bf4f6806 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java @@ -46,6 +46,11 @@ public class XDDFDataSourcesFactory { return new XDDFCategoryDataSource() { private CTNumData category = (CTNumData) categoryDS.getNumRef().getNumCache().copy(); + @Override + public boolean isCellRange() { + return true; + } + @Override public boolean isNumeric() { return true; @@ -70,6 +75,11 @@ public class XDDFDataSourcesFactory { return new XDDFCategoryDataSource() { private CTStrData category = (CTStrData) categoryDS.getStrRef().getStrCache().copy(); + @Override + public boolean isCellRange() { + return true; + } + @Override public String getFormula() { return categoryDS.getStrRef().getF(); @@ -108,6 +118,11 @@ public class XDDFDataSourcesFactory { this.formatCode = formatCode; } + @Override + public boolean isCellRange() { + return true; + } + @Override public boolean isNumeric() { return true; @@ -199,6 +214,11 @@ public class XDDFDataSourcesFactory { return elements[index]; } + @Override + public boolean isCellRange() { + return false; + } + @Override public boolean isReference() { return dataRange != null; @@ -310,6 +330,11 @@ public class XDDFDataSourcesFactory { return numOfCells; } + @Override + public boolean isCellRange() { + return true; + } + @Override public boolean isReference() { return true; -- 2.39.5