PageRenderTime 120ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/symja_android_library/matheclipse-external/src/main/java/tech/tablesaw/plotly/components/TickSettings.java

https://bitbucket.org/axelclk/symja_android_library
Java | 454 lines | 289 code | 65 blank | 100 comment | 14 complexity | f2e2a6a8c873b82c685433c5a0f8e224 MD5 | raw file
  1. package tech.tablesaw.plotly.components;
  2. import static tech.tablesaw.plotly.components.TickSettings.DisplayRules.ALL;
  3. import static tech.tablesaw.plotly.components.TickSettings.ExponentFormat.B;
  4. import com.google.common.base.Preconditions;
  5. import java.util.Map;
  6. import tech.tablesaw.plotly.Utils;
  7. public class TickSettings {
  8. /**
  9. * Sets the tick mode for this axis. If "auto", the number of ticks is set via `nticks`. If
  10. * "linear", the placement of the ticks is determined by a starting position `tick0` and a tick
  11. * step `dtick` If "array", the placement of the ticks is set via `tickvals` and the tick text is
  12. * `ticktext`.
  13. */
  14. public enum TickMode {
  15. AUTO("auto"),
  16. LINEAR("linear"),
  17. ARRAY("array");
  18. private final String value;
  19. TickMode(String value) {
  20. this.value = value;
  21. }
  22. @Override
  23. public String toString() {
  24. return value;
  25. }
  26. }
  27. /** Determines whether and where ticks are drawn */
  28. public enum TickPlacement {
  29. OUTSIDE("outside"),
  30. INSIDE("inside"),
  31. NONE("");
  32. private final String value;
  33. TickPlacement(String value) {
  34. this.value = value;
  35. }
  36. @Override
  37. public String toString() {
  38. return value;
  39. }
  40. }
  41. /** Controls the display of prefixes, suffixes, and exponents on ticks */
  42. public enum DisplayRules {
  43. ALL("outside"),
  44. FIRST("first"),
  45. LAST("last"),
  46. NONE("none");
  47. private final String value;
  48. DisplayRules(String value) {
  49. this.value = value;
  50. }
  51. @Override
  52. public String toString() {
  53. return value;
  54. }
  55. }
  56. /** Controls the display of prefixes on ticks */
  57. public enum Mirror {
  58. TRUE("true"),
  59. FALSE("false"),
  60. TICKS("ticks"),
  61. ALL("all"),
  62. ALL_TICKS("allticks");
  63. private final String value;
  64. Mirror(String value) {
  65. this.value = value;
  66. }
  67. @Override
  68. public String toString() {
  69. return value;
  70. }
  71. }
  72. /** Controls the display of prefixes on ticks */
  73. public enum ExponentFormat {
  74. NONE("none"),
  75. e("e"),
  76. E("E"),
  77. POWER("power"),
  78. SI("SI"),
  79. B("B");
  80. private final String value;
  81. ExponentFormat(String value) {
  82. this.value = value;
  83. }
  84. @Override
  85. public String toString() {
  86. return value;
  87. }
  88. }
  89. private final TickMode tickMode;
  90. private final int nTicks; // >= 0
  91. private final Object tick0;
  92. private final Object dTick;
  93. private final int
  94. length; // (number greater than or equal to 0) default: 5 Sets the tick length (in px).
  95. private final int
  96. width; // (number greater than or equal to 0) default: 1 , Sets the tick width (in px).
  97. private final String color; // (color) default: "#444" Sets the tick color.
  98. private final boolean
  99. showLabels; // (boolean) default: True Determines whether or not the tick labels are drawn.
  100. private final TickPlacement placement;
  101. private final Font tickFont;
  102. // the values and labels to use when TickMode is ARRAY
  103. private final Object[] tickText;
  104. private final double[] tickValues;
  105. private final Mirror mirror;
  106. private final int angle;
  107. private final String prefix;
  108. private final String suffix;
  109. private final boolean autoMargin;
  110. private final DisplayRules showPrefix;
  111. private final DisplayRules showSuffix;
  112. private final DisplayRules showExponent;
  113. private final ExponentFormat exponentFormat;
  114. private final boolean separateThousands;
  115. private TickSettings(TickSettingsBuilder builder) {
  116. this.tickMode = builder.tickMode;
  117. this.nTicks = builder.nTicks;
  118. this.color = builder.tickColor;
  119. this.length = builder.tickLength;
  120. this.width = builder.tickWidth;
  121. this.showLabels = builder.showTickLabels;
  122. this.tickFont = builder.font;
  123. this.placement = builder.placement;
  124. tickText = builder.tickText;
  125. tickValues = builder.tickValues;
  126. tick0 = builder.tick0;
  127. dTick = builder.dTick;
  128. showPrefix = builder.showPrefix;
  129. showSuffix = builder.showSuffix;
  130. showExponent = builder.showExponent;
  131. exponentFormat = builder.exponentFormat;
  132. autoMargin = builder.autoMargin;
  133. angle = builder.angle;
  134. prefix = builder.prefix;
  135. suffix = builder.suffix;
  136. mirror = builder.mirror;
  137. separateThousands = builder.separateThousands;
  138. }
  139. protected void updateContext(Map<String, Object> context) {
  140. context.put("showTickLabels", showLabels);
  141. context.put("tickLength", length);
  142. context.put("tickWidth", width);
  143. context.put("tickColor", color);
  144. context.put("tickFont", tickFont);
  145. context.put("ticks", placement);
  146. if (tickText != null) {
  147. context.put("tickText", Utils.dataAsString(tickText));
  148. }
  149. if (nTicks != 0) {
  150. context.put("nTicks", nTicks);
  151. }
  152. if (dTick != null) {
  153. context.put("dTick", dTick);
  154. }
  155. if (tick0 != null) {
  156. context.put("tick0", tick0);
  157. }
  158. if (showExponent != ALL) {
  159. context.put("showExponent", showExponent);
  160. }
  161. if (exponentFormat != B) {
  162. context.put("exponentFormat", exponentFormat);
  163. }
  164. if (tickValues != null) {
  165. context.put("tickValues", Utils.dataAsString(tickValues));
  166. }
  167. context.put("mirror", mirror);
  168. context.put("prefix", prefix);
  169. context.put("suffix", suffix);
  170. context.put("showPrefix", showPrefix);
  171. context.put("showSuffix", showSuffix);
  172. context.put("angle", angle);
  173. context.put("autoMargin", autoMargin);
  174. context.put("tickMode", tickMode);
  175. context.put("separateThousands", separateThousands);
  176. }
  177. public static TickSettingsBuilder builder() {
  178. return new TickSettingsBuilder();
  179. }
  180. public static class TickSettingsBuilder {
  181. private DisplayRules showExponent = ALL;
  182. private ExponentFormat exponentFormat = B;
  183. private Object tick0;
  184. private Object dTick;
  185. private TickMode tickMode = TickMode.LINEAR;
  186. private Object[] tickText;
  187. private double[] tickValues;
  188. private int tickLength = 5;
  189. private int tickWidth = 1;
  190. private String tickColor = "#444";
  191. private boolean showTickLabels = true;
  192. private Font font;
  193. private TickPlacement placement = TickPlacement.INSIDE;
  194. private int nTicks = 0;
  195. private int angle = 0;
  196. private String prefix;
  197. private String suffix;
  198. private boolean autoMargin = true;
  199. private DisplayRules showPrefix = ALL;
  200. private DisplayRules showSuffix = ALL;
  201. private Mirror mirror;
  202. private boolean separateThousands;
  203. private TickSettingsBuilder() {}
  204. /**
  205. * @param tickValues Sets the values at which ticks on this axis appear. Only has an effect if
  206. * `tickmode` is set to "array". Used with `ticktext`.
  207. * @param tickText Sets the text displayed at the ticks position via `tickvals`. Only has an
  208. * effect if `tickmode` is set to "array". Used with `tickvals`.
  209. */
  210. public TickSettings.TickSettingsBuilder arrayTicks(double[] tickValues, String[] tickText) {
  211. this.tickValues = tickValues;
  212. this.tickText = tickText;
  213. return this;
  214. }
  215. public TickSettings.TickSettingsBuilder arrayTicks(double[] tickValues) {
  216. this.tickValues = tickValues;
  217. return this;
  218. }
  219. public TickSettings.TickSettingsBuilder placement(TickPlacement placement) {
  220. this.placement = placement;
  221. return this;
  222. }
  223. /**
  224. * Specifies the maximum number of ticks for the particular axis. The actual number of ticks
  225. * will be chosen automatically to be less than or equal to `nticks`. Has an effect only if
  226. * `tickmode` is set to "auto".
  227. *
  228. * @param nTicks a non-negative int
  229. * @return this builder
  230. */
  231. public TickSettings.TickSettingsBuilder nTicks(int nTicks) {
  232. Preconditions.checkArgument(nTicks >= 0);
  233. this.nTicks = nTicks;
  234. return this;
  235. }
  236. public TickSettings.TickSettingsBuilder tickMode(TickMode tickMode) {
  237. this.tickMode = tickMode;
  238. return this;
  239. }
  240. /** Determines whether or not the tick labels are drawn. */
  241. public TickSettings.TickSettingsBuilder showTickLabels(boolean showTickLabels) {
  242. this.showTickLabels = showTickLabels;
  243. return this;
  244. }
  245. /** Sets the tick color */
  246. public TickSettings.TickSettingsBuilder color(String tickColor) {
  247. this.tickColor = tickColor;
  248. return this;
  249. }
  250. /** Sets the tick font */
  251. public TickSettings.TickSettingsBuilder font(Font font) {
  252. this.font = font;
  253. return this;
  254. }
  255. /**
  256. * Sets the tick width (in px).
  257. *
  258. * @param tickWidth number greater than or equal to 0
  259. */
  260. public TickSettings.TickSettingsBuilder width(int tickWidth) {
  261. Preconditions.checkArgument(tickWidth >= 0);
  262. this.tickWidth = tickWidth;
  263. return this;
  264. }
  265. /**
  266. * Sets the tick length (in px).
  267. *
  268. * @param tickLength number greater than or equal to 0
  269. */
  270. public TickSettings.TickSettingsBuilder length(int tickLength) {
  271. Preconditions.checkArgument(tickLength >= 0);
  272. this.tickLength = tickLength;
  273. return this;
  274. }
  275. /** Determines whether long tick labels automatically grow the figure margins. */
  276. public TickSettings.TickSettingsBuilder autoMargin(boolean adjust) {
  277. this.autoMargin = adjust;
  278. return this;
  279. }
  280. public TickSettings.TickSettingsBuilder separateThousands(boolean separate) {
  281. this.separateThousands = separate;
  282. return this;
  283. }
  284. /** */
  285. public TickSettings.TickSettingsBuilder showSuffix(DisplayRules showSuffix) {
  286. this.showSuffix = showSuffix;
  287. return this;
  288. }
  289. /** */
  290. public TickSettings.TickSettingsBuilder showExponent(DisplayRules showExponent) {
  291. this.showExponent = showExponent;
  292. return this;
  293. }
  294. /**
  295. * If "all", all exponents are shown besides their significands. If "first", only the exponent
  296. * of the first tick is shown. If "last", only the exponent of the last tick is shown. If
  297. * "none", no exponents appear.
  298. */
  299. public TickSettings.TickSettingsBuilder exponentFormat(ExponentFormat format) {
  300. this.exponentFormat = format;
  301. return this;
  302. }
  303. /**
  304. * If "all", all tick labels are displayed with a prefix. If "first", only the first tick is
  305. * displayed with a prefix. If "last", only the last tick is displayed with a prefix. If "none",
  306. * tick prefixes are hidden.
  307. */
  308. public TickSettings.TickSettingsBuilder showPrefix(DisplayRules showPrefix) {
  309. this.showPrefix = showPrefix;
  310. return this;
  311. }
  312. /**
  313. * Determines if the axis lines or/and ticks are mirrored to the opposite side of the plotting
  314. * area. If "True", the axis lines are mirrored. If "ticks", the axis lines and ticks are
  315. * mirrored. If "False", mirroring is disable. If "all", axis lines are mirrored on all
  316. * shared-axes subplots. If "allticks", axis lines and ticks are mirrored on all shared-axes
  317. * subplots.
  318. */
  319. public TickSettings.TickSettingsBuilder mirror(Mirror mirror) {
  320. this.mirror = mirror;
  321. return this;
  322. }
  323. /**
  324. * Sets the angle of the tick labels with respect to the horizontal. For example, a `tickangle`
  325. * of -90 draws the tick labels vertically.
  326. */
  327. public TickSettings.TickSettingsBuilder angle(int angle) {
  328. this.angle = angle;
  329. return this;
  330. }
  331. public TickSettings.TickSettingsBuilder prefix(String prefix) {
  332. this.prefix = prefix;
  333. return this;
  334. }
  335. public TickSettings.TickSettingsBuilder suffix(String suffix) {
  336. this.suffix = suffix;
  337. return this;
  338. }
  339. /**
  340. * TODO: this is pretty hack-y. Add a separate method for dealing with dates and maybe clean up
  341. * logs too
  342. *
  343. * <p>Sets the placement of the first tick on this axis. Use with `dtick`.
  344. *
  345. * <p>If the axis `type` is "log", then you must take the log of your starting tick (e.g. to set
  346. * the starting tick to 100, set the `tick0` to 2) except when `dtick`="L&lt;f&gt;" (see `dtick`
  347. * for more info).
  348. *
  349. * <p>If the axis `type` is "date", it should be a date string, like date data.
  350. *
  351. * <p>If the axis `type` is "category", it should be a number, using the scale where each
  352. * category is assigned a serial number from zero in the order it appears.
  353. */
  354. public TickSettings.TickSettingsBuilder tick0(Object tick0) {
  355. this.tick0 = tick0;
  356. return this;
  357. }
  358. /**
  359. * TODO: this is pretty hack-y. Add a separate method for dealing with dates and maybe clean up
  360. * logs too
  361. *
  362. * <p>Sets the step in-between ticks on this axis. Use with `tick0`. Must be a positive number,
  363. * or special strings available to "log" and "date" axes.
  364. *
  365. * <p>If the axis `type` is "log", then ticks are set every 10^(n"dtick) where n is the tick
  366. * number. For example, to set a tick mark at 1, 10, 100, 1000, ... set dtick to 1. To set tick
  367. * marks at 1, 100, 10000, ... set dtick to 2. To set tick marks at 1, 5, 25, 125, 625, 3125,
  368. * ... set dtick to log_10(5), or 0.69897000433. "log" has several special values; "L&lt;f&gt;",
  369. * where `f` is a positive number, gives ticks linearly spaced in value (but not position).
  370. *
  371. * <p>For example `tick0` = 0.1, `dtick` = "L0.5" will put ticks at 0.1, 0.6, 1.1, 1.6 etc. To
  372. * show powers of 10 plus small digits between, use "D1" (all digits) or "D2" (only 2 and 5).
  373. * `tick0` is ignored for "D1" and "D2".
  374. *
  375. * <p>If the axis `type` is "date", then you must convert the time to milliseconds. For example,
  376. * to set the interval between ticks to one day, set `dtick` to 86400000.0. "date" also has
  377. * special values "M&lt;n&gt;" gives ticks spaced by a number of months. `n` must be a positive
  378. * integer. To set ticks on the 15th of every third month, set `tick0` to "2000-01-15" and
  379. * `dtick` to "M3". To set ticks every 4 years, set `dtick` to "M48"
  380. */
  381. public TickSettings.TickSettingsBuilder dTick(Object dTick) {
  382. this.dTick = dTick;
  383. return this;
  384. }
  385. public TickSettings build() {
  386. return new TickSettings(this);
  387. }
  388. }
  389. }