UNPKG

quoslibero

Version:

FusionCharts JavaScript charting framework

1 lines 15.6 kB
import{SmartRenderer}from'../../../../fc-core/src/component-interface';import ScaleLinear from'./../../../../fc-core/src/axis/scales/linear';import{safeMax,getSuggestiveRotation,pluckNumber,UNDEF}from'./../../../../fc-core/src/lib';const identity=a=>a,isNil=a=>null===a||'undefined'==typeof a,TICKMAJOR='tick-mark-major',TICKMINOR='tick-mark-minor',LABELMAJOR='label-major',LABELMINOR='label-minor',LABELCONTEXT='label-context',START='start',END='end',MIDDLE='middle',TOP='top',RIGHT='right',BOTTOM='bottom',LEFT='left',X='x',Y='y',W='W',L='L',MAJOR='major',MINOR='minor',CONTEXT='context',DEFAULT_TICK_SIZE=12,DEFAULT_TICK_PADDING=1,DEFAULT_ROTATION=90,DEFAULT_TRANSFORM='t0,0',DEFAULT_LABEL_PADDING=5,GUTTER_2=2,mathMax=Math.max,mathMin=Math.min,ceil=Math.ceil,floor=Math.floor,translateX=a=>`t${a},0`,translateY=a=>`t0,${a}`,number=a=>b=>+a.getRangeValue(b),center=a=>b=>+a.getRangeValue(b)+a.getBandwidth()/2,getFormatter=(a,b,c)=>{if(a&&b)return a[b[c]]},getBuffer=(a,b=12)=>{let c=15>=b?0:mathMax(0,floor(b/10)),d='WW';for(c=mathMin(ceil(a/2),c);c<a;)d+=c%2?W:L,c++;return d},overlaps=(a,b)=>!(a.x>b.x+b.width||b.x>a.x+a.width)&&!(a.y>b.y+b.height||b.y>a.y+a.height),resetTickInfo=a=>{a.text='',a.dim.width=0,a.dim.height=0},getTextAnchor=a=>{let b;return b=a===LEFT?END:a===RIGHT?START:MIDDLE,b},isWithinDomain=(a,b,c,d)=>{let e;return e=b===START?c.x:b===END?c.x-a.width:c.x-a.width/2,!!(0<=e&&e+a.width<=d.width)};class Axis extends SmartRenderer{getType(){return'axis'}getName(){return'axis'}__setDefaultConfig(){super.__setDefaultConfig();let a=this.config;this.setScale(new ScaleLinear),this.setAlignment(LEFT),this.setTickArguments(),this.setTickValues(),this.setReverse(!1),this.setTickSize(DEFAULT_TICK_SIZE),this.setTickPadding(DEFAULT_TICK_PADDING),this.setDomainLine(!0),this.setTicksDraw(!0),this.setLabelOffset(),this.setDayInMajor(!1),this.setStickyContextLabel(!0),a.alignDiff=0,a.maxLabelSpace=0,a.defaultStyle=a.style={[TICKMINOR]:{stroke:'#d3d3d3',"stroke-width":'1px'},[TICKMAJOR]:{stroke:'#AAAAAA',"stroke-width":'1px'},[LABELMAJOR]:{fill:'#858585',"font-size":'12px',"font-weight":'normal',"font-style":'normal'},[LABELMINOR]:{fill:'#9A9A9A',"font-size":'11px',"font-weight":'normal',"font-style":'normal'},[LABELCONTEXT]:{fill:'#858585',"font-size":'12px',"font-weight":'normal',"font-style":'normal'},_ticks:{minor:{stroke:'#d3d3d3'},major:{stroke:'#AAAAAA'},context:{}},_text:{major:{fill:'#858585',"font-size":'12px',"font-weight":'normal',"font-style":'normal'},minor:{fill:'#9A9A9A',"font-size":'11px',"font-weight":'normal',"font-style":'normal'},context:{fill:'#858585',"font-size":'12px',"font-weight":'normal',"font-style":'normal'}},line:{fill:'#D6D6D6'},title:{fill:'#5F5F5F',"font-size":'12px',"font-weight":'normal',"font-style":'normal'}},this.config.prevTicks||(this.config.prevTicks=[])}configureAttributes(a){super.configureAttributes(a);const b=this;a.scale&&b.setScale(a.scale),a.align&&b.setAlignment(a.align,a.overlap),a.reverse&&b.setReverse(a.reverse),a.tickarguments&&b.setTickArguments(...a.tickarguments),a.title&&b.setAxisName(a.title),a.skipinfo&&b.setSkipInfo(a.skipinfo),a.offset&&b.setLabelOffset(a.offset),a.dayinmajor&&b.setDayInMajor(a.dayinmajor),a.scalemode&&b.setScaleMode(a.scalemode),'undefined'!=typeof a.stickycontextlabel&&b.setStickyContextLabel(a.stickycontextlabel),isNil(a.ticksize)||b.setTickSize(a.ticksize),isNil(a.tickpadding)||b.setTickPadding(a.tickpadding),b.setDomainLine(a.domainline),b.setTicksDraw(a.drawticks),isNil(a.formatterFn)||b.setTickFormat(a),a.style&&b.parseStyle(a.style),b.config.validateLabelDimensions=pluckNumber(a.validateLabelDimensions,0)}parseStyle(a={}){let b,c,d,e,f,g=this,h=g.config,i=h.defaultStyle,j=g.getFromEnv('getStyleDef'),k=g.getFromEnv('baseTextStyle'),l=j(a.label),m=j(a['tick-mark']);b=Object.assign({},i['tick-mark-major'],m,j(a['tick-mark-major'])),c=Object.assign({},i['tick-mark-minor'],m,j(a['tick-mark-minor'])),d=Object.assign({},i['label-major'],k,l,j(a['label-major'])),e=Object.assign({},i['label-minor'],k,l,j(a['label-minor'])),f=Object.assign({},i['label-context'],k,l,j(a['label-context'])),h.style={[TICKMAJOR]:b,[TICKMINOR]:c,[LABELMAJOR]:d,[LABELMINOR]:e,[LABELCONTEXT]:f,title:Object.assign({},i.title,k,j(a.title)),line:Object.assign({},i.line,j(a.line))}}placeAxis(){var a=Math.max;let b,c,d,e,f,g,h,j,k,l,m,n,o,p,q,r,s,t,u,v=this,w=v.config,x=w.isVertical,y=v.getScale(),z=v.getSkipInfo()||{},A=v.getTickPadding(),B=v.getFromEnv('smartLabel'),C=[],D=0,E=0,F=v.getAxisName(),G=[];for(b=(v.getTickArguments()||[]).slice(0),b.push(z),b.push(w.dayInMajor),c=isNil(v.getTickValues())?y.ticks?y.ticks(...b):y.getDomain():v.getTickValues(),d=isNil(v.getTickFormat())?y.tickFormat?y.tickFormat(...v.getTickArguments()):identity:v.getTickFormat(),s=w.style[LABELCONTEXT],B.setStyle(Object.assign({},s)),t=s['font-size']||s.fontSize,w.labelHeight=1.2*parseFloat(t,10),F&&(B.setStyle(Object.assign({},w.style.title)),k=w.axisNameDim=B.getOriSize(F,!1),j=k.height,D+=j),p=y._tickType||[],(e=0,f=c.length);e<f;e++)m=c[e],l=getFormatter(y._timeFormat,p,e),B.setStyle(Object.assign({},w.style['label-'+(p[e]||MAJOR)])),n=w.isVertical?d.formatFn({value:m,prefix:d.prefix,suffix:d.suffix,type:'axis'},l):d(m,l,p[e]),E=a(n.length,E),q=B.getOriSize(n,!1),x&&(q.width=w.vLabelWidth),g=v.getTickSize(p[e]),C.push({text:n,value:m,dim:q,tickLen:g,[w.x]:A+g}),p[e]!==CONTEXT&&G.push((x?q.width:q.height)+g);if(x&&isNil(w.vLabelWidth))for(w.vLabelWidth=0,G=[],(e=0,f=c.length);e<f;e++)B.setStyle(Object.assign({},w.style['label-'+(p[e]||MAJOR)])),o=w.style['label-'+(p[e]||MAJOR)]['font-size'],o=+o.toString().split('px')[0],q=B.getOriSize(getBuffer(E,o),!1),C[e].dim=q,w.vLabelWidth=a(q.width,w.vLabelWidth),p[e]!==CONTEXT&&G.push((x?q.width:q.height)+g);for(w.values=C,u=v.getLabelOverlapBuffer(),h=w.prevMaxLen=pluckNumber(safeMax(G),w.prevMaxLen,0),D+=h,r=x?0:w.labelHeight,w.maxLabelSpace=D+A+u+r,(e=0,f=c.length);e<f;e++)m=C[e],m[w.x]=w.orientation===TOP||w.orientation===LEFT?w.maxLabelSpace-A-u-m.tickLen:A+m.tickLen;return w.axisDimension={[w.align]:w.maxLabelSpace},w.axisDimension}getLabelOverlapBuffer(){var a=Math.max;let b,c,d,e,f,g=this,h=g.config,j=h.values,k=g.getScale(),l=k._tickType||[],m=0;for(d=0,e=j.length;d<e&&(l[d]!==MAJOR&&l[d]?l[d]===MINOR&&(c=j[d]):b=j[d],!(b&&c));d++);if(c)switch(f={x:c.dim.width,y:c.dim.height},h.k){case 1:m=a(c[h.x]+f[h.x]-b[h.x],0);break;case-1:m=a(b[h.x]-(c[h.x]+f[h.x]),0);}return h.overlapBuffer=h.k*m/3}makeBBox(a,b=DEFAULT_LABEL_PADDING){let c=this.config,d=this.getScale(),e={x:a.dim.width,y:a.dim.height};return{[c.x]:a[c.x]-b-e[c.x]/2,[c.xInverse]:d.getRangeValue(a.value)-b-e[c.xInverse]/2,width:a.dim.width+b,height:a.dim.height+b}}setMinorSkipIndex(){let a,b,c,d,e,f,g,h,k,l,m=this,n=m.config,o=m.getScale(),p=n.values,q=o._tickType||[],r=[],s=p.length,t=1,u=1,v=!1,w=!1;for(a=0;a<s;a++){if(q[a]===MAJOR||typeof q[a]===UNDEF)for(b=a+1;b<s;b++)if(q[b]===MAJOR||typeof q[b]===UNDEF){d=p[a],e=p[b],r=p.slice(a+1,b),v=!0;break}if(v)break}if(r.length){if(s=r.length,l=a,c=floor(s/2),f=m.makeBBox(d),g=m.makeBBox(e),k=m.makeBBox(r[c]),overlaps(k,f)||overlaps(k,g))return void(n.minorSkip=s);for(a=c-1;0<=a;a--)if(h=m.makeBBox(r[a]),!overlaps(h,k)){t=c-a;break}for(a=c+1;a<s+l;a++)if(h=m.makeBBox(r[a]),!overlaps(h,k)){u=a-c;break}for(s=r.length;!w;){for(w=!0,a=c+u;a<s;a+=u)if(overlaps(m.makeBBox(r[a]),m.makeBBox(r[a-u]))){u++,w=!1;break}for(a=c-t;0<=a;a-=t)if(overlaps(m.makeBBox(r[a]),m.makeBBox(r[a+t]))){t++,w=!1;break}}n.minorSkip=Math.max(t,u)}}cleanseMinorTicks(){var a=Math.min,b=Math.max;let c,d,e,f,g,h,k,l,m,n=this,o=n.config,p=n.getScale(),q=p._tickType||[],r=[],s=[],t=o.values,u=-1,v={[o.xInverse]:'height',[o.x]:'width'};for(c=0,e=t.length;c<e;c++)q[c]!==MAJOR&&q[c]||r.push(c);for(c=0,e=r.length;c<e-1;c++)if(f=t.slice(r[c]+1,r[c+1]),s.push(f),m=f.length,u=-1,!!f.length){for(g=floor(f.length/2),h=n.makeBBox(t[r[c]],0),k=n.makeBBox(t[r[c+1]],0),d=g;0<=d;d-=1)if(l=n.makeBBox(f[d],1),o.isVertical){if(h[o.xInverse]<=l[o.xInverse]+l[v[o.xInverse]]){u=b(u,d);break}}else if(l[o.xInverse]<=h[o.xInverse]+h[v[o.xInverse]]){u=b(u,d);break}for(d=g;d<f.length;d+=1)if(l=n.makeBBox(f[d],1),o.isVertical){if(k[o.xInverse]+k[v[o.xInverse]]>=l[o.xInverse]){m=a(m,d);break}}else if(l[o.xInverse]+l[v[o.xInverse]]>=k[o.xInverse]){m=a(m,d);break}for(d=0;d<f.length;d++)(d<=u||d>=m||Math.abs(g-d)%o.minorSkip)&&resetTickInfo(f[d])}}cleanseMajorTicks(){let a,b,c,d,e=this,f=e.config,g=e.getScale(),h=f.values,j=[],k=[],l=[],m=[],n=g._tickType||[],o=0,p=1,q=1,r=!1;if(d=h.length,!!d){for(c=0;c<d;c++)n[c]!==MAJOR&&n[c]?n[c]===CONTEXT&&(l.push(h[c]),m.push(c)):(j.push(h[c]),k.push(c));if(m.length)for(c=0,d=j.length;c<d;c++)+l[0].value==+j[c].value&&(o=c);else o=0;for(a=e.makeBBox(j[0]),c=o+1,d=j.length;c<d;c++)if(!overlaps(e.makeBBox(j[c]),a)){q=c-o;break}for(c=o-1;0<=c;c--)if(!overlaps(e.makeBBox(j[c]),a)){p=o-c;break}for(;!r;){for(r=!0,c=o+q;c<d;c+=q)if(overlaps(e.makeBBox(j[c]),e.makeBBox(j[c-q]))){q++,r=!1;break}for(c=o-p;0<=c;c-=q)if(overlaps(e.makeBBox(j[c]),e.makeBBox(j[c+p]))){p++,r=!1;break}}for(f.majorSkip=b=Math.max(p,q),c=0,d=j.length;c<d;c++)Math.abs(c-o)%b&&resetTickInfo(j[c])}}draw(){const a=this,b=a.config,c=a.getFromEnv('chart').config,d=a.getScale(),e=a.getLinkedParent(),f=d._tickType||[],g=(d.getBandwidth?center:number)(d.copy()),h=d.getRange(),i=d.getDomain(),j=+h[0],l=+h[h.length-1],m=a.getOrientation(),n=a.getFromEnv('smartLabel'),o=m===LEFT||m===TOP?b.maxLabelSpace+GUTTER_2:0,p=b.isVertical,q=b.orientation,r=b.xInverse,s=b.x,t=b.k,k=b.style;a.addGraphicalElement({el:'group',attr:{name:`strato-axis`,transform:b.groupTranslation},container:{id:'strato',label:'group',isParent:!0},id:'strato-axis',component:a,label:'group'}),a.addGraphicalElement({el:'group',attr:{name:`axis-${q}`},container:{id:'strato-axis',label:'group'},id:'axisGroup',component:a,label:'group'});let u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J=0;if(a.setMinorSkipIndex(),a.cleanseMinorTicks(),a.cleanseMajorTicks(),(G=a.getDomainLine())&&a.addGraphicalElement({el:'path',attr:{path:G,stroke:'#d6d6d6',fill:'none',"stroke-width":2},container:{id:'axisGroup',label:'group',isParent:!1},css:k.line,component:a,label:'path'}),(w=a.getAxisName())&&(F=parseFloat(b.style.title['font-size']||b.style.title.fontSize,10),n.setStyle(Object.assign({},b.style.title)),x=n.getSmartText(w,Math.abs(j-l),1.2*F),p?(y=m===LEFT?0+x.height/2:b.maxLabelSpace-x.height/2,z=(j-l)/2):(y=(l-j)/2,z=m===TOP?0+x.height/2:b.maxLabelSpace-x.height/2),a.addGraphicalElement({el:'text',attr:{text:x.text,x:y,y:z,opacity:k.title.opacity,"text-anchor":'middle',transform:p?getSuggestiveRotation(t*DEFAULT_ROTATION,y,z):DEFAULT_TRANSFORM},container:{id:'axisGroup',label:'group',isParent:!1},tooltext:c.showTooltip?x.tooltext:UNDEF,css:k.title,component:a,id:'axis-name',label:'name'})),a.getTicksDraw())for(J=f.length-f.indexOf(CONTEXT),D=b.values.find((a,b)=>a.value>=i[0]&&a.value<=i[1]&&f[b]===MAJOR),(A=0,B=b.values.length);A<B;A++)(C=b.values[A],v=f[A]||MAJOR,!(v!==CONTEXT&&(C.value<i[0]||C.value>i[1])))&&(v===CONTEXT&&1<J&&(C.value<i[0]||C.value>i[1])||(C[r]=v===CONTEXT&&1==J?a.getStickyContextLabel()||'undefined'==typeof D?Math.max(g(C.value),C.dim.width/2):g(D.value):g(C.value),C[s]+=b.alignDiff,u=C.value,a.addGraphicalElement({container:{id:'axisGroup',label:'group',isParent:!1},attr:{name:'tick',transform:b.translate(C[r])},data:{value:u},el:'group',id:'tick-group-'+A,component:this,label:'group'}),v!==CONTEXT&&(H=p?`M ${o} 0 H ${o+t*a.getTickSize(f[A])}`:`M 0 ${o} V ${o+t*a.getTickSize(f[A])}`,a.addGraphicalElement({el:'path',attr:{path:H},container:{id:'tick-group-'+A,label:'group',isParent:!1},id:'tick-'+A,css:k['tick-mark-'+(v||MAJOR)],component:a,label:'path'})),E=k['label-'+(v||MAJOR)]['text-anchor']||getTextAnchor(m),I={[r]:C[r]+b[r+'Offset'],[s]:C[s]+b[s+'Offset']+(v===MINOR?0:b.overlapBuffer)},(!b.validateLabelDimensions||isWithinDomain(C.dim,E,I,e.getDimension()))&&a.addGraphicalElement({el:'text',attr:{text:C.text+'',[r]:0+b[r+'Offset'],[s]:C[s]+b[s+'Offset']+(v===MINOR?0:b.overlapBuffer),"text-anchor":E,"vertical-align":q===TOP?'bottom':q===BOTTOM?'top':'middle',opacity:k['label-'+(v||MAJOR)].opacity},container:{id:'tick-group-'+A,label:'group',isParent:!1},component:a,id:'tick-label-'+A,css:k['label-'+(v||MAJOR)],label:'text'})))}updateMaxLabelSpace(a){let b=this,c=b.config,d=b.getAlignment();return c.alignDiff=a-c.maxLabelSpace,c.maxLabelSpace=c.axisDimension[d]=a,c.axisDimension}setDayInMajor(a){this.config.dayInMajor=a}setSkipInfo(a={}){this.config.tickInfo=a}getSkipInfo(){return this.config.tickInfo}setStickyContextLabel(a=!0){this.config.stickyContextLabel=a}getStickyContextLabel(){return this.config.stickyContextLabel}setLabelOffset(a={}){let b=this.config;b.xOffset=pluckNumber(a.x,0),b.yOffset=pluckNumber(a.y,0)}getLabelOffset(){return{x:this.config.xOffset,y:this.config.yOffset}}getAxisName(){return this.config.axisName}setAxisName(a=''){this.config.axisName=a}getAlignment(){return this.config.align}setAlignment(a,b=!1){let c,d,e=this,f=e.config;a===LEFT?c=b?RIGHT:LEFT:a===RIGHT?c=b?LEFT:RIGHT:a===TOP?c=b?BOTTOM:TOP:a===BOTTOM?c=b?TOP:BOTTOM:void 0;f.align=a,f.orientation=c,f.isVertical=d=a===LEFT||a===RIGHT,f.k=c===LEFT||c===TOP?-1:1,f.x=d?X:Y,f.xInverse=d?Y:X,f.translate=d?translateY:translateX,f.translateInverse=d?translateX:translateY}getOrientation(){return this.config.orientation}setTranslation(a=0,b=0){this.config._translatedX=a,this.config._translatedY=b,this.config.groupTranslation=`t${a}, ${b}`}getTranslation(){return{x:this.config._translatedX,y:this.config._translatedY}}setDomainLine(a=!0){return this.config.drawDomainLine=a,this}getDomainLine(){let a,b,c=this,d=c.config,e=c.getScale(),f=e.getRange(),g=d.isVertical,h=d.orientation,i=d.maxLabelSpace,j=+f[0],k=+f[1];return d.drawDomainLine&&(a=h===LEFT||h===TOP?i+GUTTER_2:0,b=g?`M ${a} ${j} V ${k}`:`M ${j} ${a} H ${k}`),b}setTicksDraw(a=!0){return this.config.drawTicks=a,this}getTicksDraw(){return this.config.drawTicks}setScale(a){this.config.scale=a}getScale(){return this.config.scale}setReverse(a){this.config.isReverse=a}getReverse(){return this.config.isReverse}setTickArguments(...a){return this.config.tickArguments=a,this}getTickArguments(){return this.config.tickArguments.slice()}setTickSize(a){return this.config.tickSizeOuter=+a,this.config.tickSizeInner=this.config.tickSizeOuter*(2/3),this}getTickSize(a=MAJOR){let b=this.config;return a===CONTEXT?b.tickSizeOuter+b.labelHeight:a===MINOR?b.tickSizeInner:a===MAJOR?b.tickSizeOuter:void 0}setTickPadding(a){return this.config.tickPadding=+a,this}getTickPadding(){return this.config.tickPadding}setTickValues(a){return this.config.tickValues=isNil(a)?null:a.slice(),this}getTickValues(){return this.config.tickValues&&this.config.tickValues.slice()}setTickFormat(a){return this.config.tickFormat={formatFn:a.formatterFn,prefix:a.formatLabelPrefix,suffix:a.formatLabelSuffix},this}getTickFormat(){return this.config.tickFormat}getAxisEndLabelDisplaySpace(){var a=Math.min,b=Math.max;const c=+this.getFromEnv('chartWidth'),d=+this.getFromEnv('chartHeight'),e=this.config.values,f=this.getReverse(),g=this.config.isVertical,h={};let i,j,k,l;return g?(k=f?e[e.length-1]:e[0],l=f?e[0]:e[e.length-1],h.top=0-a(0,k.y-k.dim.height),h.bottom=b(l.y+l.dim.height,d)-d):(i=f?e[e.length-1]:e[0],j=f?e[0]:e[e.length-1],h.left=0-a(0,i.x-i.dim.width),h.right=b(j.x+j.dim.width,c)-c),h}getDimension(){return this.config.axisDimension}setDimension(a){return this.placeAxis(a)}setScaleMode(a){this.config.scale.setMode(a)}getScaleMode(){return this.config.scale.getMode()}}export default Axis;