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.

XSLFChart.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import java.awt.geom.Rectangle2D;
  21. import java.io.IOException;
  22. import javax.xml.namespace.QName;
  23. import org.apache.poi.ooxml.POIXMLFactory;
  24. import org.apache.poi.ooxml.POIXMLRelation;
  25. import org.apache.poi.openxml4j.opc.PackagePart;
  26. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  27. import org.apache.poi.util.Beta;
  28. import org.apache.poi.xddf.usermodel.chart.XDDFChart;
  29. import org.apache.xmlbeans.XmlCursor;
  30. import org.apache.xmlbeans.XmlException;
  31. import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
  37. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  38. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  39. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrameNonVisual;
  40. /**
  41. * Represents a Chart in a .pptx presentation
  42. */
  43. @Beta
  44. public final class XSLFChart extends XDDFChart {
  45. private static String CHART_URI = "http://schemas.openxmlformats.org/drawingml/2006/chart";
  46. /**
  47. * Construct a PresentationML chart.
  48. */
  49. protected XSLFChart() {
  50. super();
  51. }
  52. /**
  53. * Construct a PresentationML chart from a package part.
  54. *
  55. * @param part the package part holding the chart data,
  56. * the content type must be <code>application/vnd.openxmlformats-officedocument.drawingml.chart+xml</code>
  57. * @since POI 3.14-Beta1
  58. */
  59. protected XSLFChart(PackagePart part) throws IOException, XmlException {
  60. super(part);
  61. }
  62. @Override
  63. protected POIXMLRelation getChartRelation() {
  64. return XSLFRelation.CHART;
  65. }
  66. @Override
  67. protected POIXMLRelation getChartWorkbookRelation() {
  68. return XSLFRelation.WORKBOOK;
  69. }
  70. @Override
  71. protected POIXMLFactory getChartFactory() {
  72. return XSLFFactory.getInstance();
  73. }
  74. public XSLFTextShape getTitleShape() {
  75. if (!chart.isSetTitle()) {
  76. chart.addNewTitle();
  77. }
  78. final CTTitle title = chart.getTitle();
  79. if (title.getTx() != null && title.getTx().isSetRich()) {
  80. return new XSLFTextShape(title, null) {
  81. @Override
  82. protected CTTextBody getTextBody(boolean create) {
  83. return title.getTx().getRich();
  84. }
  85. };
  86. } else {
  87. return new XSLFTextShape(title, null) {
  88. @Override
  89. protected CTTextBody getTextBody(boolean create) {
  90. return title.getTxPr();
  91. }
  92. };
  93. }
  94. }
  95. /**
  96. * method to add graphic frame for XSLF chart
  97. *
  98. * @param shapeId shape id
  99. * @param rID relation id
  100. * @param anchor size and location of chart
  101. * @return graphic frame object
  102. * @since POI 4.1.0
  103. */
  104. static CTGraphicalObjectFrame prototype(int shapeId, String rID, Rectangle2D anchor) {
  105. CTGraphicalObjectFrame frame = CTGraphicalObjectFrame.Factory.newInstance();
  106. CTGraphicalObjectFrameNonVisual nvGr = frame.addNewNvGraphicFramePr();
  107. CTNonVisualDrawingProps cnv = nvGr.addNewCNvPr();
  108. cnv.setName("Chart " + shapeId);
  109. cnv.setId(shapeId);
  110. nvGr.addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoGrp(true);
  111. nvGr.addNewNvPr();
  112. CTTransform2D xfrm = frame.addNewXfrm();
  113. CTPoint2D off = xfrm.addNewOff();
  114. off.setX((int)anchor.getX());
  115. off.setY((int)anchor.getY());
  116. CTPositiveSize2D ext = xfrm.addNewExt();
  117. ext.setCx((int)anchor.getWidth());
  118. ext.setCy((int)anchor.getHeight());
  119. xfrm.setExt(ext);
  120. xfrm.setOff(off);
  121. CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
  122. XmlCursor grCur = gr.newCursor();
  123. grCur.toNextToken();
  124. grCur.beginElement(new QName(CHART_URI, "chart"));
  125. grCur.insertAttributeWithValue("id", PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, rID);
  126. grCur.dispose();
  127. gr.setUri(CHART_URI);
  128. return frame;
  129. }
  130. }