LargeLineDraw.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. var graphic = require("../../util/graphic");
  20. var IncrementalDisplayable = require("zrender/lib/graphic/IncrementalDisplayable");
  21. var lineContain = require("zrender/lib/contain/line");
  22. var quadraticContain = require("zrender/lib/contain/quadratic");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. // TODO Batch by color
  42. var LargeLineShape = graphic.extendShape({
  43. shape: {
  44. polyline: false,
  45. curveness: 0,
  46. segs: []
  47. },
  48. buildPath: function (path, shape) {
  49. var segs = shape.segs;
  50. var curveness = shape.curveness;
  51. if (shape.polyline) {
  52. for (var i = 0; i < segs.length;) {
  53. var count = segs[i++];
  54. if (count > 0) {
  55. path.moveTo(segs[i++], segs[i++]);
  56. for (var k = 1; k < count; k++) {
  57. path.lineTo(segs[i++], segs[i++]);
  58. }
  59. }
  60. }
  61. } else {
  62. for (var i = 0; i < segs.length;) {
  63. var x0 = segs[i++];
  64. var y0 = segs[i++];
  65. var x1 = segs[i++];
  66. var y1 = segs[i++];
  67. path.moveTo(x0, y0);
  68. if (curveness > 0) {
  69. var x2 = (x0 + x1) / 2 - (y0 - y1) * curveness;
  70. var y2 = (y0 + y1) / 2 - (x1 - x0) * curveness;
  71. path.quadraticCurveTo(x2, y2, x1, y1);
  72. } else {
  73. path.lineTo(x1, y1);
  74. }
  75. }
  76. }
  77. },
  78. findDataIndex: function (x, y) {
  79. var shape = this.shape;
  80. var segs = shape.segs;
  81. var curveness = shape.curveness;
  82. if (shape.polyline) {
  83. var dataIndex = 0;
  84. for (var i = 0; i < segs.length;) {
  85. var count = segs[i++];
  86. if (count > 0) {
  87. var x0 = segs[i++];
  88. var y0 = segs[i++];
  89. for (var k = 1; k < count; k++) {
  90. var x1 = segs[i++];
  91. var y1 = segs[i++];
  92. if (lineContain.containStroke(x0, y0, x1, y1)) {
  93. return dataIndex;
  94. }
  95. }
  96. }
  97. dataIndex++;
  98. }
  99. } else {
  100. var dataIndex = 0;
  101. for (var i = 0; i < segs.length;) {
  102. var x0 = segs[i++];
  103. var y0 = segs[i++];
  104. var x1 = segs[i++];
  105. var y1 = segs[i++];
  106. if (curveness > 0) {
  107. var x2 = (x0 + x1) / 2 - (y0 - y1) * curveness;
  108. var y2 = (y0 + y1) / 2 - (x1 - x0) * curveness;
  109. if (quadraticContain.containStroke(x0, y0, x2, y2, x1, y1)) {
  110. return dataIndex;
  111. }
  112. } else {
  113. if (lineContain.containStroke(x0, y0, x1, y1)) {
  114. return dataIndex;
  115. }
  116. }
  117. dataIndex++;
  118. }
  119. }
  120. return -1;
  121. }
  122. });
  123. function LargeLineDraw() {
  124. this.group = new graphic.Group();
  125. }
  126. var largeLineProto = LargeLineDraw.prototype;
  127. largeLineProto.isPersistent = function () {
  128. return !this._incremental;
  129. };
  130. /**
  131. * Update symbols draw by new data
  132. * @param {module:echarts/data/List} data
  133. */
  134. largeLineProto.updateData = function (data) {
  135. this.group.removeAll();
  136. var lineEl = new LargeLineShape({
  137. rectHover: true,
  138. cursor: 'default'
  139. });
  140. lineEl.setShape({
  141. segs: data.getLayout('linesPoints')
  142. });
  143. this._setCommon(lineEl, data); // Add back
  144. this.group.add(lineEl);
  145. this._incremental = null;
  146. };
  147. /**
  148. * @override
  149. */
  150. largeLineProto.incrementalPrepareUpdate = function (data) {
  151. this.group.removeAll();
  152. this._clearIncremental();
  153. if (data.count() > 5e5) {
  154. if (!this._incremental) {
  155. this._incremental = new IncrementalDisplayable({
  156. silent: true
  157. });
  158. }
  159. this.group.add(this._incremental);
  160. } else {
  161. this._incremental = null;
  162. }
  163. };
  164. /**
  165. * @override
  166. */
  167. largeLineProto.incrementalUpdate = function (taskParams, data) {
  168. var lineEl = new LargeLineShape();
  169. lineEl.setShape({
  170. segs: data.getLayout('linesPoints')
  171. });
  172. this._setCommon(lineEl, data, !!this._incremental);
  173. if (!this._incremental) {
  174. lineEl.rectHover = true;
  175. lineEl.cursor = 'default';
  176. lineEl.__startIndex = taskParams.start;
  177. this.group.add(lineEl);
  178. } else {
  179. this._incremental.addDisplayable(lineEl, true);
  180. }
  181. };
  182. /**
  183. * @override
  184. */
  185. largeLineProto.remove = function () {
  186. this._clearIncremental();
  187. this._incremental = null;
  188. this.group.removeAll();
  189. };
  190. largeLineProto._setCommon = function (lineEl, data, isIncremental) {
  191. var hostModel = data.hostModel;
  192. lineEl.setShape({
  193. polyline: hostModel.get('polyline'),
  194. curveness: hostModel.get('lineStyle.curveness')
  195. });
  196. lineEl.useStyle(hostModel.getModel('lineStyle').getLineStyle());
  197. lineEl.style.strokeNoScale = true;
  198. var visualColor = data.getVisual('color');
  199. if (visualColor) {
  200. lineEl.setStyle('stroke', visualColor);
  201. }
  202. lineEl.setStyle('fill');
  203. if (!isIncremental) {
  204. // Enable tooltip
  205. // PENDING May have performance issue when path is extremely large
  206. lineEl.seriesIndex = hostModel.seriesIndex;
  207. lineEl.on('mousemove', function (e) {
  208. lineEl.dataIndex = null;
  209. var dataIndex = lineEl.findDataIndex(e.offsetX, e.offsetY);
  210. if (dataIndex > 0) {
  211. // Provide dataIndex for tooltip
  212. lineEl.dataIndex = dataIndex + lineEl.__startIndex;
  213. }
  214. });
  215. }
  216. };
  217. largeLineProto._clearIncremental = function () {
  218. var incremental = this._incremental;
  219. if (incremental) {
  220. incremental.clearDisplaybles();
  221. }
  222. };
  223. var _default = LargeLineDraw;
  224. module.exports = _default;