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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import javax.xml.namespace.QName;
  23. import org.apache.poi.POIXMLDocumentPart;
  24. import org.apache.poi.openxml4j.opc.PackagePart;
  25. import org.apache.poi.openxml4j.opc.PackageRelationship;
  26. import org.apache.poi.ss.usermodel.Chart;
  27. import org.apache.poi.ss.usermodel.charts.AxisPosition;
  28. import org.apache.poi.ss.usermodel.charts.ChartAxis;
  29. import org.apache.poi.ss.usermodel.charts.ChartAxisFactory;
  30. import org.apache.poi.ss.usermodel.charts.ChartData;
  31. import org.apache.poi.util.Internal;
  32. import org.apache.poi.xssf.usermodel.charts.XSSFCategoryAxis;
  33. import org.apache.poi.xssf.usermodel.charts.XSSFChartAxis;
  34. import org.apache.poi.xssf.usermodel.charts.XSSFChartDataFactory;
  35. import org.apache.poi.xssf.usermodel.charts.XSSFChartLegend;
  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.CTChart;
  43. import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartSpace;
  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.CTTitle;
  48. import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
  49. import org.openxmlformats.schemas.drawingml.x2006.chart.ChartSpaceDocument;
  50. import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
  51. import org.w3c.dom.NodeList;
  52. import org.w3c.dom.Text;
  53. /**
  54. * Represents a SpreadsheetML Chart
  55. * @author Nick Burch
  56. * @author Roman Kashitsyn
  57. * @author Martin Andersson
  58. */
  59. public final class XSSFChart extends POIXMLDocumentPart implements Chart, ChartAxisFactory {
  60. /**
  61. * Parent graphic frame.
  62. */
  63. private XSSFGraphicFrame frame;
  64. /**
  65. * Root element of the SpreadsheetML Chart part
  66. */
  67. private CTChartSpace chartSpace;
  68. /**
  69. * The Chart within that
  70. */
  71. private CTChart chart;
  72. List<XSSFChartAxis> axis = new ArrayList<XSSFChartAxis>();
  73. /**
  74. * Create a new SpreadsheetML chart
  75. */
  76. protected XSSFChart() {
  77. super();
  78. createChart();
  79. }
  80. /**
  81. * Construct a SpreadsheetML chart from a package part.
  82. *
  83. * @param part the package part holding the chart data,
  84. * the content type must be <code>application/vnd.openxmlformats-officedocument.drawingml.chart+xml</code>
  85. * @param rel the package relationship holding this chart,
  86. * the relationship type must be http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart
  87. */
  88. protected XSSFChart(PackagePart part, PackageRelationship rel) throws IOException, XmlException {
  89. super(part, rel);
  90. chartSpace = ChartSpaceDocument.Factory.parse(part.getInputStream()).getChartSpace();
  91. chart = chartSpace.getChart();
  92. }
  93. /**
  94. * Construct a new CTChartSpace bean.
  95. * By default, it's just an empty placeholder for chart objects.
  96. *
  97. * @return a new CTChartSpace bean
  98. */
  99. private void createChart() {
  100. chartSpace = CTChartSpace.Factory.newInstance();
  101. chart = chartSpace.addNewChart();
  102. CTPlotArea plotArea = chart.addNewPlotArea();
  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. /**
  117. * Return the underlying CTChartSpace bean, the root element of the SpreadsheetML Chart part.
  118. *
  119. * @return the underlying CTChartSpace bean
  120. */
  121. @Internal
  122. public CTChartSpace getCTChartSpace(){
  123. return chartSpace;
  124. }
  125. /**
  126. * Return the underlying CTChart bean, within the Chart Space
  127. *
  128. * @return the underlying CTChart bean
  129. */
  130. @Internal
  131. public CTChart getCTChart(){
  132. return chart;
  133. }
  134. @Override
  135. protected void commit() throws IOException {
  136. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  137. /*
  138. Saved chart space must have the following namespaces set:
  139. <c:chartSpace
  140. xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
  141. xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
  142. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  143. */
  144. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTChartSpace.type.getName().getNamespaceURI(), "chartSpace", "c"));
  145. Map<String, String> map = new HashMap<String, String>();
  146. map.put(XSSFDrawing.NAMESPACE_A, "a");
  147. map.put(XSSFDrawing.NAMESPACE_C, "c");
  148. map.put(STRelationshipId.type.getName().getNamespaceURI(), "r");
  149. xmlOptions.setSaveSuggestedPrefixes(map);
  150. PackagePart part = getPackagePart();
  151. OutputStream out = part.getOutputStream();
  152. chartSpace.save(out, xmlOptions);
  153. out.close();
  154. }
  155. /**
  156. * Returns the parent graphic frame.
  157. * @return the graphic frame this chart belongs to
  158. */
  159. public XSSFGraphicFrame getGraphicFrame() {
  160. return frame;
  161. }
  162. /**
  163. * Sets the parent graphic frame.
  164. */
  165. protected void setGraphicFrame(XSSFGraphicFrame frame) {
  166. this.frame = frame;
  167. }
  168. public XSSFChartDataFactory getChartDataFactory() {
  169. return XSSFChartDataFactory.getInstance();
  170. }
  171. public XSSFChart getChartAxisFactory() {
  172. return this;
  173. }
  174. public void plot(ChartData data, ChartAxis... axis) {
  175. data.fillChart(this, axis);
  176. }
  177. public XSSFValueAxis createValueAxis(AxisPosition pos) {
  178. long id = axis.size() + 1;
  179. XSSFValueAxis valueAxis = new XSSFValueAxis(this, id, pos);
  180. if (axis.size() == 1) {
  181. ChartAxis ax = axis.get(0);
  182. ax.crossAxis(valueAxis);
  183. valueAxis.crossAxis(ax);
  184. }
  185. axis.add(valueAxis);
  186. return valueAxis;
  187. }
  188. public XSSFCategoryAxis createCategoryAxis(AxisPosition pos) {
  189. long id = axis.size() + 1;
  190. XSSFCategoryAxis categoryAxis = new XSSFCategoryAxis(this, id, pos);
  191. if (axis.size() == 1) {
  192. ChartAxis ax = axis.get(0);
  193. ax.crossAxis(categoryAxis);
  194. categoryAxis.crossAxis(ax);
  195. }
  196. axis.add(categoryAxis);
  197. return categoryAxis;
  198. }
  199. public List<? extends XSSFChartAxis> getAxis() {
  200. if (axis.isEmpty() && hasAxis()) {
  201. parseAxis();
  202. }
  203. return axis;
  204. }
  205. public XSSFManualLayout getManualLayout() {
  206. return new XSSFManualLayout(this);
  207. }
  208. /**
  209. * @return true if only visible cells will be present on the chart,
  210. * false otherwise
  211. */
  212. public boolean isPlotOnlyVisibleCells() {
  213. return chart.getPlotVisOnly().getVal();
  214. }
  215. /**
  216. * @param plotVisOnly a flag specifying if only visible cells should be
  217. * present on the chart
  218. */
  219. public void setPlotOnlyVisibleCells(boolean plotVisOnly) {
  220. chart.getPlotVisOnly().setVal(plotVisOnly);
  221. }
  222. /**
  223. * Returns the title, or null if none is set
  224. */
  225. public XSSFRichTextString getTitle() {
  226. if(! chart.isSetTitle()) {
  227. return null;
  228. }
  229. // TODO Do properly
  230. CTTitle title = chart.getTitle();
  231. StringBuffer text = new StringBuffer();
  232. XmlObject[] t = title
  233. .selectPath("declare namespace a='"+XSSFDrawing.NAMESPACE_A+"' .//a:t");
  234. for (int m = 0; m < t.length; m++) {
  235. NodeList kids = t[m].getDomNode().getChildNodes();
  236. for (int n = 0; n < kids.getLength(); n++) {
  237. if (kids.item(n) instanceof Text) {
  238. text.append(kids.item(n).getNodeValue());
  239. }
  240. }
  241. }
  242. return new XSSFRichTextString(text.toString());
  243. }
  244. public XSSFChartLegend getOrCreateLegend() {
  245. return new XSSFChartLegend(this);
  246. }
  247. public void deleteLegend() {
  248. if (chart.isSetLegend()) {
  249. chart.unsetLegend();
  250. }
  251. }
  252. private boolean hasAxis() {
  253. CTPlotArea ctPlotArea = chart.getPlotArea();
  254. int totalAxisCount =
  255. ctPlotArea.sizeOfValAxArray() +
  256. ctPlotArea.sizeOfCatAxArray() +
  257. ctPlotArea.sizeOfDateAxArray() +
  258. ctPlotArea.sizeOfSerAxArray();
  259. return totalAxisCount > 0;
  260. }
  261. private void parseAxis() {
  262. // TODO: add other axis types
  263. parseCategoryAxis();
  264. parseValueAxis();
  265. }
  266. @SuppressWarnings("deprecation")
  267. private void parseCategoryAxis() {
  268. for (CTCatAx catAx : chart.getPlotArea().getCatAxArray()) {
  269. axis.add(new XSSFCategoryAxis(this, catAx));
  270. }
  271. }
  272. @SuppressWarnings("deprecation")
  273. private void parseValueAxis() {
  274. for (CTValAx valAx : chart.getPlotArea().getValAxArray()) {
  275. axis.add(new XSSFValueAxis(this, valAx));
  276. }
  277. }
  278. }