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.

BarChartDemo.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.AxisOrientation;
  30. import org.apache.poi.xddf.usermodel.chart.AxisPosition;
  31. import org.apache.poi.xddf.usermodel.chart.BarDirection;
  32. import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
  33. import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
  34. import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
  35. import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
  36. import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
  37. /**
  38. * Build a bar chart from a template pptx
  39. */
  40. public class BarChartDemo {
  41. private static void usage(){
  42. System.out.println("Usage: BarChartDemo <bar-chart-template.pptx> <bar-chart-data.txt>");
  43. System.out.println(" bar-chart-template.pptx template with a bar chart");
  44. System.out.println(" bar-chart-data.txt the model to set. First line is chart title, " +
  45. "then go pairs {axis-label value}");
  46. }
  47. public static void main(String[] args) throws Exception {
  48. if(args.length < 2) {
  49. usage();
  50. return;
  51. }
  52. BufferedReader modelReader = new BufferedReader(new FileReader(args[1]));
  53. XMLSlideShow pptx = null;
  54. try {
  55. String chartTitle = modelReader.readLine(); // first line is chart title
  56. // Category Axis Data
  57. List<String> listCategories = new ArrayList<String>(3);
  58. // Values
  59. List<Double> listValues = new ArrayList<Double>(3);
  60. // set model
  61. String ln;
  62. while((ln = modelReader.readLine()) != null){
  63. String[] vals = ln.split("\\s+");
  64. listCategories.add(vals[0]);
  65. listValues.add(Double.valueOf(vals[1]));
  66. }
  67. String[] categories = listCategories.toArray(new String[listCategories.size()]);
  68. Double[] values = listValues.toArray(new Double[listValues.size()]);
  69. pptx = new XMLSlideShow(new FileInputStream(args[0]));
  70. XSLFSlide slide = pptx.getSlides().get(0);
  71. setBarData(findChart(slide), chartTitle, categories, values);
  72. XSLFChart chart = findChart(pptx.createSlide().importContent(slide));
  73. setColumnData(chart, "Column variant");
  74. // save the result
  75. OutputStream out = new FileOutputStream("bar-chart-demo-output.pptx");
  76. try {
  77. pptx.write(out);
  78. } finally {
  79. out.close();
  80. }
  81. } finally {
  82. if (pptx != null) {
  83. pptx.close();
  84. }
  85. modelReader.close();
  86. }
  87. }
  88. private static void setBarData(XSLFChart chart, String chartTitle, String[] categories, Double[] values) {
  89. final List<XDDFChartData> series = chart.getChartSeries();
  90. final XDDFBarChartData bar = (XDDFBarChartData) series.get(0);
  91. final int numOfPoints = categories.length;
  92. final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
  93. final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
  94. final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange);
  95. final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange);
  96. bar.getSeries().get(0).replaceData(categoriesData, valuesData);
  97. bar.getSeries().get(0).setTitle(chartTitle, chart.setSheetTitle(chartTitle));
  98. chart.plot(bar);
  99. }
  100. private static void setColumnData(XSLFChart chart, String chartTitle) {
  101. // Series Text
  102. List<XDDFChartData> series = chart.getChartSeries();
  103. XDDFBarChartData bar = (XDDFBarChartData) series.get(0);
  104. bar.getSeries().get(0).setTitle(chartTitle, chart.setSheetTitle(chartTitle));
  105. // in order to transform a bar chart into a column chart, you just need to change the bar direction
  106. bar.setBarDirection(BarDirection.COL);
  107. // additionally, you can adjust the axes
  108. bar.getCategoryAxis().setOrientation(AxisOrientation.MAX_MIN);
  109. bar.getValueAxes().get(0).setPosition(AxisPosition.TOP);
  110. }
  111. private static XSLFChart findChart(XSLFSlide slide) {
  112. // find chart in the slide
  113. XSLFChart chart = null;
  114. for(POIXMLDocumentPart part : slide.getRelations()){
  115. if(part instanceof XSLFChart){
  116. chart = (XSLFChart) part;
  117. break;
  118. }
  119. }
  120. if(chart == null) {
  121. throw new IllegalStateException("chart not found in the template");
  122. }
  123. return chart;
  124. }
  125. }