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.

DoughnutChartFromScratch.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 org.apache.poi.ss.util.CellRangeAddress;
  21. import org.apache.poi.util.Units;
  22. import org.apache.poi.xddf.usermodel.chart.AxisCrossBetween;
  23. import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
  24. import org.apache.poi.xddf.usermodel.chart.AxisPosition;
  25. import org.apache.poi.xddf.usermodel.chart.AxisTickMark;
  26. import org.apache.poi.xddf.usermodel.chart.ChartTypes;
  27. import org.apache.poi.xddf.usermodel.chart.LegendPosition;
  28. import org.apache.poi.xddf.usermodel.chart.XDDFChartAxis;
  29. import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
  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.XDDFDoughnutChartData;
  33. import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
  34. import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
  35. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  36. import org.apache.poi.xslf.usermodel.XSLFChart;
  37. import org.apache.poi.xslf.usermodel.XSLFGraphicFrame;
  38. import org.apache.poi.xslf.usermodel.XSLFShape;
  39. import org.apache.poi.xslf.usermodel.XSLFSlide;
  40. import java.awt.geom.Rectangle2D;
  41. import java.io.BufferedReader;
  42. import java.io.FileInputStream;
  43. import java.io.FileOutputStream;
  44. import java.io.OutputStream;
  45. import java.nio.charset.StandardCharsets;
  46. import java.nio.file.Files;
  47. import java.nio.file.Paths;
  48. import java.util.ArrayList;
  49. import java.util.List;
  50. /**
  51. * Build a chart without reading template file
  52. */
  53. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  54. public final class DoughnutChartFromScratch {
  55. private DoughnutChartFromScratch() {}
  56. private static void usage(){
  57. System.out.println("Usage: DoughnutChartFromScratch <bar-chart-data.txt>");
  58. System.out.println(" bar-chart-data.txt the model to set. First line is chart title, " +
  59. "then go pairs {axis-label value}");
  60. }
  61. public static void main(String[] args) throws Exception {
  62. if(args.length < 1) {
  63. usage();
  64. return;
  65. }
  66. try (BufferedReader modelReader = Files.newBufferedReader(Paths.get(args[0]), StandardCharsets.UTF_8)) {
  67. String chartTitle = modelReader.readLine(); // first line is chart title
  68. String[] series = modelReader.readLine().split(",");
  69. // Category Axis Data
  70. List<String> listLanguages = new ArrayList<>(10);
  71. // Values
  72. List<Double> listCountries = new ArrayList<>(10);
  73. List<Double> listSpeakers = new ArrayList<>(10);
  74. // set model
  75. String ln;
  76. while((ln = modelReader.readLine()) != null) {
  77. String[] vals = ln.split(",");
  78. listCountries.add(Double.valueOf(vals[0]));
  79. listSpeakers.add(Double.valueOf(vals[1]));
  80. listLanguages.add(vals[2]);
  81. }
  82. String[] categories = listLanguages.toArray(new String[0]);
  83. Double[] values1 = listCountries.toArray(new Double[0]);
  84. Double[] values2 = listSpeakers.toArray(new Double[0]);
  85. try (XMLSlideShow ppt = new XMLSlideShow()) {
  86. createSlideWithChart(ppt, chartTitle, series, categories, values1, COLUMN_COUNTRIES);
  87. createSlideWithChart(ppt, chartTitle, series, categories, values2, COLUMN_SPEAKERS);
  88. // save the result
  89. try (OutputStream out = new FileOutputStream("doughnut-chart-from-scratch.pptx")) {
  90. ppt.write(out);
  91. }
  92. }
  93. try (FileInputStream is = new FileInputStream("doughnut-chart-from-scratch.pptx")) {
  94. try (XMLSlideShow ppt = new XMLSlideShow(is)) {
  95. for (XSLFSlide slide : ppt.getSlides()) {
  96. for (XSLFShape shape : slide.getShapes()) {
  97. if (shape instanceof XSLFGraphicFrame) {
  98. XSLFGraphicFrame frame = (XSLFGraphicFrame) shape;
  99. if (frame.hasChart()) {
  100. System.out.println(frame.getChart().getTitleShape().getText());
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. System.out.println("Done");
  109. }
  110. private static void createSlideWithChart(XMLSlideShow ppt, String chartTitle, String[] series, String[] categories,
  111. Double[] values, int valuesColumn) {
  112. XSLFSlide slide = ppt.createSlide();
  113. XSLFChart chart = ppt.createChart();
  114. Rectangle2D rect2D = new java.awt.Rectangle(fromCM(1.5), fromCM(4), fromCM(22), fromCM(14));
  115. slide.addChart(chart, rect2D);
  116. setDoughnutData(chart, chartTitle, series, categories, values, valuesColumn);
  117. }
  118. private static int fromCM(double cm) {
  119. return (int) (Math.rint(cm * Units.EMU_PER_CENTIMETER));
  120. }
  121. private static void setDoughnutData(XSLFChart chart, String chartTitle, String[] series, String[] categories,
  122. Double[] values, int valuesColumn) {
  123. final int numOfPoints = categories.length;
  124. final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, COLUMN_LANGUAGES, COLUMN_LANGUAGES));
  125. final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, valuesColumn, valuesColumn));
  126. final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, COLUMN_LANGUAGES);
  127. final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange, valuesColumn);
  128. valuesData.setFormatCode("General");
  129. XDDFDoughnutChartData data = (XDDFDoughnutChartData) chart.createData(ChartTypes.DOUGHNUT, null, null);
  130. XDDFDoughnutChartData.Series series1 = (XDDFDoughnutChartData.Series) data.addSeries(categoriesData, valuesData);
  131. series1.setTitle(series[0], chart.setSheetTitle(series[valuesColumn - 1], valuesColumn));
  132. data.setVaryColors(true);
  133. data.setHoleSize(42);
  134. data.setFirstSliceAngle(90);
  135. chart.plot(data);
  136. XDDFChartLegend legend = chart.getOrAddLegend();
  137. legend.setPosition(LegendPosition.LEFT);
  138. legend.setOverlay(false);
  139. chart.setTitleText(chartTitle);
  140. chart.setTitleOverlay(false);
  141. chart.setAutoTitleDeleted(false);
  142. }
  143. private static final int COLUMN_LANGUAGES = 0;
  144. private static final int COLUMN_COUNTRIES = 1;
  145. private static final int COLUMN_SPEAKERS = 2;
  146. }