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.

BarAndLineChart.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.xssf.usermodel;
  20. import java.io.FileOutputStream;
  21. import java.util.Random;
  22. import org.apache.poi.common.usermodel.fonts.FontGroup;
  23. import org.apache.poi.ss.util.CellRangeAddress;
  24. import org.apache.poi.ss.util.CellReference;
  25. import org.apache.poi.xddf.usermodel.PresetColor;
  26. import org.apache.poi.xddf.usermodel.XDDFColor;
  27. import org.apache.poi.xddf.usermodel.XDDFFillProperties;
  28. import org.apache.poi.xddf.usermodel.XDDFLineProperties;
  29. import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
  30. import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
  31. import org.apache.poi.xddf.usermodel.chart.AxisPosition;
  32. import org.apache.poi.xddf.usermodel.chart.BarDirection;
  33. import org.apache.poi.xddf.usermodel.chart.ChartTypes;
  34. import org.apache.poi.xddf.usermodel.chart.LayoutMode;
  35. import org.apache.poi.xddf.usermodel.chart.LegendPosition;
  36. import org.apache.poi.xddf.usermodel.chart.MarkerStyle;
  37. import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
  38. import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
  39. import org.apache.poi.xddf.usermodel.chart.XDDFCategoryDataSource;
  40. import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
  41. import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
  42. import org.apache.poi.xddf.usermodel.chart.XDDFLineChartData;
  43. import org.apache.poi.xddf.usermodel.chart.XDDFManualLayout;
  44. import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
  45. import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
  46. import org.apache.poi.xddf.usermodel.text.UnderlineType;
  47. import org.apache.poi.xddf.usermodel.text.XDDFFont;
  48. import org.apache.poi.xddf.usermodel.text.XDDFRunProperties;
  49. import org.apache.poi.xddf.usermodel.text.XDDFTextParagraph;
  50. import org.apache.poi.xssf.usermodel.XSSFCell;
  51. import org.apache.poi.xssf.usermodel.XSSFChart;
  52. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  53. import org.apache.poi.xssf.usermodel.XSSFDrawing;
  54. import org.apache.poi.xssf.usermodel.XSSFRow;
  55. import org.apache.poi.xssf.usermodel.XSSFSheet;
  56. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  57. public final class BarAndLineChart {
  58. private static final int NUM_OF_ROWS = 7;
  59. private static final Random RNG = new Random();
  60. private BarAndLineChart() {}
  61. public static void main(String[] args) throws Exception {
  62. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  63. XSSFSheet sheet = wb.createSheet("Sheet1");
  64. XSSFRow row = sheet.createRow(0);
  65. row.createCell(0);
  66. row.createCell(1).setCellValue("Bars");
  67. row.createCell(2).setCellValue("Lines");
  68. XSSFCell cell;
  69. for (int r = 1; r < NUM_OF_ROWS; r++) {
  70. row = sheet.createRow(r);
  71. cell = row.createCell(0);
  72. cell.setCellValue("C" + r);
  73. cell = row.createCell(1);
  74. cell.setCellValue(RNG.nextDouble());
  75. cell = row.createCell(2);
  76. cell.setCellValue(RNG.nextDouble() * 10);
  77. }
  78. XSSFDrawing drawing = sheet.createDrawingPatriarch();
  79. XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 0, 11, 15);
  80. XSSFChart chart = drawing.createChart(anchor);
  81. chart.setTitleText("This is my title");
  82. chart.setTitleOverlay(true);
  83. XDDFRunProperties properties = new XDDFRunProperties();
  84. properties.setBold(true);
  85. properties.setItalic(true);
  86. properties.setUnderline(UnderlineType.DOT_DOT_DASH_HEAVY);
  87. properties.setFontSize(22.5);
  88. XDDFFont[] fonts = new XDDFFont[] {
  89. new XDDFFont(FontGroup.LATIN, "Calibri", null, null, null),
  90. new XDDFFont(FontGroup.COMPLEX_SCRIPT, "Liberation Sans", null, null, null)
  91. };
  92. properties.setFonts(fonts);
  93. properties.setLineProperties(new XDDFLineProperties(
  94. new XDDFSolidFillProperties(XDDFColor.from(PresetColor.SIENNA))));
  95. XDDFTextParagraph paragraph = chart.getTitle().getBody().getParagraph(0);
  96. paragraph.setDefaultRunProperties(properties);
  97. // the data sources
  98. XDDFCategoryDataSource xs = XDDFDataSourcesFactory.fromStringCellRange(sheet,
  99. new CellRangeAddress(1, NUM_OF_ROWS - 1, 0, 0));
  100. XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
  101. new CellRangeAddress(1, NUM_OF_ROWS - 1, 1, 1));
  102. XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
  103. new CellRangeAddress(1, NUM_OF_ROWS - 1, 2, 2));
  104. // cat axis 1 (bars)
  105. XDDFCategoryAxis barCategories = chart.createCategoryAxis(AxisPosition.BOTTOM);
  106. // val axis 1 (left)
  107. XDDFValueAxis leftValues = chart.createValueAxis(AxisPosition.LEFT);
  108. leftValues.crossAxis(barCategories);
  109. barCategories.crossAxis(leftValues);
  110. // cat axis 2 (lines)
  111. XDDFCategoryAxis lineCategories = chart.createCategoryAxis(AxisPosition.BOTTOM);
  112. lineCategories.setVisible(false); // this cat axis is deleted
  113. // val axis 2 (right)
  114. XDDFValueAxis rightValues = chart.createValueAxis(AxisPosition.RIGHT);
  115. // this value axis crosses its category axis at max value
  116. rightValues.setCrosses(AxisCrosses.MAX);
  117. rightValues.crossAxis(lineCategories);
  118. lineCategories.crossAxis(rightValues);
  119. // the bar chart
  120. XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, barCategories, leftValues);
  121. XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(xs, ys1);
  122. series1.setTitle(null, new CellReference(sheet.getSheetName(), 0, 1, true,true));
  123. bar.setVaryColors(true);
  124. bar.setBarDirection(BarDirection.COL);
  125. chart.plot(bar);
  126. // the line chart on secondary axis
  127. XDDFLineChartData lines = (XDDFLineChartData) chart.createData(ChartTypes.LINE, lineCategories,
  128. rightValues);
  129. //uncomment below line if only primary axis required and comment above line
  130. // the line chart on primary axis
  131. /*XDDFLineChartData lines = (XDDFLineChartData) chart.createData(ChartTypes.LINE, lineCategories,
  132. leftValues);*/
  133. XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) lines.addSeries(xs, ys2);
  134. series2.setTitle(null, new CellReference(sheet.getSheetName(), 0, 2, true, true));
  135. series2.setSmooth(false);
  136. series2.setMarkerStyle(MarkerStyle.DIAMOND);
  137. series2.setMarkerSize((short)14);
  138. lines.setVaryColors(true);
  139. chart.plot(lines);
  140. // some colors
  141. XDDFFillProperties solidChartreuse = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.CHARTREUSE));
  142. XDDFFillProperties solidTurquoise = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.TURQUOISE));
  143. XDDFLineProperties linesChartreuse = new XDDFLineProperties(solidChartreuse);
  144. XDDFLineProperties linesTurquoise = new XDDFLineProperties(solidTurquoise);
  145. series1.setFillProperties(solidChartreuse);
  146. series1.setLineProperties(linesTurquoise); // bar border color different from fill
  147. series1.getDataPoint(2).setFillProperties(solidTurquoise); // this specific bar has inverted colors
  148. series1.getDataPoint(2).setLineProperties(linesChartreuse);
  149. series2.setLineProperties(linesTurquoise);
  150. series2.getDataPoint(2).setMarkerStyle(MarkerStyle.STAR);
  151. series2.getDataPoint(2).setLineProperties(linesChartreuse);
  152. // legend
  153. XDDFChartLegend legend = chart.getOrAddLegend();
  154. legend.setPosition(LegendPosition.LEFT);
  155. legend.setOverlay(false);
  156. XDDFManualLayout layout = legend.getOrAddManualLayout();
  157. layout.setXMode(LayoutMode.EDGE);
  158. layout.setYMode(LayoutMode.EDGE);
  159. layout.setX(0.00); //left edge of the chart
  160. layout.setY(0.25); //25% of chart's height from top edge of the chart
  161. try (FileOutputStream fileOut = new FileOutputStream("BarAndLineChart.xlsx")) {
  162. wb.write(fileOut);
  163. }
  164. }
  165. }
  166. }