area.js 3.9 KB

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