]> source.dussan.org Git - poi.git/commitdiff
XDDF improvements based on StackOverflow examples
authorAlain Béarez <abearez@apache.org>
Wed, 3 Oct 2018 14:32:34 +0000 (14:32 +0000)
committerAlain Béarez <abearez@apache.org>
Wed, 3 Oct 2018 14:32:34 +0000 (14:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842728 13f79535-47bb-0310-9956-ffa450edef68

12 files changed:
src/examples/src/org/apache/poi/xslf/usermodel/BarChartDemo.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/BarChart.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java
src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExample.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/DisplayBlanks.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFBarChartData.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFLineChartData.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFPieChartData.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFRadarChartData.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFScatterChartData.java

index 7b052b7dafb38e3e673e7a6aaed5c96994b9c0ae..bd26f8c731c5b59d0096840ab4d1a811d9ce4a4e 100644 (file)
@@ -107,10 +107,13 @@ public class BarChartDemo {
         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);
-        bar.getSeries().get(0).replaceData(categoriesData, valuesData);
-        bar.addSeries(categoriesData, valuesData2);
-        bar.getSeries().get(0).setTitle(series[0], chart.setSheetTitle(series[0], 0));
-        bar.getSeries().get(1).setTitle(series[1], chart.setSheetTitle(series[1], 1));
+
+        XDDFChartData.Series series1 = bar.getSeries().get(0);
+        series1.replaceData(categoriesData, valuesData);
+        series1.setTitle(series[0], chart.setSheetTitle(series[0], 0));
+        XDDFChartData.Series series2 = bar.addSeries(categoriesData, valuesData2);
+        series2.setTitle(series[1], chart.setSheetTitle(series[1], 1));
+
         chart.plot(bar);
     }
 
index 5e695decf225956d28b0df6ae5323354e5370976..c9e73efaba972e9a19c4f44eb171312839e227f2 100644 (file)
@@ -84,8 +84,10 @@ public class BarChart {
             XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
 
             XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
-            data.addSeries(xs, ys1);
-            data.addSeries(xs, ys2);
+            XDDFChartData.Series series1 = data.addSeries(xs, ys1);
+            series1.setTitle("2x", null); // https://stackoverflow.com/questions/21855842
+            XDDFChartData.Series series2 = data.addSeries(xs, ys2);
+            series2.setTitle("3x", null);
             chart.plot(data);
 
             // in order to transform a bar chart into a column chart, you just need to change the bar direction
@@ -106,12 +108,12 @@ public class BarChart {
 
     private static void solidFillSeries(XDDFChartData data, int index, PresetColor color) {
         XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
-        XDDFChartData.Series firstSeries = data.getSeries().get(index);
-        XDDFShapeProperties properties = firstSeries.getShapeProperties();
+        XDDFChartData.Series series = data.getSeries().get(index);
+        XDDFShapeProperties properties = series.getShapeProperties();
         if (properties == null) {
             properties = new XDDFShapeProperties();
         }
         properties.setFillProperties(fill);
-        firstSeries.setShapeProperties(properties);
+        series.setShapeProperties(properties);
     }
 }
index 3a1e0e8580d89ad997f760823546c4916e4f39bd..9a48f4757a4e2ee90a0b2661bf847be48e5beca7 100644 (file)
@@ -22,15 +22,22 @@ import java.io.IOException;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xddf.usermodel.PresetColor;
+import org.apache.poi.xddf.usermodel.XDDFColor;
+import org.apache.poi.xddf.usermodel.XDDFLineProperties;
+import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
+import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
 import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
 import org.apache.poi.xddf.usermodel.chart.AxisPosition;
 import org.apache.poi.xddf.usermodel.chart.ChartTypes;
 import org.apache.poi.xddf.usermodel.chart.LegendPosition;
+import org.apache.poi.xddf.usermodel.chart.MarkerStyle;
 import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
 import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
 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.XDDFLineChartData;
 import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
 import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
 import org.apache.poi.xssf.usermodel.XSSFChart;
@@ -77,15 +84,42 @@ public class LineChart {
             XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
             XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
 
-            XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
-            data.addSeries(xs, ys1);
-            data.addSeries(xs, ys2);
+            XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
+            XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs, ys1);
+            series1.setTitle("2x", null); // https://stackoverflow.com/questions/21855842
+            series1.setSmooth(false); // https://stackoverflow.com/questions/29014848
+            series1.setMarkerStyle(MarkerStyle.STAR); // https://stackoverflow.com/questions/39636138
+            XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs, ys2);
+            series2.setTitle("3x", null);
+            series2.setSmooth(true);
+            series2.setMarkerSize((short) 6);
+            series2.setMarkerStyle(MarkerStyle.TRIANGLE); // https://stackoverflow.com/questions/39636138
             chart.plot(data);
 
+            // if your series have missing values like https://stackoverflow.com/questions/29014848
+            // chart.displayBlanksAs(DisplayBlanks.GAP);
+
+            // https://stackoverflow.com/questions/24676460
+            solidLineSeries(data, 0, PresetColor.CHARTREUSE);
+            solidLineSeries(data, 1, PresetColor.TURQUOISE);
+
             // Write the output to a file
             try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {
                 wb.write(fileOut);
             }
         }
     }
+
+    private static void solidLineSeries(XDDFChartData data, int index, PresetColor color) {
+        XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
+        XDDFLineProperties line = new XDDFLineProperties();
+        line.setFillProperties(fill);
+        XDDFChartData.Series series = data.getSeries().get(index);
+        XDDFShapeProperties properties = series.getShapeProperties();
+        if (properties == null) {
+            properties = new XDDFShapeProperties();
+        }
+        properties.setLineProperties(line);
+        series.setShapeProperties(properties);
+    }
 }
index 9b96ee31230a0ed013d482c3b18408dbb2c9a39f..8c62436e5afc207b8446a6c624bbc8c5cd62c562 100644 (file)
@@ -25,6 +25,11 @@ import java.io.IOException;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xddf.usermodel.PresetColor;
+import org.apache.poi.xddf.usermodel.XDDFColor;
+import org.apache.poi.xddf.usermodel.XDDFLineProperties;
+import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
+import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
 import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
 import org.apache.poi.xddf.usermodel.chart.AxisPosition;
 import org.apache.poi.xddf.usermodel.chart.ChartTypes;
@@ -34,6 +39,7 @@ 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.XDDFNumericalDataSource;
+import org.apache.poi.xddf.usermodel.chart.XDDFScatterChartData;
 import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
 import org.apache.poi.xssf.usermodel.XSSFChart;
 import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
@@ -79,16 +85,34 @@ public class ScatterChart {
             XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
 
 
-            XDDFChartData data = chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);
-
-            data.addSeries(xs, ys1);
-            data.addSeries(xs, ys2);
+            XDDFScatterChartData data = (XDDFScatterChartData) chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);
+            XDDFScatterChartData.Series series1 = (XDDFScatterChartData.Series) data.addSeries(xs, ys1);
+            series1.setTitle("2x", null); // https://stackoverflow.com/questions/21855842
+            series1.setSmooth(false); // https://stackoverflow.com/questions/39636138
+            XDDFScatterChartData.Series series2 = (XDDFScatterChartData.Series) data.addSeries(xs, ys2);
+            series2.setTitle("3x", null);
             chart.plot(data);
 
+            solidLineSeries(data, 0, PresetColor.CHARTREUSE);
+            solidLineSeries(data, 1, PresetColor.TURQUOISE);
+
             // Write the output to a file
             try (FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx")) {
                 wb.write(fileOut);
             }
         }
     }
+
+    private static void solidLineSeries(XDDFChartData data, int index, PresetColor color) {
+        XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
+        XDDFLineProperties line = new XDDFLineProperties();
+        line.setFillProperties(fill);
+        XDDFChartData.Series series = data.getSeries().get(index);
+        XDDFShapeProperties properties = series.getShapeProperties();
+        if (properties == null) {
+            properties = new XDDFShapeProperties();
+        }
+        properties.setLineProperties(line);
+        series.setShapeProperties(properties);
+    }
 }
index d1e7f6b8bc16822a66deefce59d7fbe390255d45..55185cbf8117dc628cfe67d0fc4144aae7555a65 100644 (file)
@@ -109,10 +109,13 @@ public class BarChartExample {
         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);
-        bar.getSeries().get(0).replaceData(categoriesData, valuesData);
-        bar.addSeries(categoriesData, valuesData2);
-        bar.getSeries().get(0).setTitle(series[0], chart.setSheetTitle(series[0], 0));
-        bar.getSeries().get(1).setTitle(series[1], chart.setSheetTitle(series[1], 1));
+
+        XDDFChartData.Series series1 = bar.getSeries().get(0);
+        series1.replaceData(categoriesData, valuesData);
+        series1.setTitle(series[0], chart.setSheetTitle(series[0], 0));
+        XDDFChartData.Series series2 = bar.addSeries(categoriesData, valuesData2);
+        series2.setTitle(series[1], chart.setSheetTitle(series[1], 1));
+
         chart.plot(bar);
     }
 
diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/DisplayBlanks.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/DisplayBlanks.java
new file mode 100644 (file)
index 0000000..0d8bbbf
--- /dev/null
@@ -0,0 +1,45 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xddf.usermodel.chart;
+
+import java.util.HashMap;
+
+import org.openxmlformats.schemas.drawingml.x2006.chart.STDispBlanksAs;
+
+public enum DisplayBlanks {
+    GAP(STDispBlanksAs.GAP),
+    SPAN(STDispBlanksAs.SPAN),
+    ZERO(STDispBlanksAs.ZERO);
+
+    final STDispBlanksAs.Enum underlying;
+
+    DisplayBlanks(STDispBlanksAs.Enum mode) {
+        this.underlying = mode;
+    }
+
+    private final static HashMap<STDispBlanksAs.Enum, DisplayBlanks> reverse = new HashMap<>();
+    static {
+        for (DisplayBlanks value : values()) {
+            reverse.put(value.underlying, value);
+        }
+    }
+
+    static DisplayBlanks valueOf(STDispBlanksAs.Enum mode) {
+        return reverse.get(mode);
+    }
+}
index 9f1004ad25fe8287352607409d79259391e70533..ce79733aa85bbac87319505473e67455838048be 100644 (file)
@@ -135,7 +135,11 @@ public class XDDFBarChartData extends XDDFChartData {
 
         @Override
         protected CTSerTx getSeriesText() {
-            return series.getTx();
+            if (series.isSetTx()) {
+                return series.getTx();
+            } else {
+                return series.addNewTx();
+            }
         }
 
         @Override
index d146e59d0decfabe185b8818dfdc246665c20bc0..7357381e78e217ee0ecdb3f2b42c7ee663a731b3 100644 (file)
@@ -214,6 +214,24 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
         chart.getAutoTitleDeleted().setVal(deleted);
     }
 
+    /**
+     * @since 4.0.1
+     *
+     */
+    public void displayBlanksAs(DisplayBlanks as) {
+        if (as == null){
+            if (chart.isSetDispBlanksAs()) {
+                chart.unsetDispBlanksAs();
+            }
+        } else {
+            if (chart.isSetDispBlanksAs()) {
+              chart.getDispBlanksAs().setVal(as.underlying);
+            } else {
+                chart.addNewDispBlanksAs().setVal(as.underlying);
+            }
+        }
+    }
+
     /**
      * @since 4.0.1
      */
index 687bc44abe99bb29b78bc28674d9d19ca472c5ed..4d4f00ce9ae9faefd532258fd74a3d77c8cd9098 100644 (file)
@@ -100,7 +100,11 @@ public class XDDFLineChartData extends XDDFChartData {
 
         @Override
         protected CTSerTx getSeriesText() {
-            return series.getTx();
+            if (series.isSetTx()) {
+                return series.getTx();
+            } else {
+                return series.addNewTx();
+            }
         }
 
         @Override
@@ -139,7 +143,44 @@ public class XDDFLineChartData extends XDDFChartData {
             }
         }
 
+        /**
+         * @since 4.0.1
+         */
+        public Boolean getSmooth() {
+            if (series.isSetSmooth()) {
+                return series.getSmooth().getVal();
+            } else {
+                return null;
+            }
+        }
+
+        /**
+         * @param smooth
+         *        whether or not to smooth lines, if <code>null</code> then reverts to default.
+         * @since 4.0.1
+         */
+        public void setSmooth(Boolean smooth) {
+            if (smooth == null) {
+                if (series.isSetSmooth()) {
+                    series.unsetSmooth();
+                }
+            } else {
+                if (series.isSetSmooth()) {
+                    series.getSmooth().setVal(smooth);
+                } else {
+                    series.addNewSmooth().setVal(smooth);
+                }
+            }
+        }
+
+        /**
+         * @param size
+         * <dl><dt>Minimum inclusive:</dt><dd>2</dd><dt>Maximum inclusive:</dt><dd>72</dd></dl>
+         */
         public void setMarkerSize(short size) {
+            if (size < 2 || 72 < size) {
+                throw new IllegalArgumentException("Minimum inclusive: 2; Maximum inclusive: 72");
+            }
             CTMarker marker = getMarker();
             if (marker.isSetSize()) {
                 marker.getSize().setVal(size);
index 3d7cac674037f6316bee3f04c201d38f5f56d2af..988b48e84cd7db626565c185e935158efea409dc 100644 (file)
@@ -75,7 +75,11 @@ public class XDDFPieChartData extends XDDFChartData {
 
         @Override
         protected CTSerTx getSeriesText() {
-            return series.getTx();
+            if (series.isSetTx()) {
+                return series.getTx();
+            } else {
+                return series.addNewTx();
+            }
         }
 
         @Override
index ce1cd59656c95ec9e4aafc41f44d62ede739297e..a7917c30f95e4864c80ba047891d5d33874ebf2c 100644 (file)
@@ -104,7 +104,11 @@ public class XDDFRadarChartData extends XDDFChartData {
 
         @Override
         protected CTSerTx getSeriesText() {
-            return series.getTx();
+            if (series.isSetTx()) {
+                return series.getTx();
+            } else {
+                return series.addNewTx();
+            }
         }
 
         @Override
index 22f7eea39670b367cd16799754d6a5fd8809e5c1..eec3a3730deab2eccc749ef4e49db7f0e457ffd9 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Map;
 import org.apache.poi.util.Beta;
 import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTMarker;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterChart;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterSer;
@@ -108,7 +109,11 @@ public class XDDFScatterChartData extends XDDFChartData {
 
         @Override
         protected CTSerTx getSeriesText() {
-            return series.getTx();
+            if (series.isSetTx()) {
+                return series.getTx();
+            } else {
+                return series.addNewTx();
+            }
         }
 
         /**
@@ -123,13 +128,58 @@ public class XDDFScatterChartData extends XDDFChartData {
         }
 
         /**
+         * @param smooth
+         *        whether or not to smooth lines, if <code>null</code> then reverts to default.
          * @since 4.0.1
          */
-        public void setSmooth(boolean smooth) {
-            if (series.isSetSmooth()) {
-                series.getSmooth().setVal(smooth);
+        public void setSmooth(Boolean smooth) {
+            if (smooth == null) {
+                if (series.isSetSmooth()) {
+                    series.unsetSmooth();
+                }
+            } else {
+                if (series.isSetSmooth()) {
+                    series.getSmooth().setVal(smooth);
+                } else {
+                    series.addNewSmooth().setVal(smooth);
+                }
+            }
+        }
+
+        /**
+         * @param size
+         * <dl><dt>Minimum inclusive:</dt><dd>2</dd><dt>Maximum inclusive:</dt><dd>72</dd></dl>
+         * @since 4.0.1
+         */
+        public void setMarkerSize(short size) {
+            if (size < 2 || 72 < size) {
+                throw new IllegalArgumentException("Minimum inclusive: 2; Maximum inclusive: 72");
+            }
+            CTMarker marker = getMarker();
+            if (marker.isSetSize()) {
+                marker.getSize().setVal(size);
+            } else {
+                marker.addNewSize().setVal(size);
+            }
+        }
+
+        /**
+         * @since 4.0.1
+         */
+        public void setMarkerStyle(MarkerStyle style) {
+            CTMarker marker = getMarker();
+            if (marker.isSetSymbol()) {
+                marker.getSymbol().setVal(style.underlying);
+            } else {
+                marker.addNewSymbol().setVal(style.underlying);
+            }
+        }
+
+        private CTMarker getMarker() {
+            if (series.isSetMarker()) {
+                return series.getMarker();
             } else {
-                series.addNewSmooth().setVal(smooth);
+                return series.addNewMarker();
             }
         }