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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.POIXMLDocumentPart;
  23. import org.apache.poi.openxml4j.opc.PackagePart;
  24. import org.apache.poi.openxml4j.opc.PackageRelationship;
  25. import org.apache.poi.ss.usermodel.Chart;
  26. import org.apache.poi.ss.usermodel.charts.AxisPosition;
  27. import org.apache.poi.ss.usermodel.charts.ChartAxis;
  28. import org.apache.poi.ss.usermodel.charts.ChartAxisFactory;
  29. import org.apache.poi.ss.usermodel.charts.ChartData;
  30. import org.apache.poi.util.Internal;
  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.XSSFManualLayout;
  36. import org.apache.poi.xssf.usermodel.charts.XSSFValueAxis;
  37. import org.apache.xmlbeans.XmlException;
  38. import org.apache.xmlbeans.XmlObject;
  39. import org.apache.xmlbeans.XmlOptions;
  40. import org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx;
  41. import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
  42. import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartSpace;
  43. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPageMargins;
  44. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
  45. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPrintSettings;
  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.chart.ChartSpaceDocument;
  50. import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
  51. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
  52. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextField;
  53. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
  54. import org.w3c.dom.NodeList;
  55. import org.w3c.dom.Text;
  56. /**
  57. * Represents a SpreadsheetML Chart
  58. * @author Nick Burch
  59. * @author Roman Kashitsyn
  60. * @author Martin Andersson
  61. */
  62. public final class XSSFChart extends POIXMLDocumentPart implements Chart, ChartAxisFactory {
  63. /**
  64. * Parent graphic frame.
  65. */
  66. private XSSFGraphicFrame frame;
  67. /**
  68. * Root element of the SpreadsheetML Chart part
  69. */
  70. private CTChartSpace chartSpace;
  71. /**
  72. * The Chart within that
  73. */
  74. private CTChart chart;
  75. List<XSSFChartAxis> axis = new ArrayList<XSSFChartAxis>();
  76. /**
  77. * Create a new SpreadsheetML chart
  78. */
  79. protected XSSFChart() {
  80. super();
  81. createChart();
  82. }
  83. /**
  84. * Construct a SpreadsheetML chart from a package part.
  85. *
  86. * @param part the package part holding the chart data,
  87. * the content type must be <code>application/vnd.openxmlformats-officedocument.drawingml.chart+xml</code>
  88. *
  89. * @since POI 3.14-Beta1
  90. */
  91. protected XSSFChart(PackagePart part) throws IOException, XmlException {
  92. super(part);
  93. chartSpace = ChartSpaceDocument.Factory.parse(part.getInputStream(), DEFAULT_XML_OPTIONS).getChartSpace();
  94. chart = chartSpace.getChart();
  95. }
  96. /**
  97. * @deprecated in POI 3.14, scheduled for removal in POI 3.16
  98. */
  99. @Deprecated
  100. protected XSSFChart(PackagePart part, PackageRelationship rel) throws IOException, XmlException {
  101. this(part);
  102. }
  103. /**
  104. * Construct a new CTChartSpace bean.
  105. * By default, it's just an empty placeholder for chart objects.
  106. *
  107. * @return a new CTChartSpace bean
  108. */
  109. private void createChart() {
  110. chartSpace = CTChartSpace.Factory.newInstance();
  111. chart = chartSpace.addNewChart();
  112. CTPlotArea plotArea = chart.addNewPlotArea();
  113. plotArea.addNewLayout();
  114. chart.addNewPlotVisOnly().setVal(true);
  115. CTPrintSettings printSettings = chartSpace.addNewPrintSettings();
  116. printSettings.addNewHeaderFooter();
  117. CTPageMargins pageMargins = printSettings.addNewPageMargins();
  118. pageMargins.setB(0.75);
  119. pageMargins.setL(0.70);
  120. pageMargins.setR(0.70);
  121. pageMargins.setT(0.75);
  122. pageMargins.setHeader(0.30);
  123. pageMargins.setFooter(0.30);
  124. printSettings.addNewPageSetup();
  125. }
  126. /**
  127. * Return the underlying CTChartSpace bean, the root element of the SpreadsheetML Chart part.
  128. *
  129. * @return the underlying CTChartSpace bean
  130. */
  131. @Internal
  132. public CTChartSpace getCTChartSpace(){
  133. return chartSpace;
  134. }
  135. /**
  136. * Return the underlying CTChart bean, within the Chart Space
  137. *
  138. * @return the underlying CTChart bean
  139. */
  140. @Internal
  141. public CTChart getCTChart(){
  142. return chart;
  143. }
  144. @Override
  145. protected void commit() throws IOException {
  146. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  147. /*
  148. Saved chart space must have the following namespaces set:
  149. <c:chartSpace
  150. xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
  151. xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
  152. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  153. */
  154. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTChartSpace.type.getName().getNamespaceURI(), "chartSpace", "c"));
  155. PackagePart part = getPackagePart();
  156. OutputStream out = part.getOutputStream();
  157. chartSpace.save(out, xmlOptions);
  158. out.close();
  159. }
  160. /**
  161. * Returns the parent graphic frame.
  162. * @return the graphic frame this chart belongs to
  163. */
  164. public XSSFGraphicFrame getGraphicFrame() {
  165. return frame;
  166. }
  167. /**
  168. * Sets the parent graphic frame.
  169. */
  170. protected void setGraphicFrame(XSSFGraphicFrame frame) {
  171. this.frame = frame;
  172. }
  173. public XSSFChartDataFactory getChartDataFactory() {
  174. return XSSFChartDataFactory.getInstance();
  175. }
  176. public XSSFChart getChartAxisFactory() {
  177. return this;
  178. }
  179. public void plot(ChartData data, ChartAxis... chartAxis) {
  180. data.fillChart(this, chartAxis);
  181. }
  182. public XSSFValueAxis createValueAxis(AxisPosition pos) {
  183. long id = axis.size() + 1;
  184. XSSFValueAxis valueAxis = new XSSFValueAxis(this, id, pos);
  185. if (axis.size() == 1) {
  186. ChartAxis ax = axis.get(0);
  187. ax.crossAxis(valueAxis);
  188. valueAxis.crossAxis(ax);
  189. }
  190. axis.add(valueAxis);
  191. return valueAxis;
  192. }
  193. public XSSFCategoryAxis createCategoryAxis(AxisPosition pos) {
  194. long id = axis.size() + 1;
  195. XSSFCategoryAxis categoryAxis = new XSSFCategoryAxis(this, id, pos);
  196. if (axis.size() == 1) {
  197. ChartAxis ax = axis.get(0);
  198. ax.crossAxis(categoryAxis);
  199. categoryAxis.crossAxis(ax);
  200. }
  201. axis.add(categoryAxis);
  202. return categoryAxis;
  203. }
  204. public List<? extends XSSFChartAxis> getAxis() {
  205. if (axis.isEmpty() && hasAxis()) {
  206. parseAxis();
  207. }
  208. return axis;
  209. }
  210. public XSSFManualLayout getManualLayout() {
  211. return new XSSFManualLayout(this);
  212. }
  213. /**
  214. * @return true if only visible cells will be present on the chart,
  215. * false otherwise
  216. */
  217. public boolean isPlotOnlyVisibleCells() {
  218. return chart.getPlotVisOnly().getVal();
  219. }
  220. /**
  221. * @param plotVisOnly a flag specifying if only visible cells should be
  222. * present on the chart
  223. */
  224. public void setPlotOnlyVisibleCells(boolean plotVisOnly) {
  225. chart.getPlotVisOnly().setVal(plotVisOnly);
  226. }
  227. /**
  228. * Returns the title, or null if none is set
  229. */
  230. public XSSFRichTextString getTitle() {
  231. if(! chart.isSetTitle()) {
  232. return null;
  233. }
  234. // TODO Do properly
  235. CTTitle title = chart.getTitle();
  236. StringBuffer text = new StringBuffer();
  237. XmlObject[] t = title
  238. .selectPath("declare namespace a='"+XSSFDrawing.NAMESPACE_A+"' .//a:t");
  239. for (int m = 0; m < t.length; m++) {
  240. NodeList kids = t[m].getDomNode().getChildNodes();
  241. for (int n = 0; n < kids.getLength(); n++) {
  242. if (kids.item(n) instanceof Text) {
  243. text.append(kids.item(n).getNodeValue());
  244. }
  245. }
  246. }
  247. return new XSSFRichTextString(text.toString());
  248. }
  249. /**
  250. * Sets the title text.
  251. */
  252. public void setTitle(String newTitle) {
  253. CTTitle ctTitle;
  254. if (chart.isSetTitle()) {
  255. ctTitle = chart.getTitle();
  256. } else {
  257. ctTitle = chart.addNewTitle();
  258. }
  259. CTTx tx;
  260. if (ctTitle.isSetTx()) {
  261. tx = ctTitle.getTx();
  262. } else {
  263. tx = ctTitle.addNewTx();
  264. }
  265. if (tx.isSetStrRef()) {
  266. tx.unsetStrRef();
  267. }
  268. CTTextBody rich;
  269. if (tx.isSetRich()) {
  270. rich = tx.getRich();
  271. } else {
  272. rich = tx.addNewRich();
  273. rich.addNewBodyPr(); // body properties must exist (but can be empty)
  274. }
  275. CTTextParagraph para;
  276. if (rich.sizeOfPArray() > 0) {
  277. para = rich.getPArray(0);
  278. } else {
  279. para = rich.addNewP();
  280. }
  281. if (para.sizeOfRArray() > 0) {
  282. CTRegularTextRun run = para.getRArray(0);
  283. run.setT(newTitle);
  284. } else if (para.sizeOfFldArray() > 0) {
  285. CTTextField fld = para.getFldArray(0);
  286. fld.setT(newTitle);
  287. } else {
  288. CTRegularTextRun run = para.addNewR();
  289. run.setT(newTitle);
  290. }
  291. }
  292. public XSSFChartLegend getOrCreateLegend() {
  293. return new XSSFChartLegend(this);
  294. }
  295. public void deleteLegend() {
  296. if (chart.isSetLegend()) {
  297. chart.unsetLegend();
  298. }
  299. }
  300. private boolean hasAxis() {
  301. CTPlotArea ctPlotArea = chart.getPlotArea();
  302. int totalAxisCount =
  303. ctPlotArea.sizeOfValAxArray() +
  304. ctPlotArea.sizeOfCatAxArray() +
  305. ctPlotArea.sizeOfDateAxArray() +
  306. ctPlotArea.sizeOfSerAxArray();
  307. return totalAxisCount > 0;
  308. }
  309. private void parseAxis() {
  310. // TODO: add other axis types
  311. parseCategoryAxis();
  312. parseValueAxis();
  313. }
  314. private void parseCategoryAxis() {
  315. for (CTCatAx catAx : chart.getPlotArea().getCatAxArray()) {
  316. axis.add(new XSSFCategoryAxis(this, catAx));
  317. }
  318. }
  319. private void parseValueAxis() {
  320. for (CTValAx valAx : chart.getPlotArea().getValAxArray()) {
  321. axis.add(new XSSFValueAxis(this, valAx));
  322. }
  323. }
  324. }