Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

XSSFChart.java 12KB

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