Chart/api/x.ts

  1. /**
  2. * Copyright (c) 2017 ~ present NAVER Corp.
  3. * billboard.js project is licensed under the MIT license
  4. */
  5. import {isArray, isObject} from "../../module/util";
  6. export default {
  7. /**
  8. * Get and set x values for the chart.
  9. * @function x
  10. * @instance
  11. * @memberof Chart
  12. * @param {Array} x If x is given, x values of every target will be updated. If no argument is given, current x values will be returned as an Object whose keys are the target ids.
  13. * @returns {object} xs
  14. * @example
  15. * // Get current x values
  16. * chart.x();
  17. *
  18. * // Update x values for all targets
  19. * chart.x([100, 200, 300, 400, ...]);
  20. */
  21. x(x?: number[]): {[key: string]: number[]} {
  22. const $$ = this.internal;
  23. const {axis, data} = $$;
  24. const isCategorized = axis.isCustomX() && axis.isCategorized();
  25. if (isArray(x)) {
  26. if (isCategorized) {
  27. this.categories(x);
  28. } else {
  29. $$.updateTargetX(data.targets, x);
  30. $$.redraw({
  31. withUpdateOrgXDomain: true,
  32. withUpdateXDomain: true
  33. });
  34. }
  35. }
  36. return isCategorized ? this.categories() : data.xs;
  37. },
  38. /**
  39. * Get and set x values for the chart.
  40. * @function xs
  41. * @instance
  42. * @memberof Chart
  43. * @param {Array} xs If xs is given, specified target's x values will be updated. If no argument is given, current x values will be returned as an Object whose keys are the target ids.
  44. * @returns {object} xs
  45. * @example
  46. * // Get current x values
  47. * chart.xs();
  48. *
  49. * // Update x values for all targets
  50. * chart.xs({
  51. * data1: [10, 20, 30, 40, ...],
  52. * data2: [100, 200, 300, 400, ...]
  53. * });
  54. */
  55. xs(xs?: {[key: string]: number[]}): {[key: string]: number[]} {
  56. const $$ = this.internal;
  57. if (isObject(xs)) {
  58. $$.updateTargetXs($$.data.targets, xs);
  59. $$.redraw({
  60. withUpdateOrgXDomain: true,
  61. withUpdateXDomain: true
  62. });
  63. }
  64. return $$.data.xs;
  65. }
  66. };