common.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const percent = 'percent';
  2. let intFields = ['flowerLength','yield'];
  3. //农事颜色数组
  4. let rongShiColors = ["#548235","#4472C4","#FFD966","#C5E0B4","#F8CBAD",'#3FD0F9','#4962FC','#d223e7','#fe8104','#3FD0F9'];
  5. function getTwoMonthDate(){
  6. let now = new Date();
  7. // 生成日期数组
  8. let dateArray = [];
  9. for(let i = 0; i < 60; i++) {
  10. let date = new Date(now.getTime());
  11. date.setDate(date.getDate() - i);
  12. dateArray.push(date.toISOString().slice(0,10));
  13. }
  14. dateArray = dateArray.reverse();
  15. return dateArray;
  16. }
  17. function getDatesBetween(startDate, endDate) {
  18. const start = new Date(startDate);
  19. const end = new Date(endDate);
  20. const dates = [];
  21. let current = start;
  22. while (current <= end) {
  23. dates.push(new Date(current).toISOString().slice(0,10));
  24. current.setDate(current.getDate() + 1);
  25. }
  26. return dates;
  27. }
  28. function getTwoMonthDateStr(){
  29. let now = new Date();
  30. let date = new Date(now.getTime());
  31. // 生成日期数组
  32. date.setDate(date.getDate() - 60);
  33. let str1 = date.toISOString().slice(0,10);;
  34. let str2 = now.toISOString().slice(0,10);
  35. return [str1,str2];
  36. }
  37. function getLegendData(nsType){
  38. let arr = [];
  39. for(let i=0;i<nsType.length;i++){
  40. let item = nsType[i];
  41. arr.push({label:item.name, color:rongShiColors[i]})
  42. }
  43. return arr;
  44. }
  45. function getXAxis(){
  46. let date = getTwoMonthDate();
  47. return date;
  48. }
  49. function getLegend(names){
  50. let legend = {
  51. "orient":"horizontal",
  52. "right":50,
  53. "top":0,
  54. "itemWidth":20,
  55. "itemHeight":20,
  56. "align":"right",
  57. "width":"100%",
  58. "itemGap":5,
  59. "show":false,
  60. "data":[]
  61. }
  62. legend.data = names;
  63. return legend;
  64. }
  65. function getSeries(currentPeriod,targetName,targetField,list){
  66. let nsType = currentPeriod.nsType;
  67. let id = currentPeriod.id;
  68. let periodMame = currentPeriod.name;
  69. let series = [];
  70. let serie = {'name':targetName,"type":"line","symbolSize":5,"data":[],"field":targetField}
  71. series.push(serie);
  72. list.forEach(item=>{
  73. if(item.periodId == id){
  74. series.find(function(serie){
  75. if(item[serie.field]){
  76. let subItem = {"id":item.id,"value":item[serie.field],"createDate":item.createDate,"img":item.img};
  77. serie.data.push(subItem);
  78. }
  79. })
  80. }
  81. })
  82. return series;
  83. }
  84. function getMaxValue(targetField,periodId,list){
  85. if(intFields.indexOf(targetField) == -1){
  86. return 1;
  87. }
  88. let maxValue = 0;
  89. list.forEach(item=>{
  90. if(item.id == periodId && item[targetField] > maxValue){
  91. maxValue = item[targetField];
  92. }
  93. })
  94. return maxValue;
  95. }
  96. function getYAxisParams(targetField,currentPeriod,list,percent){
  97. let periodId = currentPeriod.id;
  98. let maxValue = getMaxValue(targetField,periodId,list);
  99. let nsType = currentPeriod.nsType;
  100. let nzNames = [...nsType].map(item=>item.name);
  101. let nzLength = nzNames.length == 0?2:nzNames.length;
  102. let numberInterval = Math.ceil(maxValue/nzLength/3);
  103. let interval = percent?0.1:numberInterval;
  104. let max = numberInterval*nzLength*3+numberInterval//1.1*maxValue;
  105. let min = -interval*3-3*interval*(nzLength-1);
  106. // let max = -min;
  107. if(percent){
  108. max = 1;
  109. }
  110. let d = -3*interval;
  111. let v = -2*interval;
  112. let nzv = [];
  113. for(let j = 0;j < nzLength;j++){
  114. nzv.push(v+d*j)
  115. }
  116. return [min,max,interval,nzNames,nzv,Math.ceil(maxValue/numberInterval)];
  117. }
  118. function getNongDataTotal(list){
  119. let newList = [];
  120. for(let i = 0;i<list.length;i++){
  121. let subItem = list[i];
  122. let a = newList.find(function(item){
  123. return item.actionName == subItem.actionName &&
  124. item.nsTypeId == subItem.nsTypeId &&
  125. item.endDate == subItem.endDate &&
  126. item.startDate == subItem.startDate &&
  127. item.periodName == subItem.periodName; //&&
  128. // item.createdName == subItem.createdName &&
  129. // item.executorName == subItem.executorName
  130. });
  131. if(!a){
  132. newList.push(subItem);
  133. }
  134. }
  135. return newList;
  136. }
  137. function getNSData(list,currentPeriod,nzv){
  138. let nsSeries = [];
  139. //农事类型
  140. let nsType = currentPeriod.nsType;
  141. let id = currentPeriod.id;
  142. let periodName = currentPeriod.name;
  143. nsType.forEach((item,index)=>{
  144. let serie = {
  145. "type":"line",
  146. "symbol":"circle",
  147. "symbolSize":6,
  148. "lineStyle":{"color":"#00000000"},
  149. "itemStyle":{"borderWidth":1,"borderColor":"#000000","color":rongShiColors[index]},
  150. "data":getTemplate(),
  151. "name":item.name
  152. }
  153. nsSeries.push(serie);
  154. })
  155. list.forEach(item=>{
  156. if(item.periodName == periodName){
  157. let createDate = item.startDate;
  158. let endDate = item.endDate;
  159. let nsTypeId = item.nsTypeId;
  160. nsType.forEach(function(item1,index){
  161. if(item1.id == nsTypeId){
  162. let value = nzv[index];
  163. let itemStyle = {"color":"#ffffff"};
  164. let serie = nsSeries[index];
  165. let sData = serie.data.find(item=>item.createDate == createDate);
  166. let eData = serie.data.find(item=>item.createDate == endDate);
  167. debugger;
  168. if(sData){
  169. delete sData.mid;
  170. sData.value = value;
  171. //开始的白色圆点
  172. sData.itemStyle = itemStyle;
  173. }
  174. if(eData){
  175. delete eData.mid;
  176. //结束的默认颜色圆点
  177. eData.value = value;
  178. }
  179. }
  180. })
  181. }
  182. });
  183. debugger;
  184. return nsSeries;
  185. }
  186. function getTemplate(){
  187. let arr = getTwoMonthDate();
  188. let list = [];
  189. arr.forEach(function(item,index){
  190. let option = {"value":null,"mid":"mid",createDate:item};
  191. list.push(option);
  192. })
  193. return list;
  194. }
  195. export {
  196. getXAxis,
  197. getLegend,
  198. getSeries,
  199. getMaxValue,
  200. getYAxisParams,
  201. getLegendData,
  202. getNongDataTotal,
  203. getTwoMonthDateStr,
  204. getNSData
  205. }