point.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var Util = require('../../util/common');
  2. var Global = require('../../global');
  3. var ShapeUtil = require('./util');
  4. var Shape = require('./shape');
  5. var SHAPES = ['circle', 'hollowCircle', 'rect'];
  6. var Point = Shape.registerFactory('point', {
  7. defaultShapeType: 'circle',
  8. getDefaultPoints: function getDefaultPoints(pointInfo) {
  9. return ShapeUtil.splitPoints(pointInfo);
  10. }
  11. });
  12. function getPointsCfg(cfg) {
  13. var style = {
  14. lineWidth: 0,
  15. stroke: cfg.color,
  16. fill: cfg.color
  17. };
  18. if (cfg.size) {
  19. style.size = cfg.size;
  20. }
  21. Util.mix(style, cfg.style);
  22. return Util.mix({}, Global.shape.point, style);
  23. }
  24. function drawShape(cfg, container, shape) {
  25. if (cfg.size === 0) return;
  26. var pointCfg = getPointsCfg(cfg);
  27. var size = pointCfg.r || pointCfg.size;
  28. var x = cfg.x;
  29. var y = !Util.isArray(cfg.y) ? [cfg.y] : cfg.y;
  30. if (shape === 'hollowCircle') {
  31. pointCfg.lineWidth = 1;
  32. pointCfg.fill = null;
  33. }
  34. for (var i = 0, len = y.length; i < len; i++) {
  35. if (shape === 'rect') {
  36. return container.addShape('Rect', {
  37. className: 'point',
  38. attrs: Util.mix({
  39. x: x - size,
  40. y: y[i] - size,
  41. width: size * 2,
  42. height: size * 2
  43. }, pointCfg)
  44. });
  45. }
  46. return container.addShape('Circle', {
  47. className: 'point',
  48. attrs: Util.mix({
  49. x: x,
  50. y: y[i],
  51. r: size
  52. }, pointCfg)
  53. });
  54. }
  55. }
  56. Util.each(SHAPES, function (shapeType) {
  57. Shape.registerShape('point', shapeType, {
  58. draw: function draw(cfg, container) {
  59. return drawShape(cfg, container, shapeType);
  60. }
  61. });
  62. });
  63. module.exports = Point;