chart.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup MassMailer Mass Mailer
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. function BxMassMailerChart(oOptions) {
  11. this._sContainerId = 'bx_massmailer_chart_container';
  12. this._sChartName = oOptions.sChartName;
  13. this._sReportName = oOptions.sReportName;
  14. this._sReportSelector = '#' + this._sContainerId + " .bx_massmailer_report_selector";
  15. this._sSegmentsSelector = '#' + this._sContainerId + " .bx_massmailer_segments_selector";
  16. this._sContainerDataSelector = '#' + this._sContainerId + " .bx_massmailer_data";
  17. this._sCanvasSelector = '#' + this._sContainerId + " .bx_massmailer_chart";
  18. this._sDatePickerSelector = '#' + this._sContainerId + " .bx_massmailer_date_picker";
  19. this._sActionsUri = oOptions.sActionUri;
  20. this._aStrings = oOptions.aStrings;
  21. this._oChart = null;
  22. this._oChartOptionsDefault = {
  23. layout: {
  24. padding: {
  25. left: 40,
  26. right: 40,
  27. top: 40,
  28. bottom: 20
  29. }
  30. }
  31. };
  32. var $this = this;
  33. $(document).ready(function () {
  34. $this.init();
  35. });
  36. }
  37. BxMassMailerChart.prototype.init = function () {
  38. var $this = this;
  39. if ($($this._sReportSelector).length > 0) {
  40. $($this._sReportSelector).change(function () {
  41. $this.reloadData();
  42. });
  43. }
  44. if ($($this._sSegmentsSelector).length > 0) {
  45. $($this._sSegmentsSelector).change(function () {
  46. $this.reloadData();
  47. });
  48. }
  49. $($this._sDatePickerSelector).daterangepicker({
  50. opens: 'left',
  51. locale: {
  52. "format": "DD/MM/YYYY"
  53. },
  54. }, function (start, end, label) {
  55. $this.reloadData();
  56. });
  57. $this.reloadData();
  58. }
  59. BxMassMailerChart.prototype.reloadData = function () {
  60. var $this = this;
  61. bx_loading($($this._sContainerDataSelector), true);
  62. var sStartDate = $($this._sDatePickerSelector).length > 0 ? $($this._sDatePickerSelector).data('daterangepicker').startDate.format('YYYY-MM-DD') : '-';
  63. var sEndDate = $($this._sDatePickerSelector).length > 0 ? $($this._sDatePickerSelector).data('daterangepicker').endDate.format('YYYY-MM-DD') : '-';
  64. var sReport = $($this._sReportSelector).length > 0 ? $($this._sReportSelector).val() : $this._sReportName;
  65. var sSegment = $($this._sSegmentsSelector).length > 0 ? $($this._sSegmentsSelector).val() : '-';
  66. $.getJSON($this._sActionsUri + 'GetReportsData/' + $this._sChartName + '/' + sReport + '/' + sStartDate + '/' + sEndDate + '/' + sSegment + '/', function (oData) {
  67. bx_loading($($this._sContainerDataSelector), false);
  68. var oDataForChart = oData.data || {};
  69. var oChartOptions = oData.options || {};
  70. if ($this._oChart)
  71. $this._oChart.destroy();
  72. $this._oChart = new Chart($($this._sCanvasSelector), {
  73. type: oData.type != undefined ? oData.type : 'line',
  74. data: oDataForChart,
  75. options: $.extend({}, $this._oChartOptionsDefault, oChartOptions)
  76. });
  77. });
  78. };
  79. /** @} */