core.ts

  1. /**
  2. * Copyright (c) 2017 ~ present NAVER Corp.
  3. * billboard project is licensed under the MIT license
  4. */
  5. import Chart from "./Chart/Chart";
  6. import {isObject, mergeObj} from "./module/util";
  7. let defaults = Object.create(null);
  8. /**
  9. * @namespace bb
  10. * @version __VERSION__
  11. */
  12. const bb = {
  13. /**
  14. * Version information
  15. * @property {string} version version
  16. * @example
  17. * bb.version; // "1.0.0"
  18. * @memberof bb
  19. */
  20. version: "__VERSION__",
  21. /**
  22. * Generate chart
  23. * - **NOTE:** Bear in mind for the possiblity of ***throwing an error***, during the generation when:
  24. * - Unused option value is given.
  25. * - ex) For `data.type="pie"` option, setting 'axis' option can cause unexpected generation error.
  26. * - Insufficient value is given for certain option used.
  27. * - ex) `data: { x: "x", columns: [["x"], ["data1", 30, 200, 100]] }`
  28. * @param {Options} config chart options
  29. * @memberof bb
  30. * @returns {Chart}
  31. * @see {@link Options} for different generation options
  32. * @see {@link Chart} for different methods API
  33. * @example
  34. * <!-- chart holder -->
  35. * <div id="LineChart"></div>
  36. * @example
  37. * // Generate chart with options
  38. * var chart = bb.generate({
  39. * "bindto": "#LineChart"
  40. * "data": {
  41. * "columns": [
  42. * ["data1", 30, 200, 100, 400, 150, 250],
  43. * ["data2", 50, 20, 10, 40, 15, 25]
  44. * ],
  45. * "type": "line"
  46. * }
  47. * });
  48. *
  49. * // call some API
  50. * // ex) get the data of 'data1'
  51. * chart.data("data1");
  52. * @example
  53. * // Generate chart by importing ESM
  54. * // Import types to be used only, where this will make smaller bundle size.
  55. * import bb, {
  56. * area,
  57. * areaLineRange,
  58. * areaSpline,
  59. * areaSplineRange,
  60. * areaStep,
  61. * bar,
  62. * bubble,
  63. * donut,
  64. * gauge,
  65. * line,
  66. * pie,
  67. * polar,
  68. * radar,
  69. * scatter,
  70. * spline,
  71. * step
  72. * }
  73. *
  74. * bb.generate({
  75. * "bindto": "#LineChart"
  76. * "data": {
  77. * "columns": [
  78. * ["data1", 30, 200, 100, 400, 150, 250],
  79. * ["data2", 50, 20, 10, 40, 15, 25]
  80. * ]
  81. * },
  82. * type: line(),
  83. *
  84. * // or
  85. * types: {
  86. * data1: bar(),
  87. * data2: step()
  88. * }
  89. * });
  90. */
  91. generate(config) {
  92. const options = mergeObj(Object.create(null), defaults, config);
  93. const inst = new Chart(options);
  94. inst.internal.charts = this.instance;
  95. this.instance.push(inst);
  96. return inst;
  97. },
  98. /**
  99. * Set or get global default options.
  100. * - **NOTE:**
  101. * - The options values settings are valid within page context only.
  102. * - If is called multiple times, will override the last value.
  103. * @param {Options} options chart options
  104. * @memberof bb
  105. * @returns {Options}
  106. * @see {@link Options}
  107. * @example
  108. * // Set same option value as for `.generate()`
  109. * bb.defaults({
  110. * data: {
  111. * type: "bar"
  112. * }
  113. * });
  114. *
  115. * bb.defaults(); // {data:{type: "bar"}}
  116. *
  117. * // data.type defaults to 'bar'
  118. * var chart = bb.generate({ ... });
  119. */
  120. defaults(options?) {
  121. if (isObject(options)) {
  122. defaults = options;
  123. }
  124. return defaults;
  125. },
  126. /**
  127. * An array containing instance created
  128. * @property {Array} instance instance array
  129. * @example
  130. * // generate charts
  131. * var chart1 = bb.generate(...);
  132. * var chart2 = bb.generate(...);
  133. *
  134. * bb.instance; // [ chart1, chart2, ... ]
  135. * @memberof bb
  136. */
  137. instance: [],
  138. /**
  139. * Namespace for plugins
  140. * @property {object} plugin plugin namespace
  141. * @example
  142. * // Stanford diagram plugin
  143. * bb.plugin.stanford;
  144. * @memberof bb
  145. */
  146. plugin: {}
  147. };
  148. export {bb, bb as default};