LargeLineDraw.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. // TODO Batch by color
  42. import * as graphic from '../../util/graphic.js';
  43. import * as lineContain from 'zrender/lib/contain/line.js';
  44. import * as quadraticContain from 'zrender/lib/contain/quadratic.js';
  45. import { getECData } from '../../util/innerStore.js';
  46. var LargeLinesPathShape = /** @class */function () {
  47. function LargeLinesPathShape() {
  48. this.polyline = false;
  49. this.curveness = 0;
  50. this.segs = [];
  51. }
  52. return LargeLinesPathShape;
  53. }();
  54. var LargeLinesPath = /** @class */function (_super) {
  55. __extends(LargeLinesPath, _super);
  56. function LargeLinesPath(opts) {
  57. var _this = _super.call(this, opts) || this;
  58. _this._off = 0;
  59. _this.hoverDataIdx = -1;
  60. return _this;
  61. }
  62. LargeLinesPath.prototype.reset = function () {
  63. this.notClear = false;
  64. this._off = 0;
  65. };
  66. LargeLinesPath.prototype.getDefaultStyle = function () {
  67. return {
  68. stroke: '#000',
  69. fill: null
  70. };
  71. };
  72. LargeLinesPath.prototype.getDefaultShape = function () {
  73. return new LargeLinesPathShape();
  74. };
  75. LargeLinesPath.prototype.buildPath = function (ctx, shape) {
  76. var segs = shape.segs;
  77. var curveness = shape.curveness;
  78. var i;
  79. if (shape.polyline) {
  80. for (i = this._off; i < segs.length;) {
  81. var count = segs[i++];
  82. if (count > 0) {
  83. ctx.moveTo(segs[i++], segs[i++]);
  84. for (var k = 1; k < count; k++) {
  85. ctx.lineTo(segs[i++], segs[i++]);
  86. }
  87. }
  88. }
  89. } else {
  90. for (i = this._off; i < segs.length;) {
  91. var x0 = segs[i++];
  92. var y0 = segs[i++];
  93. var x1 = segs[i++];
  94. var y1 = segs[i++];
  95. ctx.moveTo(x0, y0);
  96. if (curveness > 0) {
  97. var x2 = (x0 + x1) / 2 - (y0 - y1) * curveness;
  98. var y2 = (y0 + y1) / 2 - (x1 - x0) * curveness;
  99. ctx.quadraticCurveTo(x2, y2, x1, y1);
  100. } else {
  101. ctx.lineTo(x1, y1);
  102. }
  103. }
  104. }
  105. if (this.incremental) {
  106. this._off = i;
  107. this.notClear = true;
  108. }
  109. };
  110. LargeLinesPath.prototype.findDataIndex = function (x, y) {
  111. var shape = this.shape;
  112. var segs = shape.segs;
  113. var curveness = shape.curveness;
  114. var lineWidth = this.style.lineWidth;
  115. if (shape.polyline) {
  116. var dataIndex = 0;
  117. for (var i = 0; i < segs.length;) {
  118. var count = segs[i++];
  119. if (count > 0) {
  120. var x0 = segs[i++];
  121. var y0 = segs[i++];
  122. for (var k = 1; k < count; k++) {
  123. var x1 = segs[i++];
  124. var y1 = segs[i++];
  125. if (lineContain.containStroke(x0, y0, x1, y1, lineWidth, x, y)) {
  126. return dataIndex;
  127. }
  128. }
  129. }
  130. dataIndex++;
  131. }
  132. } else {
  133. var dataIndex = 0;
  134. for (var i = 0; i < segs.length;) {
  135. var x0 = segs[i++];
  136. var y0 = segs[i++];
  137. var x1 = segs[i++];
  138. var y1 = segs[i++];
  139. if (curveness > 0) {
  140. var x2 = (x0 + x1) / 2 - (y0 - y1) * curveness;
  141. var y2 = (y0 + y1) / 2 - (x1 - x0) * curveness;
  142. if (quadraticContain.containStroke(x0, y0, x2, y2, x1, y1, lineWidth, x, y)) {
  143. return dataIndex;
  144. }
  145. } else {
  146. if (lineContain.containStroke(x0, y0, x1, y1, lineWidth, x, y)) {
  147. return dataIndex;
  148. }
  149. }
  150. dataIndex++;
  151. }
  152. }
  153. return -1;
  154. };
  155. LargeLinesPath.prototype.contain = function (x, y) {
  156. var localPos = this.transformCoordToLocal(x, y);
  157. var rect = this.getBoundingRect();
  158. x = localPos[0];
  159. y = localPos[1];
  160. if (rect.contain(x, y)) {
  161. // Cache found data index.
  162. var dataIdx = this.hoverDataIdx = this.findDataIndex(x, y);
  163. return dataIdx >= 0;
  164. }
  165. this.hoverDataIdx = -1;
  166. return false;
  167. };
  168. LargeLinesPath.prototype.getBoundingRect = function () {
  169. // Ignore stroke for large symbol draw.
  170. var rect = this._rect;
  171. if (!rect) {
  172. var shape = this.shape;
  173. var points = shape.segs;
  174. var minX = Infinity;
  175. var minY = Infinity;
  176. var maxX = -Infinity;
  177. var maxY = -Infinity;
  178. for (var i = 0; i < points.length;) {
  179. var x = points[i++];
  180. var y = points[i++];
  181. minX = Math.min(x, minX);
  182. maxX = Math.max(x, maxX);
  183. minY = Math.min(y, minY);
  184. maxY = Math.max(y, maxY);
  185. }
  186. rect = this._rect = new graphic.BoundingRect(minX, minY, maxX, maxY);
  187. }
  188. return rect;
  189. };
  190. return LargeLinesPath;
  191. }(graphic.Path);
  192. var LargeLineDraw = /** @class */function () {
  193. function LargeLineDraw() {
  194. this.group = new graphic.Group();
  195. }
  196. /**
  197. * Update symbols draw by new data
  198. */
  199. LargeLineDraw.prototype.updateData = function (data) {
  200. this._clear();
  201. var lineEl = this._create();
  202. lineEl.setShape({
  203. segs: data.getLayout('linesPoints')
  204. });
  205. this._setCommon(lineEl, data);
  206. };
  207. ;
  208. /**
  209. * @override
  210. */
  211. LargeLineDraw.prototype.incrementalPrepareUpdate = function (data) {
  212. this.group.removeAll();
  213. this._clear();
  214. };
  215. ;
  216. /**
  217. * @override
  218. */
  219. LargeLineDraw.prototype.incrementalUpdate = function (taskParams, data) {
  220. var lastAdded = this._newAdded[0];
  221. var linePoints = data.getLayout('linesPoints');
  222. var oldSegs = lastAdded && lastAdded.shape.segs;
  223. // Merging the exists. Each element has 1e4 points.
  224. // Consider the performance balance between too much elements and too much points in one shape(may affect hover optimization)
  225. if (oldSegs && oldSegs.length < 2e4) {
  226. var oldLen = oldSegs.length;
  227. var newSegs = new Float32Array(oldLen + linePoints.length);
  228. // Concat two array
  229. newSegs.set(oldSegs);
  230. newSegs.set(linePoints, oldLen);
  231. lastAdded.setShape({
  232. segs: newSegs
  233. });
  234. } else {
  235. // Clear
  236. this._newAdded = [];
  237. var lineEl = this._create();
  238. lineEl.incremental = true;
  239. lineEl.setShape({
  240. segs: linePoints
  241. });
  242. this._setCommon(lineEl, data);
  243. lineEl.__startIndex = taskParams.start;
  244. }
  245. };
  246. /**
  247. * @override
  248. */
  249. LargeLineDraw.prototype.remove = function () {
  250. this._clear();
  251. };
  252. LargeLineDraw.prototype.eachRendered = function (cb) {
  253. this._newAdded[0] && cb(this._newAdded[0]);
  254. };
  255. LargeLineDraw.prototype._create = function () {
  256. var lineEl = new LargeLinesPath({
  257. cursor: 'default',
  258. ignoreCoarsePointer: true
  259. });
  260. this._newAdded.push(lineEl);
  261. this.group.add(lineEl);
  262. return lineEl;
  263. };
  264. LargeLineDraw.prototype._setCommon = function (lineEl, data, isIncremental) {
  265. var hostModel = data.hostModel;
  266. lineEl.setShape({
  267. polyline: hostModel.get('polyline'),
  268. curveness: hostModel.get(['lineStyle', 'curveness'])
  269. });
  270. lineEl.useStyle(hostModel.getModel('lineStyle').getLineStyle());
  271. lineEl.style.strokeNoScale = true;
  272. var style = data.getVisual('style');
  273. if (style && style.stroke) {
  274. lineEl.setStyle('stroke', style.stroke);
  275. }
  276. lineEl.setStyle('fill', null);
  277. var ecData = getECData(lineEl);
  278. // Enable tooltip
  279. // PENDING May have performance issue when path is extremely large
  280. ecData.seriesIndex = hostModel.seriesIndex;
  281. lineEl.on('mousemove', function (e) {
  282. ecData.dataIndex = null;
  283. var dataIndex = lineEl.hoverDataIdx;
  284. if (dataIndex > 0) {
  285. // Provide dataIndex for tooltip
  286. ecData.dataIndex = dataIndex + lineEl.__startIndex;
  287. }
  288. });
  289. };
  290. ;
  291. LargeLineDraw.prototype._clear = function () {
  292. this._newAdded = [];
  293. this.group.removeAll();
  294. };
  295. ;
  296. return LargeLineDraw;
  297. }();
  298. export default LargeLineDraw;