HeatmapLayer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var zrUtil = require("zrender/lib/core/util");
  20. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. /* global Uint8ClampedArray */
  39. var GRADIENT_LEVELS = 256;
  40. /**
  41. * Heatmap Chart
  42. *
  43. * @class
  44. */
  45. function Heatmap() {
  46. var canvas = zrUtil.createCanvas();
  47. this.canvas = canvas;
  48. this.blurSize = 30;
  49. this.pointSize = 20;
  50. this.maxOpacity = 1;
  51. this.minOpacity = 0;
  52. this._gradientPixels = {};
  53. }
  54. Heatmap.prototype = {
  55. /**
  56. * Renders Heatmap and returns the rendered canvas
  57. * @param {Array} data array of data, each has x, y, value
  58. * @param {number} width canvas width
  59. * @param {number} height canvas height
  60. */
  61. update: function (data, width, height, normalize, colorFunc, isInRange) {
  62. var brush = this._getBrush();
  63. var gradientInRange = this._getGradient(data, colorFunc, 'inRange');
  64. var gradientOutOfRange = this._getGradient(data, colorFunc, 'outOfRange');
  65. var r = this.pointSize + this.blurSize;
  66. var canvas = this.canvas;
  67. var ctx = canvas.getContext('2d');
  68. var len = data.length;
  69. canvas.width = width;
  70. canvas.height = height;
  71. for (var i = 0; i < len; ++i) {
  72. var p = data[i];
  73. var x = p[0];
  74. var y = p[1];
  75. var value = p[2]; // calculate alpha using value
  76. var alpha = normalize(value); // draw with the circle brush with alpha
  77. ctx.globalAlpha = alpha;
  78. ctx.drawImage(brush, x - r, y - r);
  79. }
  80. if (!canvas.width || !canvas.height) {
  81. // Avoid "Uncaught DOMException: Failed to execute 'getImageData' on
  82. // 'CanvasRenderingContext2D': The source height is 0."
  83. return canvas;
  84. } // colorize the canvas using alpha value and set with gradient
  85. var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  86. var pixels = imageData.data;
  87. var offset = 0;
  88. var pixelLen = pixels.length;
  89. var minOpacity = this.minOpacity;
  90. var maxOpacity = this.maxOpacity;
  91. var diffOpacity = maxOpacity - minOpacity;
  92. while (offset < pixelLen) {
  93. var alpha = pixels[offset + 3] / 256;
  94. var gradientOffset = Math.floor(alpha * (GRADIENT_LEVELS - 1)) * 4; // Simple optimize to ignore the empty data
  95. if (alpha > 0) {
  96. var gradient = isInRange(alpha) ? gradientInRange : gradientOutOfRange; // Any alpha > 0 will be mapped to [minOpacity, maxOpacity]
  97. alpha > 0 && (alpha = alpha * diffOpacity + minOpacity);
  98. pixels[offset++] = gradient[gradientOffset];
  99. pixels[offset++] = gradient[gradientOffset + 1];
  100. pixels[offset++] = gradient[gradientOffset + 2];
  101. pixels[offset++] = gradient[gradientOffset + 3] * alpha * 256;
  102. } else {
  103. offset += 4;
  104. }
  105. }
  106. ctx.putImageData(imageData, 0, 0);
  107. return canvas;
  108. },
  109. /**
  110. * get canvas of a black circle brush used for canvas to draw later
  111. * @private
  112. * @returns {Object} circle brush canvas
  113. */
  114. _getBrush: function () {
  115. var brushCanvas = this._brushCanvas || (this._brushCanvas = zrUtil.createCanvas()); // set brush size
  116. var r = this.pointSize + this.blurSize;
  117. var d = r * 2;
  118. brushCanvas.width = d;
  119. brushCanvas.height = d;
  120. var ctx = brushCanvas.getContext('2d');
  121. ctx.clearRect(0, 0, d, d); // in order to render shadow without the distinct circle,
  122. // draw the distinct circle in an invisible place,
  123. // and use shadowOffset to draw shadow in the center of the canvas
  124. ctx.shadowOffsetX = d;
  125. ctx.shadowBlur = this.blurSize; // draw the shadow in black, and use alpha and shadow blur to generate
  126. // color in color map
  127. ctx.shadowColor = '#000'; // draw circle in the left to the canvas
  128. ctx.beginPath();
  129. ctx.arc(-r, r, this.pointSize, 0, Math.PI * 2, true);
  130. ctx.closePath();
  131. ctx.fill();
  132. return brushCanvas;
  133. },
  134. /**
  135. * get gradient color map
  136. * @private
  137. */
  138. _getGradient: function (data, colorFunc, state) {
  139. var gradientPixels = this._gradientPixels;
  140. var pixelsSingleState = gradientPixels[state] || (gradientPixels[state] = new Uint8ClampedArray(256 * 4));
  141. var color = [0, 0, 0, 0];
  142. var off = 0;
  143. for (var i = 0; i < 256; i++) {
  144. colorFunc[state](i / 255, true, color);
  145. pixelsSingleState[off++] = color[0];
  146. pixelsSingleState[off++] = color[1];
  147. pixelsSingleState[off++] = color[2];
  148. pixelsSingleState[off++] = color[3];
  149. }
  150. return pixelsSingleState;
  151. }
  152. };
  153. var _default = Heatmap;
  154. module.exports = _default;