Chart/api/data.ts

  1. /**
  2. * Copyright (c) 2017 ~ present NAVER Corp.
  3. * billboard.js project is licensed under the MIT license
  4. */
  5. import type {DataItem} from "../../../types/types";
  6. import type {IDataRow} from "../../ChartInternal/data/IData";
  7. import {extend, isArray, isUndefined} from "../../module/util";
  8. /**
  9. * Get data loaded in the chart.
  10. * @function data
  11. * @instance
  12. * @memberof Chart
  13. * @param {string|Array} targetIds If this argument is given, this API returns the specified target data. If this argument is not given, all of data will be returned.
  14. * @returns {Array} Data objects
  15. * @example
  16. * // Get only data1 data
  17. * chart.data("data1");
  18. * // --> [{id: "data1", id_org: "data1", values: Array(6)}, ...]
  19. *
  20. * // Get data1 and data2 data
  21. * chart.data(["data1", "data2"]);
  22. *
  23. * // Get all data
  24. * chart.data();
  25. */
  26. const data = function(targetIds: string | string[]): DataItem[] {
  27. const {targets} = this.internal.data;
  28. if (!isUndefined(targetIds)) {
  29. const ids: any = isArray(targetIds) ? targetIds : [targetIds];
  30. return targets.filter(t => ids.some(v => v === t.id));
  31. }
  32. return targets;
  33. };
  34. extend(data, {
  35. /**
  36. * Get data shown in the chart.
  37. * @function data․shown
  38. * @instance
  39. * @memberof Chart
  40. * @param {string|Array} targetIds If this argument is given, this API filters the data with specified target ids. If this argument is not given, all shown data will be returned.
  41. * @returns {Array} Data objects
  42. * @example
  43. * // Get shown data by filtering to include only data1 data
  44. * chart.data.shown("data1");
  45. * // --> [{id: "data1", id_org: "data1", values: Array(6)}, ...]
  46. *
  47. * // Get shown data by filtering to include data1 and data2 data
  48. * chart.data.shown(["data1", "data2"]);
  49. *
  50. * // Get all shown data
  51. * chart.data.shown();
  52. */
  53. shown: function(targetIds: string | string[]): DataItem[] {
  54. return this.internal.filterTargetsToShow(this.data(targetIds));
  55. },
  56. /**
  57. * Get values of the data loaded in the chart.
  58. * @function data․values
  59. * @instance
  60. * @memberof Chart
  61. * @param {string|Array|null} targetIds This API returns the values of specified target. If this argument is not given, null will be retruned
  62. * @param {boolean} [flat=true] Get flatten values
  63. * @returns {Array} Data values
  64. * @example
  65. * // Get data1 values
  66. * chart.data.values("data1");
  67. * // --> [10, 20, 30, 40]
  68. */
  69. values: function(targetIds?: string | string[], flat: boolean = true): number[] | number[][]
  70. | null {
  71. let values: any = null;
  72. if (targetIds) {
  73. const targets = this.data(targetIds);
  74. if (isArray(targets)) {
  75. values = [];
  76. targets.forEach(v => {
  77. const dataValue = v.values.map(d => d.value);
  78. flat ? (values = values.concat(dataValue)) : values.push(dataValue);
  79. });
  80. }
  81. }
  82. return values;
  83. },
  84. /**
  85. * Get and set names of the data loaded in the chart.
  86. * @function data․names
  87. * @instance
  88. * @memberof Chart
  89. * @param {object} names If this argument is given, the names of data will be updated. If not given, the current names will be returned. The format of this argument is the same as [data.names](./Options.html#.data%25E2%2580%25A4names).
  90. * @returns {object} Corresponding names according its key value, if specified names values.
  91. * @example
  92. * // Get current names
  93. * chart.data.names();
  94. * // --> {data1: "test1", data2: "test2"}
  95. *
  96. * // Update names
  97. * chart.data.names({
  98. * data1: "New Name 1",
  99. * data2: "New Name 2"
  100. * });
  101. */
  102. names: function(names?: Array<{[key: string]: string | null}>): {[key: string]: string | null} {
  103. const $$ = this.internal;
  104. return $$.updateDataAttributes("names", names);
  105. },
  106. /**
  107. * Get and set colors of the data loaded in the chart.
  108. * @function data․colors
  109. * @instance
  110. * @memberof Chart
  111. * @param {object} colors If this argument is given, the colors of data will be updated. If not given, the current colors will be returned. The format of this argument is the same as [data.colors](./Options.html#.data%25E2%2580%25A4colors).
  112. * @returns {object} Corresponding data color value according its key value.
  113. * @example
  114. * // Get current colors
  115. * chart.data.colors();
  116. * // --> {data1: "#00c73c", data2: "#fa7171"}
  117. *
  118. * // Update colors
  119. * chart.data.colors({
  120. * data1: "#FFFFFF",
  121. * data2: "#000000"
  122. * });
  123. */
  124. colors: function(colors?: Array<{[key: string]: string}>): {[key: string]: string} {
  125. return this.internal.updateDataAttributes("colors", colors);
  126. },
  127. /**
  128. * Get and set axes of the data loaded in the chart.
  129. * - **NOTE:** If all data is related to one of the axes, the domain of axis without related data will be replaced by the domain from the axis with related data
  130. * @function data․axes
  131. * @instance
  132. * @memberof Chart
  133. * @param {object} axes If this argument is given, the axes of data will be updated. If not given, the current axes will be returned. The format of this argument is the same as
  134. * @returns {object} Corresponding axes value for data, if specified axes value.
  135. * @example
  136. * // Get current axes
  137. * chart.data.axes();
  138. * // --> {data1: "y"}
  139. *
  140. * // Update axes
  141. * chart.data.axes({
  142. * data1: "y",
  143. * data2: "y2"
  144. * });
  145. */
  146. axes: function(axes?: Array<{[key: string]: string}>): {[key: string]: string} {
  147. return this.internal.updateDataAttributes("axes", axes);
  148. },
  149. /**
  150. * Get the minimum data value bound to the chart
  151. * @function data․min
  152. * @instance
  153. * @memberof Chart
  154. * @returns {Array} Data objects
  155. * @example
  156. * // Get current axes
  157. * chart.data.min();
  158. * // --> [{x: 0, value: 30, id: "data1", index: 0}, ...]
  159. */
  160. min: function(): IDataRow[] {
  161. return this.internal.getMinMaxData().min;
  162. },
  163. /**
  164. * Get the maximum data value bound to the chart
  165. * @function data․max
  166. * @instance
  167. * @memberof Chart
  168. * @returns {Array} Data objects
  169. * @example
  170. * // Get current axes
  171. * chart.data.max();
  172. * // --> [{x: 3, value: 400, id: "data1", index: 3}, ...]
  173. */
  174. max: function(): IDataRow[] {
  175. return this.internal.getMinMaxData().max;
  176. }
  177. });
  178. export default {data};