LargeLineDraw.js 9.1 KB

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