shape.js 2.9 KB

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