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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import javax.xml.namespace.QName;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.ss.usermodel.Chart;
  24. import org.apache.poi.ss.usermodel.charts.ChartAxis;
  25. import org.apache.poi.ss.usermodel.charts.ChartAxisFactory;
  26. import org.apache.poi.ss.usermodel.charts.ChartData;
  27. import org.apache.poi.util.Removal;
  28. import org.apache.poi.xddf.usermodel.chart.XDDFChart;
  29. import org.apache.poi.xssf.usermodel.charts.XSSFCategoryAxis;
  30. import org.apache.poi.xssf.usermodel.charts.XSSFChartAxis;
  31. import org.apache.poi.xssf.usermodel.charts.XSSFChartDataFactory;
  32. import org.apache.poi.xssf.usermodel.charts.XSSFChartLegend;
  33. import org.apache.poi.xssf.usermodel.charts.XSSFDateAxis;
  34. import org.apache.poi.xssf.usermodel.charts.XSSFManualLayout;
  35. import org.apache.poi.xssf.usermodel.charts.XSSFValueAxis;
  36. import org.apache.xmlbeans.XmlException;
  37. import org.apache.xmlbeans.XmlObject;
  38. import org.apache.xmlbeans.XmlOptions;
  39. import org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx;
  40. import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartSpace;
  41. import org.openxmlformats.schemas.drawingml.x2006.chart.CTDateAx;
  42. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPageMargins;
  43. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
  44. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPrintSettings;
  45. import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;
  46. import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle;
  47. import org.openxmlformats.schemas.drawingml.x2006.chart.CTTx;
  48. import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
  49. import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
  50. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
  51. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextField;
  52. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
  53. import org.w3c.dom.Node;
  54. import org.w3c.dom.NodeList;
  55. import org.w3c.dom.Text;
  56. /**
  57. * Represents a SpreadsheetML Chart
  58. */
  59. public final class XSSFChart extends XDDFChart implements Chart, ChartAxisFactory {
  60. /**
  61. * Parent graphic frame.
  62. */
  63. private XSSFGraphicFrame frame;
  64. @Deprecated
  65. @Removal(version="4.2")
  66. List<XSSFChartAxis> axis = new ArrayList<>();
  67. /**
  68. * Create a new SpreadsheetML chart
  69. */
  70. protected XSSFChart() {
  71. super();
  72. createChart();
  73. }
  74. /**
  75. * Construct a SpreadsheetML chart from a package part.
  76. *
  77. * @param part
  78. * the package part holding the chart data, the content type must be
  79. * <code>application/vnd.openxmlformats-officedocument.drawingml.chart+xml</code>
  80. *
  81. * @since POI 3.14-Beta1
  82. */
  83. protected XSSFChart(PackagePart part) throws IOException, XmlException {
  84. super(part);
  85. }
  86. /**
  87. * Construct a new CTChartSpace bean. By default, it's just an empty placeholder for chart objects.
  88. */
  89. private void createChart() {
  90. CTPlotArea plotArea = getCTPlotArea();
  91. plotArea.addNewLayout();
  92. chart.addNewPlotVisOnly().setVal(true);
  93. CTPrintSettings printSettings = chartSpace.addNewPrintSettings();
  94. printSettings.addNewHeaderFooter();
  95. CTPageMargins pageMargins = printSettings.addNewPageMargins();
  96. pageMargins.setB(0.75);
  97. pageMargins.setL(0.70);
  98. pageMargins.setR(0.70);
  99. pageMargins.setT(0.75);
  100. pageMargins.setHeader(0.30);
  101. pageMargins.setFooter(0.30);
  102. printSettings.addNewPageSetup();
  103. }
  104. @Override
  105. protected void commit() throws IOException {
  106. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  107. /*
  108. * Saved chart space must have the following namespaces set: <c:chartSpace
  109. * xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
  110. * xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r=
  111. * "http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  112. */
  113. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTChartSpace.type.getName().getNamespaceURI(), "chartSpace", "c"));
  114. PackagePart part = getPackagePart();
  115. try (OutputStream out = part.getOutputStream()) {
  116. chartSpace.save(out, xmlOptions);
  117. }
  118. }
  119. /**
  120. * Returns the parent graphic frame.
  121. *
  122. * @return the graphic frame this chart belongs to
  123. */
  124. public XSSFGraphicFrame getGraphicFrame() {
  125. return frame;
  126. }
  127. /**
  128. * Sets the parent graphic frame.
  129. */
  130. protected void setGraphicFrame(XSSFGraphicFrame frame) {
  131. this.frame = frame;
  132. }
  133. @Override
  134. @Deprecated
  135. @Removal(version="4.2")
  136. public XSSFChartDataFactory getChartDataFactory() {
  137. return XSSFChartDataFactory.getInstance();
  138. }
  139. @Override
  140. @Deprecated
  141. @Removal(version="4.2")
  142. public XSSFChart getChartAxisFactory() {
  143. return this;
  144. }
  145. @Override
  146. @Deprecated
  147. @Removal(version="4.2")
  148. public void plot(ChartData data, ChartAxis... chartAxis) {
  149. data.fillChart(this, chartAxis);
  150. }
  151. @Override
  152. @Deprecated
  153. @Removal(version="4.2")
  154. public XSSFValueAxis createValueAxis(org.apache.poi.ss.usermodel.charts.AxisPosition pos) {
  155. long id = axis.size() + 1;
  156. XSSFValueAxis valueAxis = new XSSFValueAxis(this, id, pos);
  157. if (axis.size() == 1) {
  158. ChartAxis ax = axis.get(0);
  159. ax.crossAxis(valueAxis);
  160. valueAxis.crossAxis(ax);
  161. }
  162. axis.add(valueAxis);
  163. return valueAxis;
  164. }
  165. @Override
  166. @Deprecated
  167. @Removal(version="4.2")
  168. public XSSFCategoryAxis createCategoryAxis(org.apache.poi.ss.usermodel.charts.AxisPosition pos) {
  169. long id = axis.size() + 1;
  170. XSSFCategoryAxis categoryAxis = new XSSFCategoryAxis(this, id, pos);
  171. if (axis.size() == 1) {
  172. ChartAxis ax = axis.get(0);
  173. ax.crossAxis(categoryAxis);
  174. categoryAxis.crossAxis(ax);
  175. }
  176. axis.add(categoryAxis);
  177. return categoryAxis;
  178. }
  179. @Override
  180. @Deprecated
  181. @Removal(version="4.2")
  182. public XSSFDateAxis createDateAxis(org.apache.poi.ss.usermodel.charts.AxisPosition pos) {
  183. long id = axis.size() + 1;
  184. XSSFDateAxis dateAxis = new XSSFDateAxis(this, id, pos);
  185. if (axis.size() == 1) {
  186. ChartAxis ax = axis.get(0);
  187. ax.crossAxis(dateAxis);
  188. dateAxis.crossAxis(ax);
  189. }
  190. axis.add(dateAxis);
  191. return dateAxis;
  192. }
  193. /**
  194. * @deprecated use {@link getAxes} instead
  195. */
  196. @Override
  197. @Deprecated
  198. @Removal(version="4.2")
  199. public List<? extends XSSFChartAxis> getAxis() {
  200. if (axis.isEmpty() && hasAxis()) {
  201. parseAxis();
  202. }
  203. return axis;
  204. }
  205. @Override
  206. @Deprecated
  207. @Removal(version="4.2")
  208. public XSSFManualLayout getManualLayout() {
  209. return new XSSFManualLayout(this);
  210. }
  211. /**
  212. * Returns the title static text, or null if none is set.
  213. * Note that a title formula may be set instead.
  214. * @return static title text, if set
  215. * @deprecated POI 3.16, use {@link #getTitleText()} instead.
  216. */
  217. @Deprecated
  218. @Removal(version="4.0")
  219. public XSSFRichTextString getTitle() {
  220. return getTitleText();
  221. }
  222. /**
  223. * Returns the title static text, or null if none is set.
  224. * Note that a title formula may be set instead.
  225. * Empty text result is for backward compatibility, and could mean the title text is empty or there is a formula instead.
  226. * Check for a formula first, falling back on text for cleaner logic.
  227. * @return static title text if set,
  228. * null if there is no title,
  229. * empty string if the title text is empty or the title uses a formula instead
  230. */
  231. public XSSFRichTextString getTitleText() {
  232. if(! chart.isSetTitle()) {
  233. return null;
  234. }
  235. // TODO Do properly
  236. CTTitle title = chart.getTitle();
  237. StringBuilder text = new StringBuilder(64);
  238. XmlObject[] t = title
  239. .selectPath("declare namespace a='"+XSSFDrawing.NAMESPACE_A+"' .//a:t");
  240. for (XmlObject element : t) {
  241. NodeList kids = element.getDomNode().getChildNodes();
  242. final int count = kids.getLength();
  243. for (int n = 0; n < count; n++) {
  244. Node kid = kids.item(n);
  245. if (kid instanceof Text) {
  246. text.append(kid.getNodeValue());
  247. }
  248. }
  249. }
  250. return new XSSFRichTextString(text.toString());
  251. }
  252. /**
  253. * Sets the title text as a static string.
  254. * @param newTitle to use
  255. * @deprecated POI 3.16, use {@link #setTitleText(String)} instead.
  256. */
  257. @Deprecated
  258. @Removal(version = "4.0")
  259. public void setTitle(String newTitle) {
  260. }
  261. /**
  262. * Sets the title text as a static string.
  263. *
  264. * @param newTitle
  265. * to use
  266. */
  267. public void setTitleText(String newTitle) {
  268. CTTitle ctTitle;
  269. if (chart.isSetTitle()) {
  270. ctTitle = chart.getTitle();
  271. } else {
  272. ctTitle = chart.addNewTitle();
  273. }
  274. CTTx tx;
  275. if (ctTitle.isSetTx()) {
  276. tx = ctTitle.getTx();
  277. } else {
  278. tx = ctTitle.addNewTx();
  279. }
  280. if (tx.isSetStrRef()) {
  281. tx.unsetStrRef();
  282. }
  283. CTTextBody rich;
  284. if (tx.isSetRich()) {
  285. rich = tx.getRich();
  286. } else {
  287. rich = tx.addNewRich();
  288. rich.addNewBodyPr(); // body properties must exist (but can be
  289. // empty)
  290. }
  291. CTTextParagraph para;
  292. if (rich.sizeOfPArray() > 0) {
  293. para = rich.getPArray(0);
  294. } else {
  295. para = rich.addNewP();
  296. }
  297. if (para.sizeOfRArray() > 0) {
  298. CTRegularTextRun run = para.getRArray(0);
  299. run.setT(newTitle);
  300. } else if (para.sizeOfFldArray() > 0) {
  301. CTTextField fld = para.getFldArray(0);
  302. fld.setT(newTitle);
  303. } else {
  304. CTRegularTextRun run = para.addNewR();
  305. run.setT(newTitle);
  306. }
  307. }
  308. /**
  309. * Get the chart title formula expression if there is one
  310. *
  311. * @return formula expression or null
  312. */
  313. public String getTitleFormula() {
  314. if (!chart.isSetTitle()) {
  315. return null;
  316. }
  317. CTTitle title = chart.getTitle();
  318. if (!title.isSetTx()) {
  319. return null;
  320. }
  321. CTTx tx = title.getTx();
  322. if (!tx.isSetStrRef()) {
  323. return null;
  324. }
  325. return tx.getStrRef().getF();
  326. }
  327. /**
  328. * Set the formula expression to use for the chart title
  329. *
  330. * @param formula
  331. */
  332. public void setTitleFormula(String formula) {
  333. CTTitle ctTitle;
  334. if (chart.isSetTitle()) {
  335. ctTitle = chart.getTitle();
  336. } else {
  337. ctTitle = chart.addNewTitle();
  338. }
  339. CTTx tx;
  340. if (ctTitle.isSetTx()) {
  341. tx = ctTitle.getTx();
  342. } else {
  343. tx = ctTitle.addNewTx();
  344. }
  345. if (tx.isSetRich()) {
  346. tx.unsetRich();
  347. }
  348. CTStrRef strRef;
  349. if (tx.isSetStrRef()) {
  350. strRef = tx.getStrRef();
  351. } else {
  352. strRef = tx.addNewStrRef();
  353. }
  354. strRef.setF(formula);
  355. }
  356. @Override
  357. @Deprecated
  358. @Removal(version="4.2")
  359. public XSSFChartLegend getOrCreateLegend() {
  360. return new XSSFChartLegend(this);
  361. }
  362. @Deprecated
  363. @Removal(version="4.2")
  364. private boolean hasAxis() {
  365. CTPlotArea ctPlotArea = chart.getPlotArea();
  366. int totalAxisCount = ctPlotArea.sizeOfValAxArray() + ctPlotArea.sizeOfCatAxArray() + ctPlotArea.sizeOfDateAxArray() + ctPlotArea.sizeOfSerAxArray();
  367. return totalAxisCount > 0;
  368. }
  369. @Deprecated
  370. @Removal(version="4.2")
  371. private void parseAxis() {
  372. // TODO: add other axis types
  373. parseCategoryAxis();
  374. parseDateAxis();
  375. parseValueAxis();
  376. }
  377. @Deprecated
  378. @Removal(version="4.2")
  379. private void parseCategoryAxis() {
  380. for (CTCatAx catAx : chart.getPlotArea().getCatAxArray()) {
  381. axis.add(new XSSFCategoryAxis(this, catAx));
  382. }
  383. }
  384. @Deprecated
  385. @Removal(version="4.2")
  386. private void parseDateAxis() {
  387. for (CTDateAx dateAx : chart.getPlotArea().getDateAxArray()) {
  388. axis.add(new XSSFDateAxis(this, dateAx));
  389. }
  390. }
  391. @Deprecated
  392. @Removal(version="4.2")
  393. private void parseValueAxis() {
  394. for (CTValAx valAx : chart.getPlotArea().getValAxArray()) {
  395. axis.add(new XSSFValueAxis(this, valAx));
  396. }
  397. }
  398. }