Chart/api/category.ts

  1. /**
  2. * Copyright (c) 2017 ~ present NAVER Corp.
  3. * billboard.js project is licensed under the MIT license
  4. */
  5. import {isEmpty} from "../../module/util";
  6. export default {
  7. /**
  8. * Set specified category name on category axis.
  9. * @function category
  10. * @instance
  11. * @memberof Chart
  12. * @param {number} i index of category to be changed
  13. * @param {string} category category value to be changed
  14. * @returns {string}
  15. * @example
  16. * chart.category(2, "Category 3");
  17. */
  18. category(i: number, category: string): string {
  19. const $$ = this.internal;
  20. const {config} = $$;
  21. if (arguments.length > 1) {
  22. config.axis_x_categories[i] = category;
  23. $$.redraw();
  24. }
  25. return config.axis_x_categories[i];
  26. },
  27. /**
  28. * Set or get category names on category axis.
  29. * @function categories
  30. * @instance
  31. * @memberof Chart
  32. * @param {Array} categories This must be an array that includes category names in string. If category names are included in the date by data.x option, this is not required.
  33. * @returns {Array}
  34. * @example
  35. * chart.categories([
  36. * "Category 1", "Category 2", ...
  37. * ]);
  38. */
  39. categories(categories?: string[]): string[] {
  40. const $$ = this.internal;
  41. const {config} = $$;
  42. if (!categories || !Array.isArray(categories)) {
  43. const cat = config.axis_x_categories;
  44. return isEmpty(cat) ? Object.values($$.data.xs)[0] : cat;
  45. }
  46. config.axis_x_categories = categories;
  47. $$.redraw();
  48. return config.axis_x_categories;
  49. }
  50. };