Source

widgets/bubbles.js

  1. import processStructure from '@devoinc/applications-data-library/structures/bubble';
  2. import { set } from '@devoinc/applications-builder/utils/objects';
  3. import widgetFactory from '@devoinc/applications-builder/widgetFactory';
  4. import dependencies from '../data/dependencies';
  5. const BubbleWidget = dependencies.require('widgets').BubbleWidget;
  6. /**
  7. * This chart displays three dimensions of data over an X-Y chart,
  8. * where X and Y are the first 2 dimensions.
  9. * The third dimension is represented by the disk, whose diameter
  10. * is proportional to the value of the third parameter.
  11. * @category Widgets
  12. * @module Bubbles
  13. * @see [base](module-base.html)
  14. * @see [collapser](module-collapser.html)
  15. * @see [dataSearch](module-dataSearch.html)
  16. * @see [download](module-download.html)
  17. * @see [info](module-info.html)
  18. * @see [lifeCycle](module-lifeCycle.html)
  19. * @see [listeners](module-listeners.html)
  20. * @see [loading](module-loading.html)
  21. * @see [menu](module-menu.html)
  22. * @see [screenshot](module-screenshot.html)
  23. * @see [zoom](module-zoom.html)
  24. * @tutorial widgets-bubbles
  25. */
  26. function mixin(self) {
  27. return {
  28. /**
  29. * Set the field related to bubble size.
  30. * @param {string} value - value to show
  31. * @instance
  32. */
  33. setValue(val) {
  34. self.settings.valToShow = val;
  35. },
  36. /**
  37. * Set Y axis property object.
  38. * @param {Object} axis
  39. * @param {string} axis.name - Column name to representate.
  40. * @param {string} axis.type - Column type.
  41. * @instance
  42. */
  43. setYaxis(axis) {
  44. self.settings.yAxis = axis;
  45. },
  46. /**
  47. * Set X axis property object.
  48. * @param {Object} axis
  49. * @param {string} axis.name - Column name to representate.
  50. * @param {string} axis.type - Column type.
  51. * @instance
  52. */
  53. setXaxis(axis) {
  54. self.settings.xAxis = axis;
  55. },
  56. /**
  57. * Set the title of the Y axis.
  58. * @param {string} title - Text with the title.
  59. * @instance
  60. */
  61. setYaxisTitle(title) {
  62. self.settings.xaxis_title = title;
  63. },
  64. /**
  65. * Set the title of the X axis.
  66. * @param {string} title - Text with the title.
  67. * @instance
  68. */
  69. setXaxisTitle(title) {
  70. self.settings.xaxis_title = title;
  71. },
  72. /**
  73. * Used to partition the bubbles into columns on the plane.
  74. * @param {string} column - Column name.
  75. * @instance
  76. */
  77. setSeriesBy(column) {
  78. self.settings.seriesBy = column;
  79. },
  80. /**
  81. * Set the title for the size.
  82. * @param {string} title - Text with the title.
  83. * @instance
  84. */
  85. setSizeTitle: (title) => {
  86. self.settings.size_title = title;
  87. },
  88. /**
  89. * Set enable or disable the legend.
  90. * @param {boolean} bool - Enable or disable.
  91. * @instance
  92. */
  93. setLegend: (bool) => {
  94. self.settings.legend = bool;
  95. },
  96. /**
  97. * Set a custom function to format the legend.
  98. * @param {Function} formatter - Callback JavaScript function.
  99. * @instance
  100. */
  101. setDefaultformaterlegend: (formatter) => {
  102. self.settings.defaultformaterlegend = formatter;
  103. },
  104. // Common
  105. // -------------------------------------------------------------------------
  106. /**
  107. * Resize function
  108. * @instance
  109. * @ignore
  110. */
  111. resize() {
  112. setTimeout(() => {
  113. self?.widget?.chart.reflow();
  114. }, 100);
  115. },
  116. // Life Cycle
  117. // -------------------------------------------------------------------------
  118. /**
  119. * Render function
  120. * @param {object} orig - Data for process
  121. * @ignore
  122. */
  123. render: (orig) => {
  124. if (!self.el) return; // If not have element not render
  125. let cfg = self.settings;
  126. let data = processStructure(
  127. orig,
  128. cfg.xAxis,
  129. cfg.yAxis,
  130. cfg.valToShow,
  131. cfg.seriesBy
  132. );
  133. set(
  134. cfg,
  135. 'widgetTemplate.chart.renderTo',
  136. self.el.querySelector('.lt-vapp-widget-graphic')
  137. );
  138. if (data) {
  139. self.widget = new BubbleWidget(cfg);
  140. self.graphic.style.width = '';
  141. self.widget.setData(data);
  142. self.widget.display({ force: true, data: true });
  143. } else {
  144. this.debugError({
  145. msg: 'NO DATA',
  146. console: {
  147. method: 'error',
  148. msg: 'No data arrive to render function',
  149. },
  150. });
  151. }
  152. },
  153. };
  154. }
  155. export default widgetFactory(mixin);