shape.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _common = require("../../util/common");
  5. var _global = _interopRequireDefault(require("../../global"));
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  7. var Shape = {};
  8. var ShapeBase = {
  9. _coord: null,
  10. /**
  11. * draw the shape
  12. * @param {Object} cfg options
  13. * @param {Object} container container to store the shapes
  14. */
  15. draw: function draw(cfg, container) {
  16. if (this.drawShape) {
  17. this.drawShape(cfg, container);
  18. }
  19. },
  20. /**
  21. * set the coordinate instance
  22. * @param {Coord} coord coordinate instance
  23. */
  24. setCoord: function setCoord(coord) {
  25. this._coord = coord;
  26. },
  27. /**
  28. * convert the normalized value to the canvas position
  29. * @param {point} point the point to convert
  30. * @return {point} point return the result
  31. */
  32. parsePoint: function parsePoint(point) {
  33. var coord = this._coord;
  34. if (coord.isPolar) {
  35. if (point.x === 1) point.x = 0.9999999;
  36. if (point.y === 1) point.y = 0.9999999;
  37. }
  38. return coord.convertPoint(point);
  39. },
  40. /**
  41. * convert the normalized value to the canvas position
  42. * @param {points} points the array that store the points
  43. * @return {points} points return the result
  44. */
  45. parsePoints: function parsePoints(points) {
  46. if (!points) return false;
  47. var self = this;
  48. var rst = [];
  49. points.forEach(function (point) {
  50. rst.push(self.parsePoint(point));
  51. });
  52. return rst;
  53. }
  54. };
  55. var ShapeFactoryBase = {
  56. defaultShapeType: null,
  57. setCoord: function setCoord(coord) {
  58. this._coord = coord;
  59. },
  60. getShape: function getShape(type) {
  61. var self = this;
  62. if ((0, _common.isArray)(type)) {
  63. type = type[0];
  64. }
  65. var shape = self[type] || self[self.defaultShapeType];
  66. shape._coord = self._coord;
  67. return shape;
  68. },
  69. getShapePoints: function getShapePoints(type, cfg) {
  70. var shape = this.getShape(type);
  71. var fn = shape.getPoints || shape.getShapePoints || this.getDefaultPoints;
  72. var points = fn(cfg);
  73. return points;
  74. },
  75. getDefaultPoints: function getDefaultPoints()
  76. /* cfg */
  77. {
  78. return [];
  79. },
  80. drawShape: function drawShape(type, cfg, container) {
  81. var shape = this.getShape(type);
  82. if (!cfg.color) {
  83. cfg.color = _global["default"].colors[0];
  84. }
  85. return shape.draw(cfg, container);
  86. }
  87. };
  88. Shape.registerFactory = function (factoryName, cfg) {
  89. var className = (0, _common.upperFirst)(factoryName);
  90. var geomObj = (0, _common.mix)({}, ShapeFactoryBase, cfg);
  91. Shape[className] = geomObj;
  92. geomObj.name = factoryName;
  93. return geomObj;
  94. };
  95. Shape.registerShape = function (factoryName, shapeType, cfg) {
  96. var className = (0, _common.upperFirst)(factoryName);
  97. var factory = Shape[className];
  98. var shapeObj = (0, _common.mix)({}, ShapeBase, cfg);
  99. factory[shapeType] = shapeObj;
  100. return shapeObj;
  101. };
  102. Shape.registShape = Shape.registerShape;
  103. Shape.getShapeFactory = function (factoryName) {
  104. var self = this;
  105. factoryName = factoryName || 'point';
  106. var className = (0, _common.upperFirst)(factoryName);
  107. return self[className];
  108. };
  109. var _default = Shape;
  110. exports["default"] = _default;