graph.js 9.2 KB

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