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.

LineChart.java 4.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.xssf.usermodel.examples;
  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.chart.AxisCrosses;
  22. import org.apache.poi.xddf.usermodel.chart.AxisPosition;
  23. import org.apache.poi.xddf.usermodel.chart.ChartTypes;
  24. import org.apache.poi.xddf.usermodel.chart.LegendPosition;
  25. import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
  26. import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
  27. import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
  28. import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
  29. import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
  30. import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
  31. import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
  32. import org.apache.poi.xssf.usermodel.XSSFChart;
  33. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  34. import org.apache.poi.xssf.usermodel.XSSFDrawing;
  35. import org.apache.poi.xssf.usermodel.XSSFSheet;
  36. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  37. /**
  38. * Line chart example.
  39. */
  40. public class LineChart {
  41. public static void main(String[] args) throws IOException {
  42. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  43. XSSFSheet sheet = wb.createSheet("linechart");
  44. final int NUM_OF_ROWS = 3;
  45. final int NUM_OF_COLUMNS = 10;
  46. // Create a row and put some cells in it. Rows are 0 based.
  47. Row row;
  48. Cell cell;
  49. for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
  50. row = sheet.createRow((short) rowIndex);
  51. for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
  52. cell = row.createCell((short) colIndex);
  53. cell.setCellValue(colIndex * (rowIndex + 1));
  54. }
  55. }
  56. XSSFDrawing drawing = sheet.createDrawingPatriarch();
  57. XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
  58. XSSFChart chart = drawing.createChart(anchor);
  59. XDDFChartLegend legend = chart.getOrAddLegend();
  60. legend.setPosition(LegendPosition.TOP_RIGHT);
  61. // Use a category axis for the bottom axis.
  62. XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
  63. XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
  64. leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
  65. XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
  66. XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
  67. XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
  68. XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
  69. data.addSeries(xs, ys1);
  70. data.addSeries(xs, ys2);
  71. chart.plot(data);
  72. // Write the output to a file
  73. try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {
  74. wb.write(fileOut);
  75. }
  76. }
  77. }
  78. }