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.

ScatterChart.java 4.2KB

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