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.

XDDFChartAxis.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 org.apache.poi.util.Beta;
  17. import org.apache.poi.util.Internal;
  18. import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
  19. import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
  20. import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
  21. import org.openxmlformats.schemas.drawingml.x2006.chart.CTLogBase;
  22. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
  23. import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
  24. import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
  25. import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickMark;
  26. import org.openxmlformats.schemas.drawingml.x2006.chart.CTUnsignedInt;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  28. /**
  29. * Base class for all axis types.
  30. */
  31. @Beta
  32. public abstract class XDDFChartAxis {
  33. protected abstract CTUnsignedInt getCTAxId();
  34. protected abstract CTAxPos getCTAxPos();
  35. protected abstract CTNumFmt getCTNumFmt();
  36. protected abstract CTScaling getCTScaling();
  37. protected abstract CTCrosses getCTCrosses();
  38. protected abstract CTBoolean getDelete();
  39. protected abstract CTTickMark getMajorCTTickMark();
  40. protected abstract CTTickMark getMinorCTTickMark();
  41. @Internal
  42. public abstract CTShapeProperties getMajorGridLines();
  43. @Internal
  44. public abstract CTShapeProperties getLine();
  45. /**
  46. * @return axis id
  47. */
  48. public long getId() {
  49. return getCTAxId().getVal();
  50. }
  51. /**
  52. * @return axis position
  53. */
  54. public AxisPosition getPosition() {
  55. return AxisPosition.valueOf(getCTAxPos().getVal());
  56. }
  57. /**
  58. * @param position
  59. * new axis position
  60. */
  61. public void setPosition(AxisPosition position) {
  62. getCTAxPos().setVal(position.underlying);
  63. }
  64. /**
  65. * Use this to check before retrieving a number format, as calling {@link #getNumberFormat()} may create a default
  66. * one if none exists.
  67. *
  68. * @return true if a number format element is defined, false if not
  69. */
  70. public abstract boolean hasNumberFormat();
  71. /**
  72. * @param format
  73. * axis number format
  74. */
  75. public void setNumberFormat(String format) {
  76. getCTNumFmt().setFormatCode(format);
  77. getCTNumFmt().setSourceLinked(true);
  78. }
  79. /**
  80. * @return axis number format
  81. */
  82. public String getNumberFormat() {
  83. return getCTNumFmt().getFormatCode();
  84. }
  85. /**
  86. * @return true if log base is defined, false otherwise
  87. */
  88. public boolean isSetLogBase() {
  89. return getCTScaling().isSetLogBase();
  90. }
  91. private static final double MIN_LOG_BASE = 2.0;
  92. private static final double MAX_LOG_BASE = 1000.0;
  93. /**
  94. * @param logBase
  95. * a number between 2 and 1000 (inclusive)
  96. * @throws IllegalArgumentException
  97. * if log base not within allowed range
  98. */
  99. public void setLogBase(double logBase) {
  100. if (logBase < MIN_LOG_BASE || MAX_LOG_BASE < logBase) {
  101. throw new IllegalArgumentException("Axis log base must be between 2 and 1000 (inclusive), got: " + logBase);
  102. }
  103. CTScaling scaling = getCTScaling();
  104. if (scaling.isSetLogBase()) {
  105. scaling.getLogBase().setVal(logBase);
  106. } else {
  107. scaling.addNewLogBase().setVal(logBase);
  108. }
  109. }
  110. /**
  111. * @return axis log base or 0.0 if not set
  112. */
  113. public double getLogBase() {
  114. CTLogBase logBase = getCTScaling().getLogBase();
  115. if (logBase != null) {
  116. return logBase.getVal();
  117. }
  118. return 0.0;
  119. }
  120. /**
  121. * @return true if minimum value is defined, false otherwise
  122. */
  123. public boolean isSetMinimum() {
  124. return getCTScaling().isSetMin();
  125. }
  126. /**
  127. * @param min
  128. * axis minimum
  129. */
  130. public void setMinimum(double min) {
  131. CTScaling scaling = getCTScaling();
  132. if (scaling.isSetMin()) {
  133. scaling.getMin().setVal(min);
  134. } else {
  135. scaling.addNewMin().setVal(min);
  136. }
  137. }
  138. /**
  139. * @return axis minimum or 0.0 if not set
  140. */
  141. public double getMinimum() {
  142. CTScaling scaling = getCTScaling();
  143. if (scaling.isSetMin()) {
  144. return scaling.getMin().getVal();
  145. } else {
  146. return 0.0;
  147. }
  148. }
  149. /**
  150. * @return true if maximum value is defined, false otherwise
  151. */
  152. public boolean isSetMaximum() {
  153. return getCTScaling().isSetMax();
  154. }
  155. /**
  156. * @param max
  157. * axis maximum
  158. */
  159. public void setMaximum(double max) {
  160. CTScaling scaling = getCTScaling();
  161. if (scaling.isSetMax()) {
  162. scaling.getMax().setVal(max);
  163. } else {
  164. scaling.addNewMax().setVal(max);
  165. }
  166. }
  167. /**
  168. * @return axis maximum or 0.0 if not set
  169. */
  170. public double getMaximum() {
  171. CTScaling scaling = getCTScaling();
  172. if (scaling.isSetMax()) {
  173. return scaling.getMax().getVal();
  174. } else {
  175. return 0.0;
  176. }
  177. }
  178. /**
  179. * @return axis orientation
  180. */
  181. public AxisOrientation getOrientation() {
  182. return AxisOrientation.valueOf(getCTScaling().getOrientation().getVal());
  183. }
  184. /**
  185. * @param orientation
  186. * axis orientation
  187. */
  188. public void setOrientation(AxisOrientation orientation) {
  189. CTScaling scaling = getCTScaling();
  190. if (scaling.isSetOrientation()) {
  191. scaling.getOrientation().setVal(orientation.underlying);
  192. } else {
  193. scaling.addNewOrientation().setVal(orientation.underlying);
  194. }
  195. }
  196. /**
  197. * @return axis cross type
  198. */
  199. public AxisCrosses getCrosses() {
  200. return AxisCrosses.valueOf(getCTCrosses().getVal());
  201. }
  202. /**
  203. * @param crosses
  204. * axis cross type
  205. */
  206. public void setCrosses(AxisCrosses crosses) {
  207. getCTCrosses().setVal(crosses.underlying);
  208. }
  209. /**
  210. * Declare this axis cross another axis.
  211. *
  212. * @param axis
  213. * that this axis should cross
  214. */
  215. public abstract void crossAxis(XDDFChartAxis axis);
  216. /**
  217. * @return visibility of the axis.
  218. */
  219. public boolean isVisible() {
  220. return !getDelete().getVal();
  221. }
  222. /**
  223. * @param value
  224. * visibility of the axis.
  225. */
  226. public void setVisible(boolean value) {
  227. getDelete().setVal(!value);
  228. }
  229. /**
  230. * @return major tick mark.
  231. */
  232. public AxisTickMark getMajorTickMark() {
  233. return AxisTickMark.valueOf(getMajorCTTickMark().getVal());
  234. }
  235. /**
  236. * @param tickMark
  237. * major tick mark type.
  238. */
  239. public void setMajorTickMark(AxisTickMark tickMark) {
  240. getMajorCTTickMark().setVal(tickMark.underlying);
  241. }
  242. /**
  243. * @return minor tick mark.
  244. */
  245. public AxisTickMark getMinorTickMark() {
  246. return AxisTickMark.valueOf(getMinorCTTickMark().getVal());
  247. }
  248. /**
  249. * @param tickMark
  250. * minor tick mark type.
  251. */
  252. public void setMinorTickMark(AxisTickMark tickMark) {
  253. getMinorCTTickMark().setVal(tickMark.underlying);
  254. }
  255. protected long getNextAxId(CTPlotArea plotArea) {
  256. long totalAxisCount = plotArea.sizeOfValAxArray() + plotArea.sizeOfCatAxArray() + plotArea.sizeOfDateAxArray()
  257. + plotArea.sizeOfSerAxArray();
  258. return totalAxisCount;
  259. }
  260. }