heatmap.min.js 9.6 KB

12345678910
  1. /*
  2. * heatmap.js v2.0.5 | JavaScript Heatmap Library
  3. *
  4. * Copyright 2008-2016 Patrick Wied <heatmapjs@patrick-wied.at> - All rights reserved.
  5. * Dual licensed under MIT and Beerware license
  6. *
  7. * :: 2018-04-19 12:47
  8. */
  9. (function(t,a,e){if(typeof module!=="undefined"&&module.exports){module.exports=e()}else if(typeof define==="function"&&define.amd){define(e)}else{a[t]=e()}})("h337",this,function(){var n={defaultRadius:40,defaultRenderer:"canvas2d",defaultGradient:{.25:"rgb(0,0,255)",.55:"rgb(0,255,0)",.85:"yellow",1:"rgb(255,0,0)"},defaultMaxOpacity:1,defaultMinOpacity:0,defaultBlur:.85,defaultXField:"x",defaultYField:"y",defaultValueField:"value",plugins:{}};var s=function t(){var a=function t(a){this._coordinator={};this._data=[];this._radi=[];this._min=10;this._max=1;this._xField=a["xField"]||a.defaultXField;this._yField=a["yField"]||a.defaultYField;this._valueField=a["valueField"]||a.defaultValueField;if(a["radius"]){this._cfgRadius=a["radius"]}};var u=n.defaultRadius;a.prototype={_organiseData:function(t,a){var e=t[this._xField];var i=t[this._yField];var r=this._radi;var n=this._data;var s=this._max;var h=this._min;var o=t[this._valueField]||1;var d=t.radius||this._cfgRadius||u;if(!n[e]){n[e]=[];r[e]=[]}if(!n[e][i]){n[e][i]=o;r[e][i]=d}else{n[e][i]+=o}var l=n[e][i];if(l>s){if(!a){this._max=l}else{this.setDataMax(l)}return false}else if(l<h){if(!a){this._min=l}else{this.setDataMin(l)}return false}else{return{x:e,y:i,value:o,radius:d,min:h,max:s}}},_unOrganizeData:function(){var t=[];var a=this._data;var e=this._radi;for(var i in a){for(var r in a[i]){t.push({x:i,y:r,radius:e[i][r],value:a[i][r]})}}return{min:this._min,max:this._max,data:t}},_onExtremaChange:function(){this._coordinator.emit("extremachange",{min:this._min,max:this._max})},addData:function(){if(arguments[0].length>0){var t=arguments[0];var a=t.length;while(a--){this.addData.call(this,t[a])}}else{var e=this._organiseData(arguments[0],true);if(e){if(this._data.length===0){this._min=this._max=e.value}this._coordinator.emit("renderpartial",{min:this._min,max:this._max,data:[e]})}}return this},setData:function(t){var a=t.data;var e=a.length;this._data=[];this._radi=[];for(var i=0;i<e;i++){this._organiseData(a[i],false)}this._max=t.max;this._min=t.min||0;this._onExtremaChange();this._coordinator.emit("renderall",this._getInternalData());return this},removeData:function(){},setDataMax:function(t){this._max=t;this._onExtremaChange();this._coordinator.emit("renderall",this._getInternalData());return this},setDataMin:function(t){this._min=t;this._onExtremaChange();this._coordinator.emit("renderall",this._getInternalData());return this},setCoordinator:function(t){this._coordinator=t},_getInternalData:function(){return{max:this._max,min:this._min,data:this._data,radi:this._radi}},getData:function(){return this._unOrganizeData()}};return a}();var e=function t(){var h=function(t){var a=t.gradient||t.defaultGradient;var e=document.createElement("canvas");var i=e.getContext("2d");e.width=256;e.height=1;var r=i.createLinearGradient(0,0,256,1);for(var n in a){r.addColorStop(n,a[n])}i.fillStyle=r;i.fillRect(0,0,256,1);return i.getImageData(0,0,256,1).data};var v=function(t,a){var e=document.createElement("canvas");var i=e.getContext("2d");var r=t;var n=t;e.width=e.height=t*2;if(a==1){i.beginPath();i.arc(r,n,t,0,2*Math.PI,false);i.fillStyle="rgba(0,0,0,1)";i.fill()}else{var s=i.createRadialGradient(r,n,t*a,r,n,t);s.addColorStop(0,"rgba(0,0,0,1)");s.addColorStop(1,"rgba(0,0,0,0)");i.fillStyle=s;i.fillRect(0,0,2*t,2*t)}return e};var a=function(t){var a=[];var e=t.min;var i=t.max;var r=t.radi;var t=t.data;var n=Object.keys(t);var s=n.length;while(s--){var h=n[s];var o=Object.keys(t[h]);var d=o.length;while(d--){var l=o[d];var u=t[h][l];var c=r[h][l];a.push({x:h,y:l,value:u,radius:c})}}return{min:e,max:i,data:a}};function e(t){var a=t.container;var e=this.shadowCanvas=document.createElement("canvas");var i=this.canvas=t.canvas||document.createElement("canvas");var r=this._renderBoundaries=[1e4,1e4,0,0];var n=this._clipPoints=t.clipPoints||[];var s=getComputedStyle(t.container)||{};i.className="heatmap-canvas";this._width=i.width=e.width=t.width||+s.width.replace(/px/,"");this._height=i.height=e.height=t.height||+s.height.replace(/px/,"");this.shadowCtx=e.getContext("2d");this.ctx=i.getContext("2d");i.style.cssText=e.style.cssText="position:absolute;left:0;top:0;";a.style.position="relative";a.appendChild(i);this._palette=h(t);this._templates={};this._setStyles(t)}e.prototype={clip:function(){var t=this.ctx,a=this.shadowCtx,e=this._clipPoints,i=this._clipType||"rect";if(e.length<6){return}if(i==="rect"){t.beginPath();a.beginPath();for(var r=0,n=e.length;r<n;r+=2){if(r===0){t.moveTo(e[r],e[r+1]);a.moveTo(e[r],e[r+1])}else{t.lineTo(e[r],e[r+1]);a.lineTo(e[r],e[r+1])}}t.closePath();a.closePath()}else if(i==="arc"){}t.clip();a.clip()},renderPartial:function(t){if(t.data.length>0){this.clip();this._drawAlpha(t);this._colorize()}},renderAll:function(t){this._clear();if(t.data.length>0){this.clip();this._drawAlpha(a(t));this._colorize()}},_updateGradient:function(t){this._palette=h(t)},updateConfig:function(t){if(t["gradient"]){this._updateGradient(t)}this._setStyles(t)},setDimensions:function(t,a){this._width=t;this._height=a;this.canvas.width=this.shadowCanvas.width=t;this.canvas.height=this.shadowCanvas.height=a},_clear:function(){this.shadowCtx.clearRect(0,0,this._width,this._height);this.ctx.clearRect(0,0,this._width,this._height)},_setStyles:function(t){this._blur=t.blur==0?0:t.blur||t.defaultBlur;if(t.backgroundColor){this.canvas.style.backgroundColor=t.backgroundColor}this._width=this.canvas.width=this.shadowCanvas.width=t.width||this._width;this._height=this.canvas.height=this.shadowCanvas.height=t.height||this._height;this._opacity=(t.opacity||0)*255;this._maxOpacity=(t.maxOpacity||t.defaultMaxOpacity)*255;this._minOpacity=(t.minOpacity||t.defaultMinOpacity)*255;this._useGradientOpacity=!!t.useGradientOpacity},_drawAlpha:function(t){var a=this._min=t.min;var e=this._max=t.max;var t=t.data||[];var i=t.length;var r=1-this._blur;while(i--){var n=t[i];var s=n.x;var h=n.y;var o=n.radius;var d=Math.min(n.value,e);var l=s-o;var u=h-o;var c=this.shadowCtx;var _;if(!this._templates[o]){this._templates[o]=_=v(o,r)}else{_=this._templates[o]}var f=(d-a)/(e-a);c.globalAlpha=f<.01?.01:f;c.drawImage(_,l,u);if(l<this._renderBoundaries[0]){this._renderBoundaries[0]=l}if(u<this._renderBoundaries[1]){this._renderBoundaries[1]=u}if(l+2*o>this._renderBoundaries[2]){this._renderBoundaries[2]=l+2*o}if(u+2*o>this._renderBoundaries[3]){this._renderBoundaries[3]=u+2*o}}},_colorize:function(){var t=this._renderBoundaries[0];var a=this._renderBoundaries[1];var e=this._renderBoundaries[2]-t;var i=this._renderBoundaries[3]-a;var r=this._width;var n=this._height;var s=this._opacity;var h=this._maxOpacity;var o=this._minOpacity;var d=this._useGradientOpacity;if(t<0){t=0}if(a<0){a=0}if(t+e>r){e=r-t}if(a+i>n){i=n-a}var l=this.shadowCtx.getImageData(t,a,e,i);var u=l.data;var c=u.length;var _=this._palette;for(var f=3;f<c;f+=4){var v=u[f];var g=v*4;if(!g){continue}var m;if(s>0){m=s}else{if(v<h){if(v<o){m=o}else{m=v}}else{m=h}}u[f-3]=_[g];u[f-2]=_[g+1];u[f-1]=_[g+2];u[f]=d?_[g+3]:m}l.data=u;this.ctx.putImageData(l,t,a);this._renderBoundaries=[1e3,1e3,0,0]},getValueAt:function(t){var a;var e=this.shadowCtx;var i=e.getImageData(t.x,t.y,1,1);var r=i.data[3];var n=this._max;var s=this._min;a=Math.abs(n-s)*(r/255)>>0;return a},getDataURL:function(){return this.canvas.toDataURL()}};return e}();var h=function t(){var a=false;if(n["defaultRenderer"]==="canvas2d"){a=e}return a}();var o={merge:function(){var t={};var a=arguments.length;for(var e=0;e<a;e++){var i=arguments[e];for(var r in i){t[r]=i[r]}}return t}};var a=function t(){var i=function t(){function a(){this.cStore={}}a.prototype={on:function(t,a,e){var i=this.cStore;if(!i[t]){i[t]=[]}i[t].push(function(t){return a.call(e,t)})},emit:function(t,a){var e=this.cStore;if(e[t]){var i=e[t].length;for(var r=0;r<i;r++){var n=e[t][r];n(a)}}}};return a}();var r=function(a){var t=a._renderer;var e=a._coordinator;var i=a._store;e.on("renderpartial",t.renderPartial,t);e.on("renderall",t.renderAll,t);e.on("extremachange",function(t){a._config.onExtremaChange&&a._config.onExtremaChange({min:t.min,max:t.max,gradient:a._config["gradient"]||a._config["defaultGradient"]})});i.setCoordinator(e)};function a(){var t=this._config=o.merge(n,arguments[0]||{});this._coordinator=new i;if(t["plugin"]){var a=t["plugin"];if(!n.plugins[a]){throw new Error("Plugin '"+a+"' not found. Maybe it was not registered.")}else{var e=n.plugins[a];this._renderer=new e.renderer(t);this._store=new e.store(t)}}else{this._renderer=new h(t);this._store=new s(t)}r(this)}a.prototype={addData:function(){this._store.addData.apply(this._store,arguments);return this},removeData:function(){this._store.removeData&&this._store.removeData.apply(this._store,arguments);return this},setData:function(){this._store.setData.apply(this._store,arguments);return this},setDataMax:function(){this._store.setDataMax.apply(this._store,arguments);return this},setDataMin:function(){this._store.setDataMin.apply(this._store,arguments);return this},configure:function(t){this._config=o.merge(this._config,t);this._renderer.updateConfig(this._config);this._coordinator.emit("renderall",this._store._getInternalData());return this},repaint:function(){this._coordinator.emit("renderall",this._store._getInternalData());return this},getData:function(){return this._store.getData()},getDataURL:function(){return this._renderer.getDataURL()},getValueAt:function(t){if(this._store.getValueAt){return this._store.getValueAt(t)}else if(this._renderer.getValueAt){return this._renderer.getValueAt(t)}else{return null}}};return a}();var t={create:function(t){return new a(t)},register:function(t,a){n.plugins[t]=a}};return t});