UNPKG

7.78 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var color_util_1 = tslib_1.__importDefault(require("@antv/color-util"));
5var util_1 = require("@antv/util");
6var constant_1 = require("../constant");
7var base_1 = tslib_1.__importDefault(require("./base"));
8/**
9 * 用于绘制热力图。
10 */
11var Heatmap = /** @class */ (function (_super) {
12 tslib_1.__extends(Heatmap, _super);
13 function Heatmap() {
14 var _this = _super !== null && _super.apply(this, arguments) || this;
15 _this.type = 'heatmap';
16 _this.paletteCache = {};
17 return _this;
18 }
19 Heatmap.prototype.createElements = function (mappingData, index, isUpdate) {
20 if (isUpdate === void 0) { isUpdate = false; }
21 var range = this.prepareRange(mappingData);
22 var radius = this.prepareSize();
23 var blur = util_1.get(this.styleOption, ['cfg', 'shadowBlur']);
24 if (!util_1.isNumber(blur)) {
25 blur = radius / 2;
26 }
27 this.prepareGreyScaleBlurredCircle(radius, blur);
28 this.drawWithRange(mappingData, range, radius, blur);
29 return null;
30 };
31 /** 热力图暂时不支持 callback 回调(文档需要说明下) */
32 Heatmap.prototype.color = function (field, cfg) {
33 this.createAttrOption('color', field, typeof cfg !== 'function' ? cfg : '');
34 return this;
35 };
36 /**
37 * clear
38 */
39 Heatmap.prototype.clear = function () {
40 _super.prototype.clear.call(this);
41 this.clearShadowCanvasCtx();
42 this.paletteCache = {};
43 };
44 Heatmap.prototype.prepareRange = function (data) {
45 var colorAttr = this.getAttribute('color');
46 var colorField = colorAttr.getFields()[0];
47 var min = Infinity;
48 var max = -Infinity;
49 data.forEach(function (row) {
50 var value = row[constant_1.FIELD_ORIGIN][colorField];
51 if (value > max) {
52 max = value;
53 }
54 if (value < min) {
55 min = value;
56 }
57 });
58 if (min === max) {
59 min = max - 1;
60 }
61 return [min, max];
62 };
63 Heatmap.prototype.prepareSize = function () {
64 var radius = this.getDefaultValue('size');
65 if (!util_1.isNumber(radius)) {
66 radius = this.getDefaultSize();
67 }
68 return radius;
69 };
70 Heatmap.prototype.prepareGreyScaleBlurredCircle = function (radius, blur) {
71 var grayScaleBlurredCanvas = this.getGrayScaleBlurredCanvas();
72 var r2 = radius + blur;
73 var ctx = grayScaleBlurredCanvas.getContext('2d');
74 grayScaleBlurredCanvas.width = grayScaleBlurredCanvas.height = r2 * 2;
75 ctx.clearRect(0, 0, grayScaleBlurredCanvas.width, grayScaleBlurredCanvas.height);
76 ctx.shadowOffsetX = ctx.shadowOffsetY = r2 * 2;
77 ctx.shadowBlur = blur;
78 ctx.shadowColor = 'black';
79 ctx.beginPath();
80 ctx.arc(-r2, -r2, radius, 0, Math.PI * 2, true);
81 ctx.closePath();
82 ctx.fill();
83 };
84 Heatmap.prototype.drawWithRange = function (data, range, radius, blur) {
85 // canvas size
86 var _a = this.coordinate, start = _a.start, end = _a.end;
87 var width = this.coordinate.getWidth();
88 var height = this.coordinate.getHeight();
89 // value, range, etc
90 var colorAttr = this.getAttribute('color');
91 var valueField = colorAttr.getFields()[0];
92 // prepare shadow canvas context
93 this.clearShadowCanvasCtx();
94 var ctx = this.getShadowCanvasCtx();
95 // filter data
96 if (range) {
97 data = data.filter(function (row) {
98 return row[constant_1.FIELD_ORIGIN][valueField] <= range[1] && row[constant_1.FIELD_ORIGIN][valueField] >= range[0];
99 });
100 }
101 // step1. draw points with shadow
102 var scale = this.scales[valueField];
103 for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
104 var obj = data_1[_i];
105 var _b = this.getDrawCfg(obj), x = _b.x, y = _b.y;
106 var alpha = scale.scale(obj[constant_1.FIELD_ORIGIN][valueField]);
107 this.drawGrayScaleBlurredCircle(x - start.x, y - end.y, radius + blur, alpha, ctx);
108 }
109 // step2. convert pixels
110 var colored = ctx.getImageData(0, 0, width, height);
111 this.clearShadowCanvasCtx();
112 this.colorize(colored);
113 ctx.putImageData(colored, 0, 0);
114 var imageShape = this.getImageShape();
115 imageShape.attr('x', start.x);
116 imageShape.attr('y', end.y);
117 imageShape.attr('width', width);
118 imageShape.attr('height', height);
119 imageShape.attr('img', ctx.canvas);
120 imageShape.set('origin', this.getShapeInfo(data)); // 存储绘图信息数据
121 };
122 Heatmap.prototype.getDefaultSize = function () {
123 var position = this.getAttribute('position');
124 var coordinate = this.coordinate;
125 return Math.min(coordinate.getWidth() / (position.scales[0].ticks.length * 4), coordinate.getHeight() / (position.scales[1].ticks.length * 4));
126 };
127 Heatmap.prototype.clearShadowCanvasCtx = function () {
128 var ctx = this.getShadowCanvasCtx();
129 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
130 };
131 Heatmap.prototype.getShadowCanvasCtx = function () {
132 var canvas = this.shadowCanvas;
133 if (!canvas) {
134 canvas = document.createElement('canvas');
135 this.shadowCanvas = canvas;
136 }
137 canvas.width = this.coordinate.getWidth();
138 canvas.height = this.coordinate.getHeight();
139 return canvas.getContext('2d');
140 };
141 Heatmap.prototype.getGrayScaleBlurredCanvas = function () {
142 if (!this.grayScaleBlurredCanvas) {
143 this.grayScaleBlurredCanvas = document.createElement('canvas');
144 }
145 return this.grayScaleBlurredCanvas;
146 };
147 Heatmap.prototype.drawGrayScaleBlurredCircle = function (x, y, r, alpha, ctx) {
148 var grayScaleBlurredCanvas = this.getGrayScaleBlurredCanvas();
149 ctx.globalAlpha = alpha;
150 ctx.drawImage(grayScaleBlurredCanvas, x - r, y - r);
151 };
152 Heatmap.prototype.colorize = function (img) {
153 var colorAttr = this.getAttribute('color');
154 var pixels = img.data;
155 var paletteCache = this.paletteCache;
156 for (var i = 3; i < pixels.length; i += 4) {
157 var alpha = pixels[i]; // get gradient color from opacity value
158 if (util_1.isNumber(alpha)) {
159 var palette = paletteCache[alpha] ? paletteCache[alpha] : color_util_1.default.rgb2arr(colorAttr.gradient(alpha / 256));
160 pixels[i - 3] = palette[0];
161 pixels[i - 2] = palette[1];
162 pixels[i - 1] = palette[2];
163 pixels[i] = alpha;
164 }
165 }
166 };
167 Heatmap.prototype.getImageShape = function () {
168 var imageShape = this.imageShape;
169 if (imageShape) {
170 return imageShape;
171 }
172 var container = this.container;
173 imageShape = container.addShape({
174 type: 'image',
175 attrs: {},
176 });
177 this.imageShape = imageShape;
178 return imageShape;
179 };
180 Heatmap.prototype.getShapeInfo = function (mappingData) {
181 var shapeCfg = this.getDrawCfg(mappingData[0]);
182 return tslib_1.__assign(tslib_1.__assign({}, shapeCfg), { mappingData: mappingData, data: this.getData(mappingData) });
183 };
184 Heatmap.prototype.getData = function (mappingData) {
185 return mappingData.map(function (obj) {
186 return obj[constant_1.FIELD_ORIGIN];
187 });
188 };
189 return Heatmap;
190}(base_1.default));
191exports.default = Heatmap;
192//# sourceMappingURL=heatmap.js.map
\No newline at end of file