UNPKG

quoslibero

Version:

FusionCharts JavaScript charting framework

1 lines 9.37 kB
import{extend2,hashify,getMouseCoordinate,componentFactory}from'../../../fc-core/src/lib';import{ComponentInterface}from'../../../fc-core/src/component-interface';let UNDEF,scale,_top,plotOut,BLANK='',math=Math,mathMax=math.max,mathMin=math.min,boxHeight=0,hasTouch='ontouchstart'in window,CANVAS='canvas',chartInclusionList={zoomlinedy:!0,zoomline:!0},createGroup=function(a,b,c){var d=c.getFromEnv('animationManager');return d.setAnimation({el:'group',attr:{name:a},container:b,state:'appearing',component:c,label:'group'})},nearestUndefined=function(a){var b,c,d,e={},f=Number.POSITIVE_INFINITY;for(b=0;b<this.length;b++)c=this[b]-a,d=0>c?scale.NEG:scale.POS,c=Math.abs(c),c<=f&&(f=c,e.absValue=c,e.noScaleSide=d);return e},getHandler=function(a){var b=Math.round;return{onMouseOut(){a.hide(),a.position=UNDEF},onMouseMove(c){var d,e,f,g,h,i,j,k,l,m,n,o,p;a.disabled||a._mouseIsDown&&!hasTouch||(e=a.getZoomInfo(),f=a.getGraphicalElement('line'),g=e.step,h=a.chart,i=h.getChildren('xAxis')[0],j=h.get('config'),k=j.canvasLeft,l=i.getAxisConfig('axisDimention'),m=getMouseCoordinate(a.getFromEnv('chart-container'),c,h).chartX,n=i.getVisibleConfig(),o=l.x-k,p=(p=a.getDataIndexFromPixel(b(m)))+((d=p%g)>g/2?g-d:-d),m=i.getPixel(p,{wrtVisible:!0})-o-k,m-=e.offset,f.transform(['T',b(m),0]),a.hidden&&0!==j.crossline.enabled&&a.show(),(p<n.minValue||p>n.maxValue)&&a.hide(),(p!==a.position||a.hidden)&&(a.position=p,a.lineX=m,a.updateLabels()))}}};class CrossLine extends ComponentInterface{constructor(){super(),this.config.handlers=getHandler(this)}configureAttributes(a){this.config.options=a}draw(){let a,b,c,d,e,f,g,h,j={},k=this,l=k.getFromEnv('chart'),m=l.getFromEnv(),n=m['number-formatter'],o=l.config,p=k.left=l.getChildren('xAxis')[0].getAxisConfig('axisDimention').x,q=k.top=o.canvasTop,r=k.height=o.canvasHeight,s=k._visout=o._visout,t=[],u=k.getFromEnv('animationManager'),v=k.getGraphicalElement('line'),w=k.config.options,x=w.labelstyle,y=w.valuestyle,z=l.getChildren('yAxis')[0],A=z.getLimit(),B=l.getChildren('yAxis')[1],C=B&&B.getLimit(),D=k.getGraphicalElement('labels'),E=[],F=k.getGraphicalElement('positionLabel'),G=k.getLinkedParent().getChildContainer('crosslineBottom'),H=k.getLinkedParent().getChildContainer('crosslineTop'),I=function(){this.remove()};if(l.iterateComponents(a=>{a.getType&&'dataset'===a.getType()&&!a.getState('removed')&&t.push(a)}),k.plots=t,k.width=o._visw,e=k.group,(e=k.getContainer('valueGroup'))||(e=k.addContainer('valueGroup',createGroup('crossline-value-group',H,k))),(h=k.getContainer('labelGroup'))||(h=k.addContainer('labelGroup',createGroup('crossline-label-group',H,k)),h.insertBefore(l.getChildContainer('plotGroup'))),k.container=G,e.attr({transform:['T',p,o._ymin]}).css(y),f=u.setAnimation({el:v||'path',container:G,doNotRemove:!0,attr:extend2({path:['M',p,q,'l',0,r]},w.line)}).toBack(),v||k.addGraphicalElement('line',f,!1),w.labelEnabled&&(j.x=s,j.y=q+r+(o.scrollHeight||0)+2.5,j['vertical-align']='top',j.direction=o.textDirection,j.text=BLANK),g=u.setAnimation({el:F||'text',attr:w.labelEnabled&&j,css:w.labelEnabled&&x,container:h,doNotRemove:!0,callback:!w.labelEnabled&&function(){this.remove()}}),!F&&w.labelEnabled&&k.addGraphicalElement('positionLabel',g,!1),k.hide(),k.ppixelRatio=-(z.config.axisDimention.axisLength/z.getVisibleLength()),k.spixelRatio=B&&-(B.config.axisDimention.axisLength/B.getVisibleLength()),k.yminValue=o._yminValue,k.pyaxisminvalue=A.min,k.pyaxismaxvalue=A.max,k.syaxisminvalue=C&&C.min,k.syaxismaxvalue=C&&C.max,k.positionLabels=o.xlabels||{data:[],parsed:[]},k.chart=l,a=0,w.valueEnabled){for(b=t.length;a<b;a+=1)c=t[a],d=hashify(c.config.linecolor),j.x=0,j.y=s,j.fill=d,j.direction=o.textDirection,j.text=BLANK,j['text-bound']=y['text-bound'],j.lineHeight=y.lineHeight,E[a]=u.setAnimation({el:D&&D[a]||'text',container:e,doNotRemove:!0,attr:j}),D&&D[a]||k.addGraphicalElement('labels',E[a],!0);k.numberFormatter=n}for(b=D&&D.length;a<b;a++)u.setAnimation({el:D[a],component:k,doNotRemove:!0,callback:I});D&&D.splice(t.length)}getType(){return'crossline'}getName(){return'crossLine'}getZoomInfo(){return this.getFromEnv('chartConfig').viewPortConfig}getDataIndexFromPixel(a){let b=this.getFromEnv('chart').getChildren('xAxis')[0];return Math.round(b.getValue(a,{wrtVisible:!0}))}getPositionLabel(a){let b=this.getFromEnv('chart').getChildren('xAxis')[0],c=b.getLabel(a);return c&&c.label||BLANK}disable(a){return a!==UNDEF&&(this.disabled=!!a,this.disabled&&this.visible&&this.hide()),this.disabled}updateLabels(){var a=this,b=2.5,c=a.getFromEnv('animationManager'),d=a.getGraphicalElement('labels'),e=a.getGraphicalElement('positionLabel'),f=a.plots,g=a.width,h=a.position,j=a.lineX,k=Math.floor(j),i=a.dummyText,l=a.numberFormatter,m=a.pyaxisminvalue,n=a.pyaxismaxvalue,o=a.syaxisminvalue,p=a.syaxismaxvalue,q=getVerticalLimits(a);plotOut=a._visout,d&&(!i&&(i=a.dummyText=a.getFromEnv('paper').text().hide()),i.attr({text:l.yAxis('0')}),i&&q.init(i.getBBox().height,d.length),d.forEach(function(b,c){if(!f[c].getState('removed')){var d,e=f[c],g=e.components.data[h]&&e.components.data[h].config.setValue,i=e.config.parentYAxis;d=g===UNDEF||!e.getState('visible')||(i?g>p||g<o:g>n||g<m)?plotOut:i?(g-o)*a.spixelRatio:(g-m)*a.ppixelRatio,q.occupy(d,b)}})),d&&d.forEach(function(d,e){if(!f[e].getState('removed')){var i,m,n,o,p,q,r=f[e],s=r.components.data[h]&&r.components.data[h].config.setValue,t=l[r.config.parentYAxis?'sYAxis':'yAxis'](s);t?(d.attr({text:t}),i=d.getBBox(),m=i&&i.width,n=m&&.5*m,o=n&&n+10,q=d.calcY,p=mathMax(0,mathMin(k,g)),q!==UNDEF&&p!==UNDEF&&c.setAnimation({el:d,attr:{x:p,y:q,"text-anchor":j<=o&&'start'||j+o>=g&&'end'||'middle',"text-bound":['rgba(255,255,255,0.8)','rgba(0,0,0,0.2)',1,b]},doNotRemove:!0,component:a})):d.attr({x:-g})}}),e&&c.setAnimation({el:e,attr:{x:j+a.left,text:a.getPositionLabel(h),"text-bound":['rgba(255,255,255,1)','rgba(0,0,0,1)',1,b]},component:a})}show(){if(!this.disabled){this.hidden=!1;let a=this.getContainer('valueGroup'),b=this.getGraphicalElement('positionLabel'),c=this.getGraphicalElement('line');a&&a.show(),b&&b.show(),c&&c.show()}}hide(){this.hidden=!0;let a=this.getContainer('valueGroup'),b=this.getGraphicalElement('positionLabel'),c=this.getGraphicalElement('line');a&&a.hide(),b&&b.hide(),c&&c.hide()}dispose(){var a,b=this;for(a in b)b.hasOwnProperty(a)&&delete b[a]}}function getVerticalLimits(a){var b=Math.floor,c=Math.abs;let d,e,f=a.yminValue,g=a.getFromEnv('chart'),h=g.getChildren('yAxis')[0],i=h.getPixel(f),j={};scale={},_top=-1*a.height;try{Object.defineProperty(scale,'POS',{enumerable:!1,configurable:!1,get:function(){return 1}}),Object.defineProperty(scale,'NEG',{enumerable:!1,configurable:!1,get:function(){return-1}})}catch(a){scale.POS=1,scale.NEG=-1}try{Object.defineProperty(j,'top',{enumerable:!1,configurable:!1,get:function(){return _top}}),Object.defineProperty(j,'bottom',{enumerable:!1,configurable:!1,get:function(){return i}})}catch(a){j.top=_top,j.bottom=i}return j.init=function(a){var f;for(boxHeight=a+2,_top+=boxHeight/2,e=b(c(_top)/boxHeight),d=new OccupancyMatrix(e),f=0;f<e;f++)d.pos.push(0)},j.occupy=function(a,e){var f=b(c(_top-a)/boxHeight);d&&d.attachShift(a,f,e)},j}class AttachableLabel{constructor(){this.y=0,this.lRef=UNDEF,this.__shift=0,this.__index=0}applyShift(a){this.__shift=a,this.lRef.calcY=this.y+=a*boxHeight}applyDirectIndex(a){this.__index=a,this.lRef.calcY=this.y=_top- -1*(a*boxHeight)}}class OccupancyMatrix{constructor(a){this.holes=[],this.pos=[];for(var b=0;b<a;b++)this.holes.push(b)}repositionHoles(){var a,b,c=0,d=this.pos;for(this.holes.length=0,a=0;a<d.length;a++)b=d[a],b||(this.holes[c++]=a)}attachShift(a,b,c){var d,e,f,g,h,i,j,k=this.pos,l=k.length;if(a===plotOut)return void(c.calcY=plotOut);if(i=b>l-1?l-1:b,d=k[i],(g=new AttachableLabel,g.y=a,g.lRef=c,!d))return g.applyDirectIndex(i),k.splice(i,1,g),void this.holes.splice(this.holes.indexOf(i),1);if(j=nearestUndefined.call(this.holes,i),e=i+j.absValue*j.noScaleSide,j.noScaleSide===scale.POS)return g.applyDirectIndex(e),k.splice(e,1,g),this.holes.splice(this.holes.indexOf(e),1),e;if(j.noScaleSide===scale.NEG){for(f=k.splice(e+1,k.length-1),k.pop(),f.forEach(function(a){a&&a.applyShift(-1)}),[].push.apply(k,f),h=e;!!k[h];)h++;return k.push(0),this.repositionHoles(),j=nearestUndefined.call(this.holes,h),h+=j.absValue*j.noScaleSide,g.applyDirectIndex(h),k.splice(h,1,g),this.repositionHoles(),k.length-1}}}export default{extension:a=>{a.addEventListener('instantiated',function(a){if(a.sender.getName()===CANVAS){let b,c,d,f=a.sender;f.registerFactory('crossLineManager-zoomline',function(){let g,e=a.sender.getFromEnv('chart'),h=e&&e.getName();h&&chartInclusionList[h.toLowerCase()]&&(c=e.config.crossline,c&&0!==c.enabled&&1===e.config.useCrossline?d=1:(c&&(c.enabled=0),d=0),componentFactory(f,CrossLine,'crossLine',d,[c]),d&&(g=f.getChildren('crossLine')[0],b=g.config.handlers,g.addExtEventListener('fc-mousemove',b.onMouseMove,f),g.addExtEventListener('fc-mouseover',b.onMouseMove,f),g.addExtEventListener('fc-dragstart',function(a){b.onMouseOut(a),g.removeExtEventListener('fc-mousemove',b.onMouseMove,f)},f),g.addExtEventListener('fc-dragend',function(){g.addExtEventListener('fc-mousemove',b.onMouseMove,f)},f),g.addExtEventListener('fc-mouseout',function(a){b.onMouseOut(a)},f)))})}})},name:'crossline-manager',type:'extension',requiresFusionCharts:!0};