graph.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. mgJsComponentHandler.addDefaultComponent('mg-graph', {
  2. template: '#t-mg-graph',
  3. props: [
  4. 'component_id',
  5. 'component_namespace',
  6. 'component_index'
  7. ],
  8. data: function () {
  9. return {
  10. loading: false,
  11. chart: null,
  12. type: '',
  13. isRestart: false,
  14. data: {},
  15. parseData: "{}",
  16. options: {},
  17. filter: {},
  18. parseFilter: "{}",
  19. };
  20. },
  21. created: function () {
  22. var self = this;
  23. self.updateProjects();
  24. self.$parent.$root.$on('reloadMgData', self.updateMgData);
  25. },
  26. mounted: function () {
  27. var self = this;
  28. },
  29. methods: {
  30. updateMgData: function (toReloadId) {
  31. var self = this;
  32. if (("graph_" + self.component_id) === toReloadId) {
  33. self.updateProjects();
  34. self.$nextTick(function () {
  35. self.$emit('restartRefreshingState');
  36. });
  37. }
  38. },
  39. createChart: function () {
  40. var self = this;
  41. if (self.chart == null || self.isRestart === true) {
  42. self.$nextTick(function () {
  43. self.isRestart = false;
  44. var ctx = document.getElementById('canv_' + self.component_id);
  45. self.fixDataStructure();
  46. self.$nextTick(function () {
  47. self.chart = new Chart(ctx, {
  48. type: self.type,
  49. data: {
  50. datasets: self.data.datasets,
  51. },
  52. options: self.options
  53. });
  54. self.reloadChart();
  55. });
  56. });
  57. }
  58. },
  59. updateProjects: function () {
  60. var self = this;
  61. self.loading = true;
  62. var request = this.$parent.$root.$options.methods.vloadData({loadData: self.component_id, namespace: self.component_namespace, index: self.component_index}, true);
  63. request.done(function (data)
  64. {
  65. data = JSON.parse(data);
  66. if (data.data && data.data.rawData) {
  67. data = data.data.rawData;
  68. if (data.type) {
  69. self.type = data.type;
  70. }
  71. if (data.data) {
  72. self.parseData = JSON.stringify(data.dataParse);
  73. self.data = data.data;
  74. }
  75. if (data.options) {
  76. self.options = data.options;
  77. }
  78. if (data.filter) {
  79. self.filter = data.filter;
  80. self.parseFilter = JSON.stringify(data.filter);
  81. }
  82. }
  83. self.$nextTick(function () {
  84. if (!self.chart) {
  85. self.createChart();
  86. } else {
  87. self.reloadChart();
  88. }
  89. self.loading = false;
  90. });
  91. })
  92. .fail(function () {});
  93. },
  94. reloadChart: function () {
  95. var self = this;
  96. if (self.chart)
  97. {
  98. self.fixDataStructure();
  99. self.chart.options = self.options;
  100. self.chart.data.datasets = self.data.datasets;
  101. self.chart.update();
  102. }
  103. },
  104. restartChart: function () {
  105. var self = this;
  106. self.isRestart = true;
  107. self.createChart();
  108. },
  109. loadModal: function (event, targetId, namespace, index) {
  110. var self = this;
  111. var params = [];
  112. params.push({name: 'customParams', value: self.parseData});
  113. params.push({name: 'defaultFilter', value: self.parseFilter});
  114. mgPageControler.vueLoader.loadM2(event, targetId, typeof namespace !== 'undefined' ? namespace : getItemNamespace(targetId), index, params);
  115. },
  116. onOffSwitch: function (event, targetId) {
  117. var switchPostData = $(event.target).is(':checked') ? {'value': 'on'} : {'value': 'off'};
  118. mgPageControler.vueLoader.ajaxAction(event, targetId, getItemNamespace(targetId), getItemIndex(targetId), switchPostData);
  119. },
  120. makeCustomAction: function (functionName, params, event, namespace, index) {
  121. mgPageControler.vueLoader.makeCustomAction(functionName, params, event, namespace, index);
  122. },
  123. redirect: function (event, params) {
  124. mgPageControler.vueLoader.redirect(event, params);
  125. },
  126. fixDataStructure: function () {
  127. var self = this;
  128. var varsToBeConverted = ['backgroundColor', 'borderColor', 'data', 'hoverBackgroundColor',
  129. 'hoverBorderColor', 'pointBackgroundColor', 'pointBorderColor', 'pointHoverBackgroundColor', 'pointHoverBorderColor'];
  130. for (var key in self.data.datasets) {
  131. if (!self.data.datasets.hasOwnProperty(key)) {
  132. continue;
  133. }
  134. var tmpObj = self.data.datasets[key];
  135. for (var convKey in varsToBeConverted)
  136. {
  137. if (typeof tmpObj[varsToBeConverted[convKey]] === 'object')
  138. {
  139. self.data.datasets[key][varsToBeConverted[convKey]] = Object.values(tmpObj[varsToBeConverted[convKey]]);
  140. } else if (typeof tmpObj[varsToBeConverted[convKey]] !== 'undefined') {
  141. self.data.datasets[key][varsToBeConverted[convKey]] = tmpObj[varsToBeConverted[convKey]];
  142. } else {
  143. //do nothing
  144. }
  145. }
  146. }
  147. if (typeof self.data.labels !== 'undefined')
  148. {
  149. self.options.labels = Object.values(self.data.labels);
  150. }
  151. if (typeof self.options.scales !== 'undefined' && typeof self.options.scales.xAxes !== 'undefined')
  152. {
  153. self.options.scales.xAxes[0].labels = Object.values(self.data.labels);
  154. }
  155. }
  156. }
  157. });