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
JavaScript
/*
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
*/
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.KU=!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.AAX&&-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.BF=null,this.C6=.5,this.Z=null,this.AAX=null,this.setMinMax=function(){if(null===this.B0||null===this.BF)for(var t=0,a=this.aData.length;t<a;t++)this.B0=Math.min(this.B0,this.aData[t][2]),this.BF=Math.max(this.BF,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.AAX||((s=zingchart.getLoader(this.AAX.loaderid))&&(r=zingchart.getGraph(s,this.AAX.graphid)),r)){var h=[],n={};if(this.AAX){var l=zingchart.exec(this.AAX.loaderid,"getobjectinfo",{graphid:this.AAX.graphid,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.AAX.loaderid,"getobjectinfo",{object:"scale",name:this.AAX.keyscale.name,graphid:this.AAX.graphid}),c=zingchart.exec(this.AAX.loaderid,"getobjectinfo",{object:"scale",name:this.AAX.valscale.name,graphid:this.AAX.graphid});if(o&&c){var u=(o.step+"").split(".");this.AAX.keyscale.decimals=2===u.length?u[1].length:0;var d=(c.step+"").split(".");this.AAX.valscale.decimals=2===d.length?d[1].length:0;var g=[],f=[],p=0,C=0,m=[];"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&&(f=[o.x+o.width/2,o.y+o.height/2]);var v=r.BM(this.AAX.keyscale.name),A=r.BM(this.AAX.valscale.name),Z=1;if(-1!==r.AF.indexOf("scatter")&&this.aData.length>1e5){var D=Math.max((v.BF-v.B0)/(v.V[v.V.length-1]-v.V[0]),(A.BF-A.B0)/(A.GW-A.FP));Z=Math.max(1,Math.round(5*D))}for(z=0,t=this.aData.length;z<t;z++)if((a=this.aData[z])[0]>=v.B0&&a[0]<=v.BF&&a[1]>=A.B0&&a[1]<=A.BF){var y=v.AR(a[0]),x=A.AR(a[1]);"radar"===r.AJ.layout?(y[0]-=r.iX,y[1]-=r.iY,x[0]-=r.iX,x[1]-=r.iY,p=180*Math.atan2(y[1]-f[1],y[0]-f[0])/Math.PI,C=f[1]-x[1]+c.offsetStart,m=ZC.AQ.BN(f[0],f[1],C,p),g.push([m[0],m[1],a[2],a[3]])):(y-=r.iX,x-=r.iY,g.push([y,x,a[2],a[3]]),-1!==r.AF.indexOf("scatter")&&(Z>1?n[Z*Math.round(y/Z)+"/"+Z*Math.round(x/Z)]=a:n[Math.round(y)+"/"+Math.round(x)]=a))}if(-1!==r.AF.indexOf("scatter")){for(var B in n){var _=B.split("/");h.push({x:parseFloat(_[0]),y:parseFloat(_[1]),key:n[B][0],val:n[B][1],v:n[B][2],info:n[B][3]})}this.oKDTree=new ZC.ZCKDTree(h)}this.aData=g}}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 B in n){_=B.split("/");h.push({x:parseFloat(_[0]),y:parseFloat(_[1]),v:n[B][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 M=this.KU?25:1,X=this.UID,b=function(s,r,h){for(z=s;z<r;z++)if(a=i.aData[z]){var n=1;if(i.B0!==i.BF&&(n=Math.max((a[2]-i.B0)/(i.BF-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.AAX&&!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.AAX||i.bComposite)){var g=e.getImageData(0,0,i.I,i.F);for(z=3,t=g.data.length;z<t;z+=4){var f=4*g.data[z];f&&(g.data[z-3]=i.oGradientData[f],g.data[z-2]=i.oGradientData[f+1],g.data[z-1]=i.oGradientData[f+2])}e.putImageData(g,0,0)}r=Math.min(i.aData.length,r+Math.round(i.aData.length/M)),s=Math.min(i.aData.length,s+Math.round(i.aData.length/M)),i.UID===X&&(!h&&i.KU?("svg"===i.AC&&ZC.AM(i.Z.id.replace(/\-c$/,"-img")).setAttribute("href",i.Z.toDataURL()),window.setTimeout(function(){b(s,r,r<=M||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.C6;e.putImageData(t,0,0),i.Z.style.opacity="1","svg"===i.AC&&ZC.AM(i.Z.id.replace(/\-c$/,"-img")).setAttribute("href",i.Z.toDataURL())}())};this.aData.length&&b(0,M>=this.aData.length?this.aData.length:Math.round(this.aData.length/M))}}},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,"mousemove",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.DS(a.x,h.iX,h.iX+h.I)&&ZC.DS(a.y,h.iY,h.iY+h.F)){var n,l=t[e.K+"-"+h.L],o=null;if(l){var c,u="",d="",g=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,g=c.info);else if(-1===h.AF.indexOf("scatter"))if(l.AAX&&!l.bComposite){if(ZC.DS(a.x,h.N.iX,h.N.iX+h.N.I)&&ZC.DS(a.y,h.N.iY,h.N.iY+h.N.F)){for(var f=zingchart.exec(a.id,"getxyinfo",{x:a.x,y:a.y}),p=0;p<f.length;p++)f[p].graphid===l.AAX.graphid&&("key-scale"===f[p].infotype&&(u=f[p].scalevalue.toFixed(l.AAX.keyscale.decimals)),"value-scale"===f[p].infotype&&(d=f[p].scalevalue.toFixed(l.AAX.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(a.x-h.iX)+Math.round(a.y-h.iY)*l.I)+3];C=Math.min(255,Math.round(C*(1/l.C6))),o=l.B0+(l.BF-l.B0)*C/255,C<=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,g=c.info);if(ZC.AO.C0("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 m=u,v=u,A=d,Z=d;if(l.AAX){var D=h.BM(l.AAX.keyscale.name);if(D){var y=D.MU();y["transform-date"]&&(y.utc=h.UL,y.timezone=h.NF,v=ZC.AO.G5(v,y,h.BO[0],!0))}var x=h.BM(l.AAX.valscale.name);x&&x.CL&&(Z=x.CL.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(z){}var _=zingchart.formatNumber(o,l.A7);n.text=n.text||_+"",-1!==n.text.indexOf("%")&&(n.text=n.text.replace(/%value/g,_).replace(/%scale-key-text/g,v).replace(/%scale-key-value/g,m).replace(/%scale-value-text/g,Z).replace(/%scale-value-value/g,A)),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.placement,n.graph?"bottom":"top");if("canvas"===e.AC)"top"===c?ZC.AM(e.K+"-tools").appendChild(l):(o=ZC.AM(h.K+"-plots-bl-0")||ZC.AM(h.K+"-plots-bl")).appendChild(l);else{var u=ZC.Q.F8("foreignObject","http://www.w3.org/2000/svg");u.id=h.K+"-heatmap",ZC.Q.G6(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","http://www.w3.org/2000/svg");d.id=h.K+"-heatmap-img",ZC.Q.G6(d,{x:h.iX,y:h.iY,width:h.I,height:h.F,href:""}),"top"===c?(ZC.AM(e.K+"-tools").appendChild(u),ZC.AM(e.K+"-tools").appendChild(d)):((o=ZC.AM(h.K+"-plots-bl-0")||ZC.AM(h.K+"-plots-bl")).appendChild(u),o.appendChild(d))}var g=t[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.KU=ZC._b_(ZC._c_(n.async,!1)),g.bSortData=ZC._b_(ZC._c_(n["sort-data"],!1)),g.C6=ZC._f_(ZC._c_(n.alpha,.5)),n.tooltip&&(g.A7=n.tooltip),ZC._n_(n["min-value"])&&(g.B0=ZC._f_(n["min-value"])),ZC._n_(n["max-value"])&&(g.BF=ZC._f_(n["max-value"])),g.bDiscrete=ZC._b_(ZC._c_(n.discrete,!1)),g.iIntensity=ZC._i_(ZC._c_(n.intensity,1)),g.iIntensity=Math.min(10,Math.max(1,g.iIntensity)),g.Z.style.opacity=""+g.C6,n.graph&&(g.bComposite=!1,g.AAX={loaderid:e.K,graphid:h.K,keyscale:{name:"scale-x",decimals:0},valscale:{name:"scale-y",decimals:0}},n.graph["key-scale"]&&(g.AAX.keyscale.name=n.graph["key-scale"]),n.graph["val-scale"]&&(g.AAX.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 m=0;m<Math.min(p.length,C.length);m++)g.aGradient.push([C[m],p[m]]);g.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.CB(e.graphid);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].BF=ZC._f_(e["max-value"])),null!==ZC._n_(e.size)&&(t[r].aSize=[ZC._f_(e.size),ZC._f_(e.size)]),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.M6=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.M6,t,0,i,1,e),0===i.length?null:i[0].H5<=a?i[0].AW.aPoint:null},ZC.ZCKDTree.prototype.getNearestNeighbours=function(t,a,e,i){var s=[];this._getNearestNeighbours_(this.M6,t,0,s,a,i);for(var r=[],h=0,n=s.length;h<n;h++)s[h].H5<=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,H5: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].H5&&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.H5>s.H5)break}t.splice(i+1,0,a),t.length>e&&t.pop()}}}();