utils.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. export default {
  2. compareDate:function(d1,d2){
  3. return d1.getTime()-d2.getTime();
  4. },
  5. roundHour:function(date,add){
  6. add=add||0;
  7. var h=Math.floor(date/(1000*60*60));
  8. return (h+add)*1000*60*60;
  9. },
  10. diffDate:function(d1,d2,tag){
  11. tag=tag||"HH";
  12. var diff=d1-d2;
  13. if(tag=="HH"){
  14. return diff/(1000*60*60);
  15. }
  16. else if(tag=="mm"){
  17. return diff/(1000*60);
  18. }
  19. },
  20. parseDate:function(str){
  21. str=str.replace(/-/g,"/");
  22. return new Date(str);
  23. },
  24. format:function(millis,fmt){
  25. fmt=fmt||"MM.dd HH:00";
  26. var date=new Date(millis);
  27. if(fmt=="MM.dd HH:00"){
  28. return (date.getMonth()+1)+"."+date.getDate()+" "+date.getHours()+":00";
  29. }
  30. if(fmt=="MM.dd HH:mm"){
  31. return (date.getMonth()+1)+"."+date.getDate()+" "+date.getHours()+":"+date.getMinutes();
  32. }
  33. if(fmt=="MM.dd HH:mm:ss"){
  34. return (date.getMonth()+1)+"."+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
  35. }
  36. return null;
  37. },
  38. getProjValDevider:function(minVal,maxVal,count){
  39. var opts=[0.1,0.2,0.5,1,2,3,4,5,10,15,20,25,30,35,40,45,50];
  40. var devider=(maxVal-minVal)/count;
  41. if(devider>50){
  42. return Math.ceil(devider/50)*50;
  43. }
  44. else{
  45. var idx=this.search(devider,opts);
  46. return opts[idx];
  47. }
  48. },
  49. search:function(item,ary){
  50. for (var i= 0, len= ary.length; i < len; i++) {
  51. if (ary[i]>=item) {
  52. return i;
  53. }
  54. }
  55. return ary.length - 1;
  56. },
  57. waveFilter:function(datas,winsize){
  58. let fileterWin=[]
  59. let sumWin=0
  60. let outItm=null
  61. let rst=datas.map((data,index)=>{
  62. if(index>=winsize){
  63. outItm=fileterWin.shift()
  64. sumWin-=outItm[1]
  65. }
  66. fileterWin.push(data)
  67. sumWin+=data[1]
  68. return [data[0],sumWin/fileterWin.length]
  69. })
  70. return rst
  71. }
  72. }