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.

XDDFDataSourcesFactory.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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.xddf.usermodel.chart;
  20. import org.apache.poi.ss.usermodel.CellType;
  21. import org.apache.poi.ss.usermodel.CellValue;
  22. import org.apache.poi.ss.util.CellRangeAddress;
  23. import org.apache.poi.util.Beta;
  24. import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
  25. import org.apache.poi.xssf.usermodel.XSSFRow;
  26. import org.apache.poi.xssf.usermodel.XSSFSheet;
  27. import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
  28. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;
  29. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
  30. import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData;
  31. /**
  32. * Class {@code XDDFDataSourcesFactory} is a factory for {@link XDDFDataSource} instances.
  33. */
  34. @Beta
  35. public class XDDFDataSourcesFactory {
  36. private XDDFDataSourcesFactory() {
  37. }
  38. public static XDDFCategoryDataSource fromDataSource(final CTAxDataSource categoryDS) {
  39. if (categoryDS.getStrRef() == null) {
  40. return new XDDFCategoryDataSource() {
  41. private CTNumData category = (CTNumData) categoryDS.getNumRef().getNumCache().copy();
  42. @Override
  43. public boolean isNumeric() {
  44. return true;
  45. }
  46. @Override
  47. public String getFormula() {
  48. return categoryDS.getNumRef().getF();
  49. }
  50. @Override
  51. public int getPointCount() {
  52. return (int) category.getPtCount().getVal();
  53. }
  54. @Override
  55. public String getPointAt(int index) {
  56. return category.getPtArray(index).getV();
  57. }
  58. };
  59. } else {
  60. return new XDDFCategoryDataSource() {
  61. private CTStrData category = (CTStrData) categoryDS.getStrRef().getStrCache().copy();
  62. @Override
  63. public String getFormula() {
  64. return categoryDS.getStrRef().getF();
  65. }
  66. @Override
  67. public int getPointCount() {
  68. return (int) category.getPtCount().getVal();
  69. }
  70. @Override
  71. public String getPointAt(int index) {
  72. return category.getPtArray(index).getV();
  73. }
  74. };
  75. }
  76. }
  77. public static XDDFNumericalDataSource<Double> fromDataSource(final CTNumDataSource valuesDS) {
  78. return new XDDFNumericalDataSource<Double>() {
  79. private CTNumData values = (CTNumData) valuesDS.getNumRef().getNumCache().copy();
  80. private String formatCode = values.isSetFormatCode() ? values.getFormatCode() : null;
  81. @Override
  82. public String getFormula() {
  83. return valuesDS.getNumRef().getF();
  84. }
  85. @Override
  86. public String getFormatCode() {
  87. return formatCode;
  88. }
  89. @Override
  90. public void setFormatCode(String formatCode) {
  91. this.formatCode = formatCode;
  92. }
  93. @Override
  94. public boolean isNumeric() {
  95. return true;
  96. }
  97. @Override
  98. public boolean isReference() {
  99. return true;
  100. }
  101. @Override
  102. public int getPointCount() {
  103. return (int) values.getPtCount().getVal();
  104. }
  105. @Override
  106. public Double getPointAt(int index) {
  107. return Double.valueOf(values.getPtArray(index).getV());
  108. }
  109. @Override
  110. public String getDataRangeReference() {
  111. return valuesDS.getNumRef().getF();
  112. }
  113. @Override
  114. public int getColIndex() {
  115. return 0;
  116. }
  117. };
  118. }
  119. public static <T extends Number> XDDFNumericalDataSource<T> fromArray(T[] elements, String dataRange) {
  120. return new NumericalArrayDataSource<>(elements, dataRange);
  121. }
  122. public static XDDFCategoryDataSource fromArray(String[] elements, String dataRange) {
  123. return new StringArrayDataSource(elements, dataRange);
  124. }
  125. public static <T extends Number> XDDFNumericalDataSource<T> fromArray(T[] elements, String dataRange, int col) {
  126. return new NumericalArrayDataSource<>(elements, dataRange, col);
  127. }
  128. public static XDDFCategoryDataSource fromArray(String[] elements, String dataRange, int col) {
  129. return new StringArrayDataSource(elements, dataRange, col);
  130. }
  131. public static XDDFNumericalDataSource<Double> fromNumericCellRange(XSSFSheet sheet,
  132. CellRangeAddress cellRangeAddress) {
  133. return new NumericalCellRangeDataSource(sheet, cellRangeAddress);
  134. }
  135. public static XDDFCategoryDataSource fromStringCellRange(XSSFSheet sheet, CellRangeAddress cellRangeAddress) {
  136. return new StringCellRangeDataSource(sheet, cellRangeAddress);
  137. }
  138. private abstract static class AbstractArrayDataSource<T> implements XDDFDataSource<T> {
  139. private final T[] elements;
  140. private final String dataRange;
  141. private int col = 0;
  142. public AbstractArrayDataSource(T[] elements, String dataRange) {
  143. this.elements = elements.clone();
  144. this.dataRange = dataRange;
  145. }
  146. public AbstractArrayDataSource(T[] elements, String dataRange, int col) {
  147. this.elements = elements.clone();
  148. this.dataRange = dataRange;
  149. this.col = col;
  150. }
  151. @Override
  152. public int getPointCount() {
  153. return elements.length;
  154. }
  155. @Override
  156. public T getPointAt(int index) {
  157. return elements[index];
  158. }
  159. @Override
  160. public boolean isReference() {
  161. return dataRange != null;
  162. }
  163. @Override
  164. public boolean isNumeric() {
  165. Class<?> arrayComponentType = elements.getClass().getComponentType();
  166. return (Number.class.isAssignableFrom(arrayComponentType));
  167. }
  168. @Override
  169. public String getDataRangeReference() {
  170. if (dataRange == null) {
  171. throw new UnsupportedOperationException("Literal data source can not be expressed by reference.");
  172. } else {
  173. return dataRange;
  174. }
  175. }
  176. @Override
  177. public int getColIndex() {
  178. return col;
  179. }
  180. }
  181. private static class NumericalArrayDataSource<T extends Number> extends AbstractArrayDataSource<T>
  182. implements XDDFNumericalDataSource<T> {
  183. private String formatCode;
  184. public NumericalArrayDataSource(T[] elements, String dataRange) {
  185. super(elements, dataRange);
  186. }
  187. public NumericalArrayDataSource(T[] elements, String dataRange, int col) {
  188. super(elements, dataRange, col);
  189. }
  190. @Override
  191. public String getFormula() {
  192. return getDataRangeReference();
  193. }
  194. @Override
  195. public String getFormatCode() {
  196. return formatCode;
  197. }
  198. @Override
  199. public void setFormatCode(String formatCode) {
  200. this.formatCode = formatCode;
  201. }
  202. }
  203. private static class StringArrayDataSource extends AbstractArrayDataSource<String>
  204. implements XDDFCategoryDataSource {
  205. public StringArrayDataSource(String[] elements, String dataRange) {
  206. super(elements, dataRange);
  207. }
  208. public StringArrayDataSource(String[] elements, String dataRange, int col) {
  209. super(elements, dataRange, col);
  210. }
  211. @Override
  212. public String getFormula() {
  213. return getDataRangeReference();
  214. }
  215. }
  216. private abstract static class AbstractCellRangeDataSource<T> implements XDDFDataSource<T> {
  217. private final XSSFSheet sheet;
  218. private final CellRangeAddress cellRangeAddress;
  219. private final int numOfCells;
  220. private XSSFFormulaEvaluator evaluator;
  221. protected AbstractCellRangeDataSource(XSSFSheet sheet, CellRangeAddress cellRangeAddress) {
  222. this.sheet = sheet;
  223. // Make copy since CellRangeAddress is mutable.
  224. this.cellRangeAddress = cellRangeAddress.copy();
  225. this.numOfCells = this.cellRangeAddress.getNumberOfCells();
  226. this.evaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator();
  227. }
  228. @Override
  229. public int getPointCount() {
  230. return numOfCells;
  231. }
  232. @Override
  233. public boolean isReference() {
  234. return true;
  235. }
  236. @Override
  237. public int getColIndex() {
  238. return cellRangeAddress.getFirstColumn();
  239. }
  240. @Override
  241. public String getDataRangeReference() {
  242. return cellRangeAddress.formatAsString(sheet.getSheetName(), true);
  243. }
  244. protected CellValue getCellValueAt(int index) {
  245. if (index < 0 || index >= numOfCells) {
  246. throw new IndexOutOfBoundsException(
  247. "Index must be between 0 and " + (numOfCells - 1) + " (inclusive), given: " + index);
  248. }
  249. int firstRow = cellRangeAddress.getFirstRow();
  250. int firstCol = cellRangeAddress.getFirstColumn();
  251. int lastCol = cellRangeAddress.getLastColumn();
  252. int width = lastCol - firstCol + 1;
  253. int rowIndex = firstRow + index / width;
  254. int cellIndex = firstCol + index % width;
  255. XSSFRow row = sheet.getRow(rowIndex);
  256. return (row == null) ? null : evaluator.evaluate(row.getCell(cellIndex));
  257. }
  258. }
  259. private static class NumericalCellRangeDataSource extends AbstractCellRangeDataSource<Double>
  260. implements XDDFNumericalDataSource<Double> {
  261. protected NumericalCellRangeDataSource(XSSFSheet sheet, CellRangeAddress cellRangeAddress) {
  262. super(sheet, cellRangeAddress);
  263. }
  264. @Override
  265. public String getFormula() {
  266. return getDataRangeReference();
  267. }
  268. private String formatCode;
  269. @Override
  270. public String getFormatCode() {
  271. return formatCode;
  272. }
  273. @Override
  274. public void setFormatCode(String formatCode) {
  275. this.formatCode = formatCode;
  276. }
  277. @Override
  278. public Double getPointAt(int index) {
  279. CellValue cellValue = getCellValueAt(index);
  280. if (cellValue != null && cellValue.getCellType() == CellType.NUMERIC) {
  281. return Double.valueOf(cellValue.getNumberValue());
  282. } else {
  283. return null;
  284. }
  285. }
  286. @Override
  287. public boolean isNumeric() {
  288. return true;
  289. }
  290. }
  291. private static class StringCellRangeDataSource extends AbstractCellRangeDataSource<String>
  292. implements XDDFCategoryDataSource {
  293. protected StringCellRangeDataSource(XSSFSheet sheet, CellRangeAddress cellRangeAddress) {
  294. super(sheet, cellRangeAddress);
  295. }
  296. @Override
  297. public String getFormula() {
  298. return getDataRangeReference();
  299. }
  300. @Override
  301. public String getPointAt(int index) {
  302. CellValue cellValue = getCellValueAt(index);
  303. if (cellValue != null && cellValue.getCellType() == CellType.STRING) {
  304. return cellValue.getStringValue();
  305. } else {
  306. return null;
  307. }
  308. }
  309. @Override
  310. public boolean isNumeric() {
  311. return false;
  312. }
  313. }
  314. }