UNPKG

quoslibero

Version:

FusionCharts JavaScript charting framework

1 lines 5.81 kB
import{SmartRenderer}from'../../../../fc-core/src/component-interface';import{extend2,pluck,pluckNumber}from'../../../../fc-core/src/lib';import isValidNumber from'../../../../fc-utils/src/type/is-valid-number';const anchorRadius=3,defaultTagWidth=29,GUTTER_5=5,M='M',L='L',h='h',v='v',z='z',COLOR_F3F3F3='#f3f3f3',COLOR_595959='#595959';class ReferenceLine extends SmartRenderer{constructor(){super(),this.hoverInHandler=()=>{this.setState('hovered',!0),this.asyncDraw()},this.hoverOutHandler=()=>{this.setState('hovered',!1),this.asyncDraw()}}__setDefaultConfig(){super.__setDefaultConfig(),this.config.defaultStyle={text:{fill:'#5F5F5F',"vertical-align":'top',opacity:'1',"fill-opacity":'1',"stroke-opacity":'1',"font-size":'11px',"font-style":'normal'},marker:{fill:'#B70000',stroke:'#B70000',"fill-opacity":'1',"stroke-opacity":'1',opacity:'1',borderthickness:0,borderpadding:2,borderradius:0,borderdash:'none',"stroke-width":'1'}},this.config.direction='right'}configureAttributes(a={}){super.configureAttributes(a);let b=this,c=b.config,d=b.getFromEnv('getStyleDef'),e=a.referenceLine||{};c.markerStyle=d(e.style&&e.style.marker),c.textStyle=d(e.style&&e.style.text),c.label=pluck(e.label,''),c.value=pluckNumber(e.value,0),a.yScale&&(c.yScale=a.yScale),a.formatterFn&&(c.formatterFn=a.formatterFn),a.prefix&&(c.prefix=a.prefix),a.suffix&&(c.suffix=a.suffix),a.direction&&(c.direction=a.direction)}getPathArr(){let a,b=this,c=b.config,d=c.direction,e=b.getLinkedParent().config,f=c.yScale,g=f.getDomain(),h=c.value,i=[];return isValidNumber(g[0])&&isValidNumber(g[1])&&h>=g[0]&&h<=g[1]&&(a=f.getRangeValue(h),'left'===d?i=['M',e.canvasLeft+e.canvasWidth-e.borderConfig.rightBorder,a,'h',-e.canvasWidth]:'right'===d?i=['M',e.canvasLeft+e.borderConfig.leftBorder,a,'h',e.canvasWidth]:'top'===d||'bottom'===d?i=['M',a,e.canvasTop,'v',e.canvasHeight-e.borderConfig.bottomBorder]:void 0),i}getLabelsProps(a,b){let c=this.config,d=c.direction,e={isValidLabel:!1};return a.length&&c.label&&(e.isValidLabel=!0,'top'===d?(e.x=a[1],e.y=a[2],e.textAnchor='start'):'bottom'===d?(e.x=a[1],e.y=a[4],e.textAnchor='start'):'left'===d?(e.x=a[1]+a[4]+anchorRadius,e.y=a[2]+b.borderpadding,e.textAnchor='start'):'right'===d?(e.x=a[1]+a[4]-b.borderpadding-1,e.y=a[2]+b.borderpadding,e.textAnchor='end'):void 0),e}getBulbPoints(a){let b=this.config,c=b.direction,d={};return a&&('left'===c?(d.cx=a[1]+a[4]-anchorRadius,d.cy=a[2]):'right'===c?(d.cx=a[1]+a[4]+anchorRadius,d.cy=a[2]):void 0),d}getTagProps(a,b){let c,d,e,f,g=this,i=this.config,j=i.value,k=g.getFromEnv('smartLabel'),l=g.getLinkedParent().config,m=l.canvasLeft,n=l.canvasWidth,o=l.canvasLeft+n,p={},q={},r=i.direction;return d=i.yScale.getRangeValue(j),c=k.getSmartText(j),e=c.width+2*a.borderpadding,e=e>defaultTagWidth?e:defaultTagWidth,'right'===r?(f=[M,m+l.borderConfig.leftBorder,d,L,m-GUTTER_5,d,L,m-10,d-c.height/2-2*b.marker.borderpadding,h,-e,v,c.height+4*b.marker.borderpadding,h,e,L,m-GUTTER_5,d,z],p.x=m-10-e/2,p.y=d):'left'===r?(f=[M,o,d,L,o+GUTTER_5,d,L,o+10,d-c.height/2-2*b.marker.borderpadding,h,e,v,c.height+4*b.marker.borderpadding,h,-e,L,o+GUTTER_5,d,z],p.x=o+10+e/2,p.y=d):void 0,(q.tagPath=f,q.tagTextPosition=p,q)}draw(){let a,b,c,d,e,f,g,h,i,j,k,l,m=this,n=m.config,o=n.value,p=m.getFromEnv('smartLabel'),q=n.defaultStyle,r=n.textStyle,s=n.markerStyle,t=n.label,u=m.getState('hovered'),v=m.getFromEnv('baseTextStyle');m.addGraphicalElement({el:'group',container:{id:'thermo',label:'group',isParent:!0},component:m,label:'refLine',attr:{name:'reference-line-thermo'},id:'thermo'},!0),m.addGraphicalElement({el:'group',container:{id:'thermo',label:'refLine'},component:m,label:'refLine-thermo-hover-group',attr:{name:'reference-line-hover-elem-group'},id:'thermo'},!0),m.addGraphicalElement({el:'group',container:{id:'thermo',label:'refLine'},component:m,label:'refLine-thermo-group',attr:{name:'reference-line-group'},id:'thermo'},!0),Object.assign(q.text,v),a=extend2(extend2({},q.text),r),b=extend2(extend2({},q.marker),s),d=extend2({},b),g=m.getPathArr(),h=m.getLabelsProps(g,b),i=m.getBulbPoints(g),g.length&&(m.addGraphicalElement({el:'path',attr:{path:g,stroke:b.stroke||b.fill,"fill-opacity":b['fill-opacity'],"stroke-opacity":b['stroke-opacity'],"stroke-width":b['stroke-width'],"stroke-linecap":b['stroke-linecap'],opacity:b.opacity*(u?1:.4),"stroke-dasharray":b['stroke-dasharray']||'none'},container:{label:'refLine-thermo-group'},component:m,label:'line'},!0),d['stroke-width']=u?b['stroke-width']||1:0,d.stroke=s&&s.stroke||COLOR_595959,m.addGraphicalElement({el:'circle',attr:{cx:i.cx,cy:i.cy,r:anchorRadius},container:{label:'refLine-thermo-group'},css:d,component:m,label:'anchor'}),u&&(c=extend2(extend2(extend2({},q),{text:{fill:COLOR_F3F3F3}}),{text:r,marker:s}),h.isValidLabel&&m.addGraphicalElement({el:'text',attr:{text:t,x:h.x,y:h.y-b.borderpadding*('top'===a['vertical-align']?-1:1),opacity:a.opacity,"text-anchor":h.textAnchor,"text-bound":[b.fill,b.fill,b.borderthickness,b.borderpadding,b.borderradius,b.borderdash,.2]},css:a,container:{label:'refLine-thermo-hover-group'},component:m,label:'label'},!0),p.setStyle({fontFamily:a['font-family'],fontWeight:a['font-weight'],fontSize:a['font-size'],fontStyle:a['font-style']}),j=m.getTagProps(b,c),k=j.tagPath,l=j.tagTextPosition,e=extend2({},c.marker),!e.stroke&&(e.stroke='none'),m.addGraphicalElement({el:'path',attr:{path:k},container:{label:'refLine-thermo-hover-group'},css:e,component:m,label:'tag'},!0),f=extend2({},c.text),!f['text-anchor']&&(f['text-anchor']='middle'),f['vertical-align']='middle',m.addGraphicalElement({el:'text',attr:{text:n.formatterFn({value:o,type:'referenceline',prefix:n.prefix,suffix:n.suffix}),x:l.x,y:l.y},css:f,container:{label:'refLine-thermo-hover-group'},component:m,label:'tag-text'},!0)))}}export default ReferenceLine;