chart.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup Charts Charts
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. function BxCharts(oOptions) {
  11. this._sObjName = undefined == oOptions.sObjName ? 'oCharts' : oOptions.sObjName; // javascript object name, to run current object instance from onTimer
  12. this._sChartName = oOptions.sChartName == undefined ? '' : oOptions.sChartName;
  13. this._sActionsUri = oOptions.sActionUri;
  14. this._sKeyGraph = 'bx_chart_growth_graph_' + this._sChartName;
  15. this._oChartOptionsDefault = { layout: {
  16. autoPadding: true,
  17. padding: {
  18. left: 40,
  19. right: 40,
  20. top: 40,
  21. bottom: 20
  22. }
  23. }};
  24. var $this = this;
  25. $(document).ready(function () {
  26. $this.loadData();
  27. });
  28. }
  29. BxCharts.prototype.loadData = function () {
  30. var $this = this;
  31. var oGraph = $('.' + this._sKeyGraph);
  32. var oGraphWrp = oGraph.parents('div:first');
  33. var oGraphErr = oGraphWrp.find('.bx-chart-growth-graph-error').hide();
  34. var sAdditionalParameters = "";
  35. if ($('.bx_chart_growth_selector_' + this._sChartName).length > 0) {
  36. sAdditionalParameters = '?m=' + $('.bx_chart_growth_selector_' + this._sChartName).val();
  37. $('.bx_chart_growth_selector_' + this._sChartName).off().change(function () {
  38. $this.loadData();
  39. });
  40. }
  41. bx_loading(oGraphWrp, true);
  42. $.get(
  43. this._sActionsUri + 'get_chart_data/' + $this._sChartName + '/' + sAdditionalParameters +'',
  44. {
  45. },
  46. function (oData) {
  47. bx_loading(oGraphWrp, false);
  48. if (oData.error != undefined) {
  49. if ($this._oChart)
  50. $this._oChart.destroy();
  51. oGraphErr.html(oData.error).show();
  52. return;
  53. }
  54. if (oData.links) {
  55. oData.options.plugins.legend.onClick = function (e, legendItem, legend) {
  56. if (legendItem.index != undefined)
  57. window.open(oData.links[legendItem.index], '_blank');
  58. };
  59. }
  60. var oDataForChart = oData.data || false;
  61. var oChartOptions = oData.options || {};
  62. if ($this._oChart)
  63. $this._oChart.destroy();
  64. $this._oChart = new Chart(oGraph, {
  65. type: oData.type != undefined ? oData.type : 'line',
  66. data: oDataForChart,
  67. options: $.extend({}, $this._oChartOptionsDefault, oChartOptions)
  68. });
  69. if (oData.links) {
  70. oGraph.click(function (evt) {
  71. var activePoint = $this._oChart.getElementsAtEventForMode(evt, 'nearest', { intersect: true }, true);
  72. if (activePoint && activePoint.length) {
  73. var sLink = oData.links[activePoint[0].index];
  74. if (sLink != undefined && sLink != "")
  75. window.open(sLink, '_blank');
  76. }
  77. });
  78. }
  79. },
  80. 'json'
  81. );
  82. };
  83. /** @} */