UNPKG

quoslibero

Version:

FusionCharts JavaScript charting framework

6 lines 11 kB
import Column from'./column';import{pluckNumber,defined,safeMin,extend2,convertColor,UNDEF,pluck,safeMax,DEFAULT_FT_FONT}from'../../../../../fc-core/src/lib';import isValidNumber from'../../../../../fc-utils/src/type/is-valid-number';import{addDep}from'../../../../../fc-core/src/dependency-manager';import candlestickAnimation from'./candlestick.animation';const _reducerFn=(a,b)=>Object.assign(a,b);addDep({name:'timeseriesCandlestickAnimation',type:'animationRule',extension:candlestickAnimation});class CandleStick extends Column{getHoverInConfig(a){let b=this.config;return{bullStyle:b.bullHoverInStyle,bearStyle:b.bearHoverInStyle,index:a,hovered:!0}}getHighlightConfig(a){let b=this.config;return{bullStyle:b.bullHighlightStyle,bearStyle:b.bearHighlightStyle,index:a,hovered:!0}}getHoverOutConfig(a){let b,c=this.config,d=Object.assign({},c.bullHoverOutStyle),e=Object.assign({},c.bearHoverOutStyle);for(b in d)d[b]=null;for(b in e)e[b]=null;return{bullStyle:d,bearStyle:e,index:a,hovered:!1}}__setDefaultConfig(){var a=this,b=a.config;b.bullStyle={},b.bullHoverInStyle={},b.bullHighlightStyle={},b.bullHoverOutStyle={},b.bearStyle={},b.bearHoverInStyle={},b.bearHighlightStyle={},b.bearHoverOutStyle={},b.plotSpacePercent=40,b.dataMarkerPadding=0,b.visibility='visible',b.prevBoundaryInfo={},b.prevDataInfo=[],b.defaultBullStyle={"stroke-width":1,opacity:1,stroke:'000000',fill:'ffffff'},b.defaultBearStyle={"stroke-width":1,opacity:1,stroke:'000000',fill:'868AC8'},b.defaultBearHoverInStyle={"stroke-width":2},b.defaultBullHoverInStyle={"stroke-width":2},b.legendDefaultTextStyle={"font-family":DEFAULT_FT_FONT,"font-weight":'normal',"font-style":'normal',"font-size":'12px',fill:'#5F5F5F'},b.strokeColor='#464E56',b.hoverInfo=[]}configureAttributes(a){let b,c=this,d=c.getFromEnv('dataSource'),e=c.getFromEnv('getStyleDef'),f=d.legend,g=e(f&&f.item&&f.item.style&&f.item.style.text),h=c.config;if(h.mergedLegendStyle=extend2(extend2({},h.legendDefaultTextStyle),g),a.hasOwnProperty('index'))h.hoverInfo.push(a),h.hoverMode=!0;else{for(b in a)if('primaryColor'===b){let b,d=a.plotCosmetics,f=a.genericCosmetics,g=h.bullStyle,i=h.bearStyle,j=h.bullHoverInStyle,k=h.bearHoverInStyle,l=h.bullHighlightStyle,m=h.bearHighlightStyle,n=h.bullHoverOutStyle,o=h.bearHoverOutStyle,p=e(d.style&&d.style.bear),q=e(d.style&&d.style.bull),r=e(f.style&&f.style.plot),s=e(f.style&&f.style['plot:hover']),t=e(f.style&&f.style['plot:highlight']),u=e(f.style&&f.style.bull),v=e(f.style&&f.style['bull:hover']),w=e(f.style&&f.style['bull:highlight']),x=e(f.style&&f.style.bear),y=e(f.style&&f.style['bear:hover']),z=e(f.style&&f.style['bear:highlight']),A=e(d.style&&d.style['bear:hover']),B=e(d.style&&d.style['bear:highlight']),C=e(d.style&&d.style['bull:hover']),D=e(d.style&&d.style['bull:highlight']),E=e(a.styleConfig&&a.styleConfig.bear),F=e(a.styleConfig&&a.styleConfig.bull),G=e(a.styleConfig&&a.styleConfig['bear:hover']),H=e(a.styleConfig&&a.styleConfig['bear:highlight']),I=e(a.styleConfig&&a.styleConfig['bull:hover']),J=e(a.styleConfig&&a.styleConfig['bull:highlight']),K=e(d.style&&d.style.plot),L=e(a.styleConfig&&a.styleConfig.plot),M=e(d.style&&d.style['generic:hover']),N=e(d.style&&d.style['generic:highlight']),O=e(a.styleConfig&&a.styleConfig['plot:hover']),P=e(a.styleConfig&&a.styleConfig['plot:highlight']);for(b in delete r.fill,delete K.fill,delete L.fill,'timeseries-ohlc'===c.getName()&&(delete L.stroke,delete r.stroke,delete K.stroke),[g,h.defaultBullStyle,r,K,L,u,q,F].reduce(_reducerFn),[i,h.defaultBearStyle,r,K,L,x,p,E].reduce(_reducerFn),[j,h.defaultBullHoverInStyle,s,M,O,v,C,I].reduce(_reducerFn),[l,h.defaultBullHighlightStyle,t,N,P,w,D,J].reduce(_reducerFn),[k,h.defaultBearHoverInStyle,s,M,O,y,A,G].reduce(_reducerFn),[m,h.defaultBearHighlightStyle,t,N,P,z,B,H].reduce(_reducerFn),Object.keys(l).length||(l=h.bullHighlightStyle=extend2({},j)),Object.keys(m).length||(l=h.bearHighlightStyle=extend2({},k)),g.stroke=convertColor(g.stroke,100*pluckNumber(g['stroke-opacity'],1)),g.fill=convertColor(g.fill,100*pluckNumber(g['fill-opacity'],1)),i.stroke=convertColor(i.stroke,100*pluckNumber(i['fill-opacity'],1)),i.fill=convertColor(i.fill,100*pluckNumber(i['fill-opacity'],1)),j.fill&&(j.fill=convertColor(j.fill,100*pluckNumber(j['fill-opacity'],g['fill-opacity']))),j.stroke&&(j.stroke=convertColor(j.stroke,100*pluckNumber(j['stroke-opacity'],g['stroke-opacity']))),k.fill&&(k.fill=convertColor(k.fill,100*pluckNumber(k['fill-opacity'],i['fill-opacity']))),k.stroke&&(k.stroke=convertColor(k.stroke,100*pluckNumber(k['stroke-opacity'],i['stroke-opacity']))),l.fill&&(l.fill=convertColor(l.fill,100*pluckNumber(l['fill-opacity'],g['fill-opacity']))),l.stroke&&(l.stroke=convertColor(l.stroke,100*pluckNumber(l['stroke-opacity'],g['stroke-opacity']))),k.fill&&(k.fill=convertColor(k.fill,100*pluckNumber(k['fill-opacity'],i['fill-opacity']))),k.stroke&&(k.stroke=convertColor(k.stroke,100*pluckNumber(k['stroke-opacity'],i['stroke-opacity']))),h.plotBorderThickness=i['stroke-width']=g['stroke-width']=pluckNumber(safeMin([+i['stroke-width'],+g['stroke-width']])),Object.assign(Object.assign({},j),l))n[b]=pluck(g[b],'');for(b in defined(n['stroke-dasharray'])&&(n['stroke-dasharray']=''),Object.assign(Object.assign({},k),m))o[b]=i[b]||'';defined(o['stroke-dasharray'])&&(n['stroke-dasharray']=''),h.bullOpacity=g.opacity,h['bull-fill-opacity']=g['fill-opacity'],h['bull-stroke-opacity']=g['stroke-opacity'],h.bearOpacity=i.opacity,h['bear-fill-opacity']=i['fill-opacity'],h['bear-stroke-opacity']=i['stroke-opacity'],delete g.opacity,delete g['fill-opacity'],delete g['stroke-opacity'],delete i.opacity,delete i['fill-opacity'],delete i['stroke-opacity']}else h[b]=a[b];if(!h.skipLimitCalc){h.limit=c._calculateLimits();const a=c.getFromEnv('chart');a.setYScaleLimit(h.scaleY.getId(),c.getId(),h.limit.y,h.limit.baseRequired),a.setXScaleLimit(h.scaleX.getId(),c.getId(),h.limit.x)}}}_calculateLimits(){let a,{indices:b,data:c,scaleY:d,scaleX:e}=this.config,f=c[0],g=c[c.length-1],h=e.showPlotOverTick(),i='visible'===this.config.visibility,j='log'===d.getType(),k=this.getFromEnv('chart'),l=k.getFocusLimit(),m=l[0],n=l[1],o={},p=a=>j&&0>=a?null:a;return a=this.getPadding(),o.x=h?i?[f&&f[b[0]].start-a,g&&g[b[0]].start+a]:UNDEF:i?[f&&f[b[0]].start-a,g&&g[b[0]].end-a]:UNDEF,o.y=i?[safeMin(c,a=>{if(!(a[0].end<m)&&!(a[0].start>n))return safeMin([a[1],a[2],a[3],a[4]],p)}),safeMax(c,a=>{if(!(a[0].end<m)&&!(a[0].start>n))return safeMax([a[1],a[2],a[3],a[4]],p)})]:UNDEF,o.baseRequired=!1,o}_createContainer(){let a=this,b=a.config,c=b.bullStyle,d=b.bearStyle;a.addGraphicalElement({el:'group',container:{id:'meso',label:'group',isParent:!0},component:a,label:'group',id:'meso',attr:{name:'candlestick-dataset-meso'}}),a.addGraphicalElement({el:'group',container:{id:'meso',label:'group'},component:a,label:'group',id:'meso-bull',attr:Object.assign({name:'bullGroup-meso',visibility:b.visibility},c)}),a.addGraphicalElement({el:'group',container:{id:'meso',label:'group'},component:a,label:'group',id:'meso-bear',attr:Object.assign({name:'bearGroup-meso',visibility:b.visibility},d)})}getName(){return'timeseriesCandlestick'}allocatePosition(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,z=Math.max,A=this,B=A.getFromEnv('binDecider'),C=A.getFromEnv('xScale'),D=A.getFromEnv('yScale'),E=A.config,[F,G,H,I,J]=E.indices,K=E.dataInfo,L=B.getRangeThreshold()[2],[M,N]=C.getDomain(),O=E.data;(E.repositioningDone=A._isRepositioningNeeded())&&(E.timeStampGap=L,s=C.getRangeValue(L)-C.getRangeValue(0),b=z(s*(1-E.plotSpacePercent/100)-E.plotBorderThickness,1),w=+C.getDomainValue(s*(E.plotSpacePercent/200))-+C.getDomainValue(0),E.actualStartDomain=+M+w,E.actualEndDomain=+N-w,K=E.dataInfo=[],O.forEach((s,t)=>{c=s[F],j=s[G],k=s[J],l=s[H],m=s[I],t||(o=E.firstTimeStamp=c.start),v=D.getRangeValue(k),u=D.getRangeValue(j),r=D.getRangeValue(l),q=D.getRangeValue(m),isValidNumber(u)&&isValidNumber(v)&&(j>k?(h=v,g=u,i='bear'):(g=v,h=u,i='bull'),a=z(Math.abs(h-g),1),e=g>h?h:g),f=C.getRangeValue(new Date(c.start),new Date(c.end)),d=f-b/2,n={startDate:c.start,endDate:c.end},n.x=d,n.midX=f,n.colHeight=a||0,n.colY=e||u||v,n.y=n.highStickYExtend=r,n.width=b,n.openValue=j,n.closeValue=k,n.highValue=l,n.lowValue=m,n.value=k,n.lowStickYEntend=q,n.height=q-r||0,n.groupId=i||'bull',n.groupConfig='bull'===i?'bullConfig':'bearConfig',p=C.getBinIndex(c.start,o),K[p]=n})),(t=E.hoverInfo)&&t.forEach(a=>{'object'==typeof K[a.index]&&(K[a.index].style=extend2(K[a.index].style,a[K[a.index].groupId+'Style']))})}_drawPlot(){var a,b,c=this,d=c.config,e=d.dataInfo,f=defined(d.bullOpacity),g=defined(d['bull-fill-opacity']),h=defined(d['bull-stroke-opacity']),i=defined(d.bearOpacity),j=defined(d['bear-fill-opacity']),k=defined(d['bear-stroke-opacity']),l={},m={};f&&(l.opacity=d.bullOpacity),g&&(l.opacity=d['bull-fill-opacity']),h&&(l.opacity=d['bull-stroke-opacity']),i&&(m.opacity=d.bearOpacity),j&&(m.opacity=d['bear-fill-opacity']),k&&(m.opacity=d['bear-stroke-opacity']),e.forEach((d,f)=>{d.groupConfig&&(b='bull'===d.groupId?l:m,0<d.colHeight?(a=Object.assign({x:d.x,y:d.colY,height:d.colHeight,width:d.width},b),d.style&&Object.assign(a,d.style),c.addGraphicalElement({el:'rect',component:c,props:{index:f,dataLength:e.length},container:{label:'group',id:'meso-'+d.groupId},label:'rect',attr:a},!0)):(d.openValue||d.closeValue)&&(a=Object.assign({path:`M${d.x},${d.colY},H${d.x+d.width}`},b),d.style&&Object.assign(a,d.style),c.addGraphicalElement({el:'path',component:c,props:{index:f,dataLength:e.length},container:{label:'group',id:'meso-'+d.groupId},label:'line',attr:a},!0)),d.highStickYExtend&&(a=Object.assign({path:`M${d.midX},${d.highStickYExtend},V${d.colY||d.highStickYExtend+d.height}`,"stroke-linecap":'round'},b),d.style&&Object.assign(a,d.style),c.addGraphicalElement({el:'path',component:c,props:{index:f,dataLength:e.length},container:{label:'group',id:'meso-'+d.groupId},label:'high',attr:a},!0)),d.lowStickYEntend&&(a=Object.assign({path:`M${d.midX},${d.lowStickYEntend},V${d.colY+d.colHeight||d.lowStickYEntend-d.height}`,"stroke-linecap":'round'},b),d.style&&Object.assign(a,d.style),c.addGraphicalElement({el:'path',component:c,props:{index:f,dataLength:e.length},container:{label:'group',id:'meso-'+d.groupId},label:'low',attr:a},!0)))}),d.hoverInfo=[]}_getTooltext(a,b,c){var d,e,f=this,g=f.config,h=g.dataInfo,i=h[c],j=a?b?1:.5:1;return f._isInvalidTooltext(i)?'':(d=defined(i.groupConfig),e=`<div style='margin-bottom: 5px; opacity:${j}'> <div>${g.series}</div> </div>`,d?(g.series?e:'')+['open','high','low','close'].reduce((a,b)=>{let c=i[b+'TooltipValue']||(i[b+'TooltipValue']=i[b+'Value']&&g.formatterFn({value:i[b+'Value'],type:'tooltip',prefix:g.prefix,suffix:g.suffix}));return a+(void 0===c?'':`<div style = 'overflow: hidden;margin-top:4px;'> <div style = 'float: left;'>${b}</div> <div style = 'float: right;'>${c}</div> </div>`)},''):e)}}export default CandleStick;