SymbolDraw.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 * as graphic from '../../util/graphic.js';
  41. import SymbolClz from './Symbol.js';
  42. import { isObject } from 'zrender/lib/core/util.js';
  43. import { getLabelStatesModels } from '../../label/labelStyle.js';
  44. function symbolNeedsDraw(data, point, idx, opt) {
  45. return point && !isNaN(point[0]) && !isNaN(point[1]) && !(opt.isIgnore && opt.isIgnore(idx))
  46. // We do not set clipShape on group, because it will cut part of
  47. // the symbol element shape. We use the same clip shape here as
  48. // the line clip.
  49. && !(opt.clipShape && !opt.clipShape.contain(point[0], point[1])) && data.getItemVisual(idx, 'symbol') !== 'none';
  50. }
  51. function normalizeUpdateOpt(opt) {
  52. if (opt != null && !isObject(opt)) {
  53. opt = {
  54. isIgnore: opt
  55. };
  56. }
  57. return opt || {};
  58. }
  59. function makeSeriesScope(data) {
  60. var seriesModel = data.hostModel;
  61. var emphasisModel = seriesModel.getModel('emphasis');
  62. return {
  63. emphasisItemStyle: emphasisModel.getModel('itemStyle').getItemStyle(),
  64. blurItemStyle: seriesModel.getModel(['blur', 'itemStyle']).getItemStyle(),
  65. selectItemStyle: seriesModel.getModel(['select', 'itemStyle']).getItemStyle(),
  66. focus: emphasisModel.get('focus'),
  67. blurScope: emphasisModel.get('blurScope'),
  68. emphasisDisabled: emphasisModel.get('disabled'),
  69. hoverScale: emphasisModel.get('scale'),
  70. labelStatesModels: getLabelStatesModels(seriesModel),
  71. cursorStyle: seriesModel.get('cursor')
  72. };
  73. }
  74. var SymbolDraw = /** @class */function () {
  75. function SymbolDraw(SymbolCtor) {
  76. this.group = new graphic.Group();
  77. this._SymbolCtor = SymbolCtor || SymbolClz;
  78. }
  79. /**
  80. * Update symbols draw by new data
  81. */
  82. SymbolDraw.prototype.updateData = function (data, opt) {
  83. // Remove progressive els.
  84. this._progressiveEls = null;
  85. opt = normalizeUpdateOpt(opt);
  86. var group = this.group;
  87. var seriesModel = data.hostModel;
  88. var oldData = this._data;
  89. var SymbolCtor = this._SymbolCtor;
  90. var disableAnimation = opt.disableAnimation;
  91. var seriesScope = makeSeriesScope(data);
  92. var symbolUpdateOpt = {
  93. disableAnimation: disableAnimation
  94. };
  95. var getSymbolPoint = opt.getSymbolPoint || function (idx) {
  96. return data.getItemLayout(idx);
  97. };
  98. // There is no oldLineData only when first rendering or switching from
  99. // stream mode to normal mode, where previous elements should be removed.
  100. if (!oldData) {
  101. group.removeAll();
  102. }
  103. data.diff(oldData).add(function (newIdx) {
  104. var point = getSymbolPoint(newIdx);
  105. if (symbolNeedsDraw(data, point, newIdx, opt)) {
  106. var symbolEl = new SymbolCtor(data, newIdx, seriesScope, symbolUpdateOpt);
  107. symbolEl.setPosition(point);
  108. data.setItemGraphicEl(newIdx, symbolEl);
  109. group.add(symbolEl);
  110. }
  111. }).update(function (newIdx, oldIdx) {
  112. var symbolEl = oldData.getItemGraphicEl(oldIdx);
  113. var point = getSymbolPoint(newIdx);
  114. if (!symbolNeedsDraw(data, point, newIdx, opt)) {
  115. group.remove(symbolEl);
  116. return;
  117. }
  118. var newSymbolType = data.getItemVisual(newIdx, 'symbol') || 'circle';
  119. var oldSymbolType = symbolEl && symbolEl.getSymbolType && symbolEl.getSymbolType();
  120. if (!symbolEl
  121. // Create a new if symbol type changed.
  122. || oldSymbolType && oldSymbolType !== newSymbolType) {
  123. group.remove(symbolEl);
  124. symbolEl = new SymbolCtor(data, newIdx, seriesScope, symbolUpdateOpt);
  125. symbolEl.setPosition(point);
  126. } else {
  127. symbolEl.updateData(data, newIdx, seriesScope, symbolUpdateOpt);
  128. var target = {
  129. x: point[0],
  130. y: point[1]
  131. };
  132. disableAnimation ? symbolEl.attr(target) : graphic.updateProps(symbolEl, target, seriesModel);
  133. }
  134. // Add back
  135. group.add(symbolEl);
  136. data.setItemGraphicEl(newIdx, symbolEl);
  137. }).remove(function (oldIdx) {
  138. var el = oldData.getItemGraphicEl(oldIdx);
  139. el && el.fadeOut(function () {
  140. group.remove(el);
  141. }, seriesModel);
  142. }).execute();
  143. this._getSymbolPoint = getSymbolPoint;
  144. this._data = data;
  145. };
  146. ;
  147. SymbolDraw.prototype.updateLayout = function () {
  148. var _this = this;
  149. var data = this._data;
  150. if (data) {
  151. // Not use animation
  152. data.eachItemGraphicEl(function (el, idx) {
  153. var point = _this._getSymbolPoint(idx);
  154. el.setPosition(point);
  155. el.markRedraw();
  156. });
  157. }
  158. };
  159. ;
  160. SymbolDraw.prototype.incrementalPrepareUpdate = function (data) {
  161. this._seriesScope = makeSeriesScope(data);
  162. this._data = null;
  163. this.group.removeAll();
  164. };
  165. ;
  166. /**
  167. * Update symbols draw by new data
  168. */
  169. SymbolDraw.prototype.incrementalUpdate = function (taskParams, data, opt) {
  170. // Clear
  171. this._progressiveEls = [];
  172. opt = normalizeUpdateOpt(opt);
  173. function updateIncrementalAndHover(el) {
  174. if (!el.isGroup) {
  175. el.incremental = true;
  176. el.ensureState('emphasis').hoverLayer = true;
  177. }
  178. }
  179. for (var idx = taskParams.start; idx < taskParams.end; idx++) {
  180. var point = data.getItemLayout(idx);
  181. if (symbolNeedsDraw(data, point, idx, opt)) {
  182. var el = new this._SymbolCtor(data, idx, this._seriesScope);
  183. el.traverse(updateIncrementalAndHover);
  184. el.setPosition(point);
  185. this.group.add(el);
  186. data.setItemGraphicEl(idx, el);
  187. this._progressiveEls.push(el);
  188. }
  189. }
  190. };
  191. ;
  192. SymbolDraw.prototype.eachRendered = function (cb) {
  193. graphic.traverseElements(this._progressiveEls || this.group, cb);
  194. };
  195. SymbolDraw.prototype.remove = function (enableAnimation) {
  196. var group = this.group;
  197. var data = this._data;
  198. // Incremental model do not have this._data.
  199. if (data && enableAnimation) {
  200. data.eachItemGraphicEl(function (el) {
  201. el.fadeOut(function () {
  202. group.remove(el);
  203. }, data.hostModel);
  204. });
  205. } else {
  206. group.removeAll();
  207. }
  208. };
  209. ;
  210. return SymbolDraw;
  211. }();
  212. export default SymbolDraw;