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.

TestXDDFChart.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.xddf.usermodel.chart;
  20. import static org.junit.jupiter.api.Assertions.assertEquals;
  21. import static org.junit.jupiter.api.Assertions.assertNotNull;
  22. import org.apache.poi.ooxml.POIXMLFactory;
  23. import org.apache.poi.ooxml.POIXMLRelation;
  24. import org.apache.poi.ss.util.CellRangeAddress;
  25. import org.apache.poi.xssf.XSSFTestDataSamples;
  26. import org.apache.poi.xssf.usermodel.XSSFSheet;
  27. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  28. import org.junit.jupiter.api.Test;
  29. import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartSpace;
  30. import java.io.FileOutputStream;
  31. import java.io.IOException;
  32. import java.io.OutputStream;
  33. class TestXDDFChart {
  34. @Test
  35. void testConstruct() {
  36. // minimal test to cause ooxml-lite to include all the classes in poi-ooxml-lite
  37. XDDFChart xddfChart = newXDDFChart();
  38. assertNotNull(xddfChart.getCTChartSpace());
  39. assertNotNull(xddfChart.getCTPlotArea());
  40. }
  41. @Test
  42. void testSetExternalId() {
  43. XDDFChart xddfChart = newXDDFChart();
  44. CTChartSpace ctChartSpace = xddfChart.getCTChartSpace();
  45. xddfChart.setExternalId("rid1");
  46. assertEquals("rid1", ctChartSpace.getExternalData().getId());
  47. xddfChart.setExternalId("rid2");
  48. assertEquals("rid2", ctChartSpace.getExternalData().getId());
  49. }
  50. @Test
  51. public void test65016() throws IOException {
  52. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("65016.xlsx")) {
  53. XSSFSheet splitSheet = wb.getSheet("Splits");
  54. XDDFChart chart = newXDDFChart();
  55. XDDFChartLegend legend = chart.getOrAddLegend();
  56. legend.setPosition(LegendPosition.BOTTOM);
  57. // Use a category axis for the bottom axis.
  58. XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
  59. XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
  60. leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
  61. XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
  62. // starting row 1 to include description
  63. XDDFNumericalDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(splitSheet,
  64. new CellRangeAddress(2, 100, 0, 0));
  65. XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(splitSheet,
  66. new CellRangeAddress(2, 100, 1, 1));
  67. XDDFLineChartData.Series series = (XDDFLineChartData.Series) data.addSeries(xs, ys1);
  68. assertEquals(series.categoryData.getPointCount(), xs.getPointCount());
  69. chart.plot(data);
  70. try (OutputStream out = new FileOutputStream("/tmp/chart20201220.xlsx")) {
  71. wb.write(out);
  72. }
  73. }
  74. }
  75. private XDDFChart newXDDFChart() {
  76. XDDFChart xddfChart = new XDDFChart() {
  77. @Override
  78. protected POIXMLRelation getChartRelation() {
  79. return null;
  80. }
  81. @Override
  82. protected POIXMLRelation getChartWorkbookRelation() {
  83. return null;
  84. }
  85. @Override
  86. protected POIXMLFactory getChartFactory() {
  87. return null;
  88. }
  89. };
  90. return xddfChart;
  91. }
  92. }