LineDraw.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 LineGroup = require("./Line");
  21. /*
  22. * Licensed to the Apache Software Foundation (ASF) under one
  23. * or more contributor license agreements. See the NOTICE file
  24. * distributed with this work for additional information
  25. * regarding copyright ownership. The ASF licenses this file
  26. * to you under the Apache License, Version 2.0 (the
  27. * "License"); you may not use this file except in compliance
  28. * with the License. You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing,
  33. * software distributed under the License is distributed on an
  34. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  35. * KIND, either express or implied. See the License for the
  36. * specific language governing permissions and limitations
  37. * under the License.
  38. */
  39. /**
  40. * @module echarts/chart/helper/LineDraw
  41. */
  42. // import IncrementalDisplayable from 'zrender/src/graphic/IncrementalDisplayable';
  43. /**
  44. * @alias module:echarts/component/marker/LineDraw
  45. * @constructor
  46. */
  47. function LineDraw(ctor) {
  48. this._ctor = ctor || LineGroup;
  49. this.group = new graphic.Group();
  50. }
  51. var lineDrawProto = LineDraw.prototype;
  52. lineDrawProto.isPersistent = function () {
  53. return true;
  54. };
  55. /**
  56. * @param {module:echarts/data/List} lineData
  57. */
  58. lineDrawProto.updateData = function (lineData) {
  59. var lineDraw = this;
  60. var group = lineDraw.group;
  61. var oldLineData = lineDraw._lineData;
  62. lineDraw._lineData = lineData; // There is no oldLineData only when first rendering or switching from
  63. // stream mode to normal mode, where previous elements should be removed.
  64. if (!oldLineData) {
  65. group.removeAll();
  66. }
  67. var seriesScope = makeSeriesScope(lineData);
  68. lineData.diff(oldLineData).add(function (idx) {
  69. doAdd(lineDraw, lineData, idx, seriesScope);
  70. }).update(function (newIdx, oldIdx) {
  71. doUpdate(lineDraw, oldLineData, lineData, oldIdx, newIdx, seriesScope);
  72. }).remove(function (idx) {
  73. group.remove(oldLineData.getItemGraphicEl(idx));
  74. }).execute();
  75. };
  76. function doAdd(lineDraw, lineData, idx, seriesScope) {
  77. var itemLayout = lineData.getItemLayout(idx);
  78. if (!lineNeedsDraw(itemLayout)) {
  79. return;
  80. }
  81. var el = new lineDraw._ctor(lineData, idx, seriesScope);
  82. lineData.setItemGraphicEl(idx, el);
  83. lineDraw.group.add(el);
  84. }
  85. function doUpdate(lineDraw, oldLineData, newLineData, oldIdx, newIdx, seriesScope) {
  86. var itemEl = oldLineData.getItemGraphicEl(oldIdx);
  87. if (!lineNeedsDraw(newLineData.getItemLayout(newIdx))) {
  88. lineDraw.group.remove(itemEl);
  89. return;
  90. }
  91. if (!itemEl) {
  92. itemEl = new lineDraw._ctor(newLineData, newIdx, seriesScope);
  93. } else {
  94. itemEl.updateData(newLineData, newIdx, seriesScope);
  95. }
  96. newLineData.setItemGraphicEl(newIdx, itemEl);
  97. lineDraw.group.add(itemEl);
  98. }
  99. lineDrawProto.updateLayout = function () {
  100. var lineData = this._lineData; // Do not support update layout in incremental mode.
  101. if (!lineData) {
  102. return;
  103. }
  104. lineData.eachItemGraphicEl(function (el, idx) {
  105. el.updateLayout(lineData, idx);
  106. }, this);
  107. };
  108. lineDrawProto.incrementalPrepareUpdate = function (lineData) {
  109. this._seriesScope = makeSeriesScope(lineData);
  110. this._lineData = null;
  111. this.group.removeAll();
  112. };
  113. function isEffectObject(el) {
  114. return el.animators && el.animators.length > 0;
  115. }
  116. lineDrawProto.incrementalUpdate = function (taskParams, lineData) {
  117. function updateIncrementalAndHover(el) {
  118. if (!el.isGroup && !isEffectObject(el)) {
  119. el.incremental = el.useHoverLayer = true;
  120. }
  121. }
  122. for (var idx = taskParams.start; idx < taskParams.end; idx++) {
  123. var itemLayout = lineData.getItemLayout(idx);
  124. if (lineNeedsDraw(itemLayout)) {
  125. var el = new this._ctor(lineData, idx, this._seriesScope);
  126. el.traverse(updateIncrementalAndHover);
  127. this.group.add(el);
  128. lineData.setItemGraphicEl(idx, el);
  129. }
  130. }
  131. };
  132. function makeSeriesScope(lineData) {
  133. var hostModel = lineData.hostModel;
  134. return {
  135. lineStyle: hostModel.getModel('lineStyle').getLineStyle(),
  136. hoverLineStyle: hostModel.getModel('emphasis.lineStyle').getLineStyle(),
  137. labelModel: hostModel.getModel('label'),
  138. hoverLabelModel: hostModel.getModel('emphasis.label')
  139. };
  140. }
  141. lineDrawProto.remove = function () {
  142. this._clearIncremental();
  143. this._incremental = null;
  144. this.group.removeAll();
  145. };
  146. lineDrawProto._clearIncremental = function () {
  147. var incremental = this._incremental;
  148. if (incremental) {
  149. incremental.clearDisplaybles();
  150. }
  151. };
  152. function isPointNaN(pt) {
  153. return isNaN(pt[0]) || isNaN(pt[1]);
  154. }
  155. function lineNeedsDraw(pts) {
  156. return !isPointNaN(pts[0]) && !isPointNaN(pts[1]);
  157. }
  158. var _default = LineDraw;
  159. module.exports = _default;