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.

BarChart.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.xssf.usermodel;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import org.apache.poi.ss.usermodel.Cell;
  19. import org.apache.poi.ss.usermodel.Row;
  20. import org.apache.poi.ss.util.CellRangeAddress;
  21. import org.apache.poi.xddf.usermodel.PresetColor;
  22. import org.apache.poi.xddf.usermodel.XDDFColor;
  23. import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
  24. import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
  25. import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
  26. import org.apache.poi.xddf.usermodel.chart.AxisPosition;
  27. import org.apache.poi.xddf.usermodel.chart.BarDirection;
  28. import org.apache.poi.xddf.usermodel.chart.ChartTypes;
  29. import org.apache.poi.xddf.usermodel.chart.LegendPosition;
  30. import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
  31. import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
  32. import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
  33. import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
  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. import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
  38. import org.apache.poi.xssf.usermodel.XSSFChart;
  39. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  40. import org.apache.poi.xssf.usermodel.XSSFDrawing;
  41. import org.apache.poi.xssf.usermodel.XSSFSheet;
  42. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  43. /**
  44. * Line chart example.
  45. */
  46. public final class BarChart {
  47. private BarChart() {}
  48. public static void main(String[] args) throws IOException {
  49. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  50. XSSFSheet sheet = wb.createSheet("barchart");
  51. final int NUM_OF_ROWS = 3;
  52. final int NUM_OF_COLUMNS = 10;
  53. // Create a row and put some cells in it. Rows are 0 based.
  54. Row row;
  55. Cell cell;
  56. for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
  57. row = sheet.createRow((short) rowIndex);
  58. for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
  59. cell = row.createCell((short) colIndex);
  60. cell.setCellValue(colIndex * (rowIndex + 1.0));
  61. }
  62. }
  63. XSSFDrawing drawing = sheet.createDrawingPatriarch();
  64. XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
  65. XSSFChart chart = drawing.createChart(anchor);
  66. chart.setTitleText("x = 2x and x = 3x");
  67. chart.setTitleOverlay(false);
  68. XDDFChartLegend legend = chart.getOrAddLegend();
  69. legend.setPosition(LegendPosition.TOP_RIGHT);
  70. // Use a category axis for the bottom axis.
  71. XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
  72. bottomAxis.setTitle("x");
  73. XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
  74. leftAxis.setTitle("f(x)");
  75. leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
  76. XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
  77. XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
  78. XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
  79. XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
  80. XDDFChartData.Series series1 = data.addSeries(xs, ys1);
  81. series1.setTitle("2x", null);
  82. XDDFChartData.Series series2 = data.addSeries(xs, ys2);
  83. series2.setTitle("3x", null);
  84. chart.plot(data);
  85. // in order to transform a bar chart into a column chart, you just need to change the bar direction
  86. XDDFBarChartData bar = (XDDFBarChartData) data;
  87. bar.setBarDirection(BarDirection.COL);
  88. // looking for "Stacked Bar Chart"? uncomment the following line
  89. // bar.setBarGrouping(BarGrouping.STACKED);
  90. solidFillSeries(data, 0, PresetColor.CHARTREUSE);
  91. solidFillSeries(data, 1, PresetColor.TURQUOISE);
  92. // Write the output to a file
  93. try (FileOutputStream fileOut = new FileOutputStream("ooxml-bar-chart.xlsx")) {
  94. wb.write(fileOut);
  95. }
  96. }
  97. }
  98. private static void solidFillSeries(XDDFChartData data, int index, PresetColor color) {
  99. XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
  100. XDDFChartData.Series series = data.getSeries(index);
  101. XDDFShapeProperties properties = series.getShapeProperties();
  102. if (properties == null) {
  103. properties = new XDDFShapeProperties();
  104. }
  105. properties.setFillProperties(fill);
  106. series.setShapeProperties(properties);
  107. }
  108. }