]> source.dussan.org Git - poi.git/commitdiff
adapting some contributions by Axel Richter on SO
authorAlain Béarez <abearez@apache.org>
Wed, 26 Sep 2018 00:30:33 +0000 (00:30 +0000)
committerAlain Béarez <abearez@apache.org>
Wed, 26 Sep 2018 00:30:33 +0000 (00:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1841988 13f79535-47bb-0310-9956-ffa450edef68

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/xwpf/usermodel/examples/BarChartExample.java [new file with mode: 0644]
src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExampleDOCX.java [deleted file]
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/XDDFRadarChartData.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFScatterChartData.java

index 3b45f166c2ffccf42326608b14c3f83f97354fb4..ac1d68a7dfa1c80b0bc07d815dbb74c9a2c6516e 100644 (file)
@@ -115,6 +115,9 @@ public class BarChartDemo {
         // in order to transform a bar chart into a column chart, you just need to change the bar direction
         bar.setBarDirection(BarDirection.COL);
 
+        // looking for "Stacked Bar Chart"? uncomment the following line
+        // bar.setBarGrouping(BarGrouping.STACKED);
+
         // additionally, you can adjust the axes
         bar.getCategoryAxis().setOrientation(AxisOrientation.MAX_MIN);
         bar.getValueAxes().get(0).setPosition(AxisPosition.TOP);
index e05599f9fb094e698b965e80cfbc398c20f63289..d9090d7dd1f47b6b5cea624cb686457a3848c572 100644 (file)
@@ -28,8 +28,10 @@ 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.BarDirection;
 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.XDDFCategoryAxis;
 import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
 import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
@@ -86,14 +88,14 @@ public class BarChart {
             data.addSeries(xs, ys2);
             chart.plot(data);
 
-            XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.CHARTREUSE));
-            XDDFChartData.Series firstSeries = data.getSeries().get(0);
-            XDDFShapeProperties properties = firstSeries.getShapeProperties();
-            if (properties == null) {
-                properties = new XDDFShapeProperties();
-            }
-            properties.setFillProperties(fill);
-            firstSeries.setShapeProperties(properties);
+            // in order to transform a bar chart into a column chart, you just need to change the bar direction
+            XDDFBarChartData bar = (XDDFBarChartData) data;
+            bar.setBarDirection(BarDirection.COL);
+            // looking for "Stacked Bar Chart"? uncomment the following line
+            // bar.setBarGrouping(BarGrouping.STACKED);
+
+            solidFillSeries(data, 0, PresetColor.CHARTREUSE);
+            solidFillSeries(data, 1, PresetColor.TURQUOISE);
 
             // Write the output to a file
             try (FileOutputStream fileOut = new FileOutputStream("ooxml-bar-chart.xlsx")) {
@@ -101,4 +103,15 @@ 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();
+        if (properties == null) {
+            properties = new XDDFShapeProperties();
+        }
+        properties.setFillProperties(fill);
+        firstSeries.setShapeProperties(properties);
+    }
 }
diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExample.java b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExample.java
new file mode 100644 (file)
index 0000000..12a2718
--- /dev/null
@@ -0,0 +1,131 @@
+
+/*
+ *  ====================================================================
+ *    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.xwpf.usermodel.examples;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xddf.usermodel.chart.AxisOrientation;
+import org.apache.poi.xddf.usermodel.chart.AxisPosition;
+import org.apache.poi.xddf.usermodel.chart.BarDirection;
+import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
+import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
+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.xwpf.usermodel.XWPFChart;
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+
+/**
+ * Build a bar chart from a template docx
+ */
+public class BarChartExample {
+    private static void usage(){
+        System.out.println("Usage: BarChartExample <bar-chart-template.docx> <bar-chart-data.txt>");
+        System.out.println("    bar-chart-template.docx     template with a bar chart");
+        System.out.println("    bar-chart-data.txt          the model to set. First line is chart title, " +
+                "then go pairs {axis-label value}");
+    }
+
+    public static void main(String[] args) throws Exception {
+        if(args.length < 2) {
+            usage();
+            return;
+        }
+
+        try (FileInputStream argIS = new FileInputStream(args[0]);
+                BufferedReader modelReader = new BufferedReader(new FileReader(args[1]))) {
+
+            String chartTitle = modelReader.readLine();  // first line is chart title
+
+            // Category Axis Data
+            List<String> listCategories = new ArrayList<String>(3);
+
+            // Values
+            List<Double> listValues = new ArrayList<Double>(3);
+
+            // set model
+            String ln;
+            while((ln = modelReader.readLine()) != null) {
+                String[] vals = ln.split("\\s+");
+                listCategories.add(vals[0]);
+                listValues.add(Double.valueOf(vals[1]));
+            }
+            String[] categories = listCategories.toArray(new String[listCategories.size()]);
+            Double[] values = listValues.toArray(new Double[listValues.size()]);
+
+            try (XWPFDocument doc = new XWPFDocument(argIS)) {
+                XWPFChart chart = doc.getCharts().get(0);
+                setBarData(chart, chartTitle, categories, values);
+                chart = doc.getCharts().get(1);
+                setColumnData(chart, "Column variant");
+
+                // save the result
+                try (OutputStream out = new FileOutputStream("bar-chart-demo-output.docx")) {
+                    doc.write(out);
+                }
+            }
+        }
+        System.out.println("Done");
+    }
+
+    private static void setBarData(XWPFChart chart, String chartTitle, String[] categories, Double[] values) {
+        final List<XDDFChartData> series = chart.getChartSeries();
+        final XDDFBarChartData bar = (XDDFBarChartData) series.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<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange, 1);
+        values[2] = 10.0;
+        final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values, valuesDataRange2, 2);
+        bar.getSeries().get(0).replaceData(categoriesData, valuesData);
+        bar.addSeries(categoriesData, valuesData2);
+        bar.getSeries().get(0).setTitle(chartTitle, chart.setSheetTitle(chartTitle));
+        chart.plot(bar);
+    }
+
+    private static void setColumnData(XWPFChart chart, String chartTitle) {
+        // Series Text
+        List<XDDFChartData> series = chart.getChartSeries();
+        XDDFBarChartData bar = (XDDFBarChartData) series.get(0);
+        bar.getSeries().get(0).setTitle(chartTitle, chart.setSheetTitle(chartTitle));
+
+        // in order to transform a bar chart into a column chart, you just need to change the bar direction
+        bar.setBarDirection(BarDirection.COL);
+        
+        // looking for "Stacked Bar Chart"? uncomment the following line
+        // bar.setBarGrouping(BarGrouping.STACKED);
+
+        // additionally, you can adjust the axes
+        bar.getCategoryAxis().setOrientation(AxisOrientation.MAX_MIN);
+        bar.getValueAxes().get(0).setPosition(AxisPosition.TOP);
+    }
+}
+
diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExampleDOCX.java b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExampleDOCX.java
deleted file mode 100644 (file)
index a2fa59c..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-
-/*
- *  ====================================================================
- *    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.xwpf.usermodel.examples;
-
-import java.io.BufferedReader;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.poi.ss.util.CellRangeAddress;
-import org.apache.poi.xddf.usermodel.chart.AxisOrientation;
-import org.apache.poi.xddf.usermodel.chart.AxisPosition;
-import org.apache.poi.xddf.usermodel.chart.BarDirection;
-import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
-import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
-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.xwpf.usermodel.XWPFChart;
-import org.apache.poi.xwpf.usermodel.XWPFDocument;
-
-/**
- * Build a bar chart from a template docx
- */
-public class BarChartExampleDOCX {
-    private static void usage(){
-        System.out.println("Usage: BarChartDemo <bar-chart-template.docx> <bar-chart-data.txt>");
-        System.out.println("    bar-chart-template.docx     template with a bar chart");
-        System.out.println("    bar-chart-data.txt          the model to set. First line is chart title, " +
-                "then go pairs {axis-label value}");
-    }
-
-    public static void main(String[] args) throws Exception {
-        if(args.length < 2) {
-            usage();
-            return;
-        }
-
-        try (FileInputStream argIS = new FileInputStream(args[0]);
-                BufferedReader modelReader = new BufferedReader(new FileReader(args[1]))) {
-
-            String chartTitle = modelReader.readLine();  // first line is chart title
-
-            // Category Axis Data
-            List<String> listCategories = new ArrayList<String>(3);
-
-            // Values
-            List<Double> listValues = new ArrayList<Double>(3);
-
-            // set model
-            String ln;
-            while((ln = modelReader.readLine()) != null) {
-                String[] vals = ln.split("\\s+");
-                listCategories.add(vals[0]);
-                listValues.add(Double.valueOf(vals[1]));
-            }
-            String[] categories = listCategories.toArray(new String[listCategories.size()]);
-            Double[] values = listValues.toArray(new Double[listValues.size()]);
-
-            try (XWPFDocument doc = new XWPFDocument(argIS)) {
-                XWPFChart chart = doc.getCharts().get(0);
-                setBarData(chart, chartTitle, categories, values);
-                chart = doc.getCharts().get(1);
-                setColumnData(chart, "Column variant");
-
-                // save the result
-                try (OutputStream out = new FileOutputStream("bar-chart-demo-output.docx")) {
-                    doc.write(out);
-                }
-            }
-        }
-        System.out.println("Done");
-    }
-
-    private static void setBarData(XWPFChart chart, String chartTitle, String[] categories, Double[] values) {
-        final List<XDDFChartData> series = chart.getChartSeries();
-        final XDDFBarChartData bar = (XDDFBarChartData) series.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<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange, 1);
-        values[2] = 10.0;
-        final XDDFNumericalDataSource<? extends Number> valuesData2 = XDDFDataSourcesFactory.fromArray(values, valuesDataRange2, 2);
-        bar.getSeries().get(0).replaceData(categoriesData, valuesData);
-        bar.addSeries(categoriesData, valuesData2);
-        bar.getSeries().get(0).setTitle(chartTitle, chart.setSheetTitle(chartTitle));
-        chart.plot(bar);
-    }
-
-    private static void setColumnData(XWPFChart chart, String chartTitle) {
-        // Series Text
-        List<XDDFChartData> series = chart.getChartSeries();
-        XDDFBarChartData bar = (XDDFBarChartData) series.get(0);
-        bar.getSeries().get(0).setTitle(chartTitle, chart.setSheetTitle(chartTitle));
-
-        // in order to transform a bar chart into a column chart, you just need to change the bar direction
-        bar.setBarDirection(BarDirection.COL);
-
-        // additionally, you can adjust the axes
-        bar.getCategoryAxis().setOrientation(AxisOrientation.MAX_MIN);
-        bar.getValueAxes().get(0).setPosition(AxisPosition.TOP);
-    }
-}
-
index a696a3b8f472fea8807e1f2a22fea59f0ce81766..5e89e775fab9a317b451eb13a7ff8c293d5274e4 100644 (file)
@@ -34,9 +34,24 @@ public class XDDFBarChartData extends XDDFChartData {
     public XDDFBarChartData(CTBarChart chart, Map<Long, XDDFChartAxis> categories,
             Map<Long, XDDFValueAxis> values) {
         this.chart = chart;
+        if (chart.getBarDir() == null) {
+            chart.addNewBarDir().setVal(BarDirection.BAR.underlying);
+        }
         for (CTBarSer series : chart.getSerList()) {
             this.series.add(new Series(series, series.getCat(), series.getVal()));
         }
+        defineAxes(categories, values);
+    }
+
+    private void defineAxes(Map<Long, XDDFChartAxis> categories, Map<Long, XDDFValueAxis> values) {
+        if (chart.sizeOfAxIdArray() == 0) {
+            for (Long id : categories.keySet()) {
+                chart.addNewAxId().setVal(id);
+            }
+            for (Long id : values.keySet()) {
+                chart.addNewAxId().setVal(id);
+            }
+        }
         defineAxes(chart.getAxIdArray(), categories, values);
     }
 
index f23ce6d23a35a90ed72a4c25a28a2a214e0f6fea..5b43eeb292a77300ccd822491a03c3885b9ed145 100644 (file)
@@ -214,6 +214,34 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
         chart.getAutoTitleDeleted().setVal(deleted);
     }
 
+    /**
+     * @since 4.0.1
+     */
+    public Boolean getTitleOverlay() {
+        if (chart.isSetTitle()) {
+            CTTitle title = chart.getTitle();
+            if (title.isSetOverlay()) {
+                return title.getOverlay().getVal();
+            }
+        }
+        return null;
+    }
+
+    /**
+     * @since 4.0.1
+     */
+    public void setTitleOverlay(boolean overlay) {
+        if (!chart.isSetTitle()) {
+            chart.addNewTitle();
+        }
+        CTTitle title = chart.getTitle();
+        if (title.isSetOverlay()) {
+            title.getOverlay().setVal(overlay);
+        } else {
+            title.addNewOverlay().setVal(overlay);
+        }
+    }
+
     /**
      * Get the chart title body if there is one, i.e. title is set and is not a
      * formula.
@@ -327,7 +355,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
     private Map<Long, XDDFChartAxis> getCategoryAxes() {
         CTPlotArea plotArea = getCTPlotArea();
         int sizeOfArray = plotArea.sizeOfCatAxArray();
-        Map<Long, XDDFChartAxis> axes = new HashMap<Long, XDDFChartAxis>(sizeOfArray);
+        Map<Long, XDDFChartAxis> axes = new HashMap<>(sizeOfArray);
         for (int i = 0; i < sizeOfArray; i++) {
             CTCatAx category = plotArea.getCatAxArray(i);
             axes.put(category.getAxId().getVal(), new XDDFCategoryAxis(category));
index 03ee375e93cc835d9bdda2047d417e10e6d7cae4..687bc44abe99bb29b78bc28674d9d19ca472c5ed 100644 (file)
@@ -38,6 +38,18 @@ public class XDDFLineChartData extends XDDFChartData {
         for (CTLineSer series : chart.getSerList()) {
             this.series.add(new Series(series, series.getCat(), series.getVal()));
         }
+        defineAxes(categories, values);
+    }
+
+    private void defineAxes(Map<Long, XDDFChartAxis> categories, Map<Long, XDDFValueAxis> values) {
+        if (chart.sizeOfAxIdArray() == 0) {
+            for (Long id : categories.keySet()) {
+                chart.addNewAxId().setVal(id);
+            }
+            for (Long id : values.keySet()) {
+                chart.addNewAxId().setVal(id);
+            }
+        }
         defineAxes(chart.getAxIdArray(), categories, values);
     }
 
index a189391346cd07c1a7c5d3508679b1b847f52674..ce1cd59656c95ec9e4aafc41f44d62ede739297e 100644 (file)
@@ -38,6 +38,18 @@ public class XDDFRadarChartData extends XDDFChartData {
         for (CTRadarSer series : chart.getSerList()) {
             this.series.add(new Series(series, series.getCat(), series.getVal()));
         }
+        defineAxes(categories, values);
+    }
+
+    private void defineAxes(Map<Long, XDDFChartAxis> categories, Map<Long, XDDFValueAxis> values) {
+        if (chart.sizeOfAxIdArray() == 0) {
+            for (Long id : categories.keySet()) {
+                chart.addNewAxId().setVal(id);
+            }
+            for (Long id : values.keySet()) {
+                chart.addNewAxId().setVal(id);
+            }
+        }
         defineAxes(chart.getAxIdArray(), categories, values);
     }
 
index bd6c69b21c52666e7082d49a6ee36d2b1c927884..22f7eea39670b367cd16799754d6a5fd8809e5c1 100644 (file)
@@ -38,6 +38,18 @@ public class XDDFScatterChartData extends XDDFChartData {
         for (CTScatterSer series : chart.getSerList()) {
             this.series.add(new Series(series, series.getXVal(), series.getYVal()));
         }
+        defineAxes(categories, values);
+    }
+
+    private void defineAxes(Map<Long, XDDFChartAxis> categories, Map<Long, XDDFValueAxis> values) {
+        if (chart.sizeOfAxIdArray() == 0) {
+            for (Long id : categories.keySet()) {
+                chart.addNewAxId().setVal(id);
+            }
+            for (Long id : values.keySet()) {
+                chart.addNewAxId().setVal(id);
+            }
+        }
         defineAxes(chart.getAxIdArray(), categories, values);
     }
 
@@ -99,6 +111,28 @@ public class XDDFScatterChartData extends XDDFChartData {
             return series.getTx();
         }
 
+        /**
+         * @since 4.0.1
+         */
+        public Boolean getSmooth() {
+            if (series.isSetSmooth()) {
+                return series.getSmooth().getVal();
+            } else {
+                return null;
+            }
+        }
+
+        /**
+         * @since 4.0.1
+         */
+        public void setSmooth(boolean smooth) {
+            if (series.isSetSmooth()) {
+                series.getSmooth().setVal(smooth);
+            } else {
+                series.addNewSmooth().setVal(smooth);
+            }
+        }
+
         @Override
         public void setShowLeaderLines(boolean showLeaderLines) {
             if (!series.isSetDLbls()) {