Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 org.apache.poi.POIXMLDocumentPart;
  21. import org.apache.poi.ss.util.CellRangeAddress;
  22. import org.apache.poi.ss.util.CellReference;
  23. import org.apache.poi.xssf.usermodel.XSSFRow;
  24. import org.apache.poi.xssf.usermodel.XSSFSheet;
  25. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  26. import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
  27. import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
  28. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;
  29. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
  30. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal;
  31. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart;
  32. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;
  33. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
  34. import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;
  35. import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData;
  36. import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal;
  37. import java.io.BufferedReader;
  38. import java.io.FileInputStream;
  39. import java.io.FileOutputStream;
  40. import java.io.FileReader;
  41. import java.io.OutputStream;
  42. /**
  43. * Build a pie chart from a template pptx
  44. *
  45. * @author Yegor Kozlov
  46. */
  47. public class PieChartDemo {
  48. private static void usage(){
  49. System.out.println("Usage: PieChartDemo <pie-chart-template.pptx> <pie-chart-data.txt>");
  50. System.out.println(" pie-chart-template.pptx template with a pie chart");
  51. System.out.println(" pie-chart-data.txt the model to set. First line is chart title, " +
  52. "then go pairs {axis-label value}");
  53. }
  54. public static void main(String[] args) throws Exception {
  55. if(args.length < 2) {
  56. usage();
  57. return;
  58. }
  59. BufferedReader modelReader = new BufferedReader(new FileReader(args[1]));
  60. String chartTitle = modelReader.readLine(); // first line is chart title
  61. XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(args[0]));
  62. XSLFSlide slide = pptx.getSlides()[0];
  63. // find chart in the slide
  64. XSLFChart chart = null;
  65. for(POIXMLDocumentPart part : slide.getRelations()){
  66. if(part instanceof XSLFChart){
  67. chart = (XSLFChart) part;
  68. break;
  69. }
  70. }
  71. if(chart == null) throw new IllegalStateException("chart not found in the template");
  72. // embedded Excel workbook that holds the chart data
  73. POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
  74. XSSFWorkbook wb = new XSSFWorkbook();
  75. XSSFSheet sheet = wb.createSheet();
  76. CTChart ctChart = chart.getCTChart();
  77. CTPlotArea plotArea = ctChart.getPlotArea();
  78. CTPieChart pieChart = plotArea.getPieChartArray(0);
  79. //Pie Chart Series
  80. CTPieSer ser = pieChart.getSerArray(0);
  81. // Series Text
  82. CTSerTx tx = ser.getTx();
  83. tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle);
  84. sheet.createRow(0).createCell(1).setCellValue(chartTitle);
  85. String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();
  86. tx.getStrRef().setF(titleRef);
  87. // Category Axis Data
  88. CTAxDataSource cat = ser.getCat();
  89. CTStrData strData = cat.getStrRef().getStrCache();
  90. // Values
  91. CTNumDataSource val = ser.getVal();
  92. CTNumData numData = val.getNumRef().getNumCache();
  93. strData.setPtArray(null); // unset old axis text
  94. numData.setPtArray(null); // unset old values
  95. // set model
  96. int idx = 0;
  97. int rownum = 1;
  98. String ln;
  99. while((ln = modelReader.readLine()) != null){
  100. String[] vals = ln.split("\\s+");
  101. CTNumVal numVal = numData.addNewPt();
  102. numVal.setIdx(idx);
  103. numVal.setV(vals[1]);
  104. CTStrVal sVal = strData.addNewPt();
  105. sVal.setIdx(idx);
  106. sVal.setV(vals[0]);
  107. idx++;
  108. XSSFRow row = sheet.createRow(rownum++);
  109. row.createCell(0).setCellValue(vals[0]);
  110. row.createCell(1).setCellValue(Double.valueOf(vals[1]));
  111. }
  112. numData.getPtCount().setVal(idx);
  113. strData.getPtCount().setVal(idx);
  114. String numDataRange = new CellRangeAddress(1, rownum-1, 1, 1).formatAsString(sheet.getSheetName(), true);
  115. val.getNumRef().setF(numDataRange);
  116. String axisDataRange = new CellRangeAddress(1, rownum-1, 0, 0).formatAsString(sheet.getSheetName(), true);
  117. cat.getStrRef().setF(axisDataRange);
  118. // updated the embedded workbook with the data
  119. OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
  120. wb.write(xlsOut);
  121. xlsOut.close();
  122. // save the result
  123. FileOutputStream out = new FileOutputStream("pie-chart-demo-output.pptx");
  124. pptx.write(out);
  125. out.close();
  126. }
  127. }