Chart/api/axis.ts

  1. /**
  2. * Copyright (c) 2017 ~ present NAVER Corp.
  3. * billboard.js project is licensed under the MIT license
  4. */
  5. import {isDefined, isNumber, isObjectType, isValue} from "../../module/util";
  6. type AxisOption = {x?: number | false, y?: number | false, y2?: number | false} | number | false;
  7. type RangeAxisOption = {min?: AxisOption, max?: AxisOption};
  8. /**
  9. * Set the min/max value
  10. * @param {Chart} $$ Chart instance
  11. * @param {string} type Set type 'min' or 'max'
  12. * @param {object} value Value to be set
  13. * @private
  14. */
  15. function setMinMax($$, type: "min" | "max", value: AxisOption): void {
  16. const {config} = $$;
  17. const helper = (key, value) => {
  18. const v = isNumber(value) ? value : (
  19. value === false ? undefined : null
  20. );
  21. if (v !== null) {
  22. config[`axis_${key}_${type}`] = v;
  23. }
  24. };
  25. if (isDefined(value)) {
  26. if (isObjectType(value)) {
  27. Object.keys(value).forEach(key => {
  28. helper(key, value[key]);
  29. });
  30. } else if (isNumber(value) || value === false) {
  31. // shorthand values affects only y and y2.
  32. ["y", "y2"].forEach(key => {
  33. helper(key, value);
  34. });
  35. }
  36. $$.redraw({
  37. withUpdateOrgXDomain: true,
  38. withUpdateXDomain: true
  39. });
  40. }
  41. }
  42. /**
  43. * Get the min/max value
  44. * @param {Chart} $$ Chart instance
  45. * @param {string} type Set type 'min' or 'max'
  46. * @returns {{x, y, y2}}
  47. * @private
  48. */
  49. function getMinMax($$, type: "min" | "max"): {x: number, y: number, y2: number} {
  50. const {config} = $$;
  51. return {
  52. x: config[`axis_x_${type}`],
  53. y: config[`axis_y_${type}`],
  54. y2: config[`axis_y2_${type}`]
  55. };
  56. }
  57. /**
  58. * Define axis
  59. * @ignore
  60. */
  61. const axis = {
  62. /**
  63. * Get and set axis labels.
  64. * - **NOTE:** Only applicable for chart types which has x and y axes.
  65. * @function axis․labels
  66. * @instance
  67. * @memberof Chart
  68. * @param {object} labels specified axis' label to be updated.
  69. * @param {string} [labels.x] x Axis string
  70. * @param {string} [labels.y] y Axis string
  71. * @param {string} [labels.y2] y2 Axis string
  72. * @returns {object|undefined} axis labels text object
  73. * @example
  74. * // Update axis' label
  75. * chart.axis.labels({
  76. * x: "New X Axis Label",
  77. * y: "New Y Axis Label",
  78. * y2: "New Y2 Axis Label"
  79. * });
  80. *
  81. * chart.axis.labels();
  82. * // --> {
  83. * // x: "New X Axis Label",
  84. * // y: "New Y Axis Label",
  85. * // y2: "New Y2 Axis Label"
  86. * // }
  87. */
  88. labels: function<T>(labels?: {x?: string, y?: string, y2?: string}): T | undefined {
  89. const $$ = this.internal;
  90. let labelText;
  91. if (labels) {
  92. Object.keys(labels).forEach(axisId => {
  93. $$.axis.setLabelText(axisId, labels[axisId]);
  94. });
  95. $$.axis.updateLabels();
  96. }
  97. ["x", "y", "y2"].forEach(v => {
  98. const text = $$.axis.getLabelText(v);
  99. if (text) {
  100. !labelText && (labelText = {});
  101. labelText[v] = text;
  102. }
  103. });
  104. return <T>labelText;
  105. },
  106. /**
  107. * Get and set axis min value.
  108. * - **NOTE:** Only applicable for chart types which has x and y axes.
  109. * @function axis․min
  110. * @instance
  111. * @memberof Chart
  112. * @param {object} min If min is given, specified axis' min value will be updated.<br>
  113. * If no argument is given, the min values set on generating option for each axis will be returned.
  114. * If not set any min values on generation, it will return `undefined`.<br>
  115. * To unset specific axis max, set `false` to each of them.
  116. * @returns {object|undefined}
  117. * @example
  118. * // Update axis' min
  119. * chart.axis.min({
  120. * x: -10,
  121. * y: 1000,
  122. * y2: 100
  123. * });
  124. *
  125. * // To unset specific axis min, set false to each of them.
  126. * chart.axis.min({
  127. * x: false,
  128. * y: false,
  129. * y2: false
  130. * });
  131. *
  132. * // shorthand (only affects y and y2 axis)
  133. * chart.axis.min(-50);
  134. * chart.axis.min(false);
  135. */
  136. min: function(min?: AxisOption): object | void {
  137. const $$ = this.internal;
  138. return isValue(min) || min === false ?
  139. setMinMax($$, "min", min as AxisOption) :
  140. getMinMax($$, "min");
  141. },
  142. /**
  143. * Get and set axis max value.
  144. * - **NOTE:** Only applicable for chart types which has x and y axes.
  145. * @function axis․max
  146. * @instance
  147. * @memberof Chart
  148. * @param {object} max If max is given, specified axis' max value will be updated.<br>
  149. * If no argument is given, the max values set on generating option for each axis will be returned.
  150. * If not set any max values on generation, it will return `undefined`.<br>
  151. * To unset specific axis max, set `false` to each of them.
  152. * @returns {object|undefined}
  153. * @example
  154. * // Update axis' label
  155. * chart.axis.max({
  156. * x: 100,
  157. * y: 1000,
  158. * y2: 10000
  159. * });
  160. *
  161. * // To unset specific axis max, set false to each of them.
  162. * chart.axis.max({
  163. * x: false,
  164. * y: false,
  165. * y2: false
  166. * });
  167. *
  168. * // shorthand (only affects y and y2 axis)
  169. * chart.axis.max(10);
  170. * chart.axis.max(false);
  171. */
  172. max: function(max?: AxisOption): object | void {
  173. const $$ = this.internal;
  174. return isValue(max) || max === false ?
  175. setMinMax($$, "max", max as AxisOption) :
  176. getMinMax($$, "max");
  177. },
  178. /**
  179. * Get and set axis min and max value.
  180. * - **NOTE:** Only applicable for chart types which has x and y axes.
  181. * @function axis․range
  182. * @instance
  183. * @memberof Chart
  184. * @param {object} range If range is given, specified axis' min and max value will be updated.
  185. * If no argument is given, the current min and max values for each axis will be returned.<br>
  186. * To unset specific axis max, set `false` to each of them.
  187. * @returns {object|undefined}
  188. * @example
  189. * // Update axis' label
  190. * chart.axis.range({
  191. * min: {
  192. * x: -10,
  193. * y: -1000,
  194. * y2: -10000
  195. * },
  196. * max: {
  197. * x: 100,
  198. * y: 1000,
  199. * y2: 10000
  200. * },
  201. * });
  202. *
  203. * // To unset specific axis max, set false to each of them.
  204. * chart.axis.range({
  205. * min: {
  206. * x: false,
  207. * y: false,
  208. * y2: false
  209. * },
  210. * max: {
  211. * x: false,
  212. * y: false,
  213. * y2: false
  214. * },
  215. * });
  216. *
  217. * // shorthand (only affects y and y2 axis)
  218. * chart.axis.range({ min: -50, max: 1000 });
  219. * chart.axis.range({ min: false, max: false });
  220. */
  221. range: function(range: RangeAxisOption): object | void {
  222. const {axis} = this;
  223. if (arguments.length) {
  224. const {min, max} = range;
  225. isDefined(max) && axis.max(max);
  226. isDefined(min) && axis.min(min);
  227. } else {
  228. return {
  229. max: axis.max(),
  230. min: axis.min()
  231. };
  232. }
  233. return undefined;
  234. }
  235. };
  236. export default {axis};