]> source.dussan.org Git - poi.git/commitdiff
adding tricks from other answers on StackOverflow
authorAlain Béarez <abearez@apache.org>
Fri, 5 Oct 2018 18:07:55 +0000 (18:07 +0000)
committerAlain Béarez <abearez@apache.org>
Fri, 5 Oct 2018 18:07:55 +0000 (18:07 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842959 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/xssf/usermodel/examples/BarAndLineChart.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/BarChart.java
src/examples/src/org/apache/poi/xwpf/usermodel/examples/BarChartExample.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java
src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFTitle.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFChart.java

index 13d4ff654c8d4ea2c401d128563b36c4a7ddb671..1de43cb974342f6abf5eaaedf93a6daed4ca4229 100644 (file)
@@ -22,6 +22,7 @@ package org.apache.poi.xssf.usermodel.examples;
 import java.io.FileOutputStream;
 import java.util.Random;
 
+import org.apache.poi.common.usermodel.fonts.FontGroup;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.xddf.usermodel.PresetColor;
@@ -33,6 +34,7 @@ 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.LayoutMode;
 import org.apache.poi.xddf.usermodel.chart.LegendPosition;
 import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
 import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
@@ -41,8 +43,13 @@ import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
 import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
 import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
 import org.apache.poi.xddf.usermodel.chart.XDDFLineChartData;
+import org.apache.poi.xddf.usermodel.chart.XDDFManualLayout;
 import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
 import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
+import org.apache.poi.xddf.usermodel.text.UnderlineType;
+import org.apache.poi.xddf.usermodel.text.XDDFFont;
+import org.apache.poi.xddf.usermodel.text.XDDFRunProperties;
+import org.apache.poi.xddf.usermodel.text.XDDFTextParagraph;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFChart;
 import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
@@ -51,6 +58,10 @@ import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
+// original contributions by Axel Richter on https://stackoverflow.com/questions/47065690
+// additional title formatting from https://stackoverflow.com/questions/50418856
+// and legend positioning from https://stackoverflow.com/questions/49615379
+// this would probably be an answer for https://stackoverflow.com/questions/36447925 too
 public class BarAndLineChart {
     private static final int NUM_OF_ROWS = 7;
     private static final Random RNG = new Random();
@@ -79,6 +90,21 @@ public class BarAndLineChart {
             XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 0, 11, 15);
 
             XSSFChart chart = drawing.createChart(anchor);
+            chart.setTitleText("This is my title");
+            chart.setTitleOverlay(true);
+            XDDFRunProperties properties = new XDDFRunProperties();
+            properties.setBold(true);
+            properties.setItalic(true);
+            properties.setUnderline(UnderlineType.DOT_DOT_DASH_HEAVY);
+            properties.setFontSize(22.5);
+            XDDFFont[] fonts = new XDDFFont[]{
+                    new XDDFFont(FontGroup.LATIN, "Calibri", null, null, null),
+                    new XDDFFont(FontGroup.COMPLEX_SCRIPT, "Liberation Sans", null, null, null)
+                    };
+            properties.setFonts(fonts);
+            properties.setLineProperties(solidLine(PresetColor.SIENNA));
+            XDDFTextParagraph paragraph = chart.getTitle().getBody().getParagraph(0);
+            paragraph.setDefaultRunProperties(properties);
 
             // the data sources
             XDDFCategoryDataSource xs = XDDFDataSourcesFactory.fromStringCellRange(sheet,
@@ -129,8 +155,13 @@ public class BarAndLineChart {
 
             // legend
             XDDFChartLegend legend = chart.getOrAddLegend();
-            legend.setPosition(LegendPosition.BOTTOM);
+            legend.setPosition(LegendPosition.LEFT);
             legend.setOverlay(false);
+            XDDFManualLayout layout = legend.getOrAddManualLayout();
+            layout.setXMode(LayoutMode.EDGE);
+            layout.setYMode(LayoutMode.EDGE);
+            layout.setX(0.00); //left edge of the chart
+            layout.setY(0.25); //25% of chart's height from top edge of the chart
 
             try (FileOutputStream fileOut = new FileOutputStream("BarAndLineChart.xlsx")) {
                 wb.write(fileOut);
@@ -150,9 +181,7 @@ public class BarAndLineChart {
     }
 
     private static void solidLineSeries(XDDFChartData data, int index, PresetColor color) {
-        XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
-        XDDFLineProperties line = new XDDFLineProperties();
-        line.setFillProperties(fill);
+        XDDFLineProperties line = solidLine(color);
         XDDFChartData.Series series = data.getSeries().get(index);
         XDDFShapeProperties properties = series.getShapeProperties();
         if (properties == null) {
@@ -161,4 +190,11 @@ public class BarAndLineChart {
         properties.setLineProperties(line);
         series.setShapeProperties(properties);
     }
+
+    private static XDDFLineProperties solidLine(PresetColor color) {
+        XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
+        XDDFLineProperties line = new XDDFLineProperties();
+        line.setFillProperties(fill);
+        return line;
+    }
 }
index 3e58b912e8f3623f621f1e93649da56d99ac9798..b3bb44b22ecfb77f890a8aa2be9bf27422fb9af6 100644 (file)
@@ -71,6 +71,8 @@ public class BarChart {
             XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
 
             XSSFChart chart = drawing.createChart(anchor);
+            chart.setTitleText("x = 2x and x = 3x");
+            chart.setTitleOverlay(false);
             XDDFChartLegend legend = chart.getOrAddLegend();
             legend.setPosition(LegendPosition.TOP_RIGHT);
 
index 9950ffebd7f958f89812c6dac82f0a27bf696f8d..ebce9410d1eff485adec254d581b7de10d1b2387 100644 (file)
@@ -118,7 +118,7 @@ public class BarChartExample {
 
         chart.plot(bar);
         chart.setTitleText(chartTitle); // https://stackoverflow.com/questions/30532612
-        // chart.setTitleOverlay(overlay);
+        chart.setTitleOverlay(false);
     }
 
     private static void setColumnData(XWPFChart chart, String chartTitle) {
index 5022a75a2ca4ac047d0d1d02232588b864614696..321626d71fb5b7a93ff9988953bd0f3b96066624 100644 (file)
@@ -255,6 +255,10 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
     }
 
     /**
+     * Sets the title text as a static string.
+     *
+     * @param text
+     *            to use as new title
      * @since 4.0.1
      */
     public void setTitleText(String text) {
index bca40fc8b0de472a282a20734a286cfb31b8f8e5..8df7b9a713dcc648807679f29b86102259fbad25 100644 (file)
@@ -39,20 +39,17 @@ public class XDDFTitle {
     }
 
     public XDDFTextBody getBody() {
-        XDDFTextBody body;
-        if (title.isSetTxPr()) {
-            body = new XDDFTextBody(parent, title.getTxPr());
-        } else {
-            if (!title.isSetTx()) {
-                title.addNewTx();
-            }
-            CTTx tx = title.getTx();
-            if (!tx.isSetRich()) {
-                tx.addNewRich();
-            }
-            body = new XDDFTextBody(parent, tx.getRich());
+        if (!title.isSetTx()) {
+            title.addNewTx();
+        }
+        CTTx tx = title.getTx();
+        if (tx.isSetStrRef()) {
+            tx.unsetStrRef();
+        }
+        if (!tx.isSetRich()) {
+            tx.addNewRich();
         }
-        return body;
+        return new XDDFTextBody(parent, tx.getRich());
     }
 
     public void setText(String text) {
index d7971839f5a0f41b538a7901189ebbb25f1ccb2f..345b8652d091b47e083a33c758b40368bf45b22b 100644 (file)
@@ -55,10 +55,6 @@ import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTTx;
 import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTTextField;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
@@ -294,60 +290,6 @@ public final class XSSFChart extends XDDFChart implements Chart, ChartAxisFactor
         return new XSSFRichTextString(text.toString());
     }
 
-    /**
-     * Sets the title text as a static string.
-     *
-     * @param newTitle
-     *            to use
-     */
-    @Override
-    public void setTitleText(String newTitle) {
-        CTTitle ctTitle;
-        if (chart.isSetTitle()) {
-            ctTitle = chart.getTitle();
-        } else {
-            ctTitle = chart.addNewTitle();
-        }
-
-        CTTx tx;
-        if (ctTitle.isSetTx()) {
-            tx = ctTitle.getTx();
-        } else {
-            tx = ctTitle.addNewTx();
-        }
-
-        if (tx.isSetStrRef()) {
-            tx.unsetStrRef();
-        }
-
-        CTTextBody rich;
-        if (tx.isSetRich()) {
-            rich = tx.getRich();
-        } else {
-            rich = tx.addNewRich();
-            rich.addNewBodyPr(); // body properties must exist (but can be
-                                 // empty)
-        }
-
-        CTTextParagraph para;
-        if (rich.sizeOfPArray() > 0) {
-            para = rich.getPArray(0);
-        } else {
-            para = rich.addNewP();
-        }
-
-        if (para.sizeOfRArray() > 0) {
-            CTRegularTextRun run = para.getRArray(0);
-            run.setT(newTitle);
-        } else if (para.sizeOfFldArray() > 0) {
-            CTTextField fld = para.getFldArray(0);
-            fld.setT(newTitle);
-        } else {
-            CTRegularTextRun run = para.addNewR();
-            run.setT(newTitle);
-        }
-    }
-
     /**
      * Get the chart title formula expression if there is one
      *