您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TestXSLFChart.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 static org.junit.Assert.assertEquals;
  21. import static org.junit.Assert.assertFalse;
  22. import static org.junit.Assert.assertNotNull;
  23. import static org.junit.Assert.assertTrue;
  24. import java.io.IOException;
  25. import java.util.List;
  26. import org.apache.poi.POIXMLDocumentPart;
  27. import org.apache.poi.ss.util.CellRangeAddress;
  28. import org.apache.poi.xddf.usermodel.chart.AxisCrossBetween;
  29. import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
  30. import org.apache.poi.xddf.usermodel.chart.AxisOrientation;
  31. import org.apache.poi.xddf.usermodel.chart.AxisPosition;
  32. import org.apache.poi.xddf.usermodel.chart.AxisTickMark;
  33. import org.apache.poi.xddf.usermodel.chart.BarDirection;
  34. import org.apache.poi.xddf.usermodel.chart.BarGrouping;
  35. import org.apache.poi.xddf.usermodel.chart.Grouping;
  36. import org.apache.poi.xddf.usermodel.chart.LayoutMode;
  37. import org.apache.poi.xddf.usermodel.chart.LayoutTarget;
  38. import org.apache.poi.xddf.usermodel.chart.LegendPosition;
  39. import org.apache.poi.xddf.usermodel.chart.RadarStyle;
  40. import org.apache.poi.xddf.usermodel.chart.ScatterStyle;
  41. import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
  42. import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
  43. import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
  44. import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
  45. import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
  46. import org.apache.poi.xddf.usermodel.chart.XDDFLineChartData;
  47. import org.apache.poi.xddf.usermodel.chart.XDDFManualLayout;
  48. import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
  49. import org.apache.poi.xddf.usermodel.chart.XDDFPieChartData;
  50. import org.apache.poi.xddf.usermodel.chart.XDDFRadarChartData;
  51. import org.apache.poi.xddf.usermodel.chart.XDDFScatterChartData;
  52. import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
  53. import org.apache.poi.xslf.XSLFTestDataSamples;
  54. import org.junit.Test;
  55. /**
  56. * a modified version from POI-examples
  57. */
  58. public class TestXSLFChart {
  59. @Test
  60. public void testFillPieChartTemplate() throws IOException {
  61. XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument("pie-chart.pptx");
  62. XSLFChart chart = findChart(pptx.getSlides().get(0));
  63. List<XDDFChartData> data = findChartData(chart);
  64. XDDFPieChartData pie = (XDDFPieChartData) data.get(0);
  65. XDDFPieChartData.Series firstSeries = (XDDFPieChartData.Series) pie.getSeries().get(0);
  66. firstSeries.setExplosion(25);
  67. assertEquals(25, firstSeries.getExplosion());
  68. fillChartData(chart, pie);
  69. pptx.close();
  70. }
  71. @Test
  72. public void testFillBarChartTemplate() throws IOException {
  73. XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument("bar-chart.pptx");
  74. XSLFSlide slide = pptx.getSlides().get(0);
  75. // duplicate slide and chart before applying "destructive" tests to it
  76. XSLFChart chart2 = findChart(pptx.createSlide().importContent(slide));
  77. XSLFChart chart = findChart(slide);
  78. List<XDDFChartData> data = findChartData(chart);
  79. XDDFBarChartData bar = (XDDFBarChartData) data.get(0);
  80. assertEquals(BarDirection.BAR, bar.getBarDirection());
  81. assertEquals(BarGrouping.CLUSTERED, bar.getBarGrouping());
  82. assertEquals(100, bar.getGapWidth());
  83. fillChartData(chart, bar);
  84. XDDFBarChartData column = (XDDFBarChartData) findChartData(chart2).get(0);
  85. column.setBarDirection(BarDirection.COL);
  86. assertEquals(BarDirection.COL, column.getBarDirection());
  87. column.getCategoryAxis().setOrientation(AxisOrientation.MIN_MAX);
  88. column.getValueAxes().get(0).setPosition(AxisPosition.BOTTOM);
  89. fillChartData(chart2, column);
  90. pptx.close();
  91. }
  92. @Test
  93. public void testFillLineChartTemplate() throws IOException {
  94. XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument("line-chart.pptx");
  95. XSLFChart chart = findChart(pptx.getSlides().get(0));
  96. List<XDDFChartData> data = findChartData(chart);
  97. XDDFLineChartData line = (XDDFLineChartData) data.get(0);
  98. assertEquals(Grouping.STANDARD, line.getGrouping());
  99. line.setGrouping(Grouping.PERCENT_STACKED);
  100. assertEquals(Grouping.PERCENT_STACKED, line.getGrouping());
  101. fillChartData(chart, line);
  102. pptx.close();
  103. }
  104. @Test
  105. public void testFillRadarChartTemplate() throws IOException {
  106. XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument("radar-chart.pptx");
  107. XSLFChart chart = findChart(pptx.getSlides().get(0));
  108. List<XDDFChartData> data = findChartData(chart);
  109. XDDFRadarChartData radar = (XDDFRadarChartData) data.get(0);
  110. assertEquals(RadarStyle.MARKER, radar.getStyle());
  111. radar.setStyle(RadarStyle.FILLED);
  112. assertEquals(RadarStyle.FILLED, radar.getStyle());
  113. fillChartData(chart, radar);
  114. pptx.close();
  115. }
  116. @Test
  117. public void testFillScatterChartTemplate() throws IOException {
  118. XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument("scatter-chart.pptx");
  119. XSLFChart chart = findChart(pptx.getSlides().get(0));
  120. List<XDDFChartData> data = findChartData(chart);
  121. XDDFScatterChartData scatter = (XDDFScatterChartData) data.get(0);
  122. assertEquals(ScatterStyle.LINE_MARKER, scatter.getStyle());
  123. scatter.setStyle(ScatterStyle.SMOOTH);
  124. assertEquals(ScatterStyle.SMOOTH, scatter.getStyle());
  125. fillChartData(chart, scatter);
  126. pptx.close();
  127. }
  128. private void fillChartData(XSLFChart chart, XDDFChartData data) {
  129. final int numOfPoints = 3;
  130. final String[] categories = {"First", "Second", "Third"};
  131. final Integer[] values = {1, 3, 4};
  132. final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
  133. final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
  134. final XDDFChartData.Series series = data.getSeries().get(0);
  135. final XDDFDataSource<?> categoryData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange);
  136. final XDDFNumericalDataSource<Integer> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange);
  137. series.replaceData(categoryData, valuesData);
  138. final String title = "Apache POI";
  139. series.setTitle(title, chart.setSheetTitle(title));
  140. chart.plot(data);
  141. }
  142. private XSLFChart findChart(XSLFSlide slide) {
  143. // find chart in the slide
  144. XSLFChart chart = null;
  145. for(POIXMLDocumentPart part : slide.getRelations()){
  146. if(part instanceof XSLFChart){
  147. chart = (XSLFChart) part;
  148. break;
  149. }
  150. }
  151. if(chart == null) {
  152. throw new IllegalStateException("chart not found in the template");
  153. }
  154. checkLegendOperations(chart);
  155. return chart;
  156. }
  157. private List<XDDFChartData> findChartData(XSLFChart chart) {
  158. List<XDDFChartData> data = chart.getChartSeries();
  159. assertNotNull(data);
  160. assertEquals(1, data.size());
  161. XDDFChartData firstSeries = data.get(0);
  162. assertNotNull(firstSeries);
  163. if (firstSeries instanceof XDDFScatterChartData) {
  164. assertEquals(null, firstSeries.getCategoryAxis());
  165. assertEquals(2, firstSeries.getValueAxes().size());
  166. checkAxisOperations(firstSeries.getValueAxes().get(0));
  167. checkAxisOperations(firstSeries.getValueAxes().get(1));
  168. } else if (!(firstSeries instanceof XDDFPieChartData)) {
  169. assertNotNull(firstSeries.getCategoryAxis());
  170. assertEquals(1, firstSeries.getValueAxes().size());
  171. checkAxisOperations(firstSeries.getValueAxes().get(0));
  172. }
  173. return data;
  174. }
  175. private void checkLegendOperations(XSLFChart chart) {
  176. XDDFChartLegend legend = chart.getOrAddLegend();
  177. assertFalse(legend.isOverlay());
  178. legend.setOverlay(true);
  179. assertTrue(legend.isOverlay());
  180. legend.setPosition(LegendPosition.TOP_RIGHT);
  181. assertEquals(LegendPosition.TOP_RIGHT, legend.getPosition());
  182. XDDFManualLayout layout = legend.getOrAddManualLayout();
  183. assertNotNull(layout.getTarget());
  184. assertNotNull(layout.getXMode());
  185. assertNotNull(layout.getYMode());
  186. assertNotNull(layout.getHeightMode());
  187. assertNotNull(layout.getWidthMode());
  188. /*
  189. * According to interface, 0.0 should be returned for
  190. * uninitialized double properties.
  191. */
  192. assertTrue(layout.getX() == 0.0);
  193. assertTrue(layout.getY() == 0.0);
  194. assertTrue(layout.getWidthRatio() == 0.0);
  195. assertTrue(layout.getHeightRatio() == 0.0);
  196. final double newRatio = 1.1;
  197. final double newCoordinate = 0.3;
  198. final LayoutMode nonDefaultMode = LayoutMode.FACTOR;
  199. final LayoutTarget nonDefaultTarget = LayoutTarget.OUTER;
  200. layout.setWidthRatio(newRatio);
  201. assertTrue(layout.getWidthRatio() == newRatio);
  202. layout.setHeightRatio(newRatio);
  203. assertTrue(layout.getHeightRatio() == newRatio);
  204. layout.setX(newCoordinate);
  205. assertTrue(layout.getX() == newCoordinate);
  206. layout.setY(newCoordinate);
  207. assertTrue(layout.getY() == newCoordinate);
  208. layout.setXMode(nonDefaultMode);
  209. assertTrue(layout.getXMode() == nonDefaultMode);
  210. layout.setYMode(nonDefaultMode);
  211. assertTrue(layout.getYMode() == nonDefaultMode);
  212. layout.setWidthMode(nonDefaultMode);
  213. assertTrue(layout.getWidthMode() == nonDefaultMode);
  214. layout.setHeightMode(nonDefaultMode);
  215. assertTrue(layout.getHeightMode() == nonDefaultMode);
  216. layout.setTarget(nonDefaultTarget);
  217. assertTrue(layout.getTarget() == nonDefaultTarget);
  218. }
  219. private void checkAxisOperations(XDDFValueAxis axis) {
  220. axis.setCrossBetween(AxisCrossBetween.MIDPOINT_CATEGORY);
  221. assertEquals(AxisCrossBetween.MIDPOINT_CATEGORY, axis.getCrossBetween());
  222. axis.setCrosses(AxisCrosses.AUTO_ZERO);
  223. assertEquals(AxisCrosses.AUTO_ZERO, axis.getCrosses());
  224. final String numberFormat = "General";
  225. axis.setNumberFormat(numberFormat);
  226. assertEquals(numberFormat, axis.getNumberFormat());
  227. axis.setPosition(AxisPosition.BOTTOM);
  228. assertEquals(AxisPosition.BOTTOM, axis.getPosition());
  229. axis.setMajorTickMark(AxisTickMark.NONE);
  230. assertEquals(AxisTickMark.NONE, axis.getMajorTickMark());
  231. axis.setMajorTickMark(AxisTickMark.IN);
  232. assertEquals(AxisTickMark.IN, axis.getMajorTickMark());
  233. axis.setMajorTickMark(AxisTickMark.OUT);
  234. assertEquals(AxisTickMark.OUT, axis.getMajorTickMark());
  235. axis.setMajorTickMark(AxisTickMark.CROSS);
  236. assertEquals(AxisTickMark.CROSS, axis.getMajorTickMark());
  237. axis.setMinorTickMark(AxisTickMark.NONE);
  238. assertEquals(AxisTickMark.NONE, axis.getMinorTickMark());
  239. axis.setMinorTickMark(AxisTickMark.IN);
  240. assertEquals(AxisTickMark.IN, axis.getMinorTickMark());
  241. axis.setMinorTickMark(AxisTickMark.OUT);
  242. assertEquals(AxisTickMark.OUT, axis.getMinorTickMark());
  243. axis.setMinorTickMark(AxisTickMark.CROSS);
  244. assertEquals(AxisTickMark.CROSS, axis.getMinorTickMark());
  245. axis.setVisible(true);
  246. assertTrue(axis.isVisible());
  247. axis.setVisible(false);
  248. assertFalse(axis.isVisible());
  249. final double EPSILON = 1E-7;
  250. axis.setLogBase(Math.E);
  251. assertTrue(Math.abs(axis.getLogBase() - Math.E) < EPSILON);
  252. final double newValue = 10.0;
  253. axis.setMinimum(newValue);
  254. assertTrue(Math.abs(axis.getMinimum() - newValue) < EPSILON);
  255. axis.setMaximum(newValue);
  256. assertTrue(Math.abs(axis.getMaximum() - newValue) < EPSILON);
  257. IllegalArgumentException iae = null;
  258. try {
  259. axis.setLogBase(0.0);
  260. } catch (IllegalArgumentException e) {
  261. iae = e;
  262. }
  263. assertNotNull(iae);
  264. iae = null;
  265. try {
  266. axis.setLogBase(30000.0);
  267. } catch (IllegalArgumentException e) {
  268. iae = e;
  269. }
  270. assertNotNull(iae);
  271. }
  272. }