LineDraw.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var graphic = require("../../util/graphic");
  2. var LineGroup = require("./Line");
  3. /**
  4. * @module echarts/chart/helper/LineDraw
  5. */
  6. function isPointNaN(pt) {
  7. return isNaN(pt[0]) || isNaN(pt[1]);
  8. }
  9. function lineNeedsDraw(pts) {
  10. return !isPointNaN(pts[0]) && !isPointNaN(pts[1]);
  11. }
  12. /**
  13. * @alias module:echarts/component/marker/LineDraw
  14. * @constructor
  15. */
  16. function LineDraw(ctor) {
  17. this._ctor = ctor || LineGroup;
  18. this.group = new graphic.Group();
  19. }
  20. var lineDrawProto = LineDraw.prototype;
  21. /**
  22. * @param {module:echarts/data/List} lineData
  23. */
  24. lineDrawProto.updateData = function (lineData) {
  25. var oldLineData = this._lineData;
  26. var group = this.group;
  27. var LineCtor = this._ctor;
  28. var hostModel = lineData.hostModel;
  29. var seriesScope = {
  30. lineStyle: hostModel.getModel('lineStyle.normal').getLineStyle(),
  31. hoverLineStyle: hostModel.getModel('lineStyle.emphasis').getLineStyle(),
  32. labelModel: hostModel.getModel('label.normal'),
  33. hoverLabelModel: hostModel.getModel('label.emphasis')
  34. };
  35. lineData.diff(oldLineData).add(function (idx) {
  36. if (!lineNeedsDraw(lineData.getItemLayout(idx))) {
  37. return;
  38. }
  39. var lineGroup = new LineCtor(lineData, idx, seriesScope);
  40. lineData.setItemGraphicEl(idx, lineGroup);
  41. group.add(lineGroup);
  42. }).update(function (newIdx, oldIdx) {
  43. var lineGroup = oldLineData.getItemGraphicEl(oldIdx);
  44. if (!lineNeedsDraw(lineData.getItemLayout(newIdx))) {
  45. group.remove(lineGroup);
  46. return;
  47. }
  48. if (!lineGroup) {
  49. lineGroup = new LineCtor(lineData, newIdx, seriesScope);
  50. } else {
  51. lineGroup.updateData(lineData, newIdx, seriesScope);
  52. }
  53. lineData.setItemGraphicEl(newIdx, lineGroup);
  54. group.add(lineGroup);
  55. }).remove(function (idx) {
  56. group.remove(oldLineData.getItemGraphicEl(idx));
  57. }).execute();
  58. this._lineData = lineData;
  59. };
  60. lineDrawProto.updateLayout = function () {
  61. var lineData = this._lineData;
  62. lineData.eachItemGraphicEl(function (el, idx) {
  63. el.updateLayout(lineData, idx);
  64. }, this);
  65. };
  66. lineDrawProto.remove = function () {
  67. this.group.removeAll();
  68. };
  69. var _default = LineDraw;
  70. module.exports = _default;