You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ab...@apache.org on 2019/12/02 00:15:35 UTC

svn commit: r1870696 - in /poi/trunk/src: examples/src/org/apache/poi/xslf/usermodel/ examples/src/org/apache/poi/xwpf/usermodel/examples/ ooxml/java/org/apache/poi/xddf/usermodel/chart/

Author: abearez
Date: Mon Dec  2 00:15:34 2019
New Revision: 1870696

URL: http://svn.apache.org/viewvc?rev=1870696&view=rev
Log:
Fix examples to build chart from scratch

Modified:
    poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java
    poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java
    poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java
    poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java
    poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java
    poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java

Modified: poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java?rev=1870696&r1=1870695&r2=1870696&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java Mon Dec  2 00:15:34 2019
@@ -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<? extends Number> 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<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, columnSpeakers);
 
         XDDFChartData.Series series1 = bar.getSeries(0);

Modified: poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java?rev=1870696&r1=1870695&r2=1870696&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/ChartFromScratch.java Mon Dec  2 00:15:34 2019
@@ -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.cha
  */
 public class ChartFromScratch {
     private static void usage(){
-        System.out.println("Usage: BarChartExample <bar-chart-data.txt>");
+        System.out.println("Usage: ChartFromScratch <bar-chart-data.txt>");
         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<? extends Number> 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<? extends Number> 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;
 }
-

Modified: poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java?rev=1870696&r1=1870695&r2=1870696&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/ChartFromScratch.java Mon Dec  2 00:15:34 2019
@@ -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.XWP
  */
 public class ChartFromScratch {
     private static void usage(){
-        System.out.println("Usage: BarChartExample <bar-chart-data.txt>");
+        System.out.println("Usage: ChartFromScratch <bar-chart-data.txt>");
         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<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, 1);
-        values1[6] = 16.0; // if you ever want to change the underlying data
-        final XDDFNumericalDataSource<? extends Number> 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<? extends Number> 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<? extends Number> 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;
 }
 

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java?rev=1870696&r1=1870695&r2=1870696&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java Mon Dec  2 00:15:34 2019
@@ -392,7 +392,7 @@ public abstract class XDDFChart extends
             series.plot();
             XDDFDataSource<?> categoryDS = series.getCategoryData();
             XDDFNumericalDataSource<? extends Number> 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
         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());
+            }
         }
     }
 

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java?rev=1870696&r1=1870695&r2=1870696&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSource.java Mon Dec  2 00:15:34 2019
@@ -32,6 +32,11 @@ public interface XDDFDataSource<T> {
      */
     boolean isLiteral();
 
+    /**
+     * @since POI 4.1.2
+     */
+    boolean isCellRange();
+
     boolean isReference();
 
     boolean isNumeric();

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java?rev=1870696&r1=1870695&r2=1870696&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDataSourcesFactory.java Mon Dec  2 00:15:34 2019
@@ -47,6 +47,11 @@ public class XDDFDataSourcesFactory {
                 private CTNumData category = (CTNumData) categoryDS.getNumRef().getNumCache().copy();
 
                 @Override
+                public boolean isCellRange() {
+                    return true;
+                }
+
+                @Override
                 public boolean isNumeric() {
                     return true;
                 }
@@ -71,6 +76,11 @@ public class XDDFDataSourcesFactory {
                 private CTStrData category = (CTStrData) categoryDS.getStrRef().getStrCache().copy();
 
                 @Override
+                public boolean isCellRange() {
+                    return true;
+                }
+
+                @Override
                 public String getFormula() {
                     return categoryDS.getStrRef().getF();
                 }
@@ -109,6 +119,11 @@ public class XDDFDataSourcesFactory {
             }
 
             @Override
+            public boolean isCellRange() {
+                return true;
+            }
+
+            @Override
             public boolean isNumeric() {
                 return true;
             }
@@ -200,6 +215,11 @@ public class XDDFDataSourcesFactory {
         }
 
         @Override
+        public boolean isCellRange() {
+            return false;
+        }
+
+        @Override
         public boolean isReference() {
             return dataRange != null;
         }
@@ -311,6 +331,11 @@ public class XDDFDataSourcesFactory {
         }
 
         @Override
+        public boolean isCellRange() {
+            return true;
+        }
+
+        @Override
         public boolean isReference() {
             return true;
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org