SymbolDraw.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 && 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 && 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. function createEl(SymbolCtor, data, newIdx, seriesScope, symbolUpdateOpt, point, group) {
  75. var symbolEl = new SymbolCtor(data, newIdx, seriesScope, symbolUpdateOpt);
  76. symbolEl.setPosition(point);
  77. data.setItemGraphicEl(newIdx, symbolEl);
  78. group.add(symbolEl);
  79. return symbolEl;
  80. }
  81. var SymbolDraw = /** @class */function () {
  82. function SymbolDraw(SymbolCtor) {
  83. this.group = new graphic.Group();
  84. this._SymbolCtor = SymbolCtor || SymbolClz;
  85. }
  86. /**
  87. * Update symbols draw by new data
  88. */
  89. SymbolDraw.prototype.updateData = function (data, opt) {
  90. // Remove progressive els.
  91. this._progressiveEls = null;
  92. opt = normalizeUpdateOpt(opt);
  93. var group = this.group;
  94. var seriesModel = data.hostModel;
  95. var oldData = this._data;
  96. var SymbolCtor = this._SymbolCtor;
  97. var disableAnimation = opt.disableAnimation;
  98. var seriesScope = this._seriesScope = makeSeriesScope(data);
  99. var symbolUpdateOpt = {
  100. disableAnimation: disableAnimation
  101. };
  102. var getSymbolPoint = opt.getSymbolPoint || function (idx) {
  103. return data.getItemLayout(idx);
  104. };
  105. // There is no oldLineData only when first rendering or switching from
  106. // stream mode to normal mode, where previous elements should be removed.
  107. if (!oldData) {
  108. group.removeAll();
  109. }
  110. data.diff(oldData).add(function (newIdx) {
  111. var point = getSymbolPoint(newIdx);
  112. if (symbolNeedsDraw(data, point, newIdx, opt)) {
  113. createEl(SymbolCtor, data, newIdx, seriesScope, symbolUpdateOpt, point, group);
  114. }
  115. }).update(function (newIdx, oldIdx) {
  116. var symbolEl = oldData.getItemGraphicEl(oldIdx);
  117. var point = getSymbolPoint(newIdx);
  118. if (!symbolNeedsDraw(data, point, newIdx, opt)) {
  119. group.remove(symbolEl);
  120. return;
  121. }
  122. var newSymbolType = data.getItemVisual(newIdx, 'symbol') || 'circle';
  123. var oldSymbolType = symbolEl && symbolEl.getSymbolType && symbolEl.getSymbolType();
  124. if (!symbolEl
  125. // Create a new if symbol type changed.
  126. || oldSymbolType && oldSymbolType !== newSymbolType) {
  127. group.remove(symbolEl);
  128. symbolEl = new SymbolCtor(data, newIdx, seriesScope, symbolUpdateOpt);
  129. symbolEl.setPosition(point);
  130. } else {
  131. symbolEl.updateData(data, newIdx, seriesScope, symbolUpdateOpt);
  132. var target = {
  133. x: point[0],
  134. y: point[1]
  135. };
  136. disableAnimation ? symbolEl.attr(target) : graphic.updateProps(symbolEl, target, seriesModel);
  137. }
  138. // Add back
  139. group.add(symbolEl);
  140. data.setItemGraphicEl(newIdx, symbolEl);
  141. }).remove(function (oldIdx) {
  142. var el = oldData.getItemGraphicEl(oldIdx);
  143. el && el.fadeOut(function () {
  144. group.remove(el);
  145. }, seriesModel);
  146. }).execute();
  147. this._getSymbolPoint = getSymbolPoint;
  148. this._data = data;
  149. };
  150. ;
  151. SymbolDraw.prototype.updateLayout = function (opt) {
  152. var data = this._data;
  153. if (!data) {
  154. return;
  155. }
  156. var symbolDraw = this;
  157. var store = data.getStore();
  158. for (var idx = 0, len = store.count(); idx < len; idx++) {
  159. var el = data.getItemGraphicEl(idx);
  160. var point = symbolDraw._getSymbolPoint(idx);
  161. // FIXME: [FIXME_SYMBOL_CLIP_CONSIDERING_COORD_SYS_ALIGNMENT_DURING_ANIMATION]
  162. // See VIEW_COORD_SYS animation (by center/zoom change) and FIXME_VIEW_COORD_SYS_SYNC_BACK.
  163. // If we create symbols when roaming them into the clip area, they can not be laid out
  164. // based on the intermediate state but can only based on the final state of VIEW_COORD_SYS,
  165. // and introduce visual artifacts.
  166. // Consider clip, need to handle create or remove.
  167. if (symbolNeedsDraw(data, point, idx, opt)) {
  168. el = el || createEl(symbolDraw._SymbolCtor, data, idx, symbolDraw._seriesScope, {
  169. disableAnimation: true
  170. }, point, symbolDraw.group);
  171. el.stopAnimation();
  172. el.setPosition(point);
  173. el.markRedraw();
  174. } else if (el) {
  175. symbolDraw.group.remove(el);
  176. data.setItemGraphicEl(idx, null);
  177. }
  178. }
  179. };
  180. ;
  181. SymbolDraw.prototype.incrementalPrepareUpdate = function (data) {
  182. this._seriesScope = makeSeriesScope(data);
  183. this._data = null;
  184. this.group.removeAll();
  185. };
  186. ;
  187. SymbolDraw.prototype.incrementalUpdate = function (taskParams, data, incrementalId, opt) {
  188. // Clear
  189. this._progressiveEls = [];
  190. opt = normalizeUpdateOpt(opt);
  191. function updateIncrementalAndHover(el) {
  192. if (!el.isGroup) {
  193. el.incremental = incrementalId;
  194. el.ensureState('emphasis').hoverLayer = graphic.HOVER_LAYER_FOR_INCREMENTAL;
  195. }
  196. }
  197. for (var idx = taskParams.start; idx < taskParams.end; idx++) {
  198. var point = data.getItemLayout(idx);
  199. if (symbolNeedsDraw(data, point, idx, opt)) {
  200. var el = new this._SymbolCtor(data, idx, this._seriesScope);
  201. el.traverse(updateIncrementalAndHover);
  202. el.setPosition(point);
  203. this.group.add(el);
  204. data.setItemGraphicEl(idx, el);
  205. this._progressiveEls.push(el);
  206. }
  207. }
  208. };
  209. ;
  210. SymbolDraw.prototype.eachRendered = function (cb) {
  211. graphic.traverseElements(this._progressiveEls || this.group, cb);
  212. };
  213. SymbolDraw.prototype.remove = function (enableAnimation) {
  214. var group = this.group;
  215. var data = this._data;
  216. // Incremental model do not have this._data.
  217. if (data && enableAnimation) {
  218. data.eachItemGraphicEl(function (el) {
  219. el.fadeOut(function () {
  220. group.remove(el);
  221. }, data.hostModel);
  222. });
  223. } else {
  224. group.removeAll();
  225. }
  226. };
  227. ;
  228. return SymbolDraw;
  229. }();
  230. export default SymbolDraw;