HeatmapLayer.js 5.9 KB

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