選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TestXSSFChartTitle.java 7.6KB

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