Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

XDDFChartData.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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.Locale;
  20. import java.util.Map;
  21. import org.apache.poi.ss.util.CellReference;
  22. import org.apache.poi.util.Beta;
  23. import org.apache.poi.util.Internal;
  24. import org.apache.poi.util.Removal;
  25. import org.apache.poi.xddf.usermodel.XDDFFillProperties;
  26. import org.apache.poi.xddf.usermodel.XDDFLineProperties;
  27. import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
  28. import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
  29. import org.openxmlformats.schemas.drawingml.x2006.chart.CTDPt;
  30. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;
  31. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
  32. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;
  33. import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;
  34. import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData;
  35. import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;
  36. import org.openxmlformats.schemas.drawingml.x2006.chart.CTUnsignedInt;
  37. /**
  38. * Base of all XDDF Chart Data
  39. */
  40. @Beta
  41. public abstract class XDDFChartData {
  42. protected XDDFChart parent;
  43. protected List<Series> series;
  44. private XDDFCategoryAxis categoryAxis;
  45. private List<XDDFValueAxis> valueAxes;
  46. protected XDDFChartData(XDDFChart chart) {
  47. this.parent = chart;
  48. this.series = new ArrayList<>();
  49. }
  50. protected void defineAxes(CTUnsignedInt[] axes, Map<Long, XDDFChartAxis> categories,
  51. Map<Long, XDDFValueAxis> values) {
  52. List<XDDFValueAxis> list = new ArrayList<>(axes.length);
  53. for (CTUnsignedInt axe : axes) {
  54. Long axisId = axe.getVal();
  55. XDDFChartAxis category = categories.get(axisId);
  56. if (category == null) {
  57. XDDFValueAxis axis = values.get(axisId);
  58. if (axis != null) {
  59. list.add(axis);
  60. }
  61. } else if (category instanceof XDDFCategoryAxis) {
  62. this.categoryAxis = (XDDFCategoryAxis) category;
  63. }
  64. }
  65. this.valueAxes = Collections.unmodifiableList(list);
  66. }
  67. public XDDFCategoryAxis getCategoryAxis() {
  68. return categoryAxis;
  69. }
  70. public List<XDDFValueAxis> getValueAxes() {
  71. return valueAxes;
  72. }
  73. /**
  74. * Calls to {@code getSeries().add(series)} or to {@code getSeries().remove(series)}
  75. * may corrupt the workbook.
  76. *
  77. * <p>
  78. * Instead, use the following methods:
  79. * <ul>
  80. * <li>{@link #getSeriesCount()}</li>
  81. * <li>{@link #getSeries(int)}</li>
  82. * <li>{@link #addSeries(XDDFDataSource,XDDFNumericalDataSource)}</li>
  83. * <li>{@link #removeSeries(int)}</li>
  84. * </ul>
  85. *
  86. * @deprecated since POI 4.1.1
  87. */
  88. @Deprecated
  89. @Removal(version = "5.3")
  90. public List<Series> getSeries() {
  91. return Collections.unmodifiableList(series);
  92. }
  93. public final int getSeriesCount() {
  94. return series.size();
  95. }
  96. public final Series getSeries(int n) {
  97. return series.get(n);
  98. }
  99. public final void removeSeries(int n) {
  100. final String procName = "removeSeries";
  101. if (n < 0 || series.size() <= n) {
  102. throw new IllegalArgumentException(String.format(Locale.ROOT, "%s(%d): illegal index", procName, n));
  103. }
  104. series.remove(n);
  105. removeCTSeries(n);
  106. }
  107. /**
  108. * This method should be implemented in every class that extends {@code XDDFChartData}.
  109. * <p>
  110. * A typical implementation would be
  111. *
  112. * <pre>{@code
  113. * protected void removeCTSeries(int n) {
  114. * chart.removeSer(n);
  115. * }
  116. * }</pre>
  117. */
  118. @Internal
  119. protected abstract void removeCTSeries(int n);
  120. public abstract void setVaryColors(Boolean varyColors);
  121. public abstract XDDFChartData.Series addSeries(XDDFDataSource<?> category,
  122. XDDFNumericalDataSource<? extends Number> values);
  123. public abstract class Series {
  124. protected abstract CTSerTx getSeriesText();
  125. public abstract void setShowLeaderLines(boolean showLeaderLines);
  126. public abstract XDDFShapeProperties getShapeProperties();
  127. public abstract void setShapeProperties(XDDFShapeProperties properties);
  128. protected XDDFDataSource<?> categoryData;
  129. protected XDDFNumericalDataSource<? extends Number> valuesData;
  130. protected abstract CTAxDataSource getAxDS();
  131. protected abstract CTNumDataSource getNumDS();
  132. protected abstract void setIndex(long index);
  133. protected abstract void setOrder(long order);
  134. protected abstract List<CTDPt> getDPtList();
  135. protected Series(XDDFDataSource<?> category, XDDFNumericalDataSource<? extends Number> values) {
  136. replaceData(category, values);
  137. }
  138. public void replaceData(XDDFDataSource<?> category, XDDFNumericalDataSource<? extends Number> values) {
  139. if (categoryData != null && values != null) {
  140. int numOfPoints = category.getPointCount();
  141. if (numOfPoints != values.getPointCount()) {
  142. throw new IllegalStateException("Category and values must have the same point count.");
  143. }
  144. }
  145. this.categoryData = category;
  146. this.valuesData = values;
  147. }
  148. public void setTitle(String title, CellReference titleRef) {
  149. if (titleRef == null) {
  150. getSeriesText().setV(title);
  151. } else {
  152. CTStrRef ref;
  153. if (getSeriesText().isSetStrRef()) {
  154. ref = getSeriesText().getStrRef();
  155. } else {
  156. ref = getSeriesText().addNewStrRef();
  157. }
  158. ref.setF(titleRef.formatAsString());
  159. if (title != null) {
  160. CTStrData cache;
  161. if (ref.isSetStrCache()) {
  162. cache = ref.getStrCache();
  163. } else {
  164. cache = ref.addNewStrCache();
  165. }
  166. if (cache.sizeOfPtArray() < 1) {
  167. cache.addNewPtCount().setVal(1);
  168. cache.addNewPt().setIdx(0);
  169. }
  170. cache.getPtArray(0).setV(title);
  171. }
  172. }
  173. }
  174. public XDDFDataSource<?> getCategoryData() {
  175. return categoryData;
  176. }
  177. public XDDFNumericalDataSource<? extends Number> getValuesData() {
  178. return valuesData;
  179. }
  180. public void plot() {
  181. if (categoryData != null) {
  182. if (categoryData.isNumeric()) {
  183. CTNumData cache = retrieveNumCache(getAxDS(), categoryData);
  184. categoryData.fillNumericalCache(cache);
  185. } else {
  186. CTStrData cache = retrieveStrCache(getAxDS(), categoryData);
  187. categoryData.fillStringCache(cache);
  188. }
  189. }
  190. if (valuesData != null) {
  191. CTNumData cache = retrieveNumCache(getNumDS(), valuesData);
  192. valuesData.fillNumericalCache(cache);
  193. }
  194. }
  195. /**
  196. * @param fill
  197. * fill property for the shape representing the series.
  198. * @since POI 4.1.1
  199. */
  200. public void setFillProperties(XDDFFillProperties fill) {
  201. XDDFShapeProperties properties = getShapeProperties();
  202. if (properties == null) {
  203. properties = new XDDFShapeProperties();
  204. }
  205. properties.setFillProperties(fill);
  206. setShapeProperties(properties);
  207. }
  208. /**
  209. * @param line
  210. * line property for the shape representing the series.
  211. * @since POI 4.1.1
  212. */
  213. public void setLineProperties(XDDFLineProperties line) {
  214. XDDFShapeProperties properties = getShapeProperties();
  215. if (properties == null) {
  216. properties = new XDDFShapeProperties();
  217. }
  218. properties.setLineProperties(line);
  219. setShapeProperties(properties);
  220. }
  221. /**
  222. * If a data point definition with the given {@code index} exists, then remove it.
  223. * Otherwise do nothing.
  224. *
  225. * @param index
  226. * data point index.
  227. * @since POI 5.1.0
  228. */
  229. public void clearDataPoint(long index) {
  230. List<CTDPt> points = getDPtList();
  231. for (int i = 0; i < points.size(); i++) {
  232. if (points.get(i).getIdx().getVal() == index) {
  233. points.remove(i);
  234. break;
  235. }
  236. }
  237. }
  238. /**
  239. * If a data point definition with the given {@code index} exists, then return it.
  240. * Otherwise create a new data point definition and return it.
  241. *
  242. * @param index
  243. * data point index.
  244. * @return
  245. * the data point with the given {@code index}.
  246. * @since POI 5.1.0
  247. */
  248. public XDDFDataPoint getDataPoint(long index) {
  249. List<CTDPt> points = getDPtList();
  250. for (int i = 0; i < points.size(); i++) {
  251. if (points.get(i).getIdx().getVal() == index) {
  252. return new XDDFDataPoint(points.get(i));
  253. }
  254. if (points.get(i).getIdx().getVal() > index) {
  255. points.add(i, CTDPt.Factory.newInstance());
  256. CTDPt point = points.get(i);
  257. point.addNewIdx().setVal(index);
  258. return new XDDFDataPoint(point);
  259. }
  260. }
  261. points.add(CTDPt.Factory.newInstance());
  262. CTDPt point = points.get(points.size() - 1);
  263. point.addNewIdx().setVal(index);
  264. return new XDDFDataPoint(point);
  265. }
  266. private CTNumData retrieveNumCache(final CTAxDataSource axDataSource, XDDFDataSource<?> data) {
  267. CTNumData numCache;
  268. if (data.isReference()) {
  269. CTNumRef numRef;
  270. if (axDataSource.isSetNumRef()) {
  271. numRef = axDataSource.getNumRef();
  272. } else {
  273. numRef = axDataSource.addNewNumRef();
  274. }
  275. if (numRef.isSetNumCache()) {
  276. numCache = numRef.getNumCache();
  277. } else {
  278. numCache = numRef.addNewNumCache();
  279. }
  280. numRef.setF(data.getDataRangeReference());
  281. if (axDataSource.isSetNumLit()) {
  282. axDataSource.unsetNumLit();
  283. }
  284. } else {
  285. if (axDataSource.isSetNumLit()) {
  286. numCache = axDataSource.getNumLit();
  287. } else {
  288. numCache = axDataSource.addNewNumLit();
  289. }
  290. if (axDataSource.isSetNumRef()) {
  291. axDataSource.unsetNumRef();
  292. }
  293. }
  294. return numCache;
  295. }
  296. private CTStrData retrieveStrCache(final CTAxDataSource axDataSource, XDDFDataSource<?> data) {
  297. CTStrData strCache;
  298. if (data.isReference()) {
  299. CTStrRef strRef;
  300. if (axDataSource.isSetStrRef()) {
  301. strRef = axDataSource.getStrRef();
  302. } else {
  303. strRef = axDataSource.addNewStrRef();
  304. }
  305. if (strRef.isSetStrCache()) {
  306. strCache = strRef.getStrCache();
  307. } else {
  308. strCache = strRef.addNewStrCache();
  309. }
  310. strRef.setF(data.getDataRangeReference());
  311. if (axDataSource.isSetStrLit()) {
  312. axDataSource.unsetStrLit();
  313. }
  314. } else {
  315. if (axDataSource.isSetStrLit()) {
  316. strCache = axDataSource.getStrLit();
  317. } else {
  318. strCache = axDataSource.addNewStrLit();
  319. }
  320. if (axDataSource.isSetStrRef()) {
  321. axDataSource.unsetStrRef();
  322. }
  323. }
  324. return strCache;
  325. }
  326. private CTNumData retrieveNumCache(final CTNumDataSource numDataSource, XDDFDataSource<?> data) {
  327. CTNumData numCache;
  328. if (data.isReference()) {
  329. CTNumRef numRef;
  330. if (numDataSource.isSetNumRef()) {
  331. numRef = numDataSource.getNumRef();
  332. } else {
  333. numRef = numDataSource.addNewNumRef();
  334. }
  335. if (numRef.isSetNumCache()) {
  336. numCache = numRef.getNumCache();
  337. } else {
  338. numCache = numRef.addNewNumCache();
  339. }
  340. numRef.setF(data.getDataRangeReference());
  341. if (numDataSource.isSetNumLit()) {
  342. numDataSource.unsetNumLit();
  343. }
  344. } else {
  345. if (numDataSource.isSetNumLit()) {
  346. numCache = numDataSource.getNumLit();
  347. } else {
  348. numCache = numDataSource.addNewNumLit();
  349. }
  350. if (numDataSource.isSetNumRef()) {
  351. numDataSource.unsetNumRef();
  352. }
  353. }
  354. return numCache;
  355. }
  356. }
  357. }