SymbolDraw.js 7.5 KB

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