base.js 4.5 KB

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