analyze.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Created by JianJia.Zhou<jianjia.zhou> on 2022/10/3.
  3. */
  4. let AnalyzeChart = (function () {
  5. const version = "0.0.1";
  6. let chartInstance = '';
  7. let AnalyzeChart = function (selector, options) {
  8. return new AnalyzeChart.fn.init(selector, options);
  9. };
  10. AnalyzeChart.fn = AnalyzeChart.prototype = {
  11. AnalyzeChart: version,
  12. options: {btnName: '', searchBtn: '', url: '', yAxisName: '',nameSuffix: '',formName:''},
  13. constructor: AnalyzeChart,
  14. init: function (selector, options) {
  15. this.options = options
  16. chartInstance = echarts.init(selector)
  17. this.buttonEvent()
  18. },
  19. buttonEvent() {
  20. let _this = this
  21. $(`${_this.options.btnName}`).bind("click", function () {
  22. $(this).toggleClass('btn-primary').toggleClass('btn-default')
  23. _this.render()
  24. })
  25. $('.datepicker').datetimepicker({
  26. locale: 'zh-CN',
  27. format: 'YYYY-MM-DD',
  28. });
  29. $(`${_this.options.searchBtn}`).bind('click', function () {
  30. _this.render()
  31. })
  32. },
  33. render() {
  34. let data = this.getLegend();
  35. let categoryData = data.categoryData;
  36. let legendData = data.legendData;
  37. let option = this.chartOptions(legendData);
  38. chartInstance.setOption(option, true);
  39. this.query(legendData, categoryData)
  40. },
  41. query(legendData, categoryData) {
  42. chartInstance.showLoading()
  43. this.getData().then(res => {
  44. let {data} = res
  45. let series = [];
  46. legendData.forEach((item, index) => {
  47. let temp = {
  48. name: item.name,
  49. type: item.type,
  50. data: data[categoryData[index]]
  51. };
  52. series.push(temp);
  53. });
  54. chartInstance.setOption({
  55. xAxis: [{
  56. data: data.dates,
  57. axisPointer: {
  58. type: 'shadow'
  59. },
  60. splitLine: {
  61. show: true
  62. }
  63. }],
  64. series: series
  65. });
  66. chartInstance.hideLoading();
  67. });
  68. },
  69. getLegend() {
  70. let categoryData = [];
  71. let legendData = [];
  72. let _this = this
  73. $(`${_this.options.btnName}.btn-primary`).each(function () {
  74. let id = $(this).data("id");
  75. let name = $(this).text();
  76. let names = {name: `[${name}]${_this.options.nameSuffix}`, type: 'line', barMaxWidth: 30, average: false};
  77. categoryData.push(`analyze_${id}`);
  78. legendData.push(names);
  79. });
  80. return {legendData, categoryData}
  81. },
  82. chartOptions(legends) {
  83. let seriesData = [];
  84. let legendSelected = {};
  85. let colors = ["#0095F3", "#95E04A", "#605EEB", "#4ACDBB", "#FFD480"];
  86. for (let item of legends) {
  87. let series = {
  88. name: item.name,
  89. type: item.type !== undefined ? item.type : 'line',
  90. barWidth: item.barWidth !== undefined ? item.barWidth : '',
  91. yAxisIndex: item.yAxisIndex !== undefined ? item.yAxisIndex : 0,
  92. smooth: true,
  93. data: [],
  94. };
  95. legendSelected[item.name] = item.show !== undefined ? item.show : true;
  96. if (item.average) {
  97. series['markLine'] = {
  98. data: [
  99. {type: 'average', name: '平均值'}
  100. ]
  101. }
  102. }
  103. seriesData.push(series);
  104. }
  105. return {
  106. animation: true,
  107. tooltip: {
  108. trigger: 'axis',
  109. axisPointer: { // 坐标轴指示器,坐标轴触发有效
  110. type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
  111. },
  112. },
  113. toolbox: {
  114. feature: {
  115. // dataView: {show: true, readOnly: false},
  116. magicType: {show: true, type: ['line', 'bar']},
  117. restore: {show: true},
  118. saveAsImage: {show: true}
  119. }
  120. },
  121. /*legend: {
  122. type :'scroll',
  123. left:50,
  124. right:50,
  125. height:500,
  126. data: legend_data,
  127. selected:legend_selected,
  128. },*/
  129. grid: {
  130. left: '4%',
  131. right: '4%',
  132. containLabel: true
  133. },
  134. dataZoom: [{
  135. type: 'slider',
  136. show: true,
  137. xAxisIndex: 0,
  138. yAxisIndex: 1,
  139. start: 0,
  140. end: 100,
  141. }, {
  142. type: 'inside',
  143. xAxisIndex: 0,
  144. yAxisIndex: 1,
  145. start: 0,
  146. end: 100
  147. }],
  148. xAxis: {
  149. type: 'category',
  150. data: [],
  151. //boundaryGap: false
  152. },
  153. yAxis: [
  154. {
  155. name: this.options.yAxisName,
  156. type: 'value',
  157. axisLabel: {
  158. formatter: '{value} '
  159. }
  160. }
  161. ],
  162. series: seriesData,
  163. //color: colors
  164. };
  165. },
  166. getData() {
  167. let productIds = [];
  168. let _this = this
  169. $(`${this.options.btnName}.btn-primary`).each((index, obj) => {
  170. productIds.push($(obj).data('id'))
  171. })
  172. let startAt = $(`${_this.options.formName} #filter-column-name-start`).val();
  173. let endAt = $(`${_this.options.formName} #filter-column-name-end`).val();
  174. let name = $(`${_this.options.formName} .filter-column-name`).val();
  175. let data = {
  176. productIds,
  177. startAt,
  178. endAt,
  179. name
  180. }
  181. return $.post({
  182. url: this.options.url,
  183. data
  184. })
  185. },
  186. }
  187. AnalyzeChart.fn.init.prototype = AnalyzeChart.fn;
  188. return AnalyzeChart;
  189. })()