dashboard.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. define(['jquery', 'bootstrap', 'backend', 'addtabs', 'echarts', 'echarts-theme'], function ($, undefined, Backend, undefined, Echarts) {
  2. var Controller = {
  3. index: function () {
  4. var procChart = null;
  5. var chartData = Config.procuremenChart || {};
  6. var chartEl = document.getElementById('procuremen-echart');
  7. function toNumArr(arr) {
  8. if (!$.isArray(arr)) {
  9. return [];
  10. }
  11. return arr.map(function (v) {
  12. var n = parseInt(v, 10);
  13. return isNaN(n) || n < 0 ? 0 : n;
  14. });
  15. }
  16. function buildOption() {
  17. var labels = chartData.labels || [];
  18. var seriesMeta = [
  19. {name: '待确认', key: 'confirm', color: '#f0ad4e'},
  20. {name: '待审批', key: 'approval', color: '#7266ba'},
  21. {name: '已完结', key: 'completed', color: '#27C24C'}
  22. ];
  23. var maxVal = 0;
  24. var seriesData = {};
  25. seriesMeta.forEach(function (meta) {
  26. var nums = toNumArr(chartData[meta.key]);
  27. seriesData[meta.key] = nums;
  28. nums.forEach(function (n) {
  29. if (n > maxVal) {
  30. maxVal = n;
  31. }
  32. });
  33. });
  34. // 顶部留白,避免折线贴顶;至少显示到 2,刻度更易读
  35. var yMax = maxVal <= 0 ? 2 : Math.max(maxVal + 1, Math.ceil(maxVal * 1.2));
  36. return {
  37. color: seriesMeta.map(function (m) {
  38. return m.color;
  39. }),
  40. title: {
  41. text: '近7天单数',
  42. left: 8,
  43. top: 8,
  44. textStyle: {
  45. fontSize: 13,
  46. fontWeight: 600,
  47. color: '#555'
  48. }
  49. },
  50. tooltip: {
  51. trigger: 'axis',
  52. axisPointer: {type: 'line', lineStyle: {color: '#ddd'}},
  53. backgroundColor: 'rgba(50,50,50,0.92)',
  54. borderWidth: 0,
  55. textStyle: {fontSize: 12}
  56. },
  57. legend: {
  58. data: seriesMeta.map(function (m) {
  59. return m.name;
  60. }),
  61. top: 8,
  62. right: 12,
  63. itemWidth: 10,
  64. itemHeight: 10,
  65. textStyle: {fontSize: 11}
  66. },
  67. grid: {
  68. left: 16,
  69. right: 20,
  70. top: 44,
  71. bottom: 16,
  72. containLabel: true
  73. },
  74. xAxis: {
  75. type: 'category',
  76. boundaryGap: false,
  77. data: labels,
  78. axisTick: {show: false},
  79. axisLabel: {
  80. fontSize: 11,
  81. color: '#888',
  82. margin: 8,
  83. interval: 0
  84. },
  85. axisLine: {lineStyle: {color: '#e8e8e8'}}
  86. },
  87. yAxis: {
  88. type: 'value',
  89. min: 0,
  90. max: yMax,
  91. minInterval: 1,
  92. splitNumber: Math.min(5, yMax),
  93. axisLabel: {
  94. fontSize: 11,
  95. color: '#888',
  96. formatter: function (v) {
  97. return parseInt(v, 10) || 0;
  98. }
  99. },
  100. splitLine: {lineStyle: {color: '#f0f0f0', type: 'dashed'}}
  101. },
  102. series: seriesMeta.map(function (meta) {
  103. return {
  104. name: meta.name,
  105. type: 'line',
  106. // 轻度平滑:避免 true 时稀疏数据贝塞尔过冲(贴底/右上尖刺)
  107. smooth: 0.25,
  108. clip: true,
  109. showSymbol: true,
  110. symbol: 'circle',
  111. symbolSize: 6,
  112. connectNulls: false,
  113. lineStyle: {width: 2, color: meta.color},
  114. itemStyle: {color: meta.color, borderWidth: 2, borderColor: '#fff'},
  115. emphasis: {focus: 'series', lineStyle: {width: 3}},
  116. data: seriesData[meta.key]
  117. };
  118. })
  119. };
  120. }
  121. function safeResize() {
  122. if (!procChart || !chartEl) {
  123. return;
  124. }
  125. var w = chartEl.clientWidth;
  126. var h = chartEl.clientHeight;
  127. if (w < 40 || h < 40) {
  128. return;
  129. }
  130. procChart.resize({width: w, height: h});
  131. }
  132. function renderChart() {
  133. if (!chartEl) {
  134. return;
  135. }
  136. if (!procChart) {
  137. procChart = Echarts.init(chartEl, 'walden');
  138. }
  139. procChart.setOption(buildOption(), true);
  140. safeResize();
  141. }
  142. if (chartEl) {
  143. renderChart();
  144. }
  145. $(window).on('resize.procuremenDash', function () {
  146. safeResize();
  147. });
  148. // 侧栏折叠 / 标签切换后容器尺寸变化,需再 resize
  149. $(document).on('expanded.pushMenu collapsed.pushMenu', function () {
  150. setTimeout(safeResize, 280);
  151. });
  152. $(document).on('shown.bs.tab click', '.nav-addtabs a, [addtabs]', function () {
  153. setTimeout(safeResize, 120);
  154. });
  155. setTimeout(safeResize, 120);
  156. setTimeout(safeResize, 400);
  157. setTimeout(function () {
  158. // 尺寸仍异常时强制按容器重绘一次
  159. if (chartEl && procChart && (chartEl.clientHeight < 80 || chartEl.clientWidth < 80)) {
  160. renderChart();
  161. } else {
  162. safeResize();
  163. }
  164. }, 800);
  165. $(document).on('click', '.btn-refresh', function () {
  166. setTimeout(safeResize, 0);
  167. });
  168. }
  169. };
  170. return Controller;
  171. });