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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.XDDFLineProperties;
  24. import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
  25. import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
  26. import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
  27. import org.apache.poi.xddf.usermodel.chart.AxisPosition;
  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.MarkerStyle;
  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.XDDFLineChartData;
  37. import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
  38. import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
  39. import org.apache.poi.xssf.usermodel.XSSFChart;
  40. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  41. import org.apache.poi.xssf.usermodel.XSSFDrawing;
  42. import org.apache.poi.xssf.usermodel.XSSFSheet;
  43. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  44. /**
  45. * Line chart example.
  46. */
  47. public final class LineChart {
  48. private LineChart() {}
  49. public static void main(String[] args) throws IOException {
  50. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  51. XSSFSheet sheet = wb.createSheet("linechart");
  52. final int NUM_OF_ROWS = 3;
  53. final int NUM_OF_COLUMNS = 10;
  54. // Create a row and put some cells in it. Rows are 0 based.
  55. Row row;
  56. Cell cell;
  57. for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
  58. row = sheet.createRow((short) rowIndex);
  59. for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
  60. cell = row.createCell((short) colIndex);
  61. cell.setCellValue(colIndex * (rowIndex + 1.0));
  62. }
  63. }
  64. XSSFDrawing drawing = sheet.createDrawingPatriarch();
  65. XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
  66. XSSFChart chart = drawing.createChart(anchor);
  67. XDDFChartLegend legend = chart.getOrAddLegend();
  68. legend.setPosition(LegendPosition.TOP_RIGHT);
  69. // Use a category axis for the bottom axis.
  70. XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
  71. bottomAxis.setTitle("x");
  72. XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
  73. leftAxis.setTitle("f(x)");
  74. leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
  75. XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
  76. XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
  77. XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
  78. XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
  79. XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs, ys1);
  80. series1.setTitle("2x", null);
  81. series1.setSmooth(false);
  82. series1.setMarkerStyle(MarkerStyle.STAR);
  83. XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs, ys2);
  84. series2.setTitle("3x", null);
  85. series2.setSmooth(true);
  86. series2.setMarkerSize((short) 6);
  87. series2.setMarkerStyle(MarkerStyle.TRIANGLE);
  88. chart.plot(data);
  89. // if your series have missing values
  90. // chart.displayBlanksAs(DisplayBlanks.GAP);
  91. solidLineSeries(data, 0, PresetColor.CHARTREUSE);
  92. solidLineSeries(data, 1, PresetColor.TURQUOISE);
  93. // Write the output to a file
  94. try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {
  95. wb.write(fileOut);
  96. }
  97. }
  98. }
  99. private static void solidLineSeries(XDDFChartData data, int index, PresetColor color) {
  100. XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
  101. XDDFLineProperties line = new XDDFLineProperties();
  102. line.setFillProperties(fill);
  103. XDDFChartData.Series series = data.getSeries(index);
  104. XDDFShapeProperties properties = series.getShapeProperties();
  105. if (properties == null) {
  106. properties = new XDDFShapeProperties();
  107. }
  108. properties.setLineProperties(line);
  109. series.setShapeProperties(properties);
  110. }
  111. }