point.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import Global from '../../global';
  2. import Shape from './shape';
  3. import { mix, each, isArray } from '../../util/common';
  4. import { splitPoints } from './util';
  5. var SHAPES = ['circle', 'hollowCircle', 'rect'];
  6. var Point = Shape.registerFactory('point', {
  7. defaultShapeType: 'circle',
  8. getDefaultPoints(pointInfo) {
  9. return 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. mix(style, cfg.style);
  22. return 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 = !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: 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: mix({
  49. x,
  50. y: y[i],
  51. r: size
  52. }, pointCfg)
  53. });
  54. }
  55. }
  56. each(SHAPES, function (shapeType) {
  57. Shape.registerShape('point', shapeType, {
  58. draw(cfg, container) {
  59. return drawShape(cfg, container, shapeType);
  60. }
  61. });
  62. });
  63. export default Point;