UNPKG

quoslibero

Version:

FusionCharts JavaScript charting framework

1 lines 8.18 kB
import ScaleCalendar from'./calendar';import{getFloorOfDate,modifyDate}from'../scale-utils';import{pluckNumber}from'../../../fc-core/src/lib';import{getFilterdTimeFormat}from'../time-bucket';import timeConverter from'../time-converter';const DEFAULT_THRESHOLD_PIXELS=4,MAJOR='major',MINOR='minor',CONTEXT='context',DAY_1=86400000,DAY_30=2592000000,DAY_365=31536000000,MONTHS=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],CONTEXT_SEQ=['day','month','year'],dayCount=[31,28,31,30,31,30,31,31,30,31,30,31],showMonthInDayTick=!0,domainToMajorIntervalRatio=[{ratio:4,index:8},{ratio:3,index:7},{ratio:2.5,index:6},{ratio:2,index:5},{ratio:1.5,index:4},{ratio:1,index:3},{ratio:.67,index:2},{ratio:.33,index:1},{ratio:0,index:0}],validate=a=>null!==a&&!isNaN(a),niceMinorTickIndex=a=>{let b,c=domainToMajorIntervalRatio.length,d=1/a;if(4<d)return 1/0;for(b=0;b<c;b++)if(d>domainToMajorIntervalRatio[b].ratio)return domainToMajorIntervalRatio[b].index},contextChange=(a,b,c,d,e)=>{let f=getFloorOfDate(new Date(a),d[c][0].name(),d[c][1],e),g=getFloorOfDate(new Date(b),d[c][0].name(),d[c][1],e);return+f!=+g&&[f,g]},dateAPI=(a,b,c)=>a['get'+c+b](),formatDate=a=>(10>a?'0'+a:a)+'',isLeapYear=a=>0==a%4&&(0!=a%100||0==a%400),getNumberOfDay=(a,b)=>1===b&&isLeapYear(a)?29:dayCount[b],getFullDate=(a,b)=>`${MONTHS[dateAPI(a,'Month',b)]} ${formatDate(dateAPI(a,'Date',b))}, ${dateAPI(a,'FullYear',b)}`,getFullTime=(a,b)=>`${formatDate(dateAPI(a,'Hours',b))}:${formatDate(dateAPI(a,'Minutes',b))}:${formatDate(dateAPI(a,'Seconds',b))}`;class ScaleCalendarBin extends ScaleCalendar{constructor(a,b,c,d,e,f,g,h){super(a,b,c,d,e,f,g,h),this._type=''}getType(){return this._type}showPlotOverTick(){let a=this,b=a._getRangeThreshold(),c=a.getBinMin();return!!(('millisecond'===c[0].name()||-1<CONTEXT_SEQ.indexOf(b[0].name()))&&1===b[1])}getBinBounds(a,b=0){let c,e,f,g,h=this,j=h.getType(),k=h._getRangeThreshold(),l=k[2],m=h.getDomainValue(a),d=(m.getTime()-b)/l,n=dateAPI(m,'FullYear',j),o=dateAPI(m,'Month',j),p=dateAPI(m,'Date',j),q=dateAPI(m,'Hours',j),r=0;if('month'===k[0].name()){for(c=0;12>c;c+=k[1])if(o>=c&&o<c+k[1]){h.showPlotOverTick()&&14<p&&(c+=1);break}f=+new Date(m.getFullYear()+r,c,1),g=+new Date(m.getFullYear()+r,c+k[1],1)}else if('day'===k[0].name()&&h.showPlotOverTick()){for(c=0,e=getNumberOfDay(n,o);c<=e;c++)if(c===p){11<q&&(c+=1);break}f=+new Date(m.getFullYear(),o,c),g=+new Date(m.getFullYear(),o,c+1)}else 1>=l&&-1==d&&(d=0),f=b+Math.floor(d)*l,g=b+Math.ceil(d)*l;return{startDate:f,endDate:g}}getBinIndex(a,b){var c=Math.floor;let d,e,f,g,h,i,j=this,k=j._getRangeThreshold(),l=j.showPlotOverTick()?Math.round:c,m=new Date(b),n=new Date(a),o=k[2];return'year'===k[0].name()?k[0].count(b,a):'month'===k[0].name()?(g=dateAPI(m,'FullYear',j.getType()),h=dateAPI(n,'FullYear',j.getType()),e=dateAPI(m,'Month',j.getType()),f=dateAPI(n,'Month',j.getType()),d=Math.max(0,h-g-1),g===h?i=c(f/k[1])-c(e/k[1]):(i=12*d/k[1],i+=12/k[1]-c(e/k[1]),i+=c(f/k[1])),i):l((a-b)/o)}getRangeValue(a,b){return'undefined'==typeof b?super.getRangeValue(a):this.showPlotOverTick()?super.getRangeValue(a):(super.getRangeValue(a)+super.getRangeValue(b))/2}calculateIndexOfIntervals(){let a,b,c=this,d=c.thresholdIntervals,e=d.length;for(c.intervalIndexMap=a={},b=e-1;0<=b;b--)a[d[b][0].name()]=b}getNiceMinorTickInterval(a,b){let c,d,e,f,g=this,h=g.thresholdIntervals,j=g.getDomain(),k=g._getRangeThreshold()[2],l=h[a-1][0].name(),m=[];for(c=0,e=h.length;c<e;c++)if(h[c][2]>=k){f=c;break}for(c=a-1;c>=f&&h[c][0].name()===l;c--)h[c][2]%k||m.push(c);return d=Math.min(niceMinorTickIndex((j[1]-j[0])/b),m.length-1),pluckNumber(m[d],-1)}getNiceMajorTickInterval(a,b=!1){let c,d=this,e=d.thresholdIntervals,f=e.length,g=d._getRangeThreshold(),h=g[2],j=d.getMode();if('random'===j){if(!b&&'day'===e[a][0].name()&&(1<e[a][1]||e[a][2]>h)){for(c=a;c<f;c++)if('day'!==e[c][0].name()&&h<e[c][2])return c;}else for(c=a;c<f;c++)if(e[c][2]>h)return c;}else if(1!==e[a][1]||e[a][2]<=h)for(c=a;c<f;c++)if(1===e[c][1]&&e[c][2]>h)return c;return a}generateContextTicks(a,b=!1){let c,d,e,f,g=this,h=g.thresholdIntervals,i=g.intervalIndexMap,k=g.getDomain(),l=k[0],m=k[1],n=h[a][0].name(),o=CONTEXT_SEQ[CONTEXT_SEQ.indexOf(n)+1],p=h.length,q=i.day,r=-1,s=[];if(b)r=a;else for('day'===n&&showMonthInDayTick&&(o='year'),c=Math.max(a,q);c<p;c++)if(h[c][0].name()===o){r=c;break}if(-1<r){if(d=getFloorOfDate(new Date(l),h[r][0].name(),h[r][1],g.getType()),f=contextChange(new Date(l),new Date(m),r,h,g.getType())){for(e=f[1],f=e-h[r][2];f>=+l;)s.push(new Date(f)),f-=h[r][2];+l!=+d&&s.push(new Date(d)),s.reverse(),s.push(new Date(e))}else s.push(d);g._timeFormat.context=h[r][0].name()}return s}getMajorIntervalGap(a,b){let c=this,d=c.thresholdIntervals;return'month'===d[a][0].name()?DAY_30:'year'===d[a][0].name()?DAY_365:b[1]-b[0]}ticks(a={},b){let c,d,e,f,g,h,k,l,m,n,o,p,q=this,r=q.thresholdIntervals,s=q.getDomain(),t=!1,u=[],v=s[0],w=s[1],x=[],y=[],z=q.getType();if(!validate(v)||!validate(w))return q._tickType=[],[];for(q._timeFormat=p={},o=w-v+1,c=e=r.length-1;0<=c;c--)if(1<Math.floor(o/r[c][2])){for(f=q.getNiceMajorTickInterval(c,b),g=r[f][0].name(),v=getFloorOfDate(new Date(v),g,r[f][1],z),w=modifyDate(new Date(w),g,r[f][1],!1,z),w=getFloorOfDate(new Date(w),g,r[f][1],z),k=r[f][0].range(+v,+w+r[f][2],r[f][1]),p.major=g,d=0;d<k.length;d++)+k[d]>=+s[0]&&+k[d]<=+s[1]&&(t=!0);u=q.generateContextTicks(f,!t)||[];break}if(!k)return q._tickType=[],x;for(h='year'===r[f][0].name()&&10<k.length?-1:q.getNiceMinorTickInterval(f,q.getMajorIntervalGap(f,k)),c=0,e=k.length;c<e-1;c++)if(x.push(k[c]),y.push(MAJOR),-1<h&&!a.minor)for(l=r[h][0].range(+k[c],+k[c+1]+1,r[h][1]),p.minor=r[h][0].name(),(d=0,m=l.length);d<m;d++)(n=+l[d],!(d===m-1&&1<m&&.5>(+k[c+1]-n)/(n-+l[d-1])))&&n!==+k[c]&&n!==+k[c+1]&&(x.push(l[d]),y.push(MINOR));if(x.push(k[e-1]),y.push(MAJOR),u.length&&!a.context)for(c=0,e=u.length;c<e;c++)x.push(u[c]),y.push(CONTEXT);return q._tickType=y,x}setDomain(a=[]){let b;return a[0]>a[1]&&([a[1],a[0]]=[a[0],a[1]]),b=super.setDomain(a),b}nice(a,b){let c=super.nice(a,b);return this._computeRangeThreshold(DEFAULT_THRESHOLD_PIXELS),c}getFormattedTime(a={}){let b,c,d,e,f,g,h,i,j,k,l,m=this,n=m._getRangeThreshold(),o=a.dateRange,p=a.type,q=n[1],r=m.getType();return'crossline'===p||'tooltip'===p?(b=n[0].name(),c=d=getFilterdTimeFormat('%b %d, %Y, %I:%M:%S.%L %p',b),'hour'===b&&(c=c.replace(/%M/,''),d=d.replace(/%M/,'')),c=c.replace(/[:|.]*[\s]/g,' '),d=d.replace(/[:|.]*[\s]/g,' '),c.match(/%I/)||(o.endDate-=1,c=c.replace(/%p/,''),d=d.replace(/%p/,'')),f=new Date(o.startDate),g=new Date(o.endDate),j=dateAPI(f,'Date',r)===dateAPI(g,'Date',r),k=dateAPI(f,'Month',r)===dateAPI(g,'Month',r),l=dateAPI(f,'FullYear',r)===dateAPI(g,'FullYear',r),1<q?l&&(k?j?c.match(/%I/)?(c=c.replace(/%p/,''),d=d.replace(/%b/,''),d=d.replace(/%d/,''),d=d.replace(/%Y/,'')):(c=c.replace(/%b/,'%B'),d=''):(c=c.replace(/%Y/,''),!c.match(/%I/)&&(d=d.replace(/%b/,''),c=c.replace(/%b/,'%B'))):1!==q&&(c=c.replace(/%Y/,''))):!c.match(/%I/)&&(c=c.replace(/%b/,'%B')),c=c.trim(),d=d.trim(),c=c.replace(/^[,|\s|:]*/,'').replace(/(\W+$)/,'').replace(/([,]+[\s]*[,]+)|([\s]+[,]+)/g,','),d=d.replace(/^[,|\s|:]*/,'').replace(/(\W+$)/,'').replace(/([,]+[\s]*[,]+)|([\s]+[,]+)/g,','),d&&1!==q?(e='UTC'===r?timeConverter.utcFormatter(c):timeConverter.formatter(c),h=e.format(new Date(o.startDate)),e='UTC'===r?timeConverter.utcFormatter(d):timeConverter.formatter(d),i=e.format(new Date(o.endDate)),(h+' - '+i).trim()):(e='UTC'===r?timeConverter.utcFormatter(c):timeConverter.formatter(c),h=e.format(new Date(o.startDate)),h.trim())):'CRS'===p?(h=getFullDate(o.startDate,r)+(a.showTimeInLabel?', '+getFullTime(o.startDate,r):''),i=getFullDate(o.endDate,r)+(a.showTimeInLabel?', '+getFullTime(o.endDate,r):''),(h+' - '+i).trim()):void 0}setBinMin(a){return this.minBin=a,this}getBinMin(){return this.minBin}setRangeThreshold(a){return this._threshold=a,this}_getRangeThreshold(){return this._threshold}setMode(a){this.mode=a}getMode(){return this.mode}setThresholdIntervals(a){this.thresholdIntervals=a,this.calculateIndexOfIntervals()}}export{modifyDate,getFloorOfDate};export default ScaleCalendarBin;