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.

XWPFChart.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.xwpf.usermodel;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import org.apache.poi.ooxml.POIXMLException;
  19. import org.apache.poi.ooxml.POIXMLFactory;
  20. import org.apache.poi.ooxml.POIXMLRelation;
  21. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.util.Beta;
  24. import org.apache.poi.util.IOUtils;
  25. import org.apache.poi.xddf.usermodel.chart.XDDFChart;
  26. import org.apache.xmlbeans.XmlException;
  27. import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
  28. /**
  29. * Represents a Chart in a .docx file
  30. */
  31. @Beta
  32. public class XWPFChart extends XDDFChart {
  33. /**
  34. * default width of chart in emu
  35. */
  36. public static final int DEFAULT_WIDTH = XDDFChart.DEFAULT_WIDTH;
  37. /**
  38. * default height of chart in emu
  39. */
  40. public static final int DEFAULT_HEIGHT = XDDFChart.DEFAULT_HEIGHT;
  41. // lazy initialization
  42. private Long checksum;
  43. /**
  44. * this object is used to modify drawing properties
  45. */
  46. private CTInline ctInline;
  47. /**
  48. * constructor to
  49. * Create a new chart in document
  50. *
  51. * @since POI 4.0.0
  52. */
  53. protected XWPFChart() {
  54. super();
  55. }
  56. /**
  57. * Construct a chart from a package part.
  58. *
  59. * @param part the package part holding the chart data,
  60. * the content type must be {@code application/vnd.openxmlformats-officedocument.drawingml.chart+xml}
  61. * @since POI 4.0.0
  62. */
  63. protected XWPFChart(PackagePart part) throws IOException, XmlException {
  64. super(part);
  65. }
  66. @Override
  67. protected POIXMLRelation getChartRelation() {
  68. return XWPFRelation.CHART;
  69. }
  70. @Override
  71. protected POIXMLRelation getChartWorkbookRelation() {
  72. return XWPFRelation.WORKBOOK;
  73. }
  74. @Override
  75. protected POIXMLFactory getChartFactory() {
  76. return XWPFFactory.getInstance();
  77. }
  78. public Long getChecksum() {
  79. if (this.checksum == null) {
  80. byte[] data;
  81. try (InputStream is = getPackagePart().getInputStream()) {
  82. data = IOUtils.toByteArray(is);
  83. } catch (IOException e) {
  84. throw new POIXMLException(e);
  85. }
  86. this.checksum = IOUtils.calculateChecksum(data);
  87. }
  88. return this.checksum;
  89. }
  90. @Override
  91. public boolean equals(Object obj) {
  92. return obj == this;
  93. }
  94. @Override
  95. public int hashCode() {
  96. return getChecksum().hashCode();
  97. }
  98. /**
  99. * Attach this chart known by its relation id to the given text run.
  100. *
  101. * @param chartRelId the relation id of this chart in its parent document.
  102. * @param run the text run to which this chart will be inlined.
  103. * @since POI 4.0.0
  104. */
  105. protected void attach(String chartRelId, XWPFRun run)
  106. throws InvalidFormatException, IOException {
  107. ctInline = run.addChart(chartRelId);
  108. ctInline.addNewExtent();
  109. setChartBoundingBox(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  110. }
  111. /**
  112. * set chart height
  113. *
  114. * @param height height of chart
  115. * @since POI 4.0.0
  116. */
  117. public void setChartHeight(long height) {
  118. ctInline.getExtent().setCy(height);
  119. }
  120. /**
  121. * set chart width
  122. *
  123. * @param width width of chart
  124. * @since POI 4.0.0
  125. */
  126. public void setChartWidth(long width) {
  127. ctInline.getExtent().setCx(width);
  128. }
  129. /**
  130. * get chart height
  131. *
  132. * @since POI 4.0.0
  133. */
  134. public long getChartHeight() {
  135. return ctInline.getExtent().getCy();
  136. }
  137. /**
  138. * get chart width
  139. *
  140. * @since POI 4.0.0
  141. */
  142. public long getChartWidth() {
  143. return ctInline.getExtent().getCx();
  144. }
  145. /**
  146. * set chart height and width
  147. *
  148. * @param width width of chart
  149. * @param height height of chart
  150. * @since POI 4.0.0
  151. */
  152. public void setChartBoundingBox(long width, long height) {
  153. this.setChartWidth(width);
  154. this.setChartHeight(height);
  155. }
  156. /**
  157. * set margin from top
  158. *
  159. * @param margin margin from top
  160. * @since POI 4.0.0
  161. */
  162. public void setChartTopMargin(long margin) {
  163. ctInline.setDistT(margin);
  164. }
  165. /**
  166. * get margin from Top
  167. *
  168. * @since POI 4.0.0
  169. */
  170. public long getChartTopMargin(long margin) {
  171. return ctInline.getDistT();
  172. }
  173. /**
  174. * set margin from bottom
  175. *
  176. * @param margin margin from Bottom
  177. * @since POI 4.0.0
  178. */
  179. public void setChartBottomMargin(long margin) {
  180. ctInline.setDistB(margin);
  181. }
  182. /**
  183. * get margin from Bottom
  184. *
  185. * @since POI 4.0.0
  186. */
  187. public long getChartBottomMargin(long margin) {
  188. return ctInline.getDistB();
  189. }
  190. /**
  191. * set margin from left
  192. *
  193. * @param margin margin from left
  194. * @since POI 4.0.0
  195. */
  196. public void setChartLeftMargin(long margin) {
  197. ctInline.setDistL(margin);
  198. }
  199. /**
  200. * get margin from left
  201. *
  202. * @since POI 4.0.0
  203. */
  204. public long getChartLeftMargin(long margin) {
  205. return ctInline.getDistL();
  206. }
  207. /**
  208. * set margin from Right
  209. *
  210. * @param margin from right
  211. * @since POI 4.0.0
  212. */
  213. public void setChartRightMargin(long margin) {
  214. ctInline.setDistR(margin);
  215. }
  216. /**
  217. * get margin from Right
  218. *
  219. * @since POI 4.0.0
  220. */
  221. public long getChartRightMargin(long margin) {
  222. return ctInline.getDistR();
  223. }
  224. /**
  225. * set chart margin
  226. *
  227. * @param top margin from top
  228. * @param right margin from right
  229. * @param bottom margin from bottom
  230. * @param left margin from left
  231. * @since POI 4.0.0
  232. */
  233. public void setChartMargin(long top, long right, long bottom, long left) {
  234. this.setChartBottomMargin(bottom);
  235. this.setChartRightMargin(right);
  236. this.setChartLeftMargin(left);
  237. this.setChartRightMargin(right);
  238. }
  239. }