LargeSymbolDraw.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. import { __extends } from "tslib";
  41. /* global Float32Array */
  42. // TODO Batch by color
  43. import * as graphic from '../../util/graphic.js';
  44. import { createSymbol } from '../../util/symbol.js';
  45. import { getECData } from '../../util/innerStore.js';
  46. var BOOST_SIZE_THRESHOLD = 4;
  47. var LargeSymbolPathShape =
  48. /** @class */
  49. function () {
  50. function LargeSymbolPathShape() {}
  51. return LargeSymbolPathShape;
  52. }();
  53. var LargeSymbolPath =
  54. /** @class */
  55. function (_super) {
  56. __extends(LargeSymbolPath, _super);
  57. function LargeSymbolPath(opts) {
  58. var _this = _super.call(this, opts) || this;
  59. _this._off = 0;
  60. _this.hoverDataIdx = -1;
  61. return _this;
  62. }
  63. LargeSymbolPath.prototype.getDefaultShape = function () {
  64. return new LargeSymbolPathShape();
  65. };
  66. LargeSymbolPath.prototype.reset = function () {
  67. this.notClear = false;
  68. this._off = 0;
  69. };
  70. LargeSymbolPath.prototype.buildPath = function (path, shape) {
  71. var points = shape.points;
  72. var size = shape.size;
  73. var symbolProxy = this.symbolProxy;
  74. var symbolProxyShape = symbolProxy.shape;
  75. var ctx = path.getContext ? path.getContext() : path;
  76. var canBoost = ctx && size[0] < BOOST_SIZE_THRESHOLD;
  77. var softClipShape = this.softClipShape;
  78. var i; // Do draw in afterBrush.
  79. if (canBoost) {
  80. this._ctx = ctx;
  81. return;
  82. }
  83. this._ctx = null;
  84. for (i = this._off; i < points.length;) {
  85. var x = points[i++];
  86. var y = points[i++];
  87. if (isNaN(x) || isNaN(y)) {
  88. continue;
  89. }
  90. if (softClipShape && !softClipShape.contain(x, y)) {
  91. continue;
  92. }
  93. symbolProxyShape.x = x - size[0] / 2;
  94. symbolProxyShape.y = y - size[1] / 2;
  95. symbolProxyShape.width = size[0];
  96. symbolProxyShape.height = size[1];
  97. symbolProxy.buildPath(path, symbolProxyShape, true);
  98. }
  99. if (this.incremental) {
  100. this._off = i;
  101. this.notClear = true;
  102. }
  103. };
  104. LargeSymbolPath.prototype.afterBrush = function () {
  105. var shape = this.shape;
  106. var points = shape.points;
  107. var size = shape.size;
  108. var ctx = this._ctx;
  109. var softClipShape = this.softClipShape;
  110. var i;
  111. if (!ctx) {
  112. return;
  113. } // PENDING If style or other canvas status changed?
  114. for (i = this._off; i < points.length;) {
  115. var x = points[i++];
  116. var y = points[i++];
  117. if (isNaN(x) || isNaN(y)) {
  118. continue;
  119. }
  120. if (softClipShape && !softClipShape.contain(x, y)) {
  121. continue;
  122. } // fillRect is faster than building a rect path and draw.
  123. // And it support light globalCompositeOperation.
  124. ctx.fillRect(x - size[0] / 2, y - size[1] / 2, size[0], size[1]);
  125. }
  126. if (this.incremental) {
  127. this._off = i;
  128. this.notClear = true;
  129. }
  130. };
  131. LargeSymbolPath.prototype.findDataIndex = function (x, y) {
  132. // TODO ???
  133. // Consider transform
  134. var shape = this.shape;
  135. var points = shape.points;
  136. var size = shape.size;
  137. var w = Math.max(size[0], 4);
  138. var h = Math.max(size[1], 4); // Not consider transform
  139. // Treat each element as a rect
  140. // top down traverse
  141. for (var idx = points.length / 2 - 1; idx >= 0; idx--) {
  142. var i = idx * 2;
  143. var x0 = points[i] - w / 2;
  144. var y0 = points[i + 1] - h / 2;
  145. if (x >= x0 && y >= y0 && x <= x0 + w && y <= y0 + h) {
  146. return idx;
  147. }
  148. }
  149. return -1;
  150. };
  151. LargeSymbolPath.prototype.contain = function (x, y) {
  152. var localPos = this.transformCoordToLocal(x, y);
  153. var rect = this.getBoundingRect();
  154. x = localPos[0];
  155. y = localPos[1];
  156. if (rect.contain(x, y)) {
  157. // Cache found data index.
  158. var dataIdx = this.hoverDataIdx = this.findDataIndex(x, y);
  159. return dataIdx >= 0;
  160. }
  161. this.hoverDataIdx = -1;
  162. return false;
  163. };
  164. LargeSymbolPath.prototype.getBoundingRect = function () {
  165. // Ignore stroke for large symbol draw.
  166. var rect = this._rect;
  167. if (!rect) {
  168. var shape = this.shape;
  169. var points = shape.points;
  170. var size = shape.size;
  171. var w = size[0];
  172. var h = size[1];
  173. var minX = Infinity;
  174. var minY = Infinity;
  175. var maxX = -Infinity;
  176. var maxY = -Infinity;
  177. for (var i = 0; i < points.length;) {
  178. var x = points[i++];
  179. var y = points[i++];
  180. minX = Math.min(x, minX);
  181. maxX = Math.max(x, maxX);
  182. minY = Math.min(y, minY);
  183. maxY = Math.max(y, maxY);
  184. }
  185. rect = this._rect = new graphic.BoundingRect(minX - w / 2, minY - h / 2, maxX - minX + w, maxY - minY + h);
  186. }
  187. return rect;
  188. };
  189. return LargeSymbolPath;
  190. }(graphic.Path);
  191. var LargeSymbolDraw =
  192. /** @class */
  193. function () {
  194. function LargeSymbolDraw() {
  195. this.group = new graphic.Group();
  196. }
  197. /**
  198. * Update symbols draw by new data
  199. */
  200. LargeSymbolDraw.prototype.updateData = function (data, opt) {
  201. this._clear();
  202. var symbolEl = this._create();
  203. symbolEl.setShape({
  204. points: data.getLayout('points')
  205. });
  206. this._setCommon(symbolEl, data, opt);
  207. };
  208. LargeSymbolDraw.prototype.updateLayout = function (data) {
  209. var points = data.getLayout('points');
  210. this.group.eachChild(function (child) {
  211. if (child.startIndex != null) {
  212. var len = (child.endIndex - child.startIndex) * 2;
  213. var byteOffset = child.startIndex * 4 * 2;
  214. points = new Float32Array(points.buffer, byteOffset, len);
  215. }
  216. child.setShape('points', points); // Reset draw cursor.
  217. child.reset();
  218. });
  219. };
  220. LargeSymbolDraw.prototype.incrementalPrepareUpdate = function (data) {
  221. this._clear();
  222. };
  223. LargeSymbolDraw.prototype.incrementalUpdate = function (taskParams, data, opt) {
  224. var lastAdded = this._newAdded[0];
  225. var points = data.getLayout('points');
  226. var oldPoints = lastAdded && lastAdded.shape.points; // Merging the exists. Each element has 1e4 points.
  227. // Consider the performance balance between too much elements and too much points in one shape(may affect hover optimization)
  228. if (oldPoints && oldPoints.length < 2e4) {
  229. var oldLen = oldPoints.length;
  230. var newPoints = new Float32Array(oldLen + points.length); // Concat two array
  231. newPoints.set(oldPoints);
  232. newPoints.set(points, oldLen); // Update endIndex
  233. lastAdded.endIndex = taskParams.end;
  234. lastAdded.setShape({
  235. points: newPoints
  236. });
  237. } else {
  238. // Clear
  239. this._newAdded = [];
  240. var symbolEl = this._create();
  241. symbolEl.startIndex = taskParams.start;
  242. symbolEl.endIndex = taskParams.end;
  243. symbolEl.incremental = true;
  244. symbolEl.setShape({
  245. points: points
  246. });
  247. this._setCommon(symbolEl, data, opt);
  248. }
  249. };
  250. LargeSymbolDraw.prototype.eachRendered = function (cb) {
  251. this._newAdded[0] && cb(this._newAdded[0]);
  252. };
  253. LargeSymbolDraw.prototype._create = function () {
  254. var symbolEl = new LargeSymbolPath({
  255. cursor: 'default'
  256. });
  257. symbolEl.ignoreCoarsePointer = true;
  258. this.group.add(symbolEl);
  259. this._newAdded.push(symbolEl);
  260. return symbolEl;
  261. };
  262. LargeSymbolDraw.prototype._setCommon = function (symbolEl, data, opt) {
  263. var hostModel = data.hostModel;
  264. opt = opt || {};
  265. var size = data.getVisual('symbolSize');
  266. symbolEl.setShape('size', size instanceof Array ? size : [size, size]);
  267. symbolEl.softClipShape = opt.clipShape || null; // Create symbolProxy to build path for each data
  268. symbolEl.symbolProxy = createSymbol(data.getVisual('symbol'), 0, 0, 0, 0); // Use symbolProxy setColor method
  269. symbolEl.setColor = symbolEl.symbolProxy.setColor;
  270. var extrudeShadow = symbolEl.shape.size[0] < BOOST_SIZE_THRESHOLD;
  271. symbolEl.useStyle( // Draw shadow when doing fillRect is extremely slow.
  272. hostModel.getModel('itemStyle').getItemStyle(extrudeShadow ? ['color', 'shadowBlur', 'shadowColor'] : ['color']));
  273. var globalStyle = data.getVisual('style');
  274. var visualColor = globalStyle && globalStyle.fill;
  275. if (visualColor) {
  276. symbolEl.setColor(visualColor);
  277. }
  278. var ecData = getECData(symbolEl); // Enable tooltip
  279. // PENDING May have performance issue when path is extremely large
  280. ecData.seriesIndex = hostModel.seriesIndex;
  281. symbolEl.on('mousemove', function (e) {
  282. ecData.dataIndex = null;
  283. var dataIndex = symbolEl.hoverDataIdx;
  284. if (dataIndex >= 0) {
  285. // Provide dataIndex for tooltip
  286. ecData.dataIndex = dataIndex + (symbolEl.startIndex || 0);
  287. }
  288. });
  289. };
  290. LargeSymbolDraw.prototype.remove = function () {
  291. this._clear();
  292. };
  293. LargeSymbolDraw.prototype._clear = function () {
  294. this._newAdded = [];
  295. this.group.removeAll();
  296. };
  297. return LargeSymbolDraw;
  298. }();
  299. export default LargeSymbolDraw;