Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PieChartDemo.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 java.io.BufferedReader;
  21. import java.io.FileInputStream;
  22. import java.io.FileOutputStream;
  23. import java.io.FileReader;
  24. import java.io.OutputStream;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import org.apache.poi.POIXMLDocumentPart;
  28. import org.apache.poi.ss.util.CellRangeAddress;
  29. import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
  30. import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
  31. import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
  32. import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
  33. import org.apache.poi.xddf.usermodel.chart.XDDFPieChartData;
  34. /**
  35. * Build a pie chart from a template pptx
  36. */
  37. public class PieChartDemo {
  38. private static void usage(){
  39. System.out.println("Usage: PieChartDemo <pie-chart-template.pptx> <pie-chart-data.txt>");
  40. System.out.println(" pie-chart-template.pptx template with a pie chart");
  41. System.out.println(" pie-chart-data.txt the model to set. First line is chart title, " +
  42. "then go pairs {axis-label value}");
  43. }
  44. public static void main(String[] args) throws Exception {
  45. if(args.length < 2) {
  46. usage();
  47. return;
  48. }
  49. try (BufferedReader modelReader = new BufferedReader(new FileReader(args[1]))) {
  50. String chartTitle = modelReader.readLine(); // first line is chart title
  51. try (XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(args[0]))) {
  52. XSLFSlide slide = pptx.getSlides().get(0);
  53. // find chart in the slide
  54. XSLFChart chart = null;
  55. for (POIXMLDocumentPart part : slide.getRelations()) {
  56. if (part instanceof XSLFChart) {
  57. chart = (XSLFChart) part;
  58. break;
  59. }
  60. }
  61. if(chart == null) {
  62. throw new IllegalStateException("chart not found in the template");
  63. }
  64. // Series Text
  65. List<XDDFChartData> series = chart.getChartSeries();
  66. XDDFPieChartData pie = (XDDFPieChartData) series.get(0);
  67. // Category Axis Data
  68. List<String> listCategories = new ArrayList<String>(3);
  69. // Values
  70. List<Double> listValues = new ArrayList<Double>(3);
  71. // set model
  72. String ln;
  73. while((ln = modelReader.readLine()) != null){
  74. String[] vals = ln.split("\\s+");
  75. listCategories.add(vals[0]);
  76. listValues.add(Double.valueOf(vals[1]));
  77. }
  78. String[] categories = listCategories.toArray(new String[listCategories.size()]);
  79. Double[] values = listValues.toArray(new Double[listValues.size()]);
  80. final int numOfPoints = categories.length;
  81. final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
  82. final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
  83. final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange);
  84. final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange);
  85. XDDFPieChartData.Series firstSeries = (XDDFPieChartData.Series) pie.getSeries().get(0);
  86. firstSeries.replaceData(categoriesData, valuesData);
  87. firstSeries.setTitle(chartTitle, chart.setSheetTitle(chartTitle));
  88. firstSeries.setExplosion(25);
  89. chart.plot(pie);
  90. // save the result
  91. try (OutputStream out = new FileOutputStream("pie-chart-demo-output.pptx")) {
  92. pptx.write(out);
  93. }
  94. }
  95. }
  96. }
  97. }