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.

XSSFChartAxis.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 org.apache.poi.ss.usermodel.charts.AxisCrosses;
  17. import org.apache.poi.ss.usermodel.charts.AxisOrientation;
  18. import org.apache.poi.ss.usermodel.charts.AxisPosition;
  19. import org.apache.poi.ss.usermodel.charts.AxisTickMark;
  20. import org.apache.poi.ss.usermodel.charts.ChartAxis;
  21. import org.apache.poi.util.Internal;
  22. import org.apache.poi.util.Removal;
  23. import org.apache.poi.xddf.usermodel.chart.XDDFChartAxis;
  24. import org.apache.poi.xssf.usermodel.XSSFChart;
  25. import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
  26. import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
  27. import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartLines;
  28. import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
  29. import org.openxmlformats.schemas.drawingml.x2006.chart.CTLogBase;
  30. import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
  31. import org.openxmlformats.schemas.drawingml.x2006.chart.CTOrientation;
  32. import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
  33. import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickMark;
  34. import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos;
  35. import org.openxmlformats.schemas.drawingml.x2006.chart.STCrosses;
  36. import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation;
  37. import org.openxmlformats.schemas.drawingml.x2006.chart.STTickMark;
  38. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  39. /**
  40. * Base class for all axis types.
  41. *
  42. * @deprecated use {@link XDDFChartAxis} instead
  43. */
  44. @Deprecated
  45. @Removal(version="4.2")
  46. public abstract class XSSFChartAxis implements ChartAxis {
  47. protected XSSFChart chart;
  48. private static final double MIN_LOG_BASE = 2.0;
  49. private static final double MAX_LOG_BASE = 1000.0;
  50. protected XSSFChartAxis(XSSFChart chart) {
  51. this.chart = chart;
  52. }
  53. @Override
  54. public AxisPosition getPosition() {
  55. return toAxisPosition(getCTAxPos());
  56. }
  57. @Override
  58. public void setPosition(AxisPosition position) {
  59. getCTAxPos().setVal(fromAxisPosition(position));
  60. }
  61. @Override
  62. public void setNumberFormat(String format) {
  63. getCTNumFmt().setFormatCode(format);
  64. getCTNumFmt().setSourceLinked(true);
  65. }
  66. @Override
  67. public String getNumberFormat() {
  68. return getCTNumFmt().getFormatCode();
  69. }
  70. @Override
  71. public boolean isSetLogBase() {
  72. return getCTScaling().isSetLogBase();
  73. }
  74. @Override
  75. public void setLogBase(double logBase) {
  76. if (logBase < MIN_LOG_BASE ||
  77. MAX_LOG_BASE < logBase) {
  78. throw new IllegalArgumentException("Axis log base must be between 2 and 1000 (inclusive), got: " + logBase);
  79. }
  80. CTScaling scaling = getCTScaling();
  81. if (scaling.isSetLogBase()) {
  82. scaling.getLogBase().setVal(logBase);
  83. } else {
  84. scaling.addNewLogBase().setVal(logBase);
  85. }
  86. }
  87. @Override
  88. public double getLogBase() {
  89. CTLogBase logBase = getCTScaling().getLogBase();
  90. if (logBase != null) {
  91. return logBase.getVal();
  92. }
  93. return 0.0;
  94. }
  95. @Override
  96. public boolean isSetMinimum() {
  97. return getCTScaling().isSetMin();
  98. }
  99. @Override
  100. public void setMinimum(double min) {
  101. CTScaling scaling = getCTScaling();
  102. if (scaling.isSetMin()) {
  103. scaling.getMin().setVal(min);
  104. } else {
  105. scaling.addNewMin().setVal(min);
  106. }
  107. }
  108. @Override
  109. public double getMinimum() {
  110. CTScaling scaling = getCTScaling();
  111. if (scaling.isSetMin()) {
  112. return scaling.getMin().getVal();
  113. } else {
  114. return 0.0;
  115. }
  116. }
  117. @Override
  118. public boolean isSetMaximum() {
  119. return getCTScaling().isSetMax();
  120. }
  121. @Override
  122. public void setMaximum(double max) {
  123. CTScaling scaling = getCTScaling();
  124. if (scaling.isSetMax()) {
  125. scaling.getMax().setVal(max);
  126. } else {
  127. scaling.addNewMax().setVal(max);
  128. }
  129. }
  130. @Override
  131. public double getMaximum() {
  132. CTScaling scaling = getCTScaling();
  133. if (scaling.isSetMax()) {
  134. return scaling.getMax().getVal();
  135. } else {
  136. return 0.0;
  137. }
  138. }
  139. @Override
  140. public AxisOrientation getOrientation() {
  141. return toAxisOrientation(getCTScaling().getOrientation());
  142. }
  143. @Override
  144. public void setOrientation(AxisOrientation orientation) {
  145. CTScaling scaling = getCTScaling();
  146. STOrientation.Enum stOrientation = fromAxisOrientation(orientation);
  147. if (scaling.isSetOrientation()) {
  148. scaling.getOrientation().setVal(stOrientation);
  149. } else {
  150. getCTScaling().addNewOrientation().setVal(stOrientation);
  151. }
  152. }
  153. @Override
  154. public AxisCrosses getCrosses() {
  155. return toAxisCrosses(getCTCrosses());
  156. }
  157. @Override
  158. public void setCrosses(AxisCrosses crosses) {
  159. getCTCrosses().setVal(fromAxisCrosses(crosses));
  160. }
  161. @Override
  162. public boolean isVisible() {
  163. return !getDelete().getVal();
  164. }
  165. @Override
  166. public void setVisible(boolean value) {
  167. getDelete().setVal(!value);
  168. }
  169. @Override
  170. public AxisTickMark getMajorTickMark() {
  171. return toAxisTickMark(getMajorCTTickMark());
  172. }
  173. @Override
  174. public void setMajorTickMark(AxisTickMark tickMark) {
  175. getMajorCTTickMark().setVal(fromAxisTickMark(tickMark));
  176. }
  177. @Override
  178. public AxisTickMark getMinorTickMark() {
  179. return toAxisTickMark(getMinorCTTickMark());
  180. }
  181. @Override
  182. public void setMinorTickMark(AxisTickMark tickMark) {
  183. getMinorCTTickMark().setVal(fromAxisTickMark(tickMark));
  184. }
  185. protected abstract CTAxPos getCTAxPos();
  186. protected abstract CTNumFmt getCTNumFmt();
  187. protected abstract CTScaling getCTScaling();
  188. protected abstract CTCrosses getCTCrosses();
  189. protected abstract CTBoolean getDelete();
  190. protected abstract CTTickMark getMajorCTTickMark();
  191. protected abstract CTTickMark getMinorCTTickMark();
  192. @Internal public abstract CTChartLines getMajorGridLines();
  193. @Internal public abstract CTShapeProperties getLine();
  194. private static STOrientation.Enum fromAxisOrientation(AxisOrientation orientation) {
  195. switch (orientation) {
  196. case MIN_MAX: return STOrientation.MIN_MAX;
  197. case MAX_MIN: return STOrientation.MAX_MIN;
  198. default:
  199. throw new IllegalArgumentException();
  200. }
  201. }
  202. private static AxisOrientation toAxisOrientation(CTOrientation ctOrientation) {
  203. switch (ctOrientation.getVal().intValue()) {
  204. case STOrientation.INT_MIN_MAX: return AxisOrientation.MIN_MAX;
  205. case STOrientation.INT_MAX_MIN: return AxisOrientation.MAX_MIN;
  206. default:
  207. throw new IllegalArgumentException();
  208. }
  209. }
  210. private static STCrosses.Enum fromAxisCrosses(AxisCrosses crosses) {
  211. switch (crosses) {
  212. case AUTO_ZERO: return STCrosses.AUTO_ZERO;
  213. case MIN: return STCrosses.MIN;
  214. case MAX: return STCrosses.MAX;
  215. default:
  216. throw new IllegalArgumentException();
  217. }
  218. }
  219. private static AxisCrosses toAxisCrosses(CTCrosses ctCrosses) {
  220. switch (ctCrosses.getVal().intValue()) {
  221. case STCrosses.INT_AUTO_ZERO: return AxisCrosses.AUTO_ZERO;
  222. case STCrosses.INT_MAX: return AxisCrosses.MAX;
  223. case STCrosses.INT_MIN: return AxisCrosses.MIN;
  224. default:
  225. throw new IllegalArgumentException();
  226. }
  227. }
  228. private static STAxPos.Enum fromAxisPosition(AxisPosition position) {
  229. switch (position) {
  230. case BOTTOM: return STAxPos.B;
  231. case LEFT: return STAxPos.L;
  232. case RIGHT: return STAxPos.R;
  233. case TOP: return STAxPos.T;
  234. default:
  235. throw new IllegalArgumentException();
  236. }
  237. }
  238. private static AxisPosition toAxisPosition(CTAxPos ctAxPos) {
  239. switch (ctAxPos.getVal().intValue()) {
  240. case STAxPos.INT_B: return AxisPosition.BOTTOM;
  241. case STAxPos.INT_L: return AxisPosition.LEFT;
  242. case STAxPos.INT_R: return AxisPosition.RIGHT;
  243. case STAxPos.INT_T: return AxisPosition.TOP;
  244. default: return AxisPosition.BOTTOM;
  245. }
  246. }
  247. private static STTickMark.Enum fromAxisTickMark(AxisTickMark tickMark) {
  248. switch (tickMark) {
  249. case NONE: return STTickMark.NONE;
  250. case IN: return STTickMark.IN;
  251. case OUT: return STTickMark.OUT;
  252. case CROSS: return STTickMark.CROSS;
  253. default:
  254. throw new IllegalArgumentException("Unknown AxisTickMark: " + tickMark);
  255. }
  256. }
  257. private static AxisTickMark toAxisTickMark(CTTickMark ctTickMark) {
  258. switch (ctTickMark.getVal().intValue()) {
  259. case STTickMark.INT_NONE: return AxisTickMark.NONE;
  260. case STTickMark.INT_IN: return AxisTickMark.IN;
  261. case STTickMark.INT_OUT: return AxisTickMark.OUT;
  262. case STTickMark.INT_CROSS: return AxisTickMark.CROSS;
  263. default: return AxisTickMark.CROSS;
  264. }
  265. }
  266. }