analyze.js 7.0 KB

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