area.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _smooth = require("../../graphic/util/smooth");
  5. var _bbox = require("../../graphic/util/bbox");
  6. var _global = _interopRequireDefault(require("../../global"));
  7. var _shape = _interopRequireDefault(require("./shape"));
  8. var _common = require("../../util/common");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  10. function equals(v1, v2) {
  11. return Math.abs(v1 - v2) < 0.00001;
  12. }
  13. function notEmpty(value) {
  14. return !isNaN(value) && !(0, _common.isNil)(value);
  15. }
  16. function filterPoints(points) {
  17. var filteredPoints = []; // filter the point which x or y is NaN
  18. for (var i = 0, len = points.length; i < len; i++) {
  19. var point = points[i];
  20. if (notEmpty(point.x) && notEmpty(point.y)) {
  21. filteredPoints.push(point);
  22. }
  23. }
  24. return filteredPoints;
  25. }
  26. function equalsCenter(points, center) {
  27. var eqls = true;
  28. (0, _common.each)(points, function (point) {
  29. if (!equals(point.x, center.x) || !equals(point.y, center.y)) {
  30. eqls = false;
  31. return false;
  32. }
  33. });
  34. return eqls;
  35. }
  36. function drawRectShape(topPoints, bottomPoints, container, style, isSmooth) {
  37. var shape;
  38. var points = topPoints.concat(bottomPoints);
  39. if (isSmooth) {
  40. shape = container.addShape('Custom', {
  41. className: 'area',
  42. attrs: (0, _common.mix)({
  43. points: points
  44. }, style),
  45. createPath: function createPath(context) {
  46. var constaint = [[0, 0], [1, 1]];
  47. var points = filterPoints(this._attrs.attrs.points);
  48. var pointsLen = points.length;
  49. var topPoints = points.slice(0, pointsLen / 2);
  50. var bottomPoints = points.slice(pointsLen / 2, pointsLen);
  51. var topSps = (0, _smooth.smooth)(topPoints, false, constaint);
  52. context.beginPath();
  53. context.moveTo(topPoints[0].x, topPoints[0].y);
  54. for (var i = 0, n = topSps.length; i < n; i++) {
  55. var sp = topSps[i];
  56. context.bezierCurveTo(sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]);
  57. }
  58. if (bottomPoints.length) {
  59. var bottomSps = (0, _smooth.smooth)(bottomPoints, false, constaint);
  60. context.lineTo(bottomPoints[0].x, bottomPoints[0].y);
  61. for (var _i = 0, _n = bottomSps.length; _i < _n; _i++) {
  62. var _sp = bottomSps[_i];
  63. context.bezierCurveTo(_sp[1], _sp[2], _sp[3], _sp[4], _sp[5], _sp[6]);
  64. }
  65. }
  66. context.closePath();
  67. },
  68. calculateBox: function calculateBox() {
  69. var points = filterPoints(this._attrs.attrs.points);
  70. return (0, _bbox.getBBoxFromPoints)(points);
  71. }
  72. });
  73. } else {
  74. shape = container.addShape('Polyline', {
  75. className: 'area',
  76. attrs: (0, _common.mix)({
  77. points: points
  78. }, style)
  79. });
  80. }
  81. return shape;
  82. }
  83. function drawShape(cfg, container, isSmooth) {
  84. var self = this;
  85. var points = cfg.points;
  86. var topPoints = [];
  87. var bottomPoints = [];
  88. (0, _common.each)(points, function (point) {
  89. bottomPoints.push(point[0]);
  90. topPoints.push(point[1]);
  91. });
  92. var style = (0, _common.mix)({
  93. fillStyle: cfg.color
  94. }, _global["default"].shape.area, cfg.style);
  95. bottomPoints.reverse();
  96. topPoints = self.parsePoints(topPoints);
  97. bottomPoints = self.parsePoints(bottomPoints);
  98. if (cfg.isInCircle) {
  99. topPoints.push(topPoints[0]);
  100. bottomPoints.unshift(bottomPoints[bottomPoints.length - 1]);
  101. if (equalsCenter(bottomPoints, cfg.center)) {
  102. bottomPoints = [];
  103. }
  104. }
  105. return drawRectShape(topPoints, bottomPoints, container, style, isSmooth);
  106. }
  107. var Area = _shape["default"].registerFactory('area', {
  108. defaultShapeType: 'area',
  109. getDefaultPoints: function getDefaultPoints(obj) {
  110. var x = obj.x;
  111. var y = obj.y;
  112. var y0 = obj.y0;
  113. y = (0, _common.isArray)(y) ? y : [y0, y];
  114. var points = [];
  115. points.push({
  116. x: x,
  117. y: y[0]
  118. }, {
  119. x: x,
  120. y: y[1]
  121. });
  122. return points;
  123. }
  124. });
  125. var SHAPES = ['area', 'smooth'];
  126. (0, _common.each)(SHAPES, function (shapeType) {
  127. _shape["default"].registerShape('area', shapeType, {
  128. draw: function draw(cfg, container) {
  129. var smooth = shapeType === 'smooth';
  130. return drawShape.call(this, cfg, container, smooth);
  131. }
  132. });
  133. });
  134. var _default = Area;
  135. exports["default"] = _default;