no-heatmap.js 2.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,729 @@
1
+ /*
2
+ * no-heatmap.js v2.0.8 | 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
+ * :: 2026-01-18 14:13
8
+ */
9
+ ;(function (name, context, factory) {
10
+
11
+ // Supports UMD. AMD, CommonJS/Node.js and browser context
12
+ if (typeof module !== "undefined" && module.exports) {
13
+ module.exports = factory();
14
+ } else if (typeof define === "function" && define.amd) {
15
+ define(factory);
16
+ } else {
17
+ context[name] = factory();
18
+ }
19
+
20
+ })("h337", this, function () {
21
+
22
+ // Heatmap Config stores default values and will be merged with instance config
23
+ var HeatmapConfig = {
24
+ defaultRadius: 40,
25
+ defaultRenderer: 'canvas2d',
26
+ defaultGradient: { 0.25: "rgb(0,0,255)", 0.55: "rgb(0,255,0)", 0.85: "yellow", 1.0: "rgb(255,0,0)"},
27
+ defaultMaxOpacity: 1,
28
+ defaultMinOpacity: 0,
29
+ defaultBlur: .85,
30
+ defaultXField: 'x',
31
+ defaultYField: 'y',
32
+ defaultValueField: 'value',
33
+ defaultDataStack: true,
34
+ plugins: {}
35
+ };
36
+ var Store = (function StoreClosure() {
37
+
38
+ var Store = function Store(config) {
39
+ this._coordinator = {};
40
+ this._data = [];
41
+ this._radi = [];
42
+ this._min = 10;
43
+ this._max = 1;
44
+ this._xField = config['xField'] || config.defaultXField;
45
+ this._yField = config['yField'] || config.defaultYField;
46
+ this._valueField = config['valueField'] || config.defaultValueField;
47
+ this._dataStack = config['dataStack'] === undefined ? config.defaultDataStack : config['dataStack'];
48
+ if (config["radius"]) {
49
+ this._cfgRadius = config["radius"];
50
+ }
51
+ };
52
+
53
+ var defaultRadius = HeatmapConfig.defaultRadius;
54
+
55
+ Store.prototype = {
56
+ // when forceRender = false -> called from setData, omits renderall event
57
+ _organiseData: function(dataPoint, forceRender) {
58
+ var x = dataPoint[this._xField];
59
+ var y = dataPoint[this._yField];
60
+ var radi = this._radi;
61
+ var store = this._data;
62
+ var max = this._max;
63
+ var min = this._min;
64
+ var value = dataPoint[this._valueField] || 1;
65
+ var radius = dataPoint.radius || this._cfgRadius || defaultRadius;
66
+ var dataStack = this._dataStack;
67
+ if (!store[x]) {
68
+ store[x] = [];
69
+ radi[x] = [];
70
+ }
71
+
72
+ if (!store[x][y]) {
73
+ store[x][y] = value;
74
+ radi[x][y] = radius;
75
+ } else {
76
+ if (dataStack) {
77
+ store[x][y] += value;
78
+ }else {
79
+ store[x][y] = Math.max(value, store[x][y]);
80
+ }
81
+ }
82
+ var storedVal = store[x][y];
83
+
84
+ if (storedVal > max) {
85
+ if (!forceRender) {
86
+ this._max = storedVal;
87
+ } else {
88
+ this.setDataMax(storedVal);
89
+ }
90
+ return false;
91
+ } else if (storedVal < min) {
92
+ if (!forceRender) {
93
+ this._min = storedVal;
94
+ } else {
95
+ this.setDataMin(storedVal);
96
+ }
97
+ return false;
98
+ } else {
99
+ return {
100
+ x: x,
101
+ y: y,
102
+ value: value,
103
+ radius: radius,
104
+ min: min,
105
+ max: max
106
+ };
107
+ }
108
+ },
109
+ _unOrganizeData: function() {
110
+ var unorganizedData = [];
111
+ var data = this._data;
112
+ var radi = this._radi;
113
+
114
+ for (var x in data) {
115
+ for (var y in data[x]) {
116
+
117
+ unorganizedData.push({
118
+ x: x,
119
+ y: y,
120
+ radius: radi[x][y],
121
+ value: data[x][y]
122
+ });
123
+
124
+ }
125
+ }
126
+ return {
127
+ min: this._min,
128
+ max: this._max,
129
+ data: unorganizedData
130
+ };
131
+ },
132
+ _onExtremaChange: function() {
133
+ this._coordinator.emit('extremachange', {
134
+ min: this._min,
135
+ max: this._max
136
+ });
137
+ },
138
+ addData: function() {
139
+ if (arguments[0].length > 0) {
140
+ var dataArr = arguments[0];
141
+ var dataLen = dataArr.length;
142
+ while (dataLen--) {
143
+ this.addData.call(this, dataArr[dataLen]);
144
+ }
145
+ } else {
146
+ // add to store
147
+ var organisedEntry = this._organiseData(arguments[0], true);
148
+ if (organisedEntry) {
149
+ // if it's the first datapoint initialize the extremas with it
150
+ if (this._data.length === 0) {
151
+ this._min = this._max = organisedEntry.value;
152
+ }
153
+ this._coordinator.emit('renderpartial', {
154
+ min: this._min,
155
+ max: this._max,
156
+ data: [organisedEntry]
157
+ });
158
+ }
159
+ }
160
+ return this;
161
+ },
162
+ setData: function(data) {
163
+ var dataPoints = data.data;
164
+ var pointsLen = dataPoints.length;
165
+
166
+
167
+ // reset data arrays
168
+ this._data = [];
169
+ this._radi = [];
170
+
171
+ for(var i = 0; i < pointsLen; i++) {
172
+ this._organiseData(dataPoints[i], false);
173
+ }
174
+ this._max = data.max;
175
+ this._min = data.min || 0;
176
+
177
+ this._onExtremaChange();
178
+ this._coordinator.emit('renderall', this._getInternalData());
179
+ return this;
180
+ },
181
+ removeData: function() {
182
+ // TODO: implement
183
+ },
184
+ setDataMax: function(max) {
185
+ this._max = max;
186
+ this._onExtremaChange();
187
+ this._coordinator.emit('renderall', this._getInternalData());
188
+ return this;
189
+ },
190
+ setDataMin: function(min) {
191
+ this._min = min;
192
+ this._onExtremaChange();
193
+ this._coordinator.emit('renderall', this._getInternalData());
194
+ return this;
195
+ },
196
+ setCoordinator: function(coordinator) {
197
+ this._coordinator = coordinator;
198
+ },
199
+ _getInternalData: function() {
200
+ return {
201
+ max: this._max,
202
+ min: this._min,
203
+ data: this._data,
204
+ radi: this._radi
205
+ };
206
+ },
207
+ getData: function() {
208
+ return this._unOrganizeData();
209
+ }/*,
210
+
211
+ TODO: rethink.
212
+
213
+ getValueAt: function(point) {
214
+ var value;
215
+ var radius = 100;
216
+ var x = point.x;
217
+ var y = point.y;
218
+ var data = this._data;
219
+
220
+ if (data[x] && data[x][y]) {
221
+ return data[x][y];
222
+ } else {
223
+ var values = [];
224
+ // radial search for datapoints based on default radius
225
+ for(var distance = 1; distance < radius; distance++) {
226
+ var neighbors = distance * 2 +1;
227
+ var startX = x - distance;
228
+ var startY = y - distance;
229
+
230
+ for(var i = 0; i < neighbors; i++) {
231
+ for (var o = 0; o < neighbors; o++) {
232
+ if ((i == 0 || i == neighbors-1) || (o == 0 || o == neighbors-1)) {
233
+ if (data[startY+i] && data[startY+i][startX+o]) {
234
+ values.push(data[startY+i][startX+o]);
235
+ }
236
+ } else {
237
+ continue;
238
+ }
239
+ }
240
+ }
241
+ }
242
+ if (values.length > 0) {
243
+ return Math.max.apply(Math, values);
244
+ }
245
+ }
246
+ return false;
247
+ }*/
248
+ };
249
+
250
+
251
+ return Store;
252
+ })();
253
+
254
+ var Canvas2dRenderer = (function Canvas2dRendererClosure() {
255
+
256
+ var _getColorPalette = function(config) {
257
+ var gradientConfig = config.gradient || config.defaultGradient;
258
+ var paletteCanvas = document.createElement('canvas');
259
+ var paletteCtx = paletteCanvas.getContext('2d');
260
+
261
+ paletteCanvas.width = 256;
262
+ paletteCanvas.height = 1;
263
+
264
+ var gradient = paletteCtx.createLinearGradient(0, 0, 256, 1);
265
+ for (var key in gradientConfig) {
266
+ gradient.addColorStop(key, gradientConfig[key]);
267
+ }
268
+
269
+ paletteCtx.fillStyle = gradient;
270
+ paletteCtx.fillRect(0, 0, 256, 1);
271
+
272
+ return paletteCtx.getImageData(0, 0, 256, 1).data;
273
+ };
274
+
275
+ var _getPointTemplate = function(radius, blurFactor) {
276
+ var tplCanvas = document.createElement('canvas');
277
+ var tplCtx = tplCanvas.getContext('2d');
278
+ var x = radius;
279
+ var y = radius;
280
+ tplCanvas.width = tplCanvas.height = radius*2;
281
+
282
+ if (blurFactor == 1) {
283
+ tplCtx.beginPath();
284
+ tplCtx.arc(x, y, radius, 0, 2 * Math.PI, false);
285
+ tplCtx.fillStyle = 'rgba(0,0,0,1)';
286
+ tplCtx.fill();
287
+ } else {
288
+ var gradient = tplCtx.createRadialGradient(x, y, radius*blurFactor, x, y, radius);
289
+ gradient.addColorStop(0, 'rgba(0,0,0,1)');
290
+ gradient.addColorStop(1, 'rgba(0,0,0,0)');
291
+ tplCtx.fillStyle = gradient;
292
+ tplCtx.fillRect(0, 0, 2*radius, 2*radius);
293
+ }
294
+
295
+
296
+
297
+ return tplCanvas;
298
+ };
299
+
300
+ var _prepareData = function(data) {
301
+ var renderData = [];
302
+ var min = data.min;
303
+ var max = data.max;
304
+ var radi = data.radi;
305
+ var data = data.data;
306
+
307
+ var xValues = Object.keys(data);
308
+ var xValuesLen = xValues.length;
309
+
310
+ while(xValuesLen--) {
311
+ var xValue = xValues[xValuesLen];
312
+ var yValues = Object.keys(data[xValue]);
313
+ var yValuesLen = yValues.length;
314
+ while(yValuesLen--) {
315
+ var yValue = yValues[yValuesLen];
316
+ var value = data[xValue][yValue];
317
+ var radius = radi[xValue][yValue];
318
+ renderData.push({
319
+ x: xValue,
320
+ y: yValue,
321
+ value: value,
322
+ radius: radius
323
+ });
324
+ }
325
+ }
326
+
327
+ return {
328
+ min: min,
329
+ max: max,
330
+ data: renderData
331
+ };
332
+ };
333
+
334
+
335
+ function Canvas2dRenderer(config) {
336
+ var container = config.container;
337
+ var shadowCanvas = this.shadowCanvas = document.createElement('canvas');
338
+ var canvas = this.canvas = config.canvas || document.createElement('canvas');
339
+ var renderBoundaries = this._renderBoundaries = [10000, 10000, 0, 0];
340
+
341
+ var computed = getComputedStyle(config.container) || {};
342
+
343
+ canvas.className = 'heatmap-canvas';
344
+
345
+ this._width = canvas.width = shadowCanvas.width = config.width || +(computed.width.replace(/px/,''));
346
+ this._height = canvas.height = shadowCanvas.height = config.height || +(computed.height.replace(/px/,''));
347
+
348
+ this.shadowCtx = shadowCanvas.getContext('2d');
349
+ this.ctx = canvas.getContext('2d');
350
+
351
+ // @TODO:
352
+ // conditional wrapper
353
+
354
+ canvas.style.cssText = shadowCanvas.style.cssText = 'position:absolute;left:0;top:0;';
355
+
356
+ container.style.position = 'relative';
357
+ container.appendChild(canvas);
358
+
359
+ this._palette = _getColorPalette(config);
360
+ this._templates = {};
361
+
362
+ this._setStyles(config);
363
+ };
364
+
365
+ Canvas2dRenderer.prototype = {
366
+ renderPartial: function(data) {
367
+ if (data.data.length > 0) {
368
+ this._drawAlpha(data);
369
+ this._colorize();
370
+ }
371
+ },
372
+ renderAll: function(data) {
373
+ // reset render boundaries
374
+ this._clear();
375
+ if (data.data.length > 0) {
376
+ this._drawAlpha(_prepareData(data));
377
+ this._colorize();
378
+ }
379
+ },
380
+ _updateGradient: function(config) {
381
+ this._palette = _getColorPalette(config);
382
+ },
383
+ updateConfig: function(config) {
384
+ if (config['gradient']) {
385
+ this._updateGradient(config);
386
+ }
387
+ this._setStyles(config);
388
+ },
389
+ setDimensions: function(width, height) {
390
+ this._width = width;
391
+ this._height = height;
392
+ this.canvas.width = this.shadowCanvas.width = width;
393
+ this.canvas.height = this.shadowCanvas.height = height;
394
+ },
395
+ _clear: function() {
396
+ this.shadowCtx.clearRect(0, 0, this._width, this._height);
397
+ this.ctx.clearRect(0, 0, this._width, this._height);
398
+ },
399
+ _setStyles: function(config) {
400
+ this._blur = (config.blur == 0)?0:(config.blur || config.defaultBlur);
401
+
402
+ if (config.backgroundColor) {
403
+ this.canvas.style.backgroundColor = config.backgroundColor;
404
+ }
405
+
406
+ this._width = this.canvas.width = this.shadowCanvas.width = config.width || this._width;
407
+ this._height = this.canvas.height = this.shadowCanvas.height = config.height || this._height;
408
+
409
+
410
+ this._opacity = (config.opacity || 0) * 255;
411
+ this._maxOpacity = (config.maxOpacity || config.defaultMaxOpacity) * 255;
412
+ this._minOpacity = (config.minOpacity || config.defaultMinOpacity) * 255;
413
+ this._useGradientOpacity = !!config.useGradientOpacity;
414
+ },
415
+ _drawAlpha: function(data) {
416
+ var min = this._min = data.min;
417
+ var max = this._max = data.max;
418
+ var data = data.data || [];
419
+ var dataLen = data.length;
420
+ // on a point basis?
421
+ var blur = 1 - this._blur;
422
+
423
+ while(dataLen--) {
424
+
425
+ var point = data[dataLen];
426
+
427
+ var x = point.x;
428
+ var y = point.y;
429
+ var radius = point.radius;
430
+ // if value is bigger than max
431
+ // use max as value
432
+ var value = Math.min(point.value, max);
433
+ var rectX = x - radius;
434
+ var rectY = y - radius;
435
+ var shadowCtx = this.shadowCtx;
436
+
437
+
438
+
439
+
440
+ var tpl;
441
+ if (!this._templates[radius]) {
442
+ this._templates[radius] = tpl = _getPointTemplate(radius, blur);
443
+ } else {
444
+ tpl = this._templates[radius];
445
+ }
446
+ // value from minimum / value range
447
+ // => [0, 1]
448
+ var templateAlpha = (value-min)/(max-min);
449
+ // this fixes #176: small values are not visible because globalAlpha < .01 cannot be read from imageData
450
+ shadowCtx.globalAlpha = templateAlpha < .01 ? .01 : templateAlpha;
451
+
452
+ shadowCtx.drawImage(tpl, rectX, rectY);
453
+
454
+ // update renderBoundaries
455
+ if (rectX < this._renderBoundaries[0]) {
456
+ this._renderBoundaries[0] = rectX;
457
+ }
458
+ if (rectY < this._renderBoundaries[1]) {
459
+ this._renderBoundaries[1] = rectY;
460
+ }
461
+ if (rectX + 2*radius > this._renderBoundaries[2]) {
462
+ this._renderBoundaries[2] = rectX + 2*radius;
463
+ }
464
+ if (rectY + 2*radius > this._renderBoundaries[3]) {
465
+ this._renderBoundaries[3] = rectY + 2*radius;
466
+ }
467
+
468
+ }
469
+ },
470
+ _colorize: function() {
471
+ var x = this._renderBoundaries[0];
472
+ var y = this._renderBoundaries[1];
473
+ var width = this._renderBoundaries[2] - x;
474
+ var height = this._renderBoundaries[3] - y;
475
+ var maxWidth = this._width;
476
+ var maxHeight = this._height;
477
+ var opacity = this._opacity;
478
+ var maxOpacity = this._maxOpacity;
479
+ var minOpacity = this._minOpacity;
480
+ var useGradientOpacity = this._useGradientOpacity;
481
+
482
+ if (x < 0) {
483
+ x = 0;
484
+ }
485
+ if (y < 0) {
486
+ y = 0;
487
+ }
488
+ if (x + width > maxWidth) {
489
+ width = maxWidth - x;
490
+ }
491
+ if (y + height > maxHeight) {
492
+ height = maxHeight - y;
493
+ }
494
+
495
+ var img = this.shadowCtx.getImageData(x, y, width, height);
496
+ var imgData = img.data;
497
+ var len = imgData.length;
498
+ var palette = this._palette;
499
+
500
+
501
+ for (var i = 3; i < len; i+= 4) {
502
+ var alpha = imgData[i];
503
+ var offset = alpha * 4;
504
+
505
+
506
+ if (!offset) {
507
+ continue;
508
+ }
509
+
510
+ var finalAlpha;
511
+ if (opacity > 0) {
512
+ finalAlpha = opacity;
513
+ } else {
514
+ if (alpha < maxOpacity) {
515
+ if (alpha < minOpacity) {
516
+ finalAlpha = minOpacity;
517
+ } else {
518
+ finalAlpha = alpha;
519
+ }
520
+ } else {
521
+ finalAlpha = maxOpacity;
522
+ }
523
+ }
524
+
525
+ imgData[i-3] = palette[offset];
526
+ imgData[i-2] = palette[offset + 1];
527
+ imgData[i-1] = palette[offset + 2];
528
+ imgData[i] = useGradientOpacity ? palette[offset + 3] : finalAlpha;
529
+
530
+ }
531
+
532
+ // img.data = imgData;
533
+ this.ctx.putImageData(img, x, y);
534
+
535
+ this._renderBoundaries = [1000, 1000, 0, 0];
536
+
537
+ },
538
+ getValueAt: function(point) {
539
+ var value;
540
+ var shadowCtx = this.shadowCtx;
541
+ var img = shadowCtx.getImageData(point.x, point.y, 1, 1);
542
+ var data = img.data[3];
543
+ var max = this._max;
544
+ var min = this._min;
545
+
546
+ value = (Math.abs(max-min) * (data/255)) >> 0;
547
+
548
+ return value;
549
+ },
550
+ getDataURL: function() {
551
+ return this.canvas.toDataURL();
552
+ }
553
+ };
554
+
555
+
556
+ return Canvas2dRenderer;
557
+ })();
558
+
559
+
560
+ var Renderer = (function RendererClosure() {
561
+
562
+ var rendererFn = false;
563
+
564
+ if (HeatmapConfig['defaultRenderer'] === 'canvas2d') {
565
+ rendererFn = Canvas2dRenderer;
566
+ }
567
+
568
+ return rendererFn;
569
+ })();
570
+
571
+
572
+ var Util = {
573
+ merge: function() {
574
+ var merged = {};
575
+ var argsLen = arguments.length;
576
+ for (var i = 0; i < argsLen; i++) {
577
+ var obj = arguments[i]
578
+ for (var key in obj) {
579
+ merged[key] = obj[key];
580
+ }
581
+ }
582
+ return merged;
583
+ }
584
+ };
585
+ // Heatmap Constructor
586
+ var Heatmap = (function HeatmapClosure() {
587
+
588
+ var Coordinator = (function CoordinatorClosure() {
589
+
590
+ function Coordinator() {
591
+ this.cStore = {};
592
+ };
593
+
594
+ Coordinator.prototype = {
595
+ on: function(evtName, callback, scope) {
596
+ var cStore = this.cStore;
597
+
598
+ if (!cStore[evtName]) {
599
+ cStore[evtName] = [];
600
+ }
601
+ cStore[evtName].push((function(data) {
602
+ return callback.call(scope, data);
603
+ }));
604
+ },
605
+ emit: function(evtName, data) {
606
+ var cStore = this.cStore;
607
+ if (cStore[evtName]) {
608
+ var len = cStore[evtName].length;
609
+ for (var i=0; i<len; i++) {
610
+ var callback = cStore[evtName][i];
611
+ callback(data);
612
+ }
613
+ }
614
+ }
615
+ };
616
+
617
+ return Coordinator;
618
+ })();
619
+
620
+
621
+ var _connect = function(scope) {
622
+ var renderer = scope._renderer;
623
+ var coordinator = scope._coordinator;
624
+ var store = scope._store;
625
+
626
+ coordinator.on('renderpartial', renderer.renderPartial, renderer);
627
+ coordinator.on('renderall', renderer.renderAll, renderer);
628
+ coordinator.on('extremachange', function(data) {
629
+ scope._config.onExtremaChange &&
630
+ scope._config.onExtremaChange({
631
+ min: data.min,
632
+ max: data.max,
633
+ gradient: scope._config['gradient'] || scope._config['defaultGradient']
634
+ });
635
+ });
636
+ store.setCoordinator(coordinator);
637
+ };
638
+
639
+
640
+ function Heatmap() {
641
+ var config = this._config = Util.merge(HeatmapConfig, arguments[0] || {});
642
+ this._coordinator = new Coordinator();
643
+ if (config['plugin']) {
644
+ var pluginToLoad = config['plugin'];
645
+ if (!HeatmapConfig.plugins[pluginToLoad]) {
646
+ throw new Error('Plugin \''+ pluginToLoad + '\' not found. Maybe it was not registered.');
647
+ } else {
648
+ var plugin = HeatmapConfig.plugins[pluginToLoad];
649
+ // set plugin renderer and store
650
+ this._renderer = new plugin.renderer(config);
651
+ this._store = new plugin.store(config);
652
+ }
653
+ } else {
654
+ this._renderer = new Renderer(config);
655
+ this._store = new Store(config);
656
+ }
657
+ _connect(this);
658
+ };
659
+
660
+ // @TODO:
661
+ // add API documentation
662
+ Heatmap.prototype = {
663
+ addData: function() {
664
+ this._store.addData.apply(this._store, arguments);
665
+ return this;
666
+ },
667
+ removeData: function() {
668
+ this._store.removeData && this._store.removeData.apply(this._store, arguments);
669
+ return this;
670
+ },
671
+ setData: function() {
672
+ this._store.setData.apply(this._store, arguments);
673
+ return this;
674
+ },
675
+ setDataMax: function() {
676
+ this._store.setDataMax.apply(this._store, arguments);
677
+ return this;
678
+ },
679
+ setDataMin: function() {
680
+ this._store.setDataMin.apply(this._store, arguments);
681
+ return this;
682
+ },
683
+ configure: function(config) {
684
+ this._config = Util.merge(this._config, config);
685
+ this._renderer.updateConfig(this._config);
686
+ this._coordinator.emit('renderall', this._store._getInternalData());
687
+ return this;
688
+ },
689
+ repaint: function() {
690
+ this._coordinator.emit('renderall', this._store._getInternalData());
691
+ return this;
692
+ },
693
+ getData: function() {
694
+ return this._store.getData();
695
+ },
696
+ getDataURL: function() {
697
+ return this._renderer.getDataURL();
698
+ },
699
+ getValueAt: function(point) {
700
+
701
+ if (this._store.getValueAt) {
702
+ return this._store.getValueAt(point);
703
+ } else if (this._renderer.getValueAt) {
704
+ return this._renderer.getValueAt(point);
705
+ } else {
706
+ return null;
707
+ }
708
+ }
709
+ };
710
+
711
+ return Heatmap;
712
+
713
+ })();
714
+
715
+
716
+ // core
717
+ var heatmapFactory = {
718
+ create: function(config) {
719
+ return new Heatmap(config);
720
+ },
721
+ register: function(pluginKey, plugin) {
722
+ HeatmapConfig.plugins[pluginKey] = plugin;
723
+ }
724
+ };
725
+
726
+ return heatmapFactory;
727
+
728
+
729
+ });