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.

XSSFScatterChartData.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.ScatterChartData;
  22. import org.apache.poi.ss.usermodel.charts.ScatterChartSeries;
  23. import org.apache.poi.util.Removal;
  24. import org.apache.poi.xddf.usermodel.chart.XDDFScatterChartData;
  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.CTNumDataSource;
  28. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
  29. import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterChart;
  30. import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterSer;
  31. import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterStyle;
  32. import org.openxmlformats.schemas.drawingml.x2006.chart.STScatterStyle;
  33. /**
  34. * Represents DrawingML scatter charts.
  35. *
  36. * @deprecated use {@link XDDFScatterChartData} instead
  37. */
  38. @Deprecated
  39. @Removal(version="4.2")
  40. public class XSSFScatterChartData implements ScatterChartData {
  41. /**
  42. * List of all data series.
  43. */
  44. private List<Series> series;
  45. public XSSFScatterChartData() {
  46. series = new ArrayList<Series>();
  47. }
  48. /**
  49. * Package private ScatterChartSerie implementation.
  50. */
  51. static class Series extends AbstractXSSFChartSeries implements ScatterChartSeries {
  52. private int id;
  53. private int order;
  54. private ChartDataSource<?> xs;
  55. private ChartDataSource<? extends Number> ys;
  56. protected Series(int id, int order,
  57. ChartDataSource<?> xs,
  58. ChartDataSource<? extends Number> ys) {
  59. super();
  60. this.id = id;
  61. this.order = order;
  62. this.xs = xs;
  63. this.ys = ys;
  64. }
  65. /**
  66. * Returns data source used for X axis values.
  67. * @return data source used for X axis values
  68. */
  69. @Override
  70. public ChartDataSource<?> getXValues() {
  71. return xs;
  72. }
  73. /**
  74. * Returns data source used for Y axis values.
  75. * @return data source used for Y axis values
  76. */
  77. @Override
  78. public ChartDataSource<? extends Number> getYValues() {
  79. return ys;
  80. }
  81. protected void addToChart(CTScatterChart ctScatterChart) {
  82. CTScatterSer scatterSer = ctScatterChart.addNewSer();
  83. scatterSer.addNewIdx().setVal(this.id);
  84. scatterSer.addNewOrder().setVal(this.order);
  85. CTAxDataSource xVal = scatterSer.addNewXVal();
  86. XSSFChartUtil.buildAxDataSource(xVal, xs);
  87. CTNumDataSource yVal = scatterSer.addNewYVal();
  88. XSSFChartUtil.buildNumDataSource(yVal, ys);
  89. if (isTitleSet()) {
  90. scatterSer.setTx(getCTSerTx());
  91. }
  92. }
  93. }
  94. @Override
  95. public ScatterChartSeries addSerie(ChartDataSource<?> xs,
  96. ChartDataSource<? extends Number> ys) {
  97. if (!ys.isNumeric()) {
  98. throw new IllegalArgumentException("Y axis data source must be numeric.");
  99. }
  100. int numOfSeries = series.size();
  101. Series newSerie = new Series(numOfSeries, numOfSeries, xs, ys);
  102. series.add(newSerie);
  103. return newSerie;
  104. }
  105. @Override
  106. public void fillChart(Chart chart, ChartAxis... axis) {
  107. if (!(chart instanceof XSSFChart)) {
  108. throw new IllegalArgumentException("Chart must be instance of XSSFChart");
  109. }
  110. XSSFChart xssfChart = (XSSFChart) chart;
  111. CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
  112. CTScatterChart scatterChart = plotArea.addNewScatterChart();
  113. addStyle(scatterChart);
  114. for (Series s : series) {
  115. s.addToChart(scatterChart);
  116. }
  117. for (ChartAxis ax : axis) {
  118. scatterChart.addNewAxId().setVal(ax.getId());
  119. }
  120. }
  121. @Override
  122. public List<? extends Series> getSeries() {
  123. return series;
  124. }
  125. private void addStyle(CTScatterChart ctScatterChart) {
  126. CTScatterStyle scatterStyle = ctScatterChart.addNewScatterStyle();
  127. scatterStyle.setVal(STScatterStyle.LINE_MARKER);
  128. }
  129. }