base.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { __assign, __extends } from "tslib";
  2. import GroupComponent from '../abstract/group-component';
  3. import { createBBox, formatPadding } from '../util/util';
  4. var LegendBase = /** @class */ (function (_super) {
  5. __extends(LegendBase, _super);
  6. function LegendBase() {
  7. return _super !== null && _super.apply(this, arguments) || this;
  8. }
  9. LegendBase.prototype.getDefaultCfg = function () {
  10. var cfg = _super.prototype.getDefaultCfg.call(this);
  11. return __assign(__assign({}, cfg), { name: 'legend',
  12. /**
  13. * 布局方式: horizontal,vertical
  14. * @type {String}
  15. */
  16. layout: 'horizontal', locationType: 'point', x: 0, y: 0, offsetX: 0, offsetY: 0, title: null, background: null });
  17. };
  18. LegendBase.prototype.getLayoutBBox = function () {
  19. var bbox = _super.prototype.getLayoutBBox.call(this);
  20. var maxWidth = this.get('maxWidth');
  21. var maxHeight = this.get('maxHeight');
  22. var width = bbox.width, height = bbox.height;
  23. if (maxWidth) {
  24. width = Math.min(width, maxWidth);
  25. }
  26. if (maxHeight) {
  27. height = Math.min(height, maxHeight);
  28. }
  29. return createBBox(bbox.minX, bbox.minY, width, height);
  30. };
  31. LegendBase.prototype.setLocation = function (cfg) {
  32. this.set('x', cfg.x);
  33. this.set('y', cfg.y);
  34. this.resetLocation();
  35. };
  36. LegendBase.prototype.resetLocation = function () {
  37. var x = this.get('x');
  38. var y = this.get('y');
  39. var offsetX = this.get('offsetX');
  40. var offsetY = this.get('offsetY');
  41. this.moveElementTo(this.get('group'), {
  42. x: x + offsetX,
  43. y: y + offsetY,
  44. });
  45. };
  46. LegendBase.prototype.applyOffset = function () {
  47. this.resetLocation();
  48. };
  49. // 获取当前绘制的点
  50. LegendBase.prototype.getDrawPoint = function () {
  51. return this.get('currentPoint');
  52. };
  53. LegendBase.prototype.setDrawPoint = function (point) {
  54. return this.set('currentPoint', point);
  55. };
  56. // 复写父类定义的绘制方法
  57. LegendBase.prototype.renderInner = function (group) {
  58. this.resetDraw();
  59. if (this.get('title')) {
  60. this.drawTitle(group);
  61. }
  62. this.drawLegendContent(group);
  63. if (this.get('background')) {
  64. this.drawBackground(group);
  65. }
  66. // this.resetLocation(); // 在顶层已经在处理偏移时一起处理了
  67. };
  68. // 绘制背景
  69. LegendBase.prototype.drawBackground = function (group) {
  70. var background = this.get('background');
  71. var bbox = group.getBBox();
  72. var padding = formatPadding(background.padding);
  73. var attrs = __assign({
  74. // 背景从 (0,0) 开始绘制
  75. x: 0, y: 0, width: bbox.width + padding[1] + padding[3], height: bbox.height + padding[0] + padding[2] }, background.style);
  76. var backgroundShape = this.addShape(group, {
  77. type: 'rect',
  78. id: this.getElementId('background'),
  79. name: 'legend-background',
  80. attrs: attrs,
  81. });
  82. backgroundShape.toBack();
  83. };
  84. // 绘制标题,标题在图例项的上面
  85. LegendBase.prototype.drawTitle = function (group) {
  86. var currentPoint = this.get('currentPoint');
  87. var titleCfg = this.get('title');
  88. var spacing = titleCfg.spacing, style = titleCfg.style, text = titleCfg.text;
  89. var shape = this.addShape(group, {
  90. type: 'text',
  91. id: this.getElementId('title'),
  92. name: 'legend-title',
  93. attrs: __assign({ text: text, x: currentPoint.x, y: currentPoint.y }, style),
  94. });
  95. var bbox = shape.getBBox();
  96. // 标题单独在一行
  97. this.set('currentPoint', { x: currentPoint.x, y: bbox.maxY + spacing });
  98. };
  99. // 重置绘制时开始的位置,如果绘制边框,考虑边框的 padding
  100. LegendBase.prototype.resetDraw = function () {
  101. var background = this.get('background');
  102. var currentPoint = { x: 0, y: 0 };
  103. if (background) {
  104. var padding = formatPadding(background.padding);
  105. currentPoint.x = padding[3]; // 左边 padding
  106. currentPoint.y = padding[0]; // 上面 padding
  107. }
  108. this.set('currentPoint', currentPoint); // 设置绘制的初始位置
  109. };
  110. return LegendBase;
  111. }(GroupComponent));
  112. export default LegendBase;
  113. //# sourceMappingURL=base.js.map