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.

PieChartDemo.java 5.2KB

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