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.

TestXSLFChart.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 junit.framework.TestCase;
  21. import org.apache.poi.POIXMLDocumentPart;
  22. import org.apache.poi.ss.util.CellRangeAddress;
  23. import org.apache.poi.ss.util.CellReference;
  24. import org.apache.poi.xslf.XSLFTestDataSamples;
  25. import org.apache.poi.xssf.usermodel.XSSFRow;
  26. import org.apache.poi.xssf.usermodel.XSSFSheet;
  27. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  28. import org.openxmlformats.schemas.drawingml.x2006.chart.*;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.*;
  30. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
  31. import java.awt.*;
  32. import java.io.*;
  33. import java.util.LinkedHashMap;
  34. import java.util.Map;
  35. /**
  36. * @author Yegor Kozlov
  37. */
  38. public class TestXSLFChart extends TestCase {
  39. /**
  40. * a modified version from POI-examples
  41. */
  42. public void testFillChartTemplate() throws Exception {
  43. String chartTitle = "Apache POI"; // first line is chart title
  44. XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument("pie-chart.pptx");
  45. XSLFSlide slide = pptx.getSlides()[0];
  46. // find chart in the slide
  47. XSLFChart chart = null;
  48. for(POIXMLDocumentPart part : slide.getRelations()){
  49. if(part instanceof XSLFChart){
  50. chart = (XSLFChart) part;
  51. break;
  52. }
  53. }
  54. if(chart == null) throw new IllegalStateException("chart not found in the template");
  55. // embedded Excel workbook that holds the chart data
  56. POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
  57. XSSFWorkbook wb = new XSSFWorkbook();
  58. XSSFSheet sheet = wb.createSheet();
  59. CTChart ctChart = chart.getCTChart();
  60. CTPlotArea plotArea = ctChart.getPlotArea();
  61. CTPieChart pieChart = plotArea.getPieChartArray(0);
  62. //Pie Chart Series
  63. CTPieSer ser = pieChart.getSerArray(0);
  64. // Series Text
  65. CTSerTx tx = ser.getTx();
  66. tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle);
  67. sheet.createRow(0).createCell(1).setCellValue(chartTitle);
  68. String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();
  69. tx.getStrRef().setF(titleRef);
  70. // Category Axis Data
  71. CTAxDataSource cat = ser.getCat();
  72. CTStrData strData = cat.getStrRef().getStrCache();
  73. // Values
  74. CTNumDataSource valSrc = ser.getVal();
  75. CTNumData numData = valSrc.getNumRef().getNumCache();
  76. strData.setPtArray(null); // unset old axis text
  77. numData.setPtArray(null); // unset old values
  78. Map<String, Double> pieModel = new LinkedHashMap<String, Double>();
  79. pieModel.put("First", 1.0);
  80. pieModel.put("Second", 3.0);
  81. pieModel.put("Third", 4.0);
  82. // set model
  83. int idx = 0;
  84. int rownum = 1;
  85. for(String key : pieModel.keySet()){
  86. double val = pieModel.get(key);
  87. CTNumVal numVal = numData.addNewPt();
  88. numVal.setIdx(idx);
  89. numVal.setV("" + val);
  90. CTStrVal sVal = strData.addNewPt();
  91. sVal.setIdx(idx);
  92. sVal.setV(key);
  93. idx++;
  94. XSSFRow row = sheet.createRow(rownum++);
  95. row.createCell(0).setCellValue(key);
  96. row.createCell(1).setCellValue(val);
  97. }
  98. numData.getPtCount().setVal(idx);
  99. strData.getPtCount().setVal(idx);
  100. String numDataRange = new CellRangeAddress(1, rownum-1, 1, 1).formatAsString(sheet.getSheetName(), true);
  101. valSrc.getNumRef().setF(numDataRange);
  102. String axisDataRange = new CellRangeAddress(1, rownum-1, 0, 0).formatAsString(sheet.getSheetName(), true);
  103. cat.getStrRef().setF(axisDataRange);
  104. // updated the embedded workbook with the data
  105. OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
  106. wb.write(xlsOut);
  107. xlsOut.close();
  108. }
  109. }