text.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { mix } from '../../util/common';
  2. import GuideBase from './base';
  3. class Text extends GuideBase {
  4. _initDefaultCfg() {
  5. this.type = 'text';
  6. /**
  7. * the position of text
  8. * @type {Function | Array}
  9. */
  10. this.position = null;
  11. /**
  12. * the display content
  13. * @type {String}
  14. */
  15. this.content = null;
  16. /**
  17. * style configuration for text
  18. * @type {Object}
  19. */
  20. this.style = {
  21. fill: '#000'
  22. };
  23. /**
  24. * offset of horizontal direction
  25. * @type {Number}
  26. */
  27. this.offsetX = 0;
  28. /**
  29. * offset of vertical direction
  30. * @type {Number}
  31. */
  32. this.offsetY = 0;
  33. }
  34. render(coord, container) {
  35. var position = this.position;
  36. var point = this.parsePoint(coord, position);
  37. if (!point) {
  38. return;
  39. }
  40. var {
  41. content,
  42. style,
  43. offsetX,
  44. offsetY
  45. } = this;
  46. if (offsetX) {
  47. point.x += offsetX;
  48. }
  49. if (offsetY) {
  50. point.y += offsetY;
  51. }
  52. var shape = container.addShape('text', {
  53. className: 'guide-text',
  54. attrs: mix({
  55. x: point.x,
  56. y: point.y,
  57. text: content
  58. }, style)
  59. });
  60. this.element = shape;
  61. return shape;
  62. }
  63. }
  64. GuideBase.Text = Text;
  65. export default Text;