analytics.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. Chart.defaults.global.layout = {
  60. padding: {
  61. left: 0,
  62. right: 0,
  63. top: 0,
  64. bottom: 0
  65. }
  66. }
  67. Chart.defaults.scale.ticks.callback = function (label, index, labels) {
  68. if (label.length > 10)
  69. return label.slice(0, 10) + '...';
  70. else
  71. return label;
  72. };
  73. bx_loading($($this._sContainerDataSelector), false);
  74. var oDataForChart = oData.data || false;
  75. var oChartOptions = oData.options || oOptionsDefault;
  76. if ($this._oChart)
  77. $this._oChart.destroy();
  78. $this._oChart = new Chart($($this._sCanvasSelector), {
  79. type: oData.type != undefined ? oData.type : 'line',
  80. data: oDataForChart,
  81. options: $.extend({}, $this._oChartOptionsDefault, oChartOptions)
  82. });
  83. $this.dataToTable(oData);
  84. });
  85. };
  86. BxAnalytics.prototype.dataToTable = function (oDataIn) {
  87. var $this = this;
  88. var oData = oDataIn.data;
  89. var sHtml = '<table>';
  90. var sCol1 = oDataIn.strings[0];
  91. var iColumnCount = oData.datasets.length;
  92. sHtml += '<thead><tr><th> ' + sCol1 + '</th>';
  93. $.each(oData.datasets, function (iDx, oItem) {
  94. var sText = oItem.label;
  95. sHtml += '<th>' + (oItem.label != '' ? oItem.label : oDataIn.strings[1]) + '</th>';
  96. });
  97. sHtml += '</tr></thead>';
  98. var iK = 0;
  99. oData.datasets[0].data.forEach(function (oItemData, iDx) {
  100. var sTxt = "";
  101. if (oDataIn.options.scales.xAxes[0].type == 'time')
  102. sTxt = oData.datasets[0].data[iDx].x;
  103. else{
  104. sTxt = oData.labels[iDx];
  105. }
  106. if (oDataIn.links && oDataIn.links.length > 0) {
  107. sTxt = "<a href='" + oDataIn.links[iDx] + "'>" + sTxt + "</a>"
  108. }
  109. sHtml += '<tr><td>&nbsp;'+ sTxt + '</td>';
  110. for (i = 0; i < iColumnCount; i++) {
  111. var sText = '';
  112. if (typeof oData.datasets[i].data[iDx] === 'object') {
  113. sText = oData.datasets[i].data[iDx].y;
  114. }
  115. else {
  116. sText = oData.datasets[i].data[iDx];
  117. }
  118. sHtml += '<td>' + sText + '</td>';
  119. }
  120. sHtml += '</tr>';
  121. iK++;
  122. });
  123. sHtml += '</tr><tbody></table>';
  124. if ($this._oTable)
  125. $this._oTable.destroy();
  126. $('.bx_analytics_table').html(sHtml);
  127. var $aOrder = [];
  128. if (oDataIn.options.scales.xAxes[0].type == 'time') {
  129. $aOrder = [[0, 'desc']];
  130. }
  131. $this._oTable = $('.bx_analytics_table').DataTable({ dom: '<"top"i>rt<"bottom"flp><"clear">', paging: true, searching: false, ordering: true, order: $aOrder });
  132. };
  133. /** @} */