easy-three-utils 0.0.365 → 0.0.367

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