SymbolDraw.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var graphic = require("../../util/graphic");
  2. var SymbolClz = require("./Symbol");
  3. /**
  4. * @module echarts/chart/helper/SymbolDraw
  5. */
  6. /**
  7. * @constructor
  8. * @alias module:echarts/chart/helper/SymbolDraw
  9. * @param {module:zrender/graphic/Group} [symbolCtor]
  10. */
  11. function SymbolDraw(symbolCtor) {
  12. this.group = new graphic.Group();
  13. this._symbolCtor = symbolCtor || SymbolClz;
  14. }
  15. var symbolDrawProto = SymbolDraw.prototype;
  16. function symbolNeedsDraw(data, idx, isIgnore) {
  17. var point = data.getItemLayout(idx); // Is an object
  18. // if (point && point.hasOwnProperty('point')) {
  19. // point = point.point;
  20. // }
  21. return point && !isNaN(point[0]) && !isNaN(point[1]) && !(isIgnore && isIgnore(idx)) && data.getItemVisual(idx, 'symbol') !== 'none';
  22. }
  23. /**
  24. * Update symbols draw by new data
  25. * @param {module:echarts/data/List} data
  26. * @param {Array.<boolean>} [isIgnore]
  27. */
  28. symbolDrawProto.updateData = function (data, isIgnore) {
  29. var group = this.group;
  30. var seriesModel = data.hostModel;
  31. var oldData = this._data;
  32. var SymbolCtor = this._symbolCtor;
  33. var seriesScope = {
  34. itemStyle: seriesModel.getModel('itemStyle.normal').getItemStyle(['color']),
  35. hoverItemStyle: seriesModel.getModel('itemStyle.emphasis').getItemStyle(),
  36. symbolRotate: seriesModel.get('symbolRotate'),
  37. symbolOffset: seriesModel.get('symbolOffset'),
  38. hoverAnimation: seriesModel.get('hoverAnimation'),
  39. labelModel: seriesModel.getModel('label.normal'),
  40. hoverLabelModel: seriesModel.getModel('label.emphasis'),
  41. cursorStyle: seriesModel.get('cursor')
  42. };
  43. data.diff(oldData).add(function (newIdx) {
  44. var point = data.getItemLayout(newIdx);
  45. if (symbolNeedsDraw(data, newIdx, isIgnore)) {
  46. var symbolEl = new SymbolCtor(data, newIdx, seriesScope);
  47. symbolEl.attr('position', point);
  48. data.setItemGraphicEl(newIdx, symbolEl);
  49. group.add(symbolEl);
  50. }
  51. }).update(function (newIdx, oldIdx) {
  52. var symbolEl = oldData.getItemGraphicEl(oldIdx);
  53. var point = data.getItemLayout(newIdx);
  54. if (!symbolNeedsDraw(data, newIdx, isIgnore)) {
  55. group.remove(symbolEl);
  56. return;
  57. }
  58. if (!symbolEl) {
  59. symbolEl = new SymbolCtor(data, newIdx);
  60. symbolEl.attr('position', point);
  61. } else {
  62. symbolEl.updateData(data, newIdx, seriesScope);
  63. graphic.updateProps(symbolEl, {
  64. position: point
  65. }, seriesModel);
  66. } // Add back
  67. group.add(symbolEl);
  68. data.setItemGraphicEl(newIdx, symbolEl);
  69. }).remove(function (oldIdx) {
  70. var el = oldData.getItemGraphicEl(oldIdx);
  71. el && el.fadeOut(function () {
  72. group.remove(el);
  73. });
  74. }).execute();
  75. this._data = data;
  76. };
  77. symbolDrawProto.updateLayout = function () {
  78. var data = this._data;
  79. if (data) {
  80. // Not use animation
  81. data.eachItemGraphicEl(function (el, idx) {
  82. var point = data.getItemLayout(idx);
  83. el.attr('position', point);
  84. });
  85. }
  86. };
  87. symbolDrawProto.remove = function (enableAnimation) {
  88. var group = this.group;
  89. var data = this._data;
  90. if (data) {
  91. if (enableAnimation) {
  92. data.eachItemGraphicEl(function (el) {
  93. el.fadeOut(function () {
  94. group.remove(el);
  95. });
  96. });
  97. } else {
  98. group.removeAll();
  99. }
  100. }
  101. };
  102. var _default = SymbolDraw;
  103. module.exports = _default;