123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- /*
- * heatmap.js v2.0.0 | JavaScript Heatmap Library
- *
- * Copyright 2008-2014 Patrick Wied <heatmapjs@patrick-wied.at> - All rights reserved.
- * Dual licensed under MIT and Beerware license
- *
- * :: 2014-08-05 01:42
- */
- (function (a) {
- var b = {
- defaultRadius: 40,
- defaultRenderer: "canvas2d",
- defaultGradient: {
- .15: "rgb(133,167,109)",
- .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 c = function i() {
- var a = function d(a) {
- this._coordinator = {};
- this._data = [];
- this._radi = [];
- this._min = 0;
- this._max = 1;
- this._xField = a["xField"] || a.defaultXField;
- this._yField = a["yField"] || a.defaultYField;
- this._valueField = a["valueField"] || a.defaultValueField;
- if (a["sp-radius"]) {
- this._cfgRadius = a["sp-radius"]
- }
- };
- var c = b.defaultRadius;
- a.prototype = {
- _organiseData: function (a, b) {
- var d = a[this._xField];
- var e = a[this._yField];
- var f = this._radi;
- var g = this._data;
- var h = this._max;
- var i = this._min;
- var j = a[this._valueField] || 1;
- var k = a.radius || this._cfgRadius || c;
- if (!g[d]) {
- g[d] = [];
- f[d] = []
- }
- if (!g[d][e]) {
- g[d][e] = j;
- f[d][e] = k
- } else {
- g[d][e] += j
- }
- if (g[d][e] > h) {
- if (!b) {
- this._max = g[d][e]
- } else {
- this.setDataMax(g[d][e])
- }
- return false
- } else {
- return {
- x: d,
- y: e,
- value: j,
- radius: k,
- min: i,
- max: h
- }
- }
- },
- _unOrganizeData: function () {
- var a = [];
- var b = this._data;
- var c = this._radi;
- for (var d in b) {
- for (var e in b[d]) {
- a.push({
- x: d,
- y: e,
- radius: c[d][e],
- value: b[d][e]
- })
- }
- }
- return {
- min: this._min,
- max: this._max,
- data: a
- }
- },
- _onExtremaChange: function () {
- this._coordinator.emit("extremachange", {
- min: this._min,
- max: this._max
- })
- },
- addData: function () {
- if (arguments[0].length > 0) {
- var a = arguments[0];
- var b = a.length;
- while (b--) {
- this.addData.call(this, a[b])
- }
- } else {
- var c = this._organiseData(arguments[0], true);
- if (c) {
- this._coordinator.emit("renderpartial", {
- min: this._min,
- max: this._max,
- data: [c]
- })
- }
- }
- return this
- },
- setData: function (a) {
- var b = a.data;
- var c = b.length;
- this._max = a.max;
- this._min = a.min || 0;
- this._data = [];
- this._radi = [];
- for (var d = 0; d < c; d++) {
- this._organiseData(b[d], false)
- }
- this._onExtremaChange();
- this._coordinator.emit("renderall", this._getInternalData());
- return this
- },
- removeData: function () { },
- setDataMax: function (a) {
- this._max = a;
- this._onExtremaChange();
- this._coordinator.emit("renderall", this._getInternalData());
- return this
- },
- setDataMin: function (a) {
- this._min = a;
- this._onExtremaChange();
- this._coordinator.emit("renderall", this._getInternalData());
- return this
- },
- setCoordinator: function (a) {
- this._coordinator = a
- },
- _getInternalData: function () {
- return {
- max: this._max,
- min: this._min,
- data: this._data,
- radi: this._radi
- }
- },
- getData: function () {
- return this._unOrganizeData()
- }
- };
- return a
- }();
- var d = function j() {
- var a = function (a) {
- var b = a.gradient || a.defaultGradient;
- var c = document.createElement("canvas");
- var d = c.getContext("2d");
- c.width = 256;
- c.height = 1;
- var e = d.createLinearGradient(0, 0, 256, 1);
- for (var f in b) {
- e.addColorStop(f, b[f])
- }
- d.fillStyle = e;
- d.fillRect(0, 0, 256, 1);
- return d.getImageData(0, 0, 256, 1).data
- };
- var b = function (a, b) {
- var c = document.createElement("canvas");
- var d = c.getContext("2d");
- var e = a;
- var f = a;
- c.width = c.height = a * 2;
- if (b == 1) {
- d.beginPath();
- d.arc(e, f, a, 0, 2 * Math.PI, false);
- d.fillStyle = "rgba(0,0,0,1)";
- d.fill()
- } else {
- var g = d.createRadialGradient(e, f, a * b, e, f, a);
- g.addColorStop(0, "rgba(0,0,0,1)");
- g.addColorStop(1, "rgba(0,0,0,0)");
- d.fillStyle = g;
- d.fillRect(0, 0, 2 * a, 2 * a)
- }
- return c
- };
- var c = function (a) {
- var b = [];
- var c = a.min;
- var d = a.max;
- var e = a.radi;
- var a = a.data;
- var f = Object.keys(a);
- var g = f.length;
- while (g--) {
- var h = f[g];
- var i = Object.keys(a[h]);
- var j = i.length;
- while (j--) {
- var k = i[j];
- var l = a[h][k];
- var m = e[h][k];
- b.push({
- x: h,
- y: k,
- value: l,
- radius: m
- })
- }
- }
- return {
- min: c,
- max: d,
- data: b
- }
- };
- function d(b) {
- var c = b.container;
- var d = this.shadowCanvas = document.createElement("canvas");
- var e = this.canvas = b.canvas || document.createElement("canvas");
- var f = this._renderBoundaries = [1e4, 1e4, 0, 0];
- var g = getComputedStyle(b.container) || {};
- e.className = "heatmap-canvas";
- this._width = e.width = d.width = +g.width.replace(/px/, "");
- this._height = e.height = d.height = +g.height.replace(/px/, "");
- this.shadowCtx = d.getContext("2d");
- this.ctx = e.getContext("2d");
- e.style.cssText = d.style.cssText = "position:absolute;left:0;top:0;";
- c.style.position = "relative";
- c.appendChild(e);
- this._palette = a(b);
- this._templates = {};
- this._setStyles(b)
- }
- d.prototype = {
- renderPartial: function (a) {
- this._drawAlpha(a);
- this._colorize()
- },
- renderAll: function (a) {
- this._clear();
- this._drawAlpha(c(a));
- this._colorize()
- },
- _updateGradient: function (b) {
- this._palette = a(b)
- },
- updateConfig: function (a) {
- if (a["gradient"]) {
- this._updateGradient(a)
- }
- this._setStyles(a)
- },
- setDimensions: function (a, b) {
- this._width = a;
- this._height = b;
- this.canvas.width = this.shadowCanvas.width = a;
- this.canvas.height = this.shadowCanvas.height = b
- },
- _clear: function () {
- this.shadowCtx.clearRect(0, 0, this._width, this._height);
- this.ctx.clearRect(0, 0, this._width, this._height)
- },
- _setStyles: function (a) {
- this._blur = a.blur == 0 ? 0 : a.blur || a.defaultBlur;
- if (a.backgroundColor) {
- this.canvas.style.backgroundColor = a.backgroundColor
- }
- this._opacity = (a.opacity || 0) * 255;
- this._maxOpacity = (a.maxOpacity || a.defaultMaxOpacity) * 255;
- this._minOpacity = (a.minOpacity || a.defaultMinOpacity) * 255
- },
- _drawAlpha: function (a) {
- var c = this._min = a.min;
- var d = this._max = a.max;
- var a = a.data || [];
- var e = a.length;
- var f = 1 - this._blur;
- while (e--) {
- var g = a[e];
- var h = g.x;
- var i = g.y;
- var j = g.radius;
- var k = g.value;
- var l = h - j;
- var m = i - j;
- var n = this.shadowCtx;
- var o;
- if (!this._templates[j]) {
- this._templates[j] = o = b(j, f)
- } else {
- o = this._templates[j]
- }
- n.globalAlpha = k / Math.abs(d - c);
- n.drawImage(o, l, m);
- if (l < this._renderBoundaries[0]) {
- this._renderBoundaries[0] = l
- }
- if (m < this._renderBoundaries[1]) {
- this._renderBoundaries[1] = m
- }
- if (l + 2 * j > this._renderBoundaries[2]) {
- this._renderBoundaries[2] = l + 2 * j
- }
- if (m + 2 * j > this._renderBoundaries[3]) {
- this._renderBoundaries[3] = m + 2 * j
- }
- }
- },
- _colorize: function () {
- var a = this._renderBoundaries[0];
- var b = this._renderBoundaries[1];
- var c = this._renderBoundaries[2] - a;
- var d = this._renderBoundaries[3] - b;
- var e = this._width;
- var f = this._height;
- var g = this._opacity;
- var h = this._maxOpacity;
- var i = this._minOpacity;
- if (a < 0) {
- a = 0
- }
- if (b < 0) {
- b = 0
- }
- if (a + c > e) {
- c = e - a
- }
- if (b + d > f) {
- d = f - b
- }
- var j = this.shadowCtx.getImageData(a, b, c, d);
- var k = j.data;
- var l = k.length;
- var m = this._palette;
- for (var n = 3; n < l; n += 4) {
- var o = k[n];
- var p = o * 4;
- if (!p) {
- continue
- }
- var q;
- if (g > 0) {
- q = g
- } else {
- if (o < h) {
- if (o < i) {
- q = i
- } else {
- q = o
- }
- } else {
- q = h
- }
- }
- k[n - 3] = m[p];
- k[n - 2] = m[p + 1];
- k[n - 1] = m[p + 2];
- k[n] = q
- }
- j.data = k;
- this.ctx.putImageData(j, a, b);
- this._renderBoundaries = [1e3, 1e3, 0, 0]
- },
- getValueAt: function (a) {
- var b;
- var c = this.shadowCtx;
- var d = c.getImageData(a.x, a.y, 1, 1);
- var e = d.data[3];
- var f = this._max;
- var g = this._min;
- b = Math.abs(f - g) * (e / 255) >> 0;
- return b
- },
- getDataURL: function () {
- return this.canvas.toDataURL()
- }
- };
- return d
- }();
- var e = function k() {
- var a = false;
- if (b["defaultRenderer"] === "canvas2d") {
- a = d
- }
- return a
- }();
- var f = {
- merge: function () {
- var a = {};
- var b = arguments.length;
- for (var c = 0; c < b; c++) {
- var d = arguments[c];
- for (var e in d) {
- a[e] = d[e]
- }
- }
- return a
- }
- };
- var g = function l() {
- var a = function h() {
- function a() {
- this.cStore = {}
- }
- a.prototype = {
- on: function (a, b, c) {
- var d = this.cStore;
- if (!d[a]) {
- d[a] = []
- }
- d[a].push(function (a) {
- return b.call(c, a)
- })
- },
- emit: function (a, b) {
- var c = this.cStore;
- if (c[a]) {
- var d = c[a].length;
- for (var e = 0; e < d; e++) {
- var f = c[a][e];
- f(b)
- }
- }
- }
- };
- return a
- }();
- var d = function (a) {
- var b = a._renderer;
- var c = a._coordinator;
- var d = a._store;
- c.on("renderpartial", b.renderPartial, b);
- c.on("renderall", b.renderAll, b);
- c.on("extremachange",
- function (b) {
- a._config.onExtremaChange && a._config.onExtremaChange({
- min: b.min,
- max: b.max,
- gradient: a._config["gradient"] || a._config["defaultGradient"]
- })
- });
- d.setCoordinator(c)
- };
- function g() {
- var g = this._config = f.merge(b, arguments[0] || {});
- this._coordinator = new a;
- if (g["plugin"]) {
- var h = g["plugin"];
- if (!b.plugins[h]) {
- throw new Error("Plugin '" + h + "' not found. Maybe it was not registered.")
- } else {
- var i = b.plugins[h];
- this._renderer = i.renderer;
- this._store = i.store
- }
- } else {
- this._renderer = new e(g);
- this._store = new c(g)
- }
- d(this)
- }
- g.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 (a) {
- this._config = f.merge(this._config, a);
- 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 (a) {
- if (this._store.getValueAt) {
- return this._store.getValueAt(a)
- } else if (this._renderer.getValueAt) {
- return this._renderer.getValueAt(a)
- } else {
- return null
- }
- }
- };
- return g
- }();
- var h = {
- create: function (a) {
- return new g(a)
- },
- register: function (a, c) {
- b.plugins[a] = c
- }
- };
- a["h337"] = h
- })(this || window);
|