shape.js 2.7 KB

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