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.

TestXSSFChartTitle.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.charts;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import java.io.IOException;
  21. import java.util.List;
  22. import org.apache.poi.ss.usermodel.Cell;
  23. import org.apache.poi.ss.usermodel.Row;
  24. import org.apache.poi.ss.util.CellRangeAddress;
  25. import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
  26. import org.apache.poi.xddf.usermodel.chart.AxisPosition;
  27. import org.apache.poi.xddf.usermodel.chart.ChartTypes;
  28. import org.apache.poi.xddf.usermodel.chart.LegendPosition;
  29. import org.apache.poi.xddf.usermodel.chart.XDDFChartAxis;
  30. import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
  31. import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
  32. import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
  33. import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
  34. import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
  35. import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
  36. import org.apache.poi.xssf.XSSFTestDataSamples;
  37. import org.apache.poi.xssf.usermodel.XSSFChart;
  38. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  39. import org.apache.poi.xssf.usermodel.XSSFDrawing;
  40. import org.apache.poi.xssf.usermodel.XSSFRichTextString;
  41. import org.apache.poi.xssf.usermodel.XSSFSheet;
  42. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  43. import org.junit.Test;
  44. /**
  45. * Test get/set chart title.
  46. */
  47. public class TestXSSFChartTitle {
  48. private XSSFWorkbook createWorkbookWithChart() {
  49. XSSFWorkbook wb = new XSSFWorkbook();
  50. XSSFSheet sheet = wb.createSheet("linechart");
  51. final int NUM_OF_ROWS = 3;
  52. final int NUM_OF_COLUMNS = 10;
  53. // Create a row and put some cells in it. Rows are 0 based.
  54. Row row;
  55. Cell cell;
  56. for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
  57. row = sheet.createRow((short) rowIndex);
  58. for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
  59. cell = row.createCell((short) colIndex);
  60. cell.setCellValue(colIndex * (rowIndex + 1));
  61. }
  62. }
  63. XSSFDrawing drawing = sheet.createDrawingPatriarch();
  64. XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
  65. XSSFChart chart = drawing.createChart(anchor);
  66. XDDFChartLegend legend = chart.getOrAddLegend();
  67. legend.setPosition(LegendPosition.TOP_RIGHT);
  68. // Use a category axis for the bottom axis.
  69. XDDFChartAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
  70. XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
  71. leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
  72. XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
  73. XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
  74. XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
  75. XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
  76. data.addSeries(xs, ys1);
  77. data.addSeries(xs, ys2);
  78. chart.plot(data);
  79. return wb;
  80. }
  81. /**
  82. * Gets the first chart from the named sheet in the workbook.
  83. */
  84. private XSSFChart getChartFromWorkbook(XSSFWorkbook wb, String sheetName) {
  85. XSSFSheet sheet = wb.getSheet(sheetName);
  86. XSSFSheet xsheet = sheet;
  87. XSSFDrawing drawing = xsheet.getDrawingPatriarch();
  88. if (drawing != null) {
  89. List<XSSFChart> charts = drawing.getCharts();
  90. if (charts != null && charts.size() > 0) {
  91. return charts.get(0);
  92. }
  93. }
  94. return null;
  95. }
  96. @Test
  97. public void testNewChart() throws IOException {
  98. XSSFWorkbook wb = createWorkbookWithChart();
  99. XSSFChart chart = getChartFromWorkbook(wb, "linechart");
  100. assertNotNull(chart);
  101. assertNull(chart.getTitleText());
  102. final String myTitle = "My chart title";
  103. chart.setTitleText(myTitle);
  104. XSSFRichTextString queryTitle = chart.getTitleText();
  105. assertNotNull(queryTitle);
  106. assertEquals(myTitle, queryTitle.toString());
  107. final String myTitleFormula = "1 & \" and \" & 2";
  108. chart.setTitleFormula(myTitleFormula);
  109. // setting formula should unset text, but since there is a formula, returns an empty string
  110. assertEquals("", chart.getTitleText().toString());
  111. String titleFormula = chart.getTitleFormula();
  112. assertNotNull(titleFormula);
  113. assertEquals(myTitleFormula, titleFormula);
  114. wb.close();
  115. }
  116. @Test
  117. public void testExistingChartWithTitle() throws IOException {
  118. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_withTitle.xlsx");
  119. XSSFChart chart = getChartFromWorkbook(wb, "Sheet1");
  120. assertNotNull(chart);
  121. XSSFRichTextString originalTitle = chart.getTitleText();
  122. assertNotNull(originalTitle);
  123. final String myTitle = "My chart title";
  124. assertFalse(myTitle.equals(originalTitle.toString()));
  125. chart.setTitleText(myTitle);
  126. XSSFRichTextString queryTitle = chart.getTitleText();
  127. assertNotNull(queryTitle);
  128. assertEquals(myTitle, queryTitle.toString());
  129. wb.close();
  130. }
  131. @Test
  132. public void testExistingChartNoTitle() throws IOException {
  133. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_noTitle.xlsx");
  134. XSSFChart chart = getChartFromWorkbook(wb, "Sheet1");
  135. assertNotNull(chart);
  136. assertNull(chart.getTitleText());
  137. final String myTitle = "My chart title";
  138. chart.setTitleText(myTitle);
  139. XSSFRichTextString queryTitle = chart.getTitleText();
  140. assertNotNull(queryTitle);
  141. assertEquals(myTitle, queryTitle.toString());
  142. wb.close();
  143. }
  144. @Test
  145. public void testExistingChartWithFormulaTitle() throws IOException {
  146. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_withTitleFormula.xlsx");
  147. XSSFChart chart = getChartFromWorkbook(wb, "Sheet1");
  148. assertNotNull(chart);
  149. XSSFRichTextString originalTitle = chart.getTitleText();
  150. assertNotNull(originalTitle);
  151. assertEquals("", originalTitle.toString());
  152. String formula = chart.getTitleFormula();
  153. assertNotNull(formula);
  154. assertEquals("Sheet1!$E$1", formula);
  155. wb.close();
  156. }
  157. }