analyze.js 7.0 KB

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