SymbolDraw.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 SymbolClz = require("./Symbol");
  21. var _util = require("zrender/lib/core/util");
  22. var isObject = _util.isObject;
  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. /**
  42. * @module echarts/chart/helper/SymbolDraw
  43. */
  44. /**
  45. * @constructor
  46. * @alias module:echarts/chart/helper/SymbolDraw
  47. * @param {module:zrender/graphic/Group} [symbolCtor]
  48. */
  49. function SymbolDraw(symbolCtor) {
  50. this.group = new graphic.Group();
  51. this._symbolCtor = symbolCtor || SymbolClz;
  52. }
  53. var symbolDrawProto = SymbolDraw.prototype;
  54. function symbolNeedsDraw(data, point, idx, opt) {
  55. 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
  56. // the symbol element shape. We use the same clip shape here as
  57. // the line clip.
  58. && !(opt.clipShape && !opt.clipShape.contain(point[0], point[1])) && data.getItemVisual(idx, 'symbol') !== 'none';
  59. }
  60. /**
  61. * Update symbols draw by new data
  62. * @param {module:echarts/data/List} data
  63. * @param {Object} [opt] Or isIgnore
  64. * @param {Function} [opt.isIgnore]
  65. * @param {Object} [opt.clipShape]
  66. */
  67. symbolDrawProto.updateData = function (data, opt) {
  68. opt = normalizeUpdateOpt(opt);
  69. var group = this.group;
  70. var seriesModel = data.hostModel;
  71. var oldData = this._data;
  72. var SymbolCtor = this._symbolCtor;
  73. var seriesScope = makeSeriesScope(data); // There is no oldLineData only when first rendering or switching from
  74. // stream mode to normal mode, where previous elements should be removed.
  75. if (!oldData) {
  76. group.removeAll();
  77. }
  78. data.diff(oldData).add(function (newIdx) {
  79. var point = data.getItemLayout(newIdx);
  80. if (symbolNeedsDraw(data, point, newIdx, opt)) {
  81. var symbolEl = new SymbolCtor(data, newIdx, seriesScope);
  82. symbolEl.attr('position', point);
  83. data.setItemGraphicEl(newIdx, symbolEl);
  84. group.add(symbolEl);
  85. }
  86. }).update(function (newIdx, oldIdx) {
  87. var symbolEl = oldData.getItemGraphicEl(oldIdx);
  88. var point = data.getItemLayout(newIdx);
  89. if (!symbolNeedsDraw(data, point, newIdx, opt)) {
  90. group.remove(symbolEl);
  91. return;
  92. }
  93. if (!symbolEl) {
  94. symbolEl = new SymbolCtor(data, newIdx);
  95. symbolEl.attr('position', point);
  96. } else {
  97. symbolEl.updateData(data, newIdx, seriesScope);
  98. graphic.updateProps(symbolEl, {
  99. position: point
  100. }, seriesModel);
  101. } // Add back
  102. group.add(symbolEl);
  103. data.setItemGraphicEl(newIdx, symbolEl);
  104. }).remove(function (oldIdx) {
  105. var el = oldData.getItemGraphicEl(oldIdx);
  106. el && el.fadeOut(function () {
  107. group.remove(el);
  108. });
  109. }).execute();
  110. this._data = data;
  111. };
  112. symbolDrawProto.isPersistent = function () {
  113. return true;
  114. };
  115. symbolDrawProto.updateLayout = function () {
  116. var data = this._data;
  117. if (data) {
  118. // Not use animation
  119. data.eachItemGraphicEl(function (el, idx) {
  120. var point = data.getItemLayout(idx);
  121. el.attr('position', point);
  122. });
  123. }
  124. };
  125. symbolDrawProto.incrementalPrepareUpdate = function (data) {
  126. this._seriesScope = makeSeriesScope(data);
  127. this._data = null;
  128. this.group.removeAll();
  129. };
  130. /**
  131. * Update symbols draw by new data
  132. * @param {module:echarts/data/List} data
  133. * @param {Object} [opt] Or isIgnore
  134. * @param {Function} [opt.isIgnore]
  135. * @param {Object} [opt.clipShape]
  136. */
  137. symbolDrawProto.incrementalUpdate = function (taskParams, data, opt) {
  138. opt = normalizeUpdateOpt(opt);
  139. function updateIncrementalAndHover(el) {
  140. if (!el.isGroup) {
  141. el.incremental = el.useHoverLayer = true;
  142. }
  143. }
  144. for (var idx = taskParams.start; idx < taskParams.end; idx++) {
  145. var point = data.getItemLayout(idx);
  146. if (symbolNeedsDraw(data, point, idx, opt)) {
  147. var el = new this._symbolCtor(data, idx, this._seriesScope);
  148. el.traverse(updateIncrementalAndHover);
  149. el.attr('position', point);
  150. this.group.add(el);
  151. data.setItemGraphicEl(idx, el);
  152. }
  153. }
  154. };
  155. function normalizeUpdateOpt(opt) {
  156. if (opt != null && !isObject(opt)) {
  157. opt = {
  158. isIgnore: opt
  159. };
  160. }
  161. return opt || {};
  162. }
  163. symbolDrawProto.remove = function (enableAnimation) {
  164. var group = this.group;
  165. var data = this._data; // Incremental model do not have this._data.
  166. if (data && enableAnimation) {
  167. data.eachItemGraphicEl(function (el) {
  168. el.fadeOut(function () {
  169. group.remove(el);
  170. });
  171. });
  172. } else {
  173. group.removeAll();
  174. }
  175. };
  176. function makeSeriesScope(data) {
  177. var seriesModel = data.hostModel;
  178. return {
  179. itemStyle: seriesModel.getModel('itemStyle').getItemStyle(['color']),
  180. hoverItemStyle: seriesModel.getModel('emphasis.itemStyle').getItemStyle(),
  181. symbolRotate: seriesModel.get('symbolRotate'),
  182. symbolOffset: seriesModel.get('symbolOffset'),
  183. hoverAnimation: seriesModel.get('hoverAnimation'),
  184. labelModel: seriesModel.getModel('label'),
  185. hoverLabelModel: seriesModel.getModel('emphasis.label'),
  186. cursorStyle: seriesModel.get('cursor')
  187. };
  188. }
  189. var _default = SymbolDraw;
  190. module.exports = _default;