quoslibero
Version:
FusionCharts JavaScript charting framework
1 lines • 3.9 kB
JavaScript
import{ComponentInterface}from'../../../fc-core/src/component-interface';import getLinearRegressionPoints from'./linear-regression';import getPolynomialRegressionPoints from'./polynomial-regression';import{pluckNumber,pluck,toRaphaelColor}from'../../../fc-core/src/lib';import{createPolynomial,createLinear}from'./utility-functions';import{addDep}from'../../../fc-core/src/dependency-manager';import regressionLineAnimation from'./regression-line.animation';addDep({name:'regressionLineAnimation',type:'animationRule',extension:regressionLineAnimation});export default class RegressionLineExtension extends ComponentInterface{constructor(){super(),this._config={regressionPoints:[]},this.regressionLine=[]}getType(){return'extension'}getName(){return'regression'}configureAttributes(a){let b=this,c=b.getFromEnv('dataSource').chart,d=a.config,e=d.JSONData,f=b._config;f.regressionPoints.splice(0,f.regressionPoints.length),f.showYOnX=pluckNumber(e.showyonx,c.showyonx,1),f.regressionLineColor=toRaphaelColor(pluck(e.regressionlinecolor,c.regressionlinecolor,d.anchorbordercolor,d.lineColor,d.anchorBgColor,'fff000')),f.polynomialRegressionLineColor=toRaphaelColor(pluck(e.polynomialregressionlinecolor,c.polynomialregressionlinecolor,f.regressionLineColor)),f.regressionLineThickness=pluckNumber(e.regressionlinethickness,c.regressionlinethickness,e.anchorborderthickness,1),f.polynomialRegressionLineThickness=pluckNumber(e.polynomialregressionlinethickness,c.polynomialregressionlinethickness,f.regressionLineThickness),f.regressionLineAlpha=pluckNumber(e.regressionlinealpha,c.regressionlinealpha,100),f.polynomialRegressionLineAlpha=pluckNumber(e.polynomialregressionlinealpha,c.polynomialregressionlinealpha,f.regressionLineAlpha),createLinear(c,e)&&f.regressionPoints.push(getLinearRegressionPoints(e.data,f.showYOnX)),createPolynomial(c,e)&&f.regressionPoints.push(getPolynomialRegressionPoints(e.data,f.showYOnX))}draw(){let a,b,c,d,e,f,g,h=this,i=h._config,k=h.getFromEnv('xAxis'),l=h.getFromEnv('yAxis'),j=h.getFromEnv('chart'),m=j.getChildren('canvas')[0],o=m.config,p=h.getLinkedParent(),q=p.getState('visible'),r=h.getContainer('regressionlinecontainer'),s=j.getChildContainer('abovePlotGroup'),t=h.getFromEnv('animationManager'),u=i.regressionPoints,v=[],w=[],x=h.getGraphicalElement('regressionLineLinear'),y=h.getGraphicalElement('regressionLinePolynomial'),z=function(){this.hide()};q||(g='disappearing'),r=h.addContainer('regressionlinecontainer',t.setAnimation({el:r||'group',attr:{name:'regressionlinecontainer',"clip-rect":[o.canvasLeft,o.canvasTop,o.canvasWidth,o.canvasHeight]},container:s,component:h,label:'group'}));for(let b,c=0;c<u.length;c++){if(b='',v=u[c][0],w=u[c][1],a=w.length,0===v.length&&0===w.length)d=!0,b+='M0,0';else if(0!==v.length&&0===w.length)d=!1,b+='M0,0';else if(0===v.length)d=!0,b+='M'+k.getPixel(w[0].x)+','+l.getPixel(w[0].y),b+='L'+k.getPixel(w[a-1].x)+','+l.getPixel(w[a-1].y);else{d=!1,b+='M'+k.getPixel(w[0].x)+','+l.getPixel(w[0].y),b+=' R'+k.getPixel(w[1].x)+','+l.getPixel(w[1].y);for(let c=2;c<a;c++)b+=' '+k.getPixel(w[c].x)+','+l.getPixel(w[c].y)}d?e={opacity:+i.regressionLineAlpha/100,stroke:i.regressionLineColor,"stroke-width":i.regressionLineThickness,path:b}:f={opacity:+pluck(i.polynomialRegressionLineAlpha,i.regressionLineAlpha)/100,stroke:pluck(i.polynomialRegressionLineColor,i.regressionLineColor),"stroke-width":pluck(i.polynomialRegressionLineThickness,i.regressionLineThickness),path:b}}'disappearing'!==g&&(x&&x.show(),y&&y.show()),b=t.setAnimation({el:x||'path',container:r,component:h,doNotRemove:!0,attr:'disappearing'!==g&&e,callback:'disappearing'===g&&z,label:'path'}),x||h.addGraphicalElement('regressionLineLinear',b),c=t.setAnimation({el:y||'path',container:r,component:h,doNotRemove:!0,attr:'disappearing'!==g&&f,callback:'disappearing'===g&&z,label:'path'}),y||h.addGraphicalElement('regressionLinePolynomial',c)}}