analytics.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup Analytics Analytics
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. function BxAnalytics(oOptions) {
  11. this._sContainerId = 'bx_analytics_container';
  12. this._sModuleSelector = '#' + this._sContainerId + ' .bx_analytics_module_selector';
  13. this._sReportSelector = '#' + this._sContainerId + " .bx_analytics_report_selector";
  14. this._sContainerDataSelector = '#' + this._sContainerId + " .bx_analytics_data";
  15. this._sCanvasSelector = '#' + this._sContainerId + " .bx_analytics_grath";
  16. this._sDatePickerSelector = '#' + this._sContainerId + " .bx_analytics_date_picker";
  17. this._sExportUrlSelector = '#' + this._sContainerId + " .bx_analytics_export_url";
  18. this._sActionsUri = oOptions.sActionUri;
  19. this._aStrings = oOptions.aStrings;
  20. var $this = this;
  21. $(document).ready(function () {
  22. $this.init();
  23. });
  24. }
  25. BxAnalytics.prototype.init = function () {
  26. var $this = this;
  27. $($this._sModuleSelector).change(function () {
  28. $this.reloadReports();
  29. });
  30. $($this._sReportSelector).change(function () {
  31. $this.reloadData();
  32. });
  33. $this.reloadReports();
  34. $($this._sDatePickerSelector).daterangepicker({
  35. opens: 'left',
  36. locale: {
  37. "format": "DD/MM/YYYY"
  38. },
  39. }, function (start, end, label) {
  40. $this.reloadData();
  41. });
  42. }
  43. BxAnalytics.prototype.reloadReports = function () {
  44. var $this = this;
  45. $.getJSON($this._sActionsUri + 'GetReports/' + $($this._sModuleSelector).val() + '/', function (aReports) {
  46. $($this._sReportSelector).empty();
  47. $.each(aReports, function (key, value) {
  48. $($this._sReportSelector).append('<option value="' + key + '">' + value + '</option>');
  49. });
  50. $this.reloadData();
  51. });
  52. };
  53. BxAnalytics.prototype.reloadData = function () {
  54. var $this = this;
  55. bx_loading($($this._sContainerDataSelector), true);
  56. $sUrl = $this._sActionsUri + 'GetReportsData/' + $($this._sModuleSelector).val() + '/' + $($this._sReportSelector).val() + '/' + $($this._sDatePickerSelector).data('daterangepicker').startDate.format('YYYY-MM-DD') + '/' + $($this._sDatePickerSelector).data('daterangepicker').endDate.format('YYYY-MM-DD') + '/';
  57. $($this._sExportUrlSelector).attr('href', $sUrl + 'csv/');
  58. $.getJSON($sUrl, function (oData) {
  59. if(Chart.defaults.global == undefined)
  60. Chart.defaults.global = {};
  61. Chart.defaults.global.layout = {
  62. padding: {
  63. left: 0,
  64. right: 0,
  65. top: 0,
  66. bottom: 0
  67. }
  68. };
  69. Chart.defaults.scale.ticks.callback = function (label, index, labels) {
  70. if (label.length > 10)
  71. return label.slice(0, 10) + '...';
  72. else
  73. return label;
  74. };
  75. bx_loading($($this._sContainerDataSelector), false);
  76. if ($this._oChart)
  77. $this._oChart.destroy();
  78. var oDataForChart = oData.data || {};
  79. var oChartOptions = oData.options || {};
  80. $this._oChart = new Chart($($this._sCanvasSelector), {
  81. type: oData.type != undefined ? oData.type : 'line',
  82. data: oDataForChart,
  83. options: $.extend({}, $this._oChartOptionsDefault, oChartOptions)
  84. });
  85. $this.dataToTable(oData);
  86. });
  87. };
  88. BxAnalytics.prototype.dataToTable = function (oDataIn) {
  89. var $this = this;
  90. var oData = oDataIn.data;
  91. var sHtml = '<table>';
  92. var sCol1 = oDataIn.strings[0];
  93. var iColumnCount = oData.datasets.length;
  94. sHtml += '<thead><tr><th> ' + sCol1 + '</th>';
  95. $.each(oData.datasets, function (iDx, oItem) {
  96. var sText = oItem.label;
  97. sHtml += '<th>' + (oItem.label != '' ? oItem.label : oDataIn.strings[1]) + '</th>';
  98. });
  99. sHtml += '</tr></thead>';
  100. var iK = 0;
  101. oData.datasets[0].data.forEach(function (oItemData, iDx) {
  102. var sTxt = "";
  103. if (oDataIn.options.scales.x.type == 'time')
  104. sTxt = oData.datasets[0].data[iDx].x;
  105. else{
  106. sTxt = oData.labels[iDx];
  107. }
  108. if (oDataIn.links && oDataIn.links.length > 0) {
  109. sTxt = "<a href='" + oDataIn.links[iDx] + "'>" + sTxt + "</a>"
  110. }
  111. sHtml += '<tr><td>&nbsp;'+ sTxt + '</td>';
  112. for (i = 0; i < iColumnCount; i++) {
  113. var sText = '';
  114. if (typeof oData.datasets[i].data[iDx] === 'object') {
  115. sText = oData.datasets[i].data[iDx].y;
  116. }
  117. else {
  118. sText = oData.datasets[i].data[iDx];
  119. }
  120. sHtml += '<td>' + sText + '</td>';
  121. }
  122. sHtml += '</tr>';
  123. iK++;
  124. });
  125. sHtml += '</tr><tbody></table>';
  126. if ($this._oTable)
  127. $this._oTable.destroy();
  128. $('.bx_analytics_table').html(sHtml);
  129. var $aOrder = [];
  130. if (oDataIn.options.scales.x.type == 'time') {
  131. $aOrder = [[0, 'desc']];
  132. }
  133. $this._oTable = $('.bx_analytics_table').DataTable({ "stripeClasses": ['bx-def-color-bg-hl', ''], dom: '<"top"i>rt<"bottom"flp><"clear">', paging: true, searching: false, ordering: true, order: $aOrder });
  134. };
  135. /** @} */