show.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. data: Config.totalChart[i].legend_title
  62. },
  63. toolbox: {
  64. show: true,
  65. feature: {
  66. dataView: {show: true, readOnly: false},
  67. magicType: {show: true, type: ['line', 'bar']},
  68. restore: {show: true},
  69. saveAsImage: {show: true}
  70. }
  71. },
  72. calculable: true,
  73. grid: [{
  74. top: 65
  75. }],
  76. xAxis: [
  77. {
  78. type: 'category',
  79. data: Config.totalChart[i].category
  80. }
  81. ],
  82. yAxis: [
  83. {
  84. type: 'value'
  85. }
  86. ],
  87. series: Config.totalChart[i].series
  88. };
  89. } else {
  90. option[i] = {
  91. title: {
  92. text: Config.totalChart[i].title,
  93. subtext: Config.totalChart[i].subtext
  94. },
  95. tooltip: {
  96. trigger: 'axis',
  97. formatter: Config.totalChart[i].unit
  98. },
  99. legend: {
  100. data: Config.totalChart[i].legend_title
  101. },
  102. toolbox: {
  103. show: true,
  104. feature: {
  105. dataView: {show: true, readOnly: false},
  106. magicType: {show: true, type: ['line', 'bar']},
  107. restore: {show: true},
  108. saveAsImage: {show: true}
  109. }
  110. },
  111. calculable: true,
  112. grid: [{
  113. top: 65
  114. }],
  115. xAxis: [
  116. {
  117. type: 'category',
  118. data: Config.totalChart[i].category
  119. }
  120. ],
  121. yAxis: [
  122. {
  123. type: 'value'
  124. }
  125. ],
  126. series: Config.totalChart[i].series
  127. };
  128. }
  129. myChart[i] = Echarts.init($('#echarts' + Config.totalChart[i].id)[0], 'walden');
  130. myChart[i].setOption(option[i]);
  131. }
  132. //绑定搜索表单
  133. Form.api.bindevent($("#form1"));
  134. //自定义时间范围
  135. $("#customcharts_datetimerange").data("callback", function (start, end) {
  136. var date = start.format(this.locale.format) + " - " + end.format(this.locale.format);
  137. $(this.element).val(date);
  138. refresh_echart(date);
  139. });
  140. //重新查询数据
  141. var refresh_echart = function (date) {
  142. Fast.api.ajax({
  143. url: 'customcharts/show/index',
  144. data: {date: date},
  145. loading: false
  146. }, function (data) {
  147. for(j = 0,len = data.length; j < len; j++) {
  148. if (data[j].chart_type == 'pie') {
  149. if (option[j].legend != undefined) {
  150. option[j].legend.data = data[j].category;
  151. }
  152. if (option[j].xAxis != undefined) {
  153. option[j].xAxis[0].data = data[j].category;
  154. }
  155. option[j].series[0].data = data[j].data;
  156. } else {
  157. if (option[j].legend != undefined) {
  158. option[j].legend.data = data[j].legend_title;
  159. }
  160. if (option[j].xAxis != undefined) {
  161. option[j].xAxis[0].data = data[j].category;
  162. }
  163. option[j].series = data[j].series;
  164. }
  165. myChart[j].clear();
  166. myChart[j].setOption(option[j], true);
  167. }
  168. return false;
  169. });
  170. };
  171. //点击按钮
  172. $(document).on("click", ".btn-filter", function () {
  173. var label = $(this).text();
  174. var obj = $(this).closest("form").find("#customcharts_datetimerange").data("daterangepicker");
  175. var dates = obj.ranges[label];
  176. obj.startDate = dates[0];
  177. obj.endDate = dates[1];
  178. obj.clickApply();
  179. });
  180. //点击刷新
  181. $(document).on("click", ".btn-refresh", function () {
  182. var date = $('#customcharts_datetimerange').val();
  183. refresh_echart(date);
  184. });
  185. //每隔一分钟定时刷新图表
  186. setInterval(function () {
  187. $(".btn-refresh").trigger("click");
  188. }, 60000);
  189. },
  190. api: {
  191. bindevent: function () {
  192. Form.api.bindevent($("form[role=form]"));
  193. }
  194. }
  195. };
  196. return Controller;
  197. });