area.js 3.8 KB

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