You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSSFChart.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import javax.xml.namespace.QName;
  20. import org.apache.poi.ooxml.POIXMLFactory;
  21. import org.apache.poi.ooxml.POIXMLRelation;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.xddf.usermodel.chart.XDDFChart;
  24. import org.apache.xmlbeans.XmlException;
  25. import org.apache.xmlbeans.XmlObject;
  26. import org.apache.xmlbeans.XmlOptions;
  27. import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartSpace;
  28. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPageMargins;
  29. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
  30. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPrintSettings;
  31. import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;
  32. import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle;
  33. import org.openxmlformats.schemas.drawingml.x2006.chart.CTTx;
  34. import org.w3c.dom.Node;
  35. import org.w3c.dom.NodeList;
  36. import org.w3c.dom.Text;
  37. /**
  38. * Represents a SpreadsheetML Chart
  39. */
  40. public final class XSSFChart extends XDDFChart {
  41. /**
  42. * Parent graphic frame.
  43. */
  44. private XSSFGraphicFrame frame;
  45. /**
  46. * Create a new SpreadsheetML chart
  47. */
  48. protected XSSFChart() {
  49. super();
  50. createChart();
  51. }
  52. /**
  53. * Construct a SpreadsheetML chart from a package part.
  54. *
  55. * @param part
  56. * the package part holding the chart data, the content type must
  57. * be
  58. * {@code application/vnd.openxmlformats-officedocument.drawingml.chart+xml}
  59. *
  60. * @since POI 3.14-Beta1
  61. */
  62. protected XSSFChart(PackagePart part) throws IOException, XmlException {
  63. super(part);
  64. }
  65. @Override
  66. protected POIXMLRelation getChartRelation() {
  67. return null;
  68. }
  69. @Override
  70. protected POIXMLRelation getChartWorkbookRelation() {
  71. return null;
  72. }
  73. @Override
  74. protected POIXMLFactory getChartFactory() {
  75. return null;
  76. }
  77. /**
  78. * Construct a new CTChartSpace bean. By default, it's just an empty
  79. * placeholder for chart objects.
  80. */
  81. private void createChart() {
  82. CTPlotArea plotArea = getCTPlotArea();
  83. plotArea.addNewLayout();
  84. getCTChart().addNewPlotVisOnly().setVal(true);
  85. CTPrintSettings printSettings = chartSpace.addNewPrintSettings();
  86. printSettings.addNewHeaderFooter();
  87. CTPageMargins pageMargins = printSettings.addNewPageMargins();
  88. pageMargins.setB(0.75);
  89. pageMargins.setL(0.70);
  90. pageMargins.setR(0.70);
  91. pageMargins.setT(0.75);
  92. pageMargins.setHeader(0.30);
  93. pageMargins.setFooter(0.30);
  94. printSettings.addNewPageSetup();
  95. }
  96. @Override
  97. protected void commit() throws IOException {
  98. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  99. /*
  100. * Saved chart space must have the following namespaces set:
  101. * <c:chartSpace
  102. * xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
  103. * xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
  104. * xmlns:r=
  105. * "http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  106. */
  107. xmlOptions.setSaveSyntheticDocumentElement(
  108. new QName(CTChartSpace.type.getName().getNamespaceURI(), "chartSpace", "c"));
  109. PackagePart part = getPackagePart();
  110. try (OutputStream out = part.getOutputStream()) {
  111. chartSpace.save(out, xmlOptions);
  112. }
  113. }
  114. /**
  115. * Returns the parent graphic frame.
  116. *
  117. * @return the graphic frame this chart belongs to
  118. */
  119. public XSSFGraphicFrame getGraphicFrame() {
  120. return frame;
  121. }
  122. /**
  123. * Sets the parent graphic frame.
  124. */
  125. protected void setGraphicFrame(XSSFGraphicFrame frame) {
  126. this.frame = frame;
  127. }
  128. /**
  129. * Returns the title static text, or null if none is set. Note that a title
  130. * formula may be set instead. Empty text result is for backward
  131. * compatibility, and could mean the title text is empty or there is a
  132. * formula instead. Check for a formula first, falling back on text for
  133. * cleaner logic.
  134. *
  135. * @return static title text if set, null if there is no title, empty string
  136. * if the title text is empty or the title uses a formula instead
  137. */
  138. public XSSFRichTextString getTitleText() {
  139. if (!getCTChart().isSetTitle()) {
  140. return null;
  141. }
  142. // TODO Do properly
  143. CTTitle title = getCTChart().getTitle();
  144. StringBuilder text = new StringBuilder(64);
  145. XmlObject[] t = title.selectPath("declare namespace a='" + XSSFDrawing.NAMESPACE_A + "' .//a:t");
  146. for (XmlObject element : t) {
  147. NodeList kids = element.getDomNode().getChildNodes();
  148. final int count = kids.getLength();
  149. for (int n = 0; n < count; n++) {
  150. Node kid = kids.item(n);
  151. if (kid instanceof Text) {
  152. text.append(kid.getNodeValue());
  153. }
  154. }
  155. }
  156. return new XSSFRichTextString(text.toString());
  157. }
  158. /**
  159. * Get the chart title formula expression if there is one
  160. *
  161. * @return formula expression or null
  162. */
  163. public String getTitleFormula() {
  164. if (!getCTChart().isSetTitle()) {
  165. return null;
  166. }
  167. CTTitle title = getCTChart().getTitle();
  168. if (!title.isSetTx()) {
  169. return null;
  170. }
  171. CTTx tx = title.getTx();
  172. if (!tx.isSetStrRef()) {
  173. return null;
  174. }
  175. return tx.getStrRef().getF();
  176. }
  177. /**
  178. * Set the formula expression to use for the chart title
  179. */
  180. public void setTitleFormula(String formula) {
  181. CTTitle ctTitle;
  182. if (getCTChart().isSetTitle()) {
  183. ctTitle = getCTChart().getTitle();
  184. } else {
  185. ctTitle = getCTChart().addNewTitle();
  186. }
  187. CTTx tx;
  188. if (ctTitle.isSetTx()) {
  189. tx = ctTitle.getTx();
  190. } else {
  191. tx = ctTitle.addNewTx();
  192. }
  193. if (tx.isSetRich()) {
  194. tx.unsetRich();
  195. }
  196. CTStrRef strRef;
  197. if (tx.isSetStrRef()) {
  198. strRef = tx.getStrRef();
  199. } else {
  200. strRef = tx.addNewStrRef();
  201. }
  202. strRef.setF(formula);
  203. }
  204. }