show.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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%', '60%'],
  33. avoidLabelOverlap: false,
  34. label: {
  35. normal: {
  36. show: true,
  37. position: 'left',
  38. formatter: '{b}:{c}' + Config.totalChart[i].unit + '({d}%)',
  39. }
  40. },
  41. normal: {
  42. show: false,//是否显示标签
  43. },
  44. data: Config.totalChart[i].data
  45. }
  46. ]
  47. };
  48. } else if (Config.totalChart[i].chart_type == 'graph') {
  49. option[i] = {
  50. title: {
  51. text: Config.totalChart[i].title,
  52. subtext: Config.totalChart[i].subtext
  53. },
  54. tooltip: {
  55. trigger: 'axis',
  56. formatter: '{b}<br>{a0} : {c0} ' + Config.totalChart[i].unit
  57. },
  58. legend: {
  59. data: [Config.totalChart[i].legend_title]
  60. },
  61. toolbox: {
  62. show: true,
  63. feature: {
  64. dataView: {show: true, readOnly: false},
  65. magicType: {show: true, type: ['line', 'bar']},
  66. restore: {show: true},
  67. saveAsImage: {show: true}
  68. }
  69. },
  70. calculable: true,
  71. grid: [{
  72. top: 65
  73. }],
  74. xAxis: [
  75. {
  76. type: 'category',
  77. data: Config.totalChart[i].category
  78. }
  79. ],
  80. yAxis: [
  81. {
  82. type: 'value'
  83. }
  84. ],
  85. series: [
  86. {
  87. name: Config.totalChart[i].legend_title,
  88. type: 'line',
  89. data: Config.totalChart[i].data,
  90. markPoint: {
  91. data: [
  92. {type: 'max', name: '最大值'},
  93. {type: 'min', name: '最小值'}
  94. ]
  95. },
  96. markLine: {
  97. data: [
  98. {type: 'average', name: '平均值'}
  99. ]
  100. }
  101. }
  102. ]
  103. };
  104. } else {
  105. option[i] = {
  106. title: {
  107. text: Config.totalChart[i].title,
  108. subtext: Config.totalChart[i].subtext
  109. },
  110. tooltip: {
  111. trigger: 'axis',
  112. formatter: '{b}<br>{a0} : {c0} ' + Config.totalChart[i].unit
  113. },
  114. legend: {
  115. data: [Config.totalChart[i].legend_title]
  116. },
  117. toolbox: {
  118. show: true,
  119. feature: {
  120. dataView: {show: true, readOnly: false},
  121. magicType: {show: true, type: ['line', 'bar']},
  122. restore: {show: true},
  123. saveAsImage: {show: true}
  124. }
  125. },
  126. calculable: true,
  127. grid: [{
  128. top: 65
  129. }],
  130. xAxis: [
  131. {
  132. type: 'category',
  133. data: Config.totalChart[i].category
  134. }
  135. ],
  136. yAxis: [
  137. {
  138. type: 'value'
  139. }
  140. ],
  141. series: [
  142. {
  143. name: Config.totalChart[i].legend_title,
  144. type: 'bar',
  145. data: Config.totalChart[i].data,
  146. markPoint: {
  147. data: [
  148. {type: 'max', name: '最大值'},
  149. {type: 'min', name: '最小值'}
  150. ]
  151. },
  152. markLine: {
  153. data: [
  154. {type: 'average', name: '平均值'}
  155. ]
  156. }
  157. }
  158. ]
  159. };
  160. }
  161. myChart[i] = Echarts.init($('#echarts' + Config.totalChart[i].id)[0], 'walden');
  162. myChart[i].setOption(option[i]);
  163. }
  164. //绑定搜索表单
  165. Form.api.bindevent($("#form1"));
  166. //自定义时间范围
  167. $("#customcharts_datetimerange").data("callback", function (start, end) {
  168. var date = start.format(this.locale.format) + " - " + end.format(this.locale.format);
  169. $(this.element).val(date);
  170. refresh_echart(date);
  171. });
  172. //重新查询数据
  173. var refresh_echart = function (date) {
  174. Fast.api.ajax({
  175. url: 'customcharts/show/index',
  176. data: {date: date},
  177. loading: false
  178. }, function (data) {
  179. for(j = 0,len = data.length; j < len; j++) {
  180. if (option[j].legend != undefined) {
  181. option[j].legend.data = data[j].category;
  182. }
  183. if (option[j].xAxis != undefined) {
  184. option[j].xAxis[0].data = data[j].category;
  185. }
  186. option[j].series[0].data = data[j].data;
  187. myChart[j].clear();
  188. myChart[j].setOption(option[j], true);
  189. }
  190. return false;
  191. });
  192. };
  193. //点击按钮
  194. $(document).on("click", ".btn-filter", function () {
  195. var label = $(this).text();
  196. var obj = $(this).closest("form").find("#customcharts_datetimerange").data("daterangepicker");
  197. var dates = obj.ranges[label];
  198. obj.startDate = dates[0];
  199. obj.endDate = dates[1];
  200. obj.clickApply();
  201. });
  202. //点击刷新
  203. $(document).on("click", ".btn-refresh", function () {
  204. var date = $('#customcharts_datetimerange').val();
  205. refresh_echart(date);
  206. });
  207. //每隔一分钟定时刷新图表
  208. setInterval(function () {
  209. $(".btn-refresh").trigger("click");
  210. }, 60000);
  211. },
  212. api: {
  213. bindevent: function () {
  214. Form.api.bindevent($("form[role=form]"));
  215. }
  216. }
  217. };
  218. return Controller;
  219. });