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 2021/01/21 21:13:27 UTC

svn commit: r1885771 - in /poi/trunk/src: examples/src/org/apache/poi/examples/xslf/ ooxml/java/org/apache/poi/ooxml/util/ ooxml/java/org/apache/poi/xddf/usermodel/chart/ ooxml/testcases/org/apache/poi/xddf/usermodel/

Author: abearez
Date: Thu Jan 21 21:13:27 2021
New Revision: 1885771

URL: http://svn.apache.org/viewvc?rev=1885771&view=rev
Log:
Bug 64950: handle doughnut hole size

Added:
    poi/trunk/src/examples/src/org/apache/poi/examples/xslf/DoughnutChartFromScratch.java
      - copied, changed from r1885770, poi/trunk/src/examples/src/org/apache/poi/examples/xslf/ChartFromScratch.java
Modified:
    poi/trunk/src/examples/src/org/apache/poi/examples/xslf/BarChartDemo.java
    poi/trunk/src/examples/src/org/apache/poi/examples/xslf/ChartFromScratch.java
    poi/trunk/src/examples/src/org/apache/poi/examples/xslf/PieChartDemo.java
    poi/trunk/src/ooxml/java/org/apache/poi/ooxml/util/POIXMLUnits.java
    poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDoughnutChartData.java
    poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFPieChartData.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xddf/usermodel/TestNecessaryOOXMLClasses.java

Modified: poi/trunk/src/examples/src/org/apache/poi/examples/xslf/BarChartDemo.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/examples/xslf/BarChartDemo.java?rev=1885771&r1=1885770&r2=1885771&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/examples/xslf/BarChartDemo.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/examples/xslf/BarChartDemo.java Thu Jan 21 21:13:27 2021
@@ -64,7 +64,7 @@ public final class BarChartDemo {
         }
 
         try (FileInputStream argIS = new FileInputStream(args[0]);
-            BufferedReader modelReader = Files.newBufferedReader(Paths.get(args[1]), StandardCharsets.ISO_8859_1)) {
+            BufferedReader modelReader = Files.newBufferedReader(Paths.get(args[1]), StandardCharsets.UTF_8)) {
 
             String chartTitle = modelReader.readLine();  // first line is chart title
             String[] series = modelReader.readLine().split(",");

Modified: poi/trunk/src/examples/src/org/apache/poi/examples/xslf/ChartFromScratch.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/examples/xslf/ChartFromScratch.java?rev=1885771&r1=1885770&r2=1885771&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/examples/xslf/ChartFromScratch.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/examples/xslf/ChartFromScratch.java Thu Jan 21 21:13:27 2021
@@ -72,7 +72,7 @@ public final class ChartFromScratch {
             return;
         }
 
-        try (BufferedReader modelReader = Files.newBufferedReader(Paths.get(args[0]), StandardCharsets.ISO_8859_1)) {
+        try (BufferedReader modelReader = Files.newBufferedReader(Paths.get(args[0]), StandardCharsets.UTF_8)) {
 
             String chartTitle = modelReader.readLine();  // first line is chart title
             String[] series = modelReader.readLine().split(",");
@@ -163,10 +163,10 @@ public final class ChartFromScratch {
         bar.setBarGrouping(BarGrouping.CLUSTERED);
 
         XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData);
-        series1.setTitle(series[0], chart.setSheetTitle(series[0], COLUMN_COUNTRIES));
+        series1.setTitle(series[0], chart.setSheetTitle(series[COLUMN_COUNTRIES - 1], COLUMN_COUNTRIES));
 
         XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData2);
-        series2.setTitle(series[1], chart.setSheetTitle(series[1], COLUMN_SPEAKERS));
+        series2.setTitle(series[1], chart.setSheetTitle(series[COLUMN_SPEAKERS - 1], COLUMN_SPEAKERS));
 
         bar.setVaryColors(true);
         bar.setBarDirection(BarDirection.COL);

Copied: poi/trunk/src/examples/src/org/apache/poi/examples/xslf/DoughnutChartFromScratch.java (from r1885770, poi/trunk/src/examples/src/org/apache/poi/examples/xslf/ChartFromScratch.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/examples/xslf/DoughnutChartFromScratch.java?p2=poi/trunk/src/examples/src/org/apache/poi/examples/xslf/DoughnutChartFromScratch.java&p1=poi/trunk/src/examples/src/org/apache/poi/examples/xslf/ChartFromScratch.java&r1=1885770&r2=1885771&rev=1885771&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/examples/xslf/ChartFromScratch.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/examples/xslf/DoughnutChartFromScratch.java Thu Jan 21 21:13:27 2021
@@ -19,32 +19,19 @@
 
 package org.apache.poi.examples.xslf;
 
-import java.awt.geom.Rectangle2D;
-import java.io.BufferedReader;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.OutputStream;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-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.XDDFChartAxis;
 import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
 import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
 import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
+import org.apache.poi.xddf.usermodel.chart.XDDFDoughnutChartData;
 import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
 import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
 import org.apache.poi.xslf.usermodel.XMLSlideShow;
@@ -53,15 +40,26 @@ import org.apache.poi.xslf.usermodel.XSL
 import org.apache.poi.xslf.usermodel.XSLFShape;
 import org.apache.poi.xslf.usermodel.XSLFSlide;
 
+import java.awt.geom.Rectangle2D;
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Build a chart without reading template file
  */
 @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
-public final class ChartFromScratch {
-    private ChartFromScratch() {}
+public final class DoughnutChartFromScratch {
+    private DoughnutChartFromScratch() {}
 
     private static void usage(){
-        System.out.println("Usage: ChartFromScratch <bar-chart-data.txt>");
+        System.out.println("Usage: DoughnutChartFromScratch <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}");
     }
@@ -72,7 +70,7 @@ public final class ChartFromScratch {
             return;
         }
 
-        try (BufferedReader modelReader = Files.newBufferedReader(Paths.get(args[0]), StandardCharsets.ISO_8859_1)) {
+        try (BufferedReader modelReader = Files.newBufferedReader(Paths.get(args[0]), StandardCharsets.UTF_8)) {
 
             String chartTitle = modelReader.readLine();  // first line is chart title
             String[] series = modelReader.readLine().split(",");
@@ -98,15 +96,14 @@ public final class ChartFromScratch {
             Double[] values2 = listSpeakers.toArray(new Double[0]);
 
             try (XMLSlideShow ppt = new XMLSlideShow()) {
-                createSlideWithChart(ppt, chartTitle, series, categories, values1, values2);
-                createSlideWithChart(ppt, chartTitle, series, categories, values1, values2);
-                createSlideWithChart(ppt, chartTitle, series, categories, values1, values2);
+                createSlideWithChart(ppt, chartTitle, series, categories, values1, COLUMN_COUNTRIES);
+                createSlideWithChart(ppt, chartTitle, series, categories, values2, COLUMN_SPEAKERS);
                 // save the result
-                try (OutputStream out = new FileOutputStream("chart-from-scratch.pptx")) {
+                try (OutputStream out = new FileOutputStream("doughnut-chart-from-scratch.pptx")) {
                     ppt.write(out);
                 }
             }
-            try (FileInputStream is = new FileInputStream("chart-from-scratch.pptx")) {
+            try (FileInputStream is = new FileInputStream("doughnut-chart-from-scratch.pptx")) {
                 try (XMLSlideShow ppt = new XMLSlideShow(is)) {
                     for (XSLFSlide slide : ppt.getSlides()) {
                         for (XSLFShape shape : slide.getShapes()) {
@@ -125,52 +122,35 @@ public final class ChartFromScratch {
     }
 
     private static void createSlideWithChart(XMLSlideShow ppt, String chartTitle, String[] series, String[] categories,
-            Double[] values1, Double[] values2) {
+                                             Double[] values, int valuesColumn) {
         XSLFSlide slide = ppt.createSlide();
         XSLFChart chart = ppt.createChart();
         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);
+        setDoughnutData(chart, chartTitle, series, categories, values, valuesColumn);
     }
 
     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);
-        bottomAxis.setTitle(series[2]);
-        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);
-
+    private static void setDoughnutData(XSLFChart chart, String chartTitle, String[] series, String[] categories,
+                                        Double[] values, int valuesColumn) {
         final int numOfPoints = categories.length;
         final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_LANGUAGES, COLUMN_LANGUAGES));
-        final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_COUNTRIES, COLUMN_COUNTRIES));
-        final String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_SPEAKERS, COLUMN_SPEAKERS));
+        final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, valuesColumn, valuesColumn));
         final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, COLUMN_LANGUAGES);
-        final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange, COLUMN_COUNTRIES);
+        final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange, valuesColumn);
         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, COLUMN_SPEAKERS);
-        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], COLUMN_COUNTRIES));
-
-        XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(categoriesData, valuesData2);
-        series2.setTitle(series[1], chart.setSheetTitle(series[1], COLUMN_SPEAKERS));
 
-        bar.setVaryColors(true);
-        bar.setBarDirection(BarDirection.COL);
-        chart.plot(bar);
+        XDDFDoughnutChartData data = (XDDFDoughnutChartData) chart.createData(ChartTypes.DOUGHNUT, null, null);
+        XDDFDoughnutChartData.Series series1 = (XDDFDoughnutChartData.Series) data.addSeries(categoriesData, valuesData);
+        series1.setTitle(series[0], chart.setSheetTitle(series[valuesColumn - 1], valuesColumn));
+
+        data.setVaryColors(true);
+        data.setHoleSize(42);
+        data.setFirstSliceAngle(90);
+        chart.plot(data);
 
         XDDFChartLegend legend = chart.getOrAddLegend();
         legend.setPosition(LegendPosition.LEFT);

Modified: poi/trunk/src/examples/src/org/apache/poi/examples/xslf/PieChartDemo.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/examples/xslf/PieChartDemo.java?rev=1885771&r1=1885770&r2=1885771&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/examples/xslf/PieChartDemo.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/examples/xslf/PieChartDemo.java Thu Jan 21 21:13:27 2021
@@ -61,7 +61,7 @@ public final class PieChartDemo {
         }
 
         try (FileInputStream argIS = new FileInputStream(args[0]);
-             BufferedReader modelReader = Files.newBufferedReader(Paths.get(args[1]), StandardCharsets.ISO_8859_1)) {
+             BufferedReader modelReader = Files.newBufferedReader(Paths.get(args[1]), StandardCharsets.UTF_8)) {
             String chartTitle = modelReader.readLine();  // first line is chart title
 
             try (XMLSlideShow pptx = new XMLSlideShow(argIS)) {

Modified: poi/trunk/src/ooxml/java/org/apache/poi/ooxml/util/POIXMLUnits.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/ooxml/util/POIXMLUnits.java?rev=1885771&r1=1885770&r2=1885771&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/ooxml/util/POIXMLUnits.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/ooxml/util/POIXMLUnits.java Thu Jan 21 21:13:27 2021
@@ -133,6 +133,11 @@ public class POIXMLUnits {
     }
 
 
+    public static int parsePercent(org.openxmlformats.schemas.drawingml.x2006.chart.STHoleSize pctUnion) {
+        return parsePercentInner(pctUnion, 1);
+    }
+
+
     private static int parsePercentInner(org.apache.xmlbeans.XmlAnySimpleType pctUnion, int noUnitScale) {
         String strVal = pctUnion.getStringValue();
         if (strVal.endsWith("%")) {
@@ -211,7 +216,7 @@ public class POIXMLUnits {
 
 
     /**
-     * If not unit is specified, DXA (twentieth of a point) is assumed
+     * If no unit is specified, DXA (twentieth of a point) is assumed
      *
      * @return length in EMUs
      */
@@ -222,7 +227,7 @@ public class POIXMLUnits {
 
 
     /**
-     * Returns the EMUs for the given measurment (mm|cm|in|pt|pc|pi, defaults to EMUs*noUnitEmuFactor if not specified)
+     * Returns the EMUs for the given measurement (mm|cm|in|pt|pc|pi, defaults to EMUs*noUnitEmuFactor if not specified)
      *
      * @param coordUnion the raw type
      * @return the EMUs for the given attribute

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDoughnutChartData.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDoughnutChartData.java?rev=1885771&r1=1885770&r2=1885771&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDoughnutChartData.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFDoughnutChartData.java Thu Jan 21 21:13:27 2021
@@ -17,6 +17,7 @@
 
 package org.apache.poi.xddf.usermodel.chart;
 
+import org.apache.poi.ooxml.util.POIXMLUnits;
 import org.apache.poi.util.Beta;
 import org.apache.poi.util.Internal;
 import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
@@ -60,10 +61,60 @@ public class XDDFDoughnutChartData exten
         }
     }
 
+    public Integer getFirstSliceAngle() {
+        if (chart.isSetFirstSliceAng()) {
+            return chart.getFirstSliceAng().getVal();
+        } else {
+            return null;
+        }
+    }
+
+    public void setFirstSliceAngle(Integer angle) {
+        if (angle == null) {
+            if (chart.isSetFirstSliceAng()) {
+                chart.unsetFirstSliceAng();
+            }
+        } else {
+            if (angle < 0 || 360 < angle) {
+                throw new IllegalArgumentException("Value of angle must be between 0 and 360, both inclusive.");
+            }
+            if (chart.isSetFirstSliceAng()) {
+                chart.getFirstSliceAng().setVal(angle);
+            } else {
+                chart.addNewFirstSliceAng().setVal(angle);
+            }
+        }
+    }
+
+    public Integer getHoleSize() {
+        if (chart.isSetHoleSize()) {
+            return POIXMLUnits.parsePercent(chart.getHoleSize().xgetVal());
+        } else {
+            return null;
+        }
+    }
+
+    public void setHoleSize(Integer holeSize) {
+        if (holeSize == null) {
+            if (chart.isSetHoleSize()) {
+                chart.unsetHoleSize();
+            }
+        } else {
+            if (holeSize < 10 || holeSize > 90) {
+                throw new IllegalArgumentException("Value of holeSize must be between 10 and 90, both inclusive.");
+            }
+            if (chart.isSetHoleSize()) {
+                chart.getHoleSize().setVal(holeSize);
+            } else {
+                chart.addNewHoleSize().setVal(holeSize);
+            }
+        }
+    }
+
     @Override
     public XDDFChartData.Series addSeries(XDDFDataSource<?> category,
             XDDFNumericalDataSource<? extends Number> values) {
-        final int index = this.series.size();
+        final long index = this.parent.incrementSeriesCount();
         final CTPieSer ctSer = this.chart.addNewSer();
         ctSer.addNewCat();
         ctSer.addNewVal();
@@ -133,19 +184,25 @@ public class XDDFDoughnutChartData exten
             }
         }
 
-        public long getExplosion() {
+        public Long getExplosion() {
             if (series.isSetExplosion()) {
                 return series.getExplosion().getVal();
             } else {
-                return 0;
+                return null;
             }
         }
 
-        public void setExplosion(long explosion) {
-            if (series.isSetExplosion()) {
-                series.getExplosion().setVal(explosion);
+        public void setExplosion(Long explosion) {
+            if (explosion == null) {
+                if (series.isSetExplosion()) {
+                    series.unsetExplosion();
+                }
             } else {
-                series.addNewExplosion().setVal(explosion);
+                if (series.isSetExplosion()) {
+                    series.getExplosion().setVal(explosion);
+                } else {
+                    series.addNewExplosion().setVal(explosion);
+                }
             }
         }
 

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFPieChartData.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFPieChartData.java?rev=1885771&r1=1885770&r2=1885771&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFPieChartData.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFPieChartData.java Thu Jan 21 21:13:27 2021
@@ -75,7 +75,7 @@ public class XDDFPieChartData extends XD
             }
         } else {
             if (angle < 0 || 360 < angle) {
-                throw new IllegalArgumentException("angle must be between 0 and 360");
+                throw new IllegalArgumentException("Value of angle must be between 0 and 360, both inclusive.");
             }
             if (chart.isSetFirstSliceAng()) {
                 chart.getFirstSliceAng().setVal(angle);

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xddf/usermodel/TestNecessaryOOXMLClasses.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xddf/usermodel/TestNecessaryOOXMLClasses.java?rev=1885771&r1=1885770&r2=1885771&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xddf/usermodel/TestNecessaryOOXMLClasses.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xddf/usermodel/TestNecessaryOOXMLClasses.java Thu Jan 21 21:13:27 2021
@@ -177,6 +177,7 @@ class TestNecessaryOOXMLClasses {
         assertNotNull(CTErrBarType.Factory.newInstance());
         assertNotNull(CTErrValType.Factory.newInstance());
         assertNotNull(CTErrDir.Factory.newInstance());
+        assertNotNull(CTHoleSize.Factory.newInstance());
 
         STErrBarType.Enum e9 = STErrBarType.Enum.forString("both");
         assertNotNull(e9);



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