show.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, Echarts) {
  2. var Controller = {
  3. index: function () {
  4. //数据渲染
  5. var myChart = {},option = {},formatter;
  6. for(i = 0,len = Config.totalChart.length; i < len; i++) {
  7. if(Config.totalChart[i].chart_type == 'pie'){
  8. option[i] = {
  9. title: {
  10. text: Config.totalChart[i].title,
  11. subtext: Config.totalChart[i].subtext
  12. },
  13. tooltip: {
  14. trigger: 'item',
  15. formatter: '{b}:{c}' + Config.totalChart[i].unit + '({d}%)'
  16. },
  17. legend: {
  18. show: false,
  19. data: Config.totalChart[i].category
  20. },
  21. toolbox: {
  22. show: true,
  23. feature: {
  24. dataView: {show: true, readOnly: false},
  25. saveAsImage: {show: true}
  26. }
  27. },
  28. series: [
  29. {
  30. name: Config.totalChart[i].legend_title,
  31. type: 'pie',
  32. radius: ['20%', '65%'],
  33. avoidLabelOverlap: true,//防止标签重叠
  34. stillShowZeroSum:false,//为0的不显示
  35. label: {
  36. normal: {
  37. show: true,
  38. position: 'left',
  39. formatter: '{b}:{c}' + Config.totalChart[i].unit + '({d}%)',
  40. }
  41. },
  42. normal: {
  43. show: false,//是否显示标签
  44. },
  45. data: Config.totalChart[i].data
  46. }
  47. ]
  48. };
  49. } else if (Config.totalChart[i].chart_type == 'graph') {
  50. console.log(Config.totalChart[i].series);
  51. option[i] = {
  52. title: {
  53. text: Config.totalChart[i].title,
  54. subtext: Config.totalChart[i].subtext
  55. },
  56. tooltip: {
  57. trigger: 'axis',
  58. formatter: Config.totalChart[i].unit
  59. },
  60. legend: {
  61. show: false,
  62. data: Config.totalChart[i].legend_title
  63. },
  64. toolbox: {
  65. show: true,
  66. feature: {
  67. dataView: {show: true, readOnly: false},
  68. magicType: {show: true, type: ['line', 'bar']},
  69. restore: {show: true},
  70. saveAsImage: {show: true}
  71. }
  72. },
  73. calculable: true,
  74. grid: [{
  75. top: 65
  76. }],
  77. xAxis: [
  78. {
  79. type: 'category',
  80. data: Config.totalChart[i].category
  81. }
  82. ],
  83. yAxis: [
  84. {
  85. type: 'value'
  86. }
  87. ],
  88. series: Config.totalChart[i].series
  89. };
  90. } else {
  91. option[i] = {
  92. title: {
  93. text: Config.totalChart[i].title,
  94. subtext: Config.totalChart[i].subtext
  95. },
  96. tooltip: {
  97. trigger: 'axis',
  98. formatter: Config.totalChart[i].unit
  99. },
  100. legend: {
  101. data: Config.totalChart[i].legend_title
  102. },
  103. toolbox: {
  104. show: true,
  105. feature: {
  106. dataView: {show: true, readOnly: false},
  107. magicType: {show: true, type: ['line', 'bar']},
  108. restore: {show: true},
  109. saveAsImage: {show: true}
  110. }
  111. },
  112. calculable: true,
  113. grid: [{
  114. top: 65
  115. }],
  116. xAxis: [
  117. {
  118. type: 'category',
  119. data: Config.totalChart[i].category
  120. }
  121. ],
  122. yAxis: [
  123. {
  124. type: 'value'
  125. }
  126. ],
  127. series: Config.totalChart[i].series
  128. };
  129. }
  130. myChart[i] = Echarts.init($('#echarts' + Config.totalChart[i].id)[0], 'walden');
  131. myChart[i].setOption(option[i]);
  132. }
  133. //绑定搜索表单
  134. Form.api.bindevent($("#form1"));
  135. //自定义时间范围
  136. $("#customcharts_datetimerange").data("callback", function (start, end) {
  137. var date = start.format(this.locale.format) + " - " + end.format(this.locale.format);
  138. $(this.element).val(date);
  139. refresh_echart(date);
  140. });
  141. //重新查询数据
  142. var refresh_echart = function (date) {
  143. Fast.api.ajax({
  144. url: 'customcharts/show/index',
  145. data: {date: date},
  146. loading: false
  147. }, function (data) {
  148. for(j = 0,len = data.length; j < len; j++) {
  149. if (data[j].chart_type == 'pie') {
  150. if (option[j].legend != undefined) {
  151. option[j].legend.data = data[j].category;
  152. }
  153. if (option[j].xAxis != undefined) {
  154. option[j].xAxis[0].data = data[j].category;
  155. }
  156. option[j].series[0].data = data[j].data;
  157. } else {
  158. if (option[j].legend != undefined) {
  159. option[j].legend.data = data[j].legend_title;
  160. }
  161. if (option[j].xAxis != undefined) {
  162. option[j].xAxis[0].data = data[j].category;
  163. }
  164. option[j].series = data[j].series;
  165. option[j].tooltip.formatter = data[j].unit;
  166. }
  167. myChart[j].clear();
  168. myChart[j].setOption(option[j], true);
  169. }
  170. return false;
  171. });
  172. };
  173. //点击按钮
  174. $(document).on("click", ".btn-filter", function () {
  175. var label = $(this).text();
  176. var obj = $(this).closest("form").find("#customcharts_datetimerange").data("daterangepicker");
  177. var dates = obj.ranges[label];
  178. obj.startDate = dates[0];
  179. obj.endDate = dates[1];
  180. obj.clickApply();
  181. });
  182. //点击刷新
  183. $(document).on("click", ".btn-refresh", function () {
  184. var date = $('#customcharts_datetimerange').val();
  185. refresh_echart(date);
  186. });
  187. //每隔一分钟定时刷新图表
  188. setInterval(function () {
  189. $(".btn-refresh").trigger("click");
  190. }, 60000);
  191. },
  192. api: {
  193. bindevent: function () {
  194. Form.api.bindevent($("form[role=form]"));
  195. }
  196. }
  197. };
  198. return Controller;
  199. });