-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphpdf.min.js
More file actions
1 lines (1 loc) · 5.75 KB
/
graphpdf.min.js
File metadata and controls
1 lines (1 loc) · 5.75 KB
1
function Diagram(t){let e=document.createElement("canvas"),i=e.getContext("2d");e.width=Constants.PDF_WIDTH,i.fillStyle=i.strokeStyle="#9e9e9e",i.lineWidth=.5,i.font="lighter 14px sans-serif",i.translate(25,25),this.data=t.data,this.entries=Object.keys(this.data),this.diagram=t.diagram,this.legend=!!t.legend&&{x:t.legend.x?t.legend.x:0,y:t.legend.y?t.legend.y:0,percentages:t.legend.percentages},this.colors=(t.colors?t.colors:[]).concat(Constants.COLORS),this.canvas=e,this.ctx=i,this.diagram.id&&(e.id=this.diagram.id)}function PieChart(){var t,e,i;Diagram.call(this,arguments[0]);let l=this.ctx,n=this.data,s=this.legend,a=this.diagram.rad;for(t in e=i=0,this.canvas.height=(Constants.BORDER_OFFSET+a)*2,l.translate(this.diagram.x,0),l.globalCompositeOperation="destination-over",l.translate(a,a),n)n[t]&&(i+=n[t]*Math.PI*2,l.fillStyle=this.colors[e],l.beginPath(),l.arc(0,0,a,0,i),l.lineTo(0,0),l.fill()),e++;l.globalCompositeOperation="source-over",l.translate(-a,-a),s&&(s.x+=2*a+20,Utils.drawLegend(l,s,this.entries,this.colors,n))}function BarChart(){Diagram.call(this,arguments[0]);let t=this.ctx,e=this.data,i=this.entries,l=this.diagram,n=this.legend,s=l.grid,a=Utils.getNearest10(e);var o,r,$,h,f=0,g=0,c=Object.values(e)[0];for(f in"object"==typeof c?"Array"!==c.constructor.name&&(c=Object.keys(c)):c=[c],"auto"===s.rows&&(s.rows=i.length+1),"auto"===s.cols&&(s.cols=Constants.AUTO_COLUMNS),$=.6*Constants.PDF_WIDTH,h=i.length*(c.length+1)*20,this.canvas.height=h+2*Constants.BORDER_OFFSET,t.translate(l.x,0),l.width=$,l.height=h,Utils.drawGrid(t,l,a),t.font="lighter 14px sans-serif",t.textAlign="right",t.textBaseline="middle",r=0,e){if(g+=10,t.fillStyle="#000",t.fillText(f,-10,g+20*c.length/2+Constants.TEXT_OFFSET_Y),"object"!=typeof e[f])t.fillStyle=this.colors[r],t.fillRect(0,g,e[f]/a*$,20),l.indicators&&e[f]&&(t.textAlign="left",t.fillText(e[f],e[f]/a*$+5,g+10+Constants.TEXT_OFFSET_Y),t.textAlign="right"),g+=20,r++;else for(o in r=0,e[f])t.fillStyle=this.colors[r],t.fillRect(0,g,e[f][o]/a*$,20),l.indicators&&e[f][o]&&(t.textAlign="left",t.fillText(e[f][o],e[f][o]/a*$+5,g+10+Constants.TEXT_OFFSET_Y),t.textAlign="right"),r++,g+=20;g+=10}n&&(n.x+=$+10,Utils.drawLegend(t,n,c.length>1?c:i,this.colors))}function LineChart(){var t,e,i,l,n,s,a,o;Diagram.call(this,arguments[0]);let r=this.ctx,$=this.data,h=this.entries,f=this.diagram,g=f.grid,c=this.legend,d=[f.x,25],T=Utils.getNearest10($),x={};for(t in"auto"===g.rows&&(g.rows=Constants.AUTO_ROWS),g.cols=Object.values($).map(function(t){return Object.keys(t).length}).sum()-1,s=.6*Constants.PDF_WIDTH,a=250,f.width=s,f.height=a,this.canvas.height=a+50+(1===h.length?0:20),r.translate(d[0],d[1]),o=Utils.drawLCGrid(r,f,T,$),$)for(e in $[t])for(i in $[t][e])void 0===x[i]&&(x[i]=[]),x[i].push($[t][e][i]);for(t in i=0,r.lineWidth=2,x){if(l=0,n=(1-x[t][0]/T)*a,r.strokeStyle=this.colors[i],r.beginPath(),r.moveTo(l,n),1===x[t].length)r.lineTo(s,n);else for(e=1;e<x[t].length;e++)l+=o,n=(1-x[t][e]/T)*a,r.lineTo(l,n);r.stroke(),i++}c&&(c.x+=s+10,Utils.drawLegend(r,c,Object.keys(x),this.colors))}const Constants={PDF_WIDTH:880,AUTO_ROWS:6,AUTO_COLUMNS:6,BORDER_OFFSET:25,TEXT_OFFSET_Y:2,COLORS:["#2979ff","#ff8a65","#ffd600","#8e24aa","#4caf50","#e64a19","#009688","#1de9b6","#42a5f5","#ef9a9a","#6d4c41","#ffa000","#f06292","#c5cae9","#aed581","#9575cd","#f44336","#607d8b",]},defineGraphPDFConstant=function(t,e){Constants[t]=e},Utils={getNearest10:function(t){var e,i,l,n,s,a,o;for(s in e=i=l=[],s=a=o=0,t)if("object"==typeof t[s]){if("object"==typeof Object.values(t[s])[0]){for(a in t[s])if("object"==typeof t[s][a]){for(o in t[s][a])t[s][a][o]&&l.push(t[s][a][o]);i=i.concat(l)}else i=i.concat([t[s][a]]);e=e.concat(i)}else e=e.concat(Object.values(t[s]))}else e=e.concat([t[s]]);return 0==(n=10*Math.ceil(Math.max.apply(null,e)/10))?10:n},drawGrid:function(t,e,i){let l=e.width,n=e.height,s=e.grid,a=s.rows+1,o=s.cols+1,r=l/(o-1),$=n/(a-1),h=o-1;if(t.fillStyle=t.strokeStyle="#9e9e9e",t.lineWidth=.5,t.font="lighter 12px sans-serif",t.textAlign="center",t.textBaseline="top",a>1){var f,g=0;for(t.beginPath(),f=0;f<a;f++,g+=$)t.moveTo(0,g),t.lineTo(l,g);t.stroke()}if(o>1){var c=0;for(t.beginPath(),f=0;f<o;f++,c+=r)t.moveTo(c,0),t.lineTo(c,n),t.fillText(f/h*i,c,n+5);t.stroke()}},drawLCGrid:function(t,e,i,l){let n=e.width,s=e.height,a=e.grid,o=a.truncate,r=a.rows,$=a.cols,h=n/$,f=s/r;var g=Object.values(l),c=Object.keys(l);if(t.fillStyle=t.strokeStyle="#9e9e9e",t.lineWidth=.5,t.font="lighter 12px sans-serif",t.textAlign="right",t.textBaseline="middle",r){var d=f*r;for(t.beginPath(),_=0;_<=r;_++,d-=f)t.moveTo(0,d),t.lineTo(n,d),_&&t.fillText(_/r*i,-5,d+2);t.stroke()}if(t.textAlign="center",t.textBaseline="top",1===c.length){var T=Object.keys(g[0]);if(1===T.length)t.beginPath(),t.moveTo(0,0),t.lineTo(0,s),t.moveTo(n,0),t.lineTo(n,s),t.stroke(),t.fillText(T[0],n/2,s+5);else if($){var x=0;for(t.beginPath(),_=0;_<=$;_++,x+=h)t.moveTo(x,0),t.lineTo(x,s),u=T[_],o&&(u=u.slice(0,o)),t.fillText(u,x,s+5);t.stroke()}}else{var _,v,u,T,x=0;let y=c;for(t.beginPath(),_=0;_<y.length;_++)for("Janvier"===(T=Object.keys(l[c[_]]))[0]&&t.fillText(y[_],x,s+20),v=0;v<T.length;v++,x+=h)t.moveTo(x,0),t.lineTo(x,s),u=T[v],o&&(u=u.slice(0,o)),t.fillText(u,x,s+5);t.stroke()}return h},drawLegend:function(t,e,i,l,n){var s,a,o;let r=e.x,$=e.y;for(s=a=0,t.font="lighter 14px sans-serif",t.textAlign="left",t.textBaseline="middle",t.translate(r,$);a<i.length;a++,s+=30)t.fillStyle=l[a],t.fillRect(0,s,50,20),o=i[a],e.percentages&&(o+=" ("+Math.floor(1e3*n[i[a]])/10+"%)"),t.fillStyle="#000",t.fillText(o,57,s+10+Constants.TEXT_OFFSET_Y)}};Array.prototype.map=function(t){let e=[],i=this.length;for(var l=0;l<i;l++)e[l]=t(this[l],l,this);return e},Array.prototype.sum=function(){var t,e;let i=this.length;for(t=e=0;e<i;e++)t+=this[e];return t},Object.values=function(t){var e;let i=[];for(e in t)i.push(t[e]);return i};