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) 14.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.11 */ zingchart.setModule("heatmap"),function(){if(zingchart.plugins.heatmap=zingchart.plugins.heatmap||{},!ZC.ZCHeatMap){var a={};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.LM=!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(a){this.aData.push(a)},this.setData=function(a,t){if(this.AAY&&-1===t.indexOf("scatter"))for(var e=0,i=a.length;e<i;e++)this.oKVData[a[e][0]+"/"+a[e][1]]=a[e][2];this.aData=a,this.aDataInit||(this.aDataInit=a),this.bSortData&&this.aData.sort(function(a,t){return a[2]-t[2]})},this.AY=null,this.BD=null,this.C5=.5,this.Z=null,this.AAY=null,this.setMinMax=function(){if(null===this.AY||null===this.BD)for(var a=0,t=this.aData.length;a<t;a++)this.AY=Math.min(this.AY,this.aData[a][2]),this.BD=Math.max(this.BD,this.aData[a][2])},this.createGradient=function(){var a=document.createElement("canvas"),t=a.getContext("2d"),e=t.createLinearGradient(0,0,0,256);a.width=1,a.height=256;for(var i=0;i<this.aGradient.length;i++)e.addColorStop(this.aGradient[i][0],this.aGradient[i][1]);t.fillStyle=e,t.fillRect(0,0,1,256),this.oGradientData=t.getImageData(0,0,1,256).data},this.createBrush=function(){var a=document.createElement("canvas"),t=a.getContext("2d");"circle"===this.sBrushType?a.width=a.height=2*(this.aSize[0]+this.iBlur):"square"===this.sBrushType?a.width=a.height=this.aSize[0]+2*this.iBlur:"rectangle"===this.sBrushType&&(a.width=this.aSize[0]+2*this.iBlur,a.height=this.aSize[1]+2*this.iBlur),t.shadowOffsetX=t.shadowOffsetY=200,t.shadowBlur=this.iBlur,t.shadowColor="#000","circle"===this.sBrushType?(t.beginPath(),t.arc(this.aSize[0]+this.iBlur-200,this.aSize[0]+this.iBlur-200,this.aSize[0],0,2*Math.PI,!0),t.closePath(),t.fill()):"square"===this.sBrushType?t.fillRect(this.iBlur-200,this.iBlur-200,this.aSize[0],this.aSize[0]):"rectangle"===this.sBrushType&&t.fillRect(this.iBlur-200,this.iBlur-200,this.aSize[0],this.aSize[1]),this.oBrush=a},this.reset=function(){this.aData=this.aDataInit},this.paint=function(){var a,t,e;if(this.aData.length){this.UID=this.UID?++this.UID:1;var i=this.Z.getContext("2d"),s=this,r=null,h=null;if(!this.AAY||((r=zingchart.getLoader(this.AAY.loaderid))&&(h=zingchart.getGraph(r,this.AAY.graphid)),h)){if(this.AAY){var n=zingchart.exec(this.AAY.loaderid,"getobjectinfo",{graphid:this.AAY.graphid,object:"plotarea"});n&&(i.beginPath(),i.rect(n.x-h.iX,n.y-h.iY,n.width,n.height),i.clip(),i.closePath());var l=zingchart.exec(this.AAY.loaderid,"getobjectinfo",{object:"scale",name:this.AAY.keyscale.name,graphid:this.AAY.graphid}),o=zingchart.exec(this.AAY.loaderid,"getobjectinfo",{object:"scale",name:this.AAY.valscale.name,graphid:this.AAY.graphid});if(l&&o){var c=(l.step+"").split(".");this.AAY.keyscale.decimals=2===c.length?c[1].length:0;var u=(o.step+"").split(".");this.AAY.valscale.decimals=2===u.length?u[1].length:0;var d=[],g=[],f=0,p=0,C=[];"xy"===h.AJ.layout?-1===h.AF.indexOf("scatter")&&(this.aSize=[Math.ceil(l.stepSize)+.5,Math.ceil(o.stepSize)+.5]):"radar"===h.AJ.layout&&(g=[l.x+l.width/2,l.y+l.height/2]);var A=h.BM(this.AAY.keyscale.name),m=h.BM(this.AAY.valscale.name),v=[],Z={},D=1;if(-1!==h.AF.indexOf("scatter")&&this.aData.length>1e5){var x=Math.max((A.BD-A.AY)/(A.V[A.V.length-1]-A.V[0]),(m.BD-m.AY)/(m.GU-m.FR));D=Math.max(1,Math.round(5*x))}for(a=0,t=this.aData.length;a<t;a++)if((e=this.aData[a])[0]>=A.AY&&e[0]<=A.BD&&e[1]>=m.AY&&e[1]<=m.BD){var y=A.AR(e[0]),B=m.AR(e[1]);"radar"===h.AJ.layout?(y[0]-=h.iX,y[1]-=h.iY,B[0]-=h.iX,B[1]-=h.iY,f=180*Math.atan2(y[1]-g[1],y[0]-g[0])/Math.PI,p=g[1]-B[1]+o.offsetStart,C=ZC.AO.BN(g[0],g[1],p,f),d.push([C[0],C[1],e[2],e[3]])):(y-=h.iX,B-=h.iY,d.push([y,B,e[2],e[3]]),-1!==h.AF.indexOf("scatter")&&(D>1?Z[D*Math.round(y/D)+"/"+D*Math.round(B/D)]=e:Z[Math.round(y)+"/"+Math.round(B)]=e))}if(-1!==h.AF.indexOf("scatter")){for(var Y in Z){var _=Y.split("/");v.push({x:parseFloat(_[0]),y:parseFloat(_[1]),key:Z[Y][0],val:Z[Y][1],v:Z[Y][2],info:Z[Y][3]})}this.oKDTree=new ZC.ZCKDTree(v)}this.aData=d}}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(),i.clearRect(0,0,s.I,s.F);var z=this.LM?25:1,M=this.UID,b=function(r,h,n){for(a=r;a<h;a++)if(e=s.aData[a]){var l=1;if(s.AY!==s.BD&&(l=Math.max((e[2]-s.AY)/(s.BD-s.AY),.01)),s.AAY&&!s.bComposite){var o=4*Math.floor(255*l),c=s.oGradientData[o],u=s.oGradientData[o+1],d=s.oGradientData[o+2];i.fillStyle="rgb("+c+","+u+","+d+")","circle"===s.sBrushType?(i.beginPath(),i.arc(e[0],e[1],s.aSize[0],0,2*Math.PI),i.fill(),i.closePath()):i.fillRect(e[0]-s.aSize[0]/2,e[1]-s.aSize[1]/2,s.aSize[0],s.aSize[1])}else i.globalAlpha=l,"circle"===s.sBrushType?i.drawImage(s.oBrush,e[0]-s.aSize[0]-s.iBlur,e[1]-s.aSize[0]-s.iBlur):"square"===s.sBrushType?i.drawImage(s.oBrush,e[0]-s.aSize[0]/2-s.iBlur,e[1]-s.aSize[0]/2-s.iBlur):"rectangle"===s.sBrushType&&i.drawImage(s.oBrush,e[0]-s.aSize[0]/2-s.iBlur,e[1]-s.aSize[1]/2-s.iBlur)}if(!s.AAY||s.bComposite){var g=i.getImageData(0,0,s.I,s.F);for(a=3,t=g.data.length;a<t;a+=4){var f=4*g.data[a];f&&(g.data[a-3]=s.oGradientData[f],g.data[a-2]=s.oGradientData[f+1],g.data[a-1]=s.oGradientData[f+2])}i.putImageData(g,0,0)}h=Math.min(s.aData.length,h+Math.round(s.aData.length/z)),r=Math.min(s.aData.length,r+Math.round(s.aData.length/z)),s.UID===M&&(!n&&s.LM?("svg"===s.AC&&ZC.AL(s.Z.id.replace(/\-c$/,"-img")).setAttribute("href",s.Z.toDataURL()),window.setTimeout(function(){b(r,h,h>=s.aData.length)},1)):function(){var a=i.getImageData(0,0,s.I,s.F);i.clearRect(0,0,s.I,s.F);for(var t=3,e=a.data.length;t<e;t+=4)a.data[t]/=1/s.C5;i.putImageData(a,0,0),s.Z.style.opacity="1","svg"===s.AC&&ZC.AL(s.Z.id.replace(/\-c$/,"-img")).setAttribute("href",s.Z.toDataURL())}())};this.aData.length&&b(0,Math.round(this.aData.length/z))}}},this.clear=function(){this.aData=[]}},zingchart.bind(null,"postzoom",function(i){if(t)return e(i),void(t=!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=a[s.K+"-"+l.L];o.reset(),o.paint()}}}),zingchart.bind(null,"mousemove",function(t){var e=zingchart.getLoader(t.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.DP(t.x,h.iX,h.iX+h.I)&&ZC.DP(t.y,h.iY,h.iY+h.F)){var n,l=a[e.K+"-"+h.L],o=null;if(l){var c,u="",d="",g=null;if(-1===h.AF.indexOf("scatter"))if(l.AAY&&!l.bComposite){if(ZC.DP(t.x,h.P.iX,h.P.iX+h.P.I)&&ZC.DP(t.y,h.P.iY,h.P.iY+h.P.F)){for(var f=zingchart.exec(t.id,"getxyinfo",{x:t.x,y:t.y}),p=0;p<f.length;p++)f[p].graphid===l.AAY.graphid&&("key-scale"===f[p].infotype&&(u=f[p].scalevalue.toFixed(l.AAY.keyscale.decimals)),"value-scale"===f[p].infotype&&(d=f[p].scalevalue.toFixed(l.AAY.valscale.decimals)));o=l.oKVData[u+"/"+d]}}else{var C=l.Z.getContext("2d").getImageData(0,0,l.I,l.F).data[4*(Math.round(t.x-h.iX)+Math.round(t.y-h.iY)*l.I)+3];C=Math.min(255,Math.round(C*(1/l.C5))),o=l.AY+(l.BD-l.AY)*C/255,C<=0&&(o=null)}else(c=l.oKDTree.getNearestNeighbour({x:t.x,y:t.y},20))&&(o=c.v,u=c.key,d=c.val,g=c.info);if(ZC.AQ.C0("heatmap.mousemove",e,{id:e.K,graphid:h.K,ev:t.ev,x:t.x,y:t.y,value:o}),l.A7)if(null!==ZC._n_(o)){if(t.ev.target&&"AREA"!==t.ev.target.tagName.toUpperCase()){var A=u,m=u,v=d,Z=d;if(l.AAY){var D=h.BM(l.AAY.keyscale.name);if(D){var x=D.MT();x["transform-date"]&&(x.utc=h.UF,x.timezone=h.NC,m=ZC.AQ.G4(m,x,h.BO[0],!0))}var y=h.BM(l.AAY.valscale.name);y&&y.CJ&&(Z=y.CJ.replace(/%v|%scale-value/g,Z))}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.placement="xy"),l.A7["js-rule"])try{var B=ZC.execFn(l.A7["js-rule"].replace("javascript:","").replace("()",""),window,{key:u,val:d,v:o},g);B&&ZC._cp_(B,n)}catch(_){}var Y=zingchart.formatNumber(o,l.A7);n.text=n.text||Y+"",-1!==n.text.indexOf("%")&&(n.text=n.text.replace(/%value/g,o+"").replace(/%scale-key-text/g,m).replace(/%scale-key-value/g,A).replace(/%scale-value-text/g,Z).replace(/%scale-value-value/g,v)),zingchart.exec(t.id,"showtooltip",{ev:t.ev,xy:!0,data:n})}}else t.ev.target&&"AREA"!==t.ev.target.tagName.toUpperCase()&&zingchart.exec(t.id,"hidetooltip",{})}}}});var t=!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(a[s.K+"-"+l].aDataInit),n=l)}t=!0,window.setTimeout(function(){e(i),zingchart.exec(s.K,"heatmap.setdata",{data:h,graphid:n}),t=!1},33)}});var e=function(t){var e=zingchart.getLoader(t.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.placement,n.graph?"bottom":"top");if("canvas"===e.AC)"top"===c?ZC.AL(e.K+"-tools").appendChild(l):(o=ZC.AL(h.K+"-plots-bl-0")||ZC.AL(h.K+"-plots-bl")).appendChild(l);else{var u=ZC.Q.F7("foreignObject","http://www.w3.org/2000/svg");u.id=h.K+"-heatmap",ZC.Q.G5(u,{x:h.iX,y:h.iY,width:h.I,height:h.F}),u.style.opacity="0",u.appendChild(l);var d=ZC.Q.F7("image","http://www.w3.org/2000/svg");d.id=h.K+"-heatmap-img",ZC.Q.G5(d,{x:h.iX,y:h.iY,width:h.I,height:h.F,href:""}),"top"===c?(ZC.AL(e.K+"-tools").appendChild(u),ZC.AL(e.K+"-tools").appendChild(d)):((o=ZC.AL(h.K+"-plots-bl-0")||ZC.AL(h.K+"-plots-bl")).appendChild(u),o.appendChild(d))}var g=a[e.K+"-"+h.L]=new ZC.ZCHeatMap;g.I=h.I,g.F=h.F,g.Z=l,g.AC=e.AC,"canvas"===e.AC&&(g.Z.style.position="absolute",g.Z.style.left=h.iX+"px",g.Z.style.top=h.iY+"px"),g.sBrushType=ZC._c_(n["brush-type"],"circle");var f=[[15,15],45];"square"===g.sBrushType?f=[[15,15],5]:"rectangle"===g.sBrushType&&(f=[[10,15],5]),g.aSize=ZC._c_(n.size,f[0]),"object"!=typeof g.aSize&&(g.aSize=[ZC._f_(g.aSize),ZC._f_(g.aSize)]),g.iBlur=ZC._i_(ZC._c_(n.blur,f[1])),g.LM=ZC._b_(ZC._c_(n.async,!1)),g.bSortData=ZC._b_(ZC._c_(n["sort-data"],!1)),g.C5=ZC._f_(ZC._c_(n.alpha,.5)),n.tooltip&&(g.A7=n.tooltip),ZC._n_(n["min-value"])&&(g.AY=ZC._f_(n["min-value"])),ZC._n_(n["max-value"])&&(g.BD=ZC._f_(n["max-value"])),g.Z.style.opacity=""+g.C5,n.graph&&(g.bComposite=!1,g.AAY={loaderid:e.K,graphid:h.K,keyscale:{name:"scale-x",decimals:0},valscale:{name:"scale-y",decimals:0}},n.graph["key-scale"]&&(g.AAY.keyscale.name=n.graph["key-scale"]),n.graph["val-scale"]&&(g.AAY.valscale.name=n.graph["val-scale"])),ZC._n_(n.composite)&&(g.bComposite=ZC._b_(n.composite));var p=["#0000ff","#00ffff","#bfff00","#ffff00","#ff0000"],C=[.4,.6,.7,.8,1];n["gradient-colors"]&&(p=n["gradient-colors"].split(/\s|;/)),n["gradient-stops"]&&(C=n["gradient-stops"].split(/\s|;/)),g.aGradient=[];for(var A=0;A<Math.min(p.length,C.length);A++)g.aGradient.push([C[A],p[A]]);g.paint()}}};zingchart.bind(null,"load",e),zingchart.extendAPI("heatmap.setdata",function(t,e){"string"==typeof(e=e||{})&&(e=JSON.parse(e)),ZC._todash_(e,!0,["data"]);var i=zingchart.getLoader(t);if(i){var s=i.CA(e.graphid);if(s){var r=i.K+"-"+s.L;a&&a[r]&&(null!==ZC._n_(e["min-value"])&&(a[r].AY=ZC._f_(e["min-value"])),null!==ZC._n_(e["max-value"])&&(a[r].BD=ZC._f_(e["max-value"])),null!==ZC._n_(e.size)&&(a[r].aSize=[ZC._f_(e.size),ZC._f_(e.size)]),null!==ZC._n_(e.blur)&&(a[r].iBlur=ZC._f_(e.blur)),a[r].setData(e.data||[],s.AF),a[r].paint())}}}),ZC.ZCKDTreeNode=function(a){this.aPoint=a,this.oLeftChild=null,this.oRightChild=null,this.oBounds=null},ZC.ZCKDTree=function(a){for(var t={minX:ZC.MAX,maxX:-ZC.MAX,minY:ZC.MAX,maxY:-ZC.MAX},e=0,i=a.length;e<i;e++)t.minX=Math.min(t.minX,a[e].x),t.maxX=Math.max(t.maxX,a[e].x),t.minY=Math.min(t.minY,a[e].y),t.maxY=Math.max(t.maxY,a[e].y);this.MB=ZC.ZCKDTree.createKDTree(a,0,t)},ZC.ZCKDTree.createKDTree=function(a,t,e){if(0===a.length)return null;var i=t%2;a.sort(function(a,t){return 0===i?a.x-t.x:a.y-t.y});var s=Math.floor(a.length/2),r=new ZC.ZCKDTreeNode(a[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(a.slice(0,s),t+1,h),r.oRightChild=ZC.ZCKDTree.createKDTree(a.slice(s+1),t+1,n),r},ZC.ZCKDTree.getDistance=function(a,t,e,i){return Math.sqrt((a-e)*(a-e)+(t-i)*(t-i))},ZC.ZCKDTree.prototype.getNearestNeighbour=function(a,t,e){var i=[];return this._getNearestNeighbours_(this.MB,a,0,i,1,e),0===i.length?null:i[0].H1<=t?i[0].AX.aPoint:null},ZC.ZCKDTree.prototype.getNearestNeighbours=function(a,t,e,i){var s=[];this._getNearestNeighbours_(this.MB,a,0,s,t,i);for(var r=[],h=0,n=s.length;h<n;h++)s[h].H1<=e&&r.push(s[h].AX.aPoint);return r},ZC.ZCKDTree.prototype._getNearestNeighbours_=function(a,t,e,i,s,r){r&&r.push(a.aPoint);var h=e%2,n={AX:a,H1:ZC.ZCKDTree.getDistance(t.x,t.y,a.aPoint.x,a.aPoint.y)};ZC.ZCKDTree.insertResult(i,n,s);var l,o,c=(0===h?t.x:t.y)<(0===h?a.aPoint.x:a.aPoint.y),u=c?a.oLeftChild:a.oRightChild,d=c?a.oRightChild:a.oLeftChild;(u&&this._getNearestNeighbours_(u,t,e+1,i,s,r),d)&&(0===h?(l=a.aPoint.x,o=t.y,t.y<d.oBounds.minY?o=d.oBounds.minY:t.y>d.oBounds.maxY&&(o=d.oBounds.maxY)):(l=t.x,o=a.aPoint.y,t.x<d.oBounds.minX?l=d.oBounds.minX:t.x>d.oBounds.maxX&&(l=d.oBounds.maxX)),ZC.ZCKDTree.getDistance(t.x,t.y,l,o)<=i[i.length-1].H1&&this._getNearestNeighbours_(d,t,e+1,i,s,r))},ZC.ZCKDTree.insertResult=function(a,t,e){var i;for(i=a.length-1;i>=0;--i){var s=a[i];if(t.H1>s.H1)break}a.splice(i+1,0,t),a.length>e&&a.pop()}}}();