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.

XSSFLineChartData.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.charts;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.poi.ss.usermodel.Chart;
  19. import org.apache.poi.ss.usermodel.charts.ChartAxis;
  20. import org.apache.poi.ss.usermodel.charts.ChartDataSource;
  21. import org.apache.poi.ss.usermodel.charts.LineChartData;
  22. import org.apache.poi.ss.usermodel.charts.LineChartSeries;
  23. import org.apache.poi.util.Removal;
  24. import org.apache.poi.xddf.usermodel.chart.XDDFLineChartData;
  25. import org.apache.poi.xssf.usermodel.XSSFChart;
  26. import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
  27. import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineChart;
  28. import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer;
  29. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
  30. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
  31. import org.openxmlformats.schemas.drawingml.x2006.chart.STMarkerStyle;
  32. /**
  33. * Holds data for a XSSF Line Chart
  34. *
  35. * @deprecated use {@link XDDFLineChartData} instead
  36. */
  37. @Deprecated
  38. @Removal(version="4.2")
  39. public class XSSFLineChartData implements LineChartData {
  40. /**
  41. * List of all data series.
  42. */
  43. private List<Series> series;
  44. public XSSFLineChartData() {
  45. series = new ArrayList<Series>();
  46. }
  47. static class Series extends AbstractXSSFChartSeries implements LineChartSeries {
  48. private int id;
  49. private int order;
  50. private ChartDataSource<?> categories;
  51. private ChartDataSource<? extends Number> values;
  52. protected Series(int id, int order,
  53. ChartDataSource<?> categories,
  54. ChartDataSource<? extends Number> values) {
  55. this.id = id;
  56. this.order = order;
  57. this.categories = categories;
  58. this.values = values;
  59. }
  60. @Override
  61. public ChartDataSource<?> getCategoryAxisData() {
  62. return categories;
  63. }
  64. @Override
  65. public ChartDataSource<? extends Number> getValues() {
  66. return values;
  67. }
  68. protected void addToChart(CTLineChart ctLineChart) {
  69. CTLineSer ctLineSer = ctLineChart.addNewSer();
  70. ctLineSer.addNewIdx().setVal(id);
  71. ctLineSer.addNewOrder().setVal(order);
  72. // No marker symbol on the chart line.
  73. ctLineSer.addNewMarker().addNewSymbol().setVal(STMarkerStyle.NONE);
  74. CTAxDataSource catDS = ctLineSer.addNewCat();
  75. XSSFChartUtil.buildAxDataSource(catDS, categories);
  76. CTNumDataSource valueDS = ctLineSer.addNewVal();
  77. XSSFChartUtil.buildNumDataSource(valueDS, values);
  78. if (isTitleSet()) {
  79. ctLineSer.setTx(getCTSerTx());
  80. }
  81. }
  82. }
  83. @Override
  84. public LineChartSeries addSeries(ChartDataSource<?> categoryAxisData, ChartDataSource<? extends Number> values) {
  85. if (!values.isNumeric()) {
  86. throw new IllegalArgumentException("Value data source must be numeric.");
  87. }
  88. int numOfSeries = series.size();
  89. Series newSeries = new Series(numOfSeries, numOfSeries, categoryAxisData, values);
  90. series.add(newSeries);
  91. return newSeries;
  92. }
  93. @Override
  94. public List<? extends LineChartSeries> getSeries() {
  95. return series;
  96. }
  97. @Override
  98. public void fillChart(Chart chart, ChartAxis... axis) {
  99. if (!(chart instanceof XSSFChart)) {
  100. throw new IllegalArgumentException("Chart must be instance of XSSFChart");
  101. }
  102. XSSFChart xssfChart = (XSSFChart) chart;
  103. CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
  104. CTLineChart lineChart = plotArea.addNewLineChart();
  105. lineChart.addNewVaryColors().setVal(false);
  106. for (Series s : series) {
  107. s.addToChart(lineChart);
  108. }
  109. for (ChartAxis ax : axis) {
  110. lineChart.addNewAxId().setVal(ax.getId());
  111. }
  112. }
  113. }