UNPKG

zingchart

Version:

Our JavaScript charting library is a commercial product. But the full branded version is free to try, forever.

8 lines (7 loc) 15.4 kB
/* All of the code within the ZingChart software is developed and copyrighted by ZingChart, Inc., and may not be copied, replicated, or used in any other software or application without prior permission from ZingChart. All usage must coincide with the ZingChart End User License Agreement which can be requested by email at support@zingchart.com. Build 2.9.16-hf1_ES6 */ zingchart.setModule("heatmap"),function(){if(zingchart.plugins.heatmap=zingchart.plugins.heatmap||{},!ZC.ZCHeatMap){var t={};ZC.ZCHeatMap=function(){this.AC="canvas",this.I=0,this.F=0,this.aDataInit=null,this.aData=[],this.oKVData={},this.aSize=[15,15],this.iBlur=45,this.L2=!1,this.bDiscrete=!1,this.iIntensity=1,this.A7=null,this.bComposite=!0,this.aGradient=[[.4,"#0000ff"],[.6,"#00ffff"],[.7,"#bfff00"],[.8,"#ffff00"],[1,"#ff0000"]],this.oGradientData=null,this.oBrush=null,this.sBrushType="circle",this.sBrushInfo=null,this.bSortData=!1,this.addData=function(t){this.aData.push(t)},this.setData=function(t,a){if(this.ABE&&-1===a.indexOf("scatter"))for(var e=0,i=t.length;e<i;e++)this.oKVData[t[e][0]+"/"+t[e][1]]=t[e][2];this.aData=t,this.aDataInit||(this.aDataInit=t),this.bSortData&&this.aData.sort(function(t,a){return t[2]-a[2]})},this.B0=null,this.BE=null,this.C5=.5,this.Z=null,this.ABE=null,this.setMinMax=function(){if(null===this.B0||null===this.BE)for(var t=0,a=this.aData.length;t<a;t++)this.B0=Math.min(this.B0,this.aData[t][2]),this.BE=Math.max(this.BE,this.aData[t][2])},this.createGradient=function(){var t=document.createElement("canvas"),a=t.getContext("2d"),e=a.createLinearGradient(0,0,0,256);t.width=1,t.height=256;for(var i=0;i<this.aGradient.length;i++)e.addColorStop(this.aGradient[i][0],this.aGradient[i][1]);a.fillStyle=e,a.fillRect(0,0,1,256),this.oGradientData=a.getImageData(0,0,1,256).data},this.createBrush=function(){var t=document.createElement("canvas"),a=t.getContext("2d");"circle"===this.sBrushType?t.width=t.height=2*(this.aSize[0]+this.iBlur):"square"===this.sBrushType?t.width=t.height=this.aSize[0]+2*this.iBlur:"rectangle"===this.sBrushType&&(t.width=this.aSize[0]+2*this.iBlur,t.height=this.aSize[1]+2*this.iBlur);a.shadowOffsetX=a.shadowOffsetY=200,a.shadowBlur=this.iBlur,a.shadowColor="#000","circle"===this.sBrushType?(a.beginPath(),a.arc(this.aSize[0]+this.iBlur-200,this.aSize[0]+this.iBlur-200,this.aSize[0],0,2*Math.PI,!0),a.closePath(),a.fill()):"square"===this.sBrushType?a.fillRect(this.iBlur-200,this.iBlur-200,this.aSize[0],this.aSize[0]):"rectangle"===this.sBrushType&&a.fillRect(this.iBlur-200,this.iBlur-200,this.aSize[0],this.aSize[1]),this.oBrush=t},this.reset=function(){this.aData=this.aDataInit},this.paint=function(){var t,a;if(this.aData.length){this.UID=this.UID?++this.UID:1;var e=this.Z.getContext("2d",{willReadFrequently:!0}),i=this,s=null,r=null;if(!this.ABE||((s=zingchart.getLoader(this.ABE.loaderid))&&(r=zingchart.getGraph(s,this.ABE[ZC._[3]])),r)){var h=[],n={};if(this.ABE){var l=zingchart.exec(this.ABE.loaderid,"getobjectinfo",{graphid:this.ABE[ZC._[3]],object:"plotarea"});l&&(e.beginPath(),e.rect(l.x-r.iX,l.y-r.iY,l.width,l.height),e.clip(),e.closePath());var o=zingchart.exec(this.ABE.loaderid,"getobjectinfo",{object:"scale",name:this.ABE.keyscale.name,graphid:this.ABE[ZC._[3]]}),c=zingchart.exec(this.ABE.loaderid,"getobjectinfo",{object:"scale",name:this.ABE.valscale.name,graphid:this.ABE[ZC._[3]]});if(o&&c){var u=(o.step+"").split(".");this.ABE.keyscale.decimals=2===u.length?u[1].length:0;var d=(c.step+"").split(".");this.ABE.valscale.decimals=2===d.length?d[1].length:0;var f=[],g=[],C=0,p=0,Z=[];"xy"===r.AJ.layout?-1===r.AF.indexOf("scatter")&&(this.aSize=[Math.ceil(o.stepSize)+.5,Math.ceil(c.stepSize)+.5]):"radar"===r.AJ.layout&&(g=[o.x+o.width/2,o.y+o.height/2]);var m=r.BK(this.ABE.keyscale.name),v=r.BK(this.ABE.valscale.name),B=1;if(-1!==r.AF.indexOf("scatter")&&this.aData.length>1e5){var D=Math.max((m.BE-m.B0)/(m.V[m.V.length-1]-m.V[0]),(v.BE-v.B0)/(v.H0-v.FS));B=Math.max(1,Math.round(5*D))}for(z=0,t=this.aData.length;z<t;z++)if((a=this.aData[z])[0]>=m.B0&&a[0]<=m.BE&&a[1]>=v.B0&&a[1]<=v.BE){var y=m.AL(a[0]),_=v.AL(a[1]);"radar"===r.AJ.layout?(y[0]-=r.iX,y[1]-=r.iY,_[0]-=r.iX,_[1]-=r.iY,C=180*Math.atan2(y[1]-g[1],y[0]-g[0])/Math.PI,p=g[1]-_[1]+c.offsetStart,Z=ZC.AR.BN(g[0],g[1],p,C),f.push([Z[0],Z[1],a[2],a[3]])):(y-=r.iX,_-=r.iY,f.push([y,_,a[2],a[3]]),-1!==r.AF.indexOf("scatter")&&(B>1?n[B*Math.round(y/B)+"/"+B*Math.round(_/B)]=a:n[Math.round(y)+"/"+Math.round(_)]=a))}if(-1!==r.AF.indexOf("scatter")){for(var x in n){var A=x.split("/");h.push({x:parseFloat(A[0]),y:parseFloat(A[1]),key:n[x][0],val:n[x][1],v:n[x][2],info:n[x][3]})}this.oKDTree=new ZC.ZCKDTree(h)}this.aData=f}}if(this.bDiscrete){for(var z=0;z<this.aData.length;z++)n[Math.round(this.aData[z][0])+"/"+Math.round(this.aData[z][1])]=this.aData[z];for(var x in n){A=x.split("/");h.push({x:parseFloat(A[0]),y:parseFloat(A[1]),v:n[x][2]})}this.oKDTree=new ZC.ZCKDTree(h)}this.oGradientData||this.createGradient(),this.sBrushInfo||(this.sBrushInfo=[this.sBrushType,this.aSize[0],this.aSize[1]].join("-")),this.oBrush&&this.sBrushInfo===[this.sBrushType,this.aSize[0],this.aSize[1]].join("-")||(this.createBrush(),this.sBrushInfo=[this.sBrushType,this.aSize[0],this.aSize[1]].join("-")),this.setMinMax(),e.clearRect(0,0,i.I,i.F);var b=this.L2?25:1,M=this.UID,K=function(s,r,h){for(z=s;z<r;z++)if(a=i.aData[z]){var n=1;if(i.B0!==i.BE&&(n=Math.max((a[2]-i.B0)/(i.BE-i.B0),.01)),i.bDiscrete){var l=4*Math.floor(255*n),o=i.oGradientData[l],c=i.oGradientData[l+1],u=i.oGradientData[l+2];e.fillStyle="rgb("+o+","+c+","+u+")";e.shadowOffsetX=e.shadowOffsetY=99999,e.shadowBlur=i.iBlur,e.shadowColor="rgb("+o+","+c+","+u+")";for(var d=0;d<i.iIntensity;d++)e.beginPath(),e.arc(a[0]-99999,a[1]-99999,i.aSize[0],0,2*Math.PI,!0),e.fill(),e.closePath()}else if(i.ABE&&!i.bComposite){l=4*Math.floor(255*n),o=i.oGradientData[l],c=i.oGradientData[l+1],u=i.oGradientData[l+2];e.fillStyle="rgb("+o+","+c+","+u+")","circle"===i.sBrushType?(e.beginPath(),e.arc(a[0],a[1],i.aSize[0],0,2*Math.PI),e.fill(),e.closePath()):e.fillRect(a[0]-i.aSize[0]/2,a[1]-i.aSize[1]/2,i.aSize[0],i.aSize[1])}else e.globalAlpha=n,"circle"===i.sBrushType?e.drawImage(i.oBrush,a[0]-i.aSize[0]-i.iBlur,a[1]-i.aSize[0]-i.iBlur):"square"===i.sBrushType?e.drawImage(i.oBrush,a[0]-i.aSize[0]/2-i.iBlur,a[1]-i.aSize[0]/2-i.iBlur):"rectangle"===i.sBrushType&&e.drawImage(i.oBrush,a[0]-i.aSize[0]/2-i.iBlur,a[1]-i.aSize[1]/2-i.iBlur)}if(!i.bDiscrete&&(!i.ABE||i.bComposite)){var f=e.getImageData(0,0,i.I,i.F);for(z=3,t=f.data.length;z<t;z+=4){var g=4*f.data[z];g&&(f.data[z-3]=i.oGradientData[g],f.data[z-2]=i.oGradientData[g+1],f.data[z-1]=i.oGradientData[g+2])}e.putImageData(f,0,0)}r=Math.min(i.aData.length,r+Math.round(i.aData.length/b)),s=Math.min(i.aData.length,s+Math.round(i.aData.length/b)),i.UID===M&&(!h&&i.L2?("svg"===i.AC&&ZC.AN(i.Z.id.replace(/\-c$/,"-img")).setAttribute("href",i.Z.toDataURL()),window.setTimeout(function(){K(s,r,r<=b||r>=i.aData.length)},1)):function(){var t=e.getImageData(0,0,i.I,i.F);e.clearRect(0,0,i.I,i.F);for(var a=3,s=t.data.length;a<s;a+=4)t.data[a]/=1/i.C5;e.putImageData(t,0,0),i.Z.style.opacity="1","svg"===i.AC&&ZC.AN(i.Z.id.replace(/\-c$/,"-img")).setAttribute("href",i.Z.toDataURL())}())};this.aData.length&&K(0,b>=this.aData.length?this.aData.length:Math.round(this.aData.length/b))}}},this.clear=function(){this.aData=[]}},zingchart.bind(null,"postzoom",function(i){if(a)return e(i),void(a=!1);var s=zingchart.getLoader(i.id),r=zingchart.getModules(s);if(-1!==ZC.AV(r,"heatmap"))for(var h=0,n=s.AG.length;h<n;h++){var l=s.AG[h];if(l.o.heatmap){var o=t[s.K+"-"+l.L];o.reset(),o.paint()}}}),zingchart.bind(null,ZC._[48],function(a){var e=zingchart.getLoader(a.id),i=zingchart.getModules(e);if(-1!==ZC.AV(i,"heatmap"))for(var s=0,r=e.AG.length;s<r;s++){var h=e.AG[s];if(h.o.heatmap&&ZC.DQ(a.x,h.iX,h.iX+h.I)&&ZC.DQ(a.y,h.iY,h.iY+h.F)){var n,l=t[e.K+"-"+h.L],o=null;if(l){var c,u="",d="",f=null;if(l.bDiscrete)l.oKDTree&&(c=l.oKDTree.getNearestNeighbour({x:a.x,y:a.y},20))&&(o=c.v,u=c.key,d=c.val,f=c.info);else if(-1===h.AF.indexOf("scatter"))if(l.ABE&&!l.bComposite){if(ZC.DQ(a.x,h.N.iX,h.N.iX+h.N.I)&&ZC.DQ(a.y,h.N.iY,h.N.iY+h.N.F)){for(var g=zingchart.exec(a.id,"getxyinfo",{x:a.x,y:a.y}),C=0;C<g.length;C++)g[C][ZC._[3]]===l.ABE[ZC._[3]]&&("key-scale"===g[C].infotype&&(u=g[C].scalevalue.toFixed(l.ABE.keyscale.decimals)),"value-scale"===g[C].infotype&&(d=g[C].scalevalue.toFixed(l.ABE.valscale.decimals)));o=l.oKVData[u+"/"+d]}}else{var p=l.Z.getContext("2d").getImageData(0,0,l.I,l.F).data[4*(Math.round(a.x-h.iX)+Math.round(a.y-h.iY)*l.I)+3];p=Math.min(255,Math.round(p*(1/l.C5))),o=l.B0+(l.BE-l.B0)*p/255,p<=0&&(o=null)}else l.oKDTree&&(c=l.oKDTree.getNearestNeighbour({x:a.x,y:a.y},20))&&(o=c.v,u=c.key,d=c.val,f=c.info);if(ZC.AP.C1("heatmap.mousemove",e,{id:e.K,graphid:h.K,ev:a.ev,x:a.x,y:a.y,value:o}),l.A7)if(null!==ZC._n_(o)){if(a.ev.target&&"AREA"!==a.ev.target.tagName.toUpperCase()){var Z=u,m=u,v=d,B=d;if(l.ABE){var D=h.BK(l.ABE.keyscale.name);if(D){var y=D.M2();y[ZC._[68]]&&(y.utc=h.V5,y.timezone=h.NO,m=ZC.AP.G7(m,y,h.BO[0],!0))}var _=h.BK(l.ABE.valscale.name);_&&_.CK&&(B=_.CK.replace(/%v|%scale-value/g,B))}if(n={},ZC._cp_(l.A7,n),-1!==h.AF.indexOf("scatter")&&(n.x=c.x,n.y=c.y-("canvas"===e.AC?0:20),n.anchor="c",n[ZC._[7]]="xy"),l.A7["js-rule"])try{var x=ZC.execFn(l.A7["js-rule"].replace("javascript:","").replace("()",""),window,{key:u,val:d,v:o},f);x&&ZC._cp_(x,n)}catch(z){}var A=zingchart.formatNumber(o,l.A7);n.text=n.text||A+"",-1!==n.text.indexOf("%")&&(n.text=n.text.replace(/%value/g,A).replace(/%scale-key-text/g,m).replace(/%scale-key-value/g,Z).replace(/%scale-value-text/g,B).replace(/%scale-value-value/g,v)),zingchart.exec(a.id,"showtooltip",{ev:a.ev,xy:!0,data:n})}}else a.ev.target&&"AREA"!==a.ev.target.tagName.toUpperCase()&&zingchart.exec(a.id,"hidetooltip",{})}}}});var a=!1;zingchart.bind(null,"resize",function(i){var s=zingchart.getLoader(i.id),r=zingchart.getModules(s);if(-1!==ZC.AV(r,"heatmap")){for(var h=[],n=0,l=0,o=s.AG.length;l<o;l++){s.AG[l].o.heatmap&&(h=[].concat(t[s.K+"-"+l].aDataInit),n=l)}a=!0,window.setTimeout(function(){e(i),zingchart.exec(s.K,"heatmap.setdata",{data:h,graphid:n}),a=!1},33)}});var e=function(a){var e=zingchart.getLoader(a.id),i=zingchart.getModules(e);if(-1!==ZC.AV(i,"heatmap"))for(var s=0,r=e.AG.length;s<r;s++){var h=e.AG[s];if(h.o.heatmap){var n=h.o.heatmap||{};h.o.options&&(n=h.o.options.heatmap||n),ZC._todash_(n);var l=document.createElement("canvas");l.id=h.K+"-heatmap-c",l.className="canvas"===e.AC?"":"zc-no-print",l.width=h.I,l.height=h.F;var o,c=ZC._c_(n[ZC._[7]],n.graph?"bottom":"top");if("canvas"===e.AC)"top"===c?ZC.AN(e.K+"-tools").appendChild(l):(o=ZC.AN(h.K+"-plots-bl-0")||ZC.AN(h.K+"-plots-bl")).appendChild(l);else{var u=ZC.Q.F8("foreignObject",ZC._[36]);u.id=h.K+"-heatmap",ZC.Q.G8(u,{x:h.iX,y:h.iY,width:h.I,height:h.F}),u.style.opacity="0",u.appendChild(l);var d=ZC.Q.F8("image",ZC._[36]);d.id=h.K+"-heatmap-img",ZC.Q.G8(d,{x:h.iX,y:h.iY,width:h.I,height:h.F,href:""}),"top"===c?(ZC.AN(e.K+"-tools").appendChild(u),ZC.AN(e.K+"-tools").appendChild(d)):((o=ZC.AN(h.K+"-plots-bl-0")||ZC.AN(h.K+"-plots-bl")).appendChild(u),o.appendChild(d))}var f=t[e.K+"-"+h.L]=new ZC.ZCHeatMap;f.I=h.I,f.F=h.F,f.Z=l,f.AC=e.AC,"canvas"===e.AC&&(f.Z.style.position="absolute",f.Z.style.left=h.iX+"px",f.Z.style.top=h.iY+"px"),f.sBrushType=ZC._c_(n["brush-type"],"circle");var g=[[15,15],45];"square"===f.sBrushType?g=[[15,15],5]:"rectangle"===f.sBrushType&&(g=[[10,15],5]),f.aSize=ZC._c_(n[ZC._[21]],g[0]),"object"!=typeof f.aSize&&(f.aSize=[ZC._f_(f.aSize),ZC._f_(f.aSize)]),f.iBlur=ZC._i_(ZC._c_(n.blur,g[1])),f.L2=ZC._b_(ZC._c_(n.async,!1)),f.bSortData=ZC._b_(ZC._c_(n["sort-data"],!1)),f.C5=ZC._f_(ZC._c_(n.alpha,.5)),n.tooltip&&(f.A7=n.tooltip),ZC._n_(n["min-value"])&&(f.B0=ZC._f_(n["min-value"])),ZC._n_(n["max-value"])&&(f.BE=ZC._f_(n["max-value"])),f.bDiscrete=ZC._b_(ZC._c_(n.discrete,!1)),f.iIntensity=ZC._i_(ZC._c_(n.intensity,1)),f.iIntensity=Math.min(10,Math.max(1,f.iIntensity)),f.Z.style.opacity=""+f.C5,n.graph&&(f.bComposite=!1,f.ABE={loaderid:e.K,graphid:h.K,keyscale:{name:ZC._[50],decimals:0},valscale:{name:ZC._[51],decimals:0}},n.graph["key-scale"]&&(f.ABE.keyscale.name=n.graph["key-scale"]),n.graph["val-scale"]&&(f.ABE.valscale.name=n.graph["val-scale"])),ZC._n_(n.composite)&&(f.bComposite=ZC._b_(n.composite));var C=["#0000ff","#00ffff","#bfff00","#ffff00","#ff0000"],p=[.4,.6,.7,.8,1];n["gradient-colors"]&&(C=n["gradient-colors"].split(/\s|;/)),n["gradient-stops"]&&(p=n["gradient-stops"].split(/\s|;/)),f.aGradient=[];for(var Z=0;Z<Math.min(C.length,p.length);Z++)f.aGradient.push([p[Z],C[Z]]);f.paint()}}};zingchart.bind(null,"load",e),zingchart.extendAPI("heatmap.setdata",function(a,e){"string"==typeof(e=e||{})&&(e=JSON.parse(e)),ZC._todash_(e,!0,["data"]);var i=zingchart.getLoader(a);if(i){var s=i.C9(e[ZC._[3]]);if(s){var r=i.K+"-"+s.L;t&&t[r]&&(null!==ZC._n_(e["min-value"])&&(t[r].B0=ZC._f_(e["min-value"])),null!==ZC._n_(e["max-value"])&&(t[r].BE=ZC._f_(e["max-value"])),null!==ZC._n_(e[ZC._[21]])&&(t[r].aSize=[ZC._f_(e[ZC._[21]]),ZC._f_(e[ZC._[21]])]),null!==ZC._n_(e.blur)&&(t[r].iBlur=ZC._f_(e.blur)),t[r].setData(e.data||[],s.AF),t[r].paint())}}}),ZC.ZCKDTreeNode=function(t){this.aPoint=t,this.oLeftChild=null,this.oRightChild=null,this.oBounds=null},ZC.ZCKDTree=function(t){for(var a={minX:ZC.MAX,maxX:-ZC.MAX,minY:ZC.MAX,maxY:-ZC.MAX},e=0,i=t.length;e<i;e++)a.minX=Math.min(a.minX,t[e].x),a.maxX=Math.max(a.maxX,t[e].x),a.minY=Math.min(a.minY,t[e].y),a.maxY=Math.max(a.maxY,t[e].y);this.MH=ZC.ZCKDTree.createKDTree(t,0,a)},ZC.ZCKDTree.createKDTree=function(t,a,e){if(0===t.length)return null;var i=a%2;t.sort(function(t,a){return 0===i?t.x-a.x:t.y-a.y});var s=Math.floor(t.length/2),r=new ZC.ZCKDTreeNode(t[s]);r.oBounds=e;var h={minX:e.minX,maxX:0===i?r.aPoint.x:e.maxX,minY:e.minY,maxY:1===i?r.aPoint.y:e.maxY},n={minX:0===i?r.aPoint.x:e.minX,maxX:e.maxX,minY:1===i?r.aPoint.y:e.minY,maxY:e.maxY};return r.oLeftChild=ZC.ZCKDTree.createKDTree(t.slice(0,s),a+1,h),r.oRightChild=ZC.ZCKDTree.createKDTree(t.slice(s+1),a+1,n),r},ZC.ZCKDTree.getDistance=function(t,a,e,i){return Math.sqrt((t-e)*(t-e)+(a-i)*(a-i))},ZC.ZCKDTree.prototype.getNearestNeighbour=function(t,a,e){var i=[];return this._getNearestNeighbours_(this.MH,t,0,i,1,e),0===i.length?null:i[0].H8<=a?i[0].AW.aPoint:null},ZC.ZCKDTree.prototype.getNearestNeighbours=function(t,a,e,i){var s=[];this._getNearestNeighbours_(this.MH,t,0,s,a,i);for(var r=[],h=0,n=s.length;h<n;h++)s[h].H8<=e&&r.push(s[h].AW.aPoint);return r},ZC.ZCKDTree.prototype._getNearestNeighbours_=function(t,a,e,i,s,r){r&&r.push(t.aPoint);var h=e%2,n={AW:t,H8:ZC.ZCKDTree.getDistance(a.x,a.y,t.aPoint.x,t.aPoint.y)};ZC.ZCKDTree.insertResult(i,n,s);var l,o,c=(0===h?a.x:a.y)<(0===h?t.aPoint.x:t.aPoint.y),u=c?t.oLeftChild:t.oRightChild,d=c?t.oRightChild:t.oLeftChild;(u&&this._getNearestNeighbours_(u,a,e+1,i,s,r),d)&&(0===h?(l=t.aPoint.x,o=a.y,a.y<d.oBounds.minY?o=d.oBounds.minY:a.y>d.oBounds.maxY&&(o=d.oBounds.maxY)):(l=a.x,o=t.aPoint.y,a.x<d.oBounds.minX?l=d.oBounds.minX:a.x>d.oBounds.maxX&&(l=d.oBounds.maxX)),ZC.ZCKDTree.getDistance(a.x,a.y,l,o)<=i[i.length-1].H8&&this._getNearestNeighbours_(d,a,e+1,i,s,r))},ZC.ZCKDTree.insertResult=function(t,a,e){var i;for(i=t.length-1;i>=0;--i){var s=t[i];if(a.H8>s.H8)break}t.splice(i+1,0,a),t.length>e&&t.pop()}}}();