123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- const percent = 'percent';
- let intFields = ['flowerLength','yield'];
- //农事颜色数组
- let rongShiColors = ["#548235","#4472C4","#FFD966","#C5E0B4","#F8CBAD",'#3FD0F9','#4962FC','#d223e7','#fe8104','#3FD0F9'];
- function getTwoMonthDate(){
- let now = new Date();
- // 生成日期数组
- let dateArray = [];
- for(let i = 0; i < 60; i++) {
- let date = new Date(now.getTime());
- date.setDate(date.getDate() - i);
- dateArray.push(date.toISOString().slice(0,10));
- }
- dateArray = dateArray.reverse();
- return dateArray;
- }
- function getDatesBetween(startDate, endDate) {
- const start = new Date(startDate);
- const end = new Date(endDate);
- const dates = [];
- let current = start;
- while (current <= end) {
- dates.push(new Date(current).toISOString().slice(0,10));
- current.setDate(current.getDate() + 1);
- }
- return dates;
- }
- function getTwoMonthDateStr(){
- let now = new Date();
- let date = new Date(now.getTime());
- // 生成日期数组
- date.setDate(date.getDate() - 60);
- let str1 = date.toISOString().slice(0,10);;
- let str2 = now.toISOString().slice(0,10);
- return [str1,str2];
- }
- function getLegendData(nsType){
- let arr = [];
- for(let i=0;i<nsType.length;i++){
- let item = nsType[i];
- arr.push({label:item.name, color:rongShiColors[i]})
- }
- return arr;
- }
- function getXAxis(){
- let date = getTwoMonthDate();
- return date;
- }
- function getLegend(names){
- let legend = {
- "orient":"horizontal",
- "right":50,
- "top":0,
- "itemWidth":20,
- "itemHeight":20,
- "align":"right",
- "width":"100%",
- "itemGap":5,
- "show":false,
- "data":[]
- }
- legend.data = names;
- return legend;
- }
- function getSeries(currentPeriod,targetName,targetField,list){
- let nsType = currentPeriod.nsType;
- let id = currentPeriod.id;
- let periodMame = currentPeriod.name;
- let series = [];
- let serie = {'name':targetName,"type":"line","symbolSize":5,"data":[],"field":targetField}
- series.push(serie);
- list.forEach(item=>{
- if(item.periodId == id){
- series.find(function(serie){
- if(item[serie.field]){
- let subItem = {"id":item.id,"value":item[serie.field],"createDate":item.createDate,"img":item.img};
- serie.data.push(subItem);
- }
- })
- }
- })
- return series;
- }
- function getMaxValue(targetField,periodId,list){
- if(intFields.indexOf(targetField) == -1){
- return 1;
- }
- let maxValue = 0;
- list.forEach(item=>{
- if(item.id == periodId && item[targetField] > maxValue){
- maxValue = item[targetField];
- }
- })
- return maxValue;
- }
- function getYAxisParams(targetField,currentPeriod,list,percent){
- let periodId = currentPeriod.id;
- let maxValue = getMaxValue(targetField,periodId,list);
- let nsType = currentPeriod.nsType;
- let nzNames = [...nsType].map(item=>item.name);
- let nzLength = nzNames.length == 0?2:nzNames.length;
- let numberInterval = Math.ceil(maxValue/nzLength/3);
- let interval = percent?0.1:numberInterval;
- let max = numberInterval*nzLength*3+numberInterval//1.1*maxValue;
- let min = -interval*3-3*interval*(nzLength-1);
- // let max = -min;
- if(percent){
- max = 1;
- }
- let d = -3*interval;
- let v = -2*interval;
- let nzv = [];
- for(let j = 0;j < nzLength;j++){
- nzv.push(v+d*j)
- }
- return [min,max,interval,nzNames,nzv,Math.ceil(maxValue/numberInterval)];
- }
- function getNongDataTotal(list){
- let newList = [];
- for(let i = 0;i<list.length;i++){
- let subItem = list[i];
- let a = newList.find(function(item){
- return item.actionName == subItem.actionName &&
- item.nsTypeId == subItem.nsTypeId &&
- item.endDate == subItem.endDate &&
- item.startDate == subItem.startDate &&
- item.periodName == subItem.periodName; //&&
- // item.createdName == subItem.createdName &&
- // item.executorName == subItem.executorName
- });
- if(!a){
- newList.push(subItem);
- }
- }
- return newList;
- }
- function getNSData(list,currentPeriod,nzv){
- let nsSeries = [];
- //农事类型
- let nsType = currentPeriod.nsType;
- let id = currentPeriod.id;
- let periodName = currentPeriod.name;
- nsType.forEach((item,index)=>{
- let serie = {
- "type":"line",
- "symbol":"circle",
- "symbolSize":6,
- "lineStyle":{"color":"#00000000"},
- "itemStyle":{"borderWidth":1,"borderColor":"#000000","color":rongShiColors[index]},
- "data":getTemplate(),
- "name":item.name
- }
- nsSeries.push(serie);
- })
- list.forEach(item=>{
- if(item.periodName == periodName){
- let createDate = item.startDate;
- let endDate = item.endDate;
- let nsTypeId = item.nsTypeId;
- nsType.forEach(function(item1,index){
- if(item1.id == nsTypeId){
- let value = nzv[index];
- let itemStyle = {"color":"#ffffff"};
- let serie = nsSeries[index];
- let sData = serie.data.find(item=>item.createDate == createDate);
- let eData = serie.data.find(item=>item.createDate == endDate);
- debugger;
- if(sData){
- delete sData.mid;
- sData.value = value;
- //开始的白色圆点
- sData.itemStyle = itemStyle;
- }
- if(eData){
- delete eData.mid;
- //结束的默认颜色圆点
- eData.value = value;
- }
- }
- })
- }
- });
- debugger;
- return nsSeries;
- }
- function getTemplate(){
- let arr = getTwoMonthDate();
- let list = [];
- arr.forEach(function(item,index){
- let option = {"value":null,"mid":"mid",createDate:item};
- list.push(option);
- })
- return list;
- }
- export {
- getXAxis,
- getLegend,
- getSeries,
- getMaxValue,
- getYAxisParams,
- getLegendData,
- getNongDataTotal,
- getTwoMonthDateStr,
- getNSData
- }
|