SymbolDraw.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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';
  41. import SymbolClz from './Symbol';
  42. import { isObject } from 'zrender/lib/core/util';
  43. import { getLabelStatesModels } from '../../label/labelStyle';
  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. hoverScale: emphasisModel.get('scale'),
  68. labelStatesModels: getLabelStatesModels(seriesModel),
  69. cursorStyle: seriesModel.get('cursor')
  70. };
  71. }
  72. var SymbolDraw =
  73. /** @class */
  74. 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. opt = normalizeUpdateOpt(opt);
  84. var group = this.group;
  85. var seriesModel = data.hostModel;
  86. var oldData = this._data;
  87. var SymbolCtor = this._SymbolCtor;
  88. var disableAnimation = opt.disableAnimation;
  89. var seriesScope = makeSeriesScope(data);
  90. var symbolUpdateOpt = {
  91. disableAnimation: disableAnimation
  92. };
  93. var getSymbolPoint = opt.getSymbolPoint || function (idx) {
  94. return data.getItemLayout(idx);
  95. }; // There is no oldLineData only when first rendering or switching from
  96. // stream mode to normal mode, where previous elements should be removed.
  97. if (!oldData) {
  98. group.removeAll();
  99. }
  100. data.diff(oldData).add(function (newIdx) {
  101. var point = getSymbolPoint(newIdx);
  102. if (symbolNeedsDraw(data, point, newIdx, opt)) {
  103. var symbolEl = new SymbolCtor(data, newIdx, seriesScope, symbolUpdateOpt);
  104. symbolEl.setPosition(point);
  105. data.setItemGraphicEl(newIdx, symbolEl);
  106. group.add(symbolEl);
  107. }
  108. }).update(function (newIdx, oldIdx) {
  109. var symbolEl = oldData.getItemGraphicEl(oldIdx);
  110. var point = getSymbolPoint(newIdx);
  111. if (!symbolNeedsDraw(data, point, newIdx, opt)) {
  112. group.remove(symbolEl);
  113. return;
  114. }
  115. var newSymbolType = data.getItemVisual(newIdx, 'symbol') || 'circle';
  116. var oldSymbolType = symbolEl && symbolEl.getSymbolType && symbolEl.getSymbolType();
  117. if (!symbolEl // Create a new if symbol type changed.
  118. || oldSymbolType && oldSymbolType !== newSymbolType) {
  119. group.remove(symbolEl);
  120. symbolEl = new SymbolCtor(data, newIdx, seriesScope, symbolUpdateOpt);
  121. symbolEl.setPosition(point);
  122. } else {
  123. symbolEl.updateData(data, newIdx, seriesScope, symbolUpdateOpt);
  124. var target = {
  125. x: point[0],
  126. y: point[1]
  127. };
  128. disableAnimation ? symbolEl.attr(target) : graphic.updateProps(symbolEl, target, seriesModel);
  129. } // Add back
  130. group.add(symbolEl);
  131. data.setItemGraphicEl(newIdx, symbolEl);
  132. }).remove(function (oldIdx) {
  133. var el = oldData.getItemGraphicEl(oldIdx);
  134. el && el.fadeOut(function () {
  135. group.remove(el);
  136. });
  137. }).execute();
  138. this._getSymbolPoint = getSymbolPoint;
  139. this._data = data;
  140. };
  141. ;
  142. SymbolDraw.prototype.isPersistent = function () {
  143. return true;
  144. };
  145. ;
  146. SymbolDraw.prototype.updateLayout = function () {
  147. var _this = this;
  148. var data = this._data;
  149. if (data) {
  150. // Not use animation
  151. data.eachItemGraphicEl(function (el, idx) {
  152. var point = _this._getSymbolPoint(idx);
  153. el.setPosition(point);
  154. el.markRedraw();
  155. });
  156. }
  157. };
  158. ;
  159. SymbolDraw.prototype.incrementalPrepareUpdate = function (data) {
  160. this._seriesScope = makeSeriesScope(data);
  161. this._data = null;
  162. this.group.removeAll();
  163. };
  164. ;
  165. /**
  166. * Update symbols draw by new data
  167. */
  168. SymbolDraw.prototype.incrementalUpdate = function (taskParams, data, opt) {
  169. opt = normalizeUpdateOpt(opt);
  170. function updateIncrementalAndHover(el) {
  171. if (!el.isGroup) {
  172. el.incremental = true;
  173. el.ensureState('emphasis').hoverLayer = true;
  174. }
  175. }
  176. for (var idx = taskParams.start; idx < taskParams.end; idx++) {
  177. var point = data.getItemLayout(idx);
  178. if (symbolNeedsDraw(data, point, idx, opt)) {
  179. var el = new this._SymbolCtor(data, idx, this._seriesScope);
  180. el.traverse(updateIncrementalAndHover);
  181. el.setPosition(point);
  182. this.group.add(el);
  183. data.setItemGraphicEl(idx, el);
  184. }
  185. }
  186. };
  187. ;
  188. SymbolDraw.prototype.remove = function (enableAnimation) {
  189. var group = this.group;
  190. var data = this._data; // Incremental model do not have this._data.
  191. if (data && enableAnimation) {
  192. data.eachItemGraphicEl(function (el) {
  193. el.fadeOut(function () {
  194. group.remove(el);
  195. });
  196. });
  197. } else {
  198. group.removeAll();
  199. }
  200. };
  201. ;
  202. return SymbolDraw;
  203. }();
  204. export default SymbolDraw;