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.

XDDFChartData.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.xddf.usermodel.chart;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.apache.poi.ss.util.CellReference;
  21. import org.apache.poi.util.Beta;
  22. import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
  23. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;
  24. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
  25. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;
  26. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal;
  27. import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;
  28. import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData;
  29. import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;
  30. import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal;
  31. import org.openxmlformats.schemas.drawingml.x2006.chart.CTUnsignedInt;
  32. /**
  33. * Base of all XDDF Chart Data
  34. */
  35. @Beta
  36. public abstract class XDDFChartData {
  37. protected List<Series> series;
  38. private XDDFCategoryAxis categoryAxis;
  39. private List<XDDFValueAxis> valueAxes;
  40. protected XDDFChartData() {
  41. this.series = new ArrayList<Series>();
  42. }
  43. protected void defineAxes(CTUnsignedInt[] axes, Map<Long, XDDFChartAxis> categories,
  44. Map<Long, XDDFValueAxis> values) {
  45. List<XDDFValueAxis> list = new ArrayList<XDDFValueAxis>(axes.length);
  46. for (CTUnsignedInt axe : axes) {
  47. Long axisId = axe.getVal();
  48. XDDFChartAxis category = categories.get(axisId);
  49. if (category == null) {
  50. XDDFValueAxis axis = values.get(axisId);
  51. if (axis != null) {
  52. list.add(axis);
  53. }
  54. } else if (category instanceof XDDFCategoryAxis) {
  55. this.categoryAxis = (XDDFCategoryAxis) category;
  56. }
  57. }
  58. this.valueAxes = Collections.unmodifiableList(list);
  59. }
  60. public XDDFCategoryAxis getCategoryAxis() {
  61. return categoryAxis;
  62. }
  63. public List<XDDFValueAxis> getValueAxes() {
  64. return valueAxes;
  65. }
  66. public List<Series> getSeries() {
  67. return series;
  68. }
  69. public abstract void setVaryColors(boolean varyColors);
  70. public abstract XDDFChartData.Series addSeries(XDDFDataSource<?> category,
  71. XDDFNumericalDataSource<? extends Number> values);
  72. public abstract class Series {
  73. protected abstract CTSerTx getSeriesText();
  74. public abstract void setShowLeaderLines(boolean showLeaderLines);
  75. protected XDDFDataSource<?> categoryData;
  76. protected XDDFNumericalDataSource<? extends Number> valuesData;
  77. protected abstract CTAxDataSource getAxDS();
  78. protected abstract CTNumDataSource getNumDS();
  79. protected Series(XDDFDataSource<?> category, XDDFNumericalDataSource<? extends Number> values) {
  80. replaceData(category, values);
  81. }
  82. public void replaceData(XDDFDataSource<?> category, XDDFNumericalDataSource<? extends Number> values) {
  83. if (category == null || values == null) {
  84. throw new IllegalStateException("Category and values must be defined before filling chart data.");
  85. }
  86. int numOfPoints = category.getPointCount();
  87. if (numOfPoints != values.getPointCount()) {
  88. throw new IllegalStateException("Category and values must have the same point count.");
  89. }
  90. this.categoryData = category;
  91. this.valuesData = values;
  92. }
  93. public void setTitle(String title, CellReference titleRef) {
  94. if (titleRef == null) {
  95. getSeriesText().setV(title);
  96. } else {
  97. CTStrRef ref;
  98. if (getSeriesText().isSetStrRef()) {
  99. ref = getSeriesText().getStrRef();
  100. } else {
  101. ref = getSeriesText().addNewStrRef();
  102. }
  103. CTStrData cache;
  104. if (ref.isSetStrCache()) {
  105. cache = ref.getStrCache();
  106. } else {
  107. cache = ref.addNewStrCache();
  108. }
  109. cache.getPtArray(0).setV(title);
  110. ref.setF(titleRef.formatAsString());
  111. }
  112. }
  113. public XDDFDataSource<?> getCategoryData() {
  114. return categoryData;
  115. }
  116. public XDDFNumericalDataSource<? extends Number> getValuesData() {
  117. return valuesData;
  118. }
  119. public void plot() {
  120. int numOfPoints = categoryData.getPointCount();
  121. if (categoryData.isNumeric()) {
  122. CTNumData cache = retrieveNumCache(getAxDS(), categoryData);
  123. fillNumCache(cache, numOfPoints, (XDDFNumericalDataSource<?>) categoryData);
  124. } else {
  125. CTStrData cache = retrieveStrCache(getAxDS(), categoryData);
  126. fillStringCache(cache, numOfPoints, categoryData);
  127. }
  128. CTNumData cache = retrieveNumCache(getNumDS(), valuesData);
  129. fillNumCache(cache, numOfPoints, valuesData);
  130. }
  131. private CTNumData retrieveNumCache(final CTAxDataSource axDataSource, XDDFDataSource<?> data) {
  132. CTNumData numCache;
  133. if (data.isReference()) {
  134. CTNumRef numRef;
  135. if (axDataSource.isSetNumRef()) {
  136. numRef = axDataSource.getNumRef();
  137. } else {
  138. numRef = axDataSource.addNewNumRef();
  139. }
  140. if (numRef.isSetNumCache()) {
  141. numCache = numRef.getNumCache();
  142. } else {
  143. numCache = numRef.addNewNumCache();
  144. }
  145. numRef.setF(data.getDataRangeReference());
  146. if (axDataSource.isSetNumLit()) {
  147. axDataSource.unsetNumLit();
  148. }
  149. } else {
  150. if (axDataSource.isSetNumLit()) {
  151. numCache = axDataSource.getNumLit();
  152. } else {
  153. numCache = axDataSource.addNewNumLit();
  154. }
  155. if (axDataSource.isSetNumRef()) {
  156. axDataSource.unsetNumRef();
  157. }
  158. }
  159. return numCache;
  160. }
  161. private CTStrData retrieveStrCache(final CTAxDataSource axDataSource, XDDFDataSource<?> data) {
  162. CTStrData strCache;
  163. if (data.isReference()) {
  164. CTStrRef strRef;
  165. if (axDataSource.isSetStrRef()) {
  166. strRef = axDataSource.getStrRef();
  167. } else {
  168. strRef = axDataSource.addNewStrRef();
  169. }
  170. if (strRef.isSetStrCache()) {
  171. strCache = strRef.getStrCache();
  172. } else {
  173. strCache = strRef.addNewStrCache();
  174. }
  175. strRef.setF(data.getDataRangeReference());
  176. if (axDataSource.isSetStrLit()) {
  177. axDataSource.unsetStrLit();
  178. }
  179. } else {
  180. if (axDataSource.isSetStrLit()) {
  181. strCache = axDataSource.getStrLit();
  182. } else {
  183. strCache = axDataSource.addNewStrLit();
  184. }
  185. if (axDataSource.isSetStrRef()) {
  186. axDataSource.unsetStrRef();
  187. }
  188. }
  189. return strCache;
  190. }
  191. private CTNumData retrieveNumCache(final CTNumDataSource numDataSource, XDDFDataSource<?> data) {
  192. CTNumData numCache;
  193. if (data.isReference()) {
  194. CTNumRef numRef;
  195. if (numDataSource.isSetNumRef()) {
  196. numRef = numDataSource.getNumRef();
  197. } else {
  198. numRef = numDataSource.addNewNumRef();
  199. }
  200. if (numRef.isSetNumCache()) {
  201. numCache = numRef.getNumCache();
  202. } else {
  203. numCache = numRef.addNewNumCache();
  204. }
  205. numRef.setF(data.getDataRangeReference());
  206. if (numDataSource.isSetNumLit()) {
  207. numDataSource.unsetNumLit();
  208. }
  209. } else {
  210. if (numDataSource.isSetNumLit()) {
  211. numCache = numDataSource.getNumLit();
  212. } else {
  213. numCache = numDataSource.addNewNumLit();
  214. }
  215. if (numDataSource.isSetNumRef()) {
  216. numDataSource.unsetNumRef();
  217. }
  218. }
  219. return numCache;
  220. }
  221. private void fillStringCache(CTStrData cache, int numOfPoints, XDDFDataSource<?> data) {
  222. cache.setPtArray(null); // unset old values
  223. if (cache.isSetPtCount()) {
  224. cache.getPtCount().setVal(numOfPoints);
  225. } else {
  226. cache.addNewPtCount().setVal(numOfPoints);
  227. }
  228. for (int i = 0; i < numOfPoints; ++i) {
  229. String value = data.getPointAt(i).toString();
  230. if (value != null) {
  231. CTStrVal ctStrVal = cache.addNewPt();
  232. ctStrVal.setIdx(i);
  233. ctStrVal.setV(value);
  234. }
  235. }
  236. }
  237. private void fillNumCache(CTNumData cache, int numOfPoints, XDDFNumericalDataSource<?> data) {
  238. String formatCode = data.getFormatCode();
  239. if (formatCode == null) {
  240. if (cache.isSetFormatCode()) {
  241. cache.unsetFormatCode();
  242. }
  243. } else {
  244. cache.setFormatCode(formatCode);
  245. }
  246. cache.setPtArray(null); // unset old values
  247. if (cache.isSetPtCount()) {
  248. cache.getPtCount().setVal(numOfPoints);
  249. } else {
  250. cache.addNewPtCount().setVal(numOfPoints);
  251. }
  252. for (int i = 0; i < numOfPoints; ++i) {
  253. Object value = data.getPointAt(i);
  254. if (value != null) {
  255. CTNumVal ctNumVal = cache.addNewPt();
  256. ctNumVal.setIdx(i);
  257. ctNumVal.setV(value.toString());
  258. }
  259. }
  260. }
  261. }
  262. }