Chart/api/show.ts

  1. /**
  2. * Copyright (c) 2017 ~ present NAVER Corp.
  3. * billboard.js project is licensed under the MIT license
  4. */
  5. import {callFn, endall} from "../../module/util";
  6. /**
  7. * Show/Hide data series
  8. * @param {boolean} show Show or hide
  9. * @param {Array} targetIdsValue Target id values
  10. * @param {object} options Options
  11. * @private
  12. */
  13. function showHide(show: boolean, targetIdsValue: string[], options: any): void {
  14. const $$ = this.internal;
  15. const targetIds = $$.mapToTargetIds(targetIdsValue);
  16. const hiddenIds = $$.state.hiddenTargetIds
  17. .map(v => targetIds.indexOf(v) > -1 && v)
  18. .filter(Boolean);
  19. $$.state.toggling = true;
  20. $$[`${show ? "remove" : "add"}HiddenTargetIds`](targetIds);
  21. const targets = $$.$el.svg.selectAll($$.selectorTargets(targetIds));
  22. const opacity = show ? null : "0";
  23. if (show && hiddenIds.length) {
  24. targets.style("display", null);
  25. callFn($$.config.data_onshown, this, hiddenIds);
  26. }
  27. $$.$T(targets)
  28. .style("opacity", opacity, "important")
  29. .call(endall, () => {
  30. // https://github.com/naver/billboard.js/issues/1758
  31. if (!show && hiddenIds.length === 0) {
  32. targets.style("display", "none");
  33. callFn($$.config?.data_onhidden, this, targetIds);
  34. }
  35. targets.style("opacity", opacity);
  36. });
  37. options.withLegend && $$[`${show ? "show" : "hide"}Legend`](targetIds);
  38. $$.redraw({
  39. withUpdateOrgXDomain: true,
  40. withUpdateXDomain: true,
  41. withLegend: true
  42. });
  43. $$.state.toggling = false;
  44. }
  45. export default {
  46. /**
  47. * Show data series on chart
  48. * @function show
  49. * @instance
  50. * @memberof Chart
  51. * @param {string|Array} [targetIdsValue] The target id value.
  52. * @param {object} [options] The object can consist with following members:<br>
  53. *
  54. * | Key | Type | default | Description |
  55. * | --- | --- | --- | --- |
  56. * | withLegend | boolean | false | whether or not display legend |
  57. *
  58. * @example
  59. * // show 'data1'
  60. * chart.show("data1");
  61. *
  62. * // show 'data1' and 'data3'
  63. * chart.show(["data1", "data3"]);
  64. */
  65. show(targetIdsValue?: string[] | string, options = {}): void {
  66. showHide.call(this, true, targetIdsValue, options);
  67. },
  68. /**
  69. * Hide data series from chart
  70. * @function hide
  71. * @instance
  72. * @memberof Chart
  73. * @param {string|Array} [targetIdsValue] The target id value.
  74. * @param {object} [options] The object can consist with following members:<br>
  75. *
  76. * | Key | Type | default | Description |
  77. * | --- | --- | --- | --- |
  78. * | withLegend | boolean | false | whether or not display legend |
  79. *
  80. * @example
  81. * // hide 'data1'
  82. * chart.hide("data1");
  83. *
  84. * // hide 'data1' and 'data3'
  85. * chart.hide(["data1", "data3"]);
  86. */
  87. hide(targetIdsValue?: string[], options = {}): void {
  88. showHide.call(this, false, targetIdsValue, options);
  89. },
  90. /**
  91. * Toggle data series on chart. When target data is hidden, it will show. If is shown, it will hide in vice versa.
  92. * @function toggle
  93. * @instance
  94. * @memberof Chart
  95. * @param {string|Array} [targetIds] The target id value.
  96. * @param {object} [options] The object can consist with following members:<br>
  97. *
  98. * | Key | Type | default | Description |
  99. * | --- | --- | --- | --- |
  100. * | withLegend | boolean | false | whether or not display legend |
  101. *
  102. * @example
  103. * // toggle 'data1'
  104. * chart.toggle("data1");
  105. *
  106. * // toggle 'data1' and 'data3'
  107. * chart.toggle(["data1", "data3"]);
  108. */
  109. toggle(targetIds: string | string[], options = {}): void {
  110. const $$ = this.internal;
  111. const targets = {show: <string[]>[], hide: <string[]>[]};
  112. // sort show & hide target ids
  113. $$.mapToTargetIds(targetIds)
  114. .forEach((id: string) => targets[$$.isTargetToShow(id) ? "hide" : "show"].push(id));
  115. // perform show & hide task separately
  116. // https://github.com/naver/billboard.js/issues/454
  117. targets.show.length && this.show(targets.show, options);
  118. targets.hide.length && setTimeout(() => this.hide(targets.hide, options), 0);
  119. }
  120. };