common.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Ajax get chart data.
  3. *
  4. * @access public
  5. * @return bool
  6. */
  7. function ajaxGetChart(check = true, chart = DataStorage.chart, echart = window.echart, noDataDom = window.noDataDom)
  8. {
  9. var chartParams = JSON.parse(JSON.stringify(chart));
  10. if(typeof DataStorage != 'undefined') chartParams.fieldSettings = JSON.parse(JSON.stringify(DataStorage.fieldSettings));
  11. if(typeof DataStorage != 'undefined') chartParams.langs = JSON.parse(JSON.stringify(DataStorage.langs));
  12. /* Redraw echart. */
  13. /* 拿数据并重绘图表。*/
  14. $.post(createLink('chart', 'ajaxGetChart'), chartParams, function(resp)
  15. {
  16. var data = JSON.parse(resp);
  17. if(echart)
  18. {
  19. var type = chartParams.settings[0].type;
  20. if(type == 'waterpolo')
  21. {
  22. data.series[0].label.formatter = function(params) { return (params.value * 100).toFixed(2) + '%';};
  23. data.tooltip.formatter = function(params) { return (params.value * 100).toFixed(2) + '%';};
  24. }
  25. if(canLabelRotate.includes(type))
  26. {
  27. if(!data.xAxis.axisLabel) data.xAxis.axisLabel = {};
  28. if(!data.yAxis.axisLabel) data.yAxis.axisLabel = {};
  29. var labelFormatter = function(value)
  30. {
  31. value = value.toString();
  32. return (value.length <= labelMaxLength) ? value : value.substring(0, labelMaxLength) + '...';
  33. }
  34. data.xAxis.axisLabel.formatter = labelFormatter;
  35. data.yAxis.axisLabel.formatter = labelFormatter;
  36. }
  37. echart.resize();
  38. echart.clear();
  39. if(isChartHaveData(data, type))
  40. {
  41. echart.setOption(data, true);
  42. $('.btn-export').removeClass('hidden');
  43. noDataDom.addClass('hidden');
  44. }
  45. else
  46. {
  47. noDataDom.removeClass('hidden');
  48. }
  49. }
  50. });
  51. }
  52. /**
  53. * Determine whether the returned data is available.
  54. *
  55. * @access public
  56. * @return bool
  57. */
  58. function isChartHaveData(dataInfo, type)
  59. {
  60. if(type == 'waterpolo') return true;
  61. var data = [];
  62. if(type == 'pie') data = dataInfo.series[0].data;
  63. if(type == 'line') data = dataInfo.xAxis.data;
  64. if(type == 'radar') data = dataInfo.radar.indicator;
  65. if(type == 'cluBarY' || type == 'stackedBarY') data = dataInfo.yAxis.data;
  66. if(type == 'cluBarX' || type == 'stackedBar') data = dataInfo.xAxis.data;
  67. return data.length;
  68. }
  69. function resizeChart()
  70. {
  71. var filterHeight = $('.main-col .cell #filterContent').height();
  72. $('.main-col .cell #draw').css('height', 'calc(100% - ' + (filterHeight + 16) + 'px)')
  73. if(echart)
  74. {
  75. echart.resize();
  76. }
  77. }
  78. function waitForRepaint(callback)
  79. {
  80. window.requestAnimationFrame(function()
  81. {
  82. window.requestAnimationFrame(callback);
  83. });
  84. }
  85. /**
  86. * Init picker.
  87. *
  88. * @access public
  89. * @return void
  90. */
  91. function initPicker($row, pickerName = 'picker-select', onready = false)
  92. {
  93. $row.find('.' + pickerName).picker(
  94. {
  95. maxDropHeight: pickerHeight,
  96. onReady: function()
  97. {
  98. if(!onready) return;
  99. if(!$row.find('.picker')) return;
  100. if(window.getComputedStyle($row.find('.picker').find('.picker-selections')[0]).getPropertyValue('width') !== 'auto')
  101. {
  102. var pickerWidth = $row.find('.picker')[0].getBoundingClientRect().width;
  103. $row.find('.picker').find('.picker-selections').css('width', pickerWidth);
  104. }
  105. }
  106. });
  107. $row.find("." + pickerName).each(function(index)
  108. {
  109. if($(this).hasClass('required')) $(this).siblings("div .picker").addClass('required');
  110. });
  111. }
  112. /**
  113. * Init datepicker.
  114. *
  115. * @param object $obj
  116. * @param function callback
  117. * @access public
  118. * @return void
  119. */
  120. function initDatepicker($obj, callback)
  121. {
  122. $obj.find('.form-date').datepicker();
  123. $obj.find('.form-datetime').datetimepicker();
  124. if(typeof callback == 'function') callback($obj);
  125. }
  126. /**
  127. * Attr date check.
  128. *
  129. * @param object $obj
  130. * @access public
  131. * @return void
  132. */
  133. function attrDateCheck($obj)
  134. {
  135. $obj.find('.form-date').attr('onchange', 'checkDate(this, this.value)');
  136. $obj.find('.form-datetime').attr('onchange', 'checkDate(this, this.value)');
  137. }