base.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _common = require("../../util/common");
  5. var KEYWORDS_PERCENT = {
  6. min: 0,
  7. median: 0.5,
  8. max: 1
  9. };
  10. var GuideBase = /*#__PURE__*/function () {
  11. var _proto = GuideBase.prototype;
  12. _proto._initDefaultCfg = function _initDefaultCfg() {};
  13. function GuideBase(cfg) {
  14. this._initDefaultCfg();
  15. (0, _common.deepMix)(this, cfg);
  16. }
  17. _proto._getNormalizedValue = function _getNormalizedValue(val, scale) {
  18. var rst;
  19. if ((0, _common.isNil)(KEYWORDS_PERCENT[val])) {
  20. rst = scale.scale(val);
  21. } else {
  22. rst = KEYWORDS_PERCENT[val];
  23. }
  24. return rst;
  25. };
  26. _proto.parsePercentPoint = function parsePercentPoint(coord, position) {
  27. var xPercent = parseFloat(position[0]) / 100;
  28. var yPercent = parseFloat(position[1]) / 100;
  29. var start = coord.start;
  30. var end = coord.end;
  31. var width = Math.abs(start.x - end.x);
  32. var height = Math.abs(start.y - end.y);
  33. var x = width * xPercent + Math.min(start.x, end.x);
  34. var y = height * yPercent + Math.min(start.y, end.y);
  35. return {
  36. x: x,
  37. y: y
  38. };
  39. };
  40. _proto.parsePoint = function parsePoint(coord, position) {
  41. var self = this;
  42. var xScale = self.xScale;
  43. var yScales = self.yScales;
  44. if ((0, _common.isFunction)(position)) {
  45. position = position(xScale, yScales); // position 必须是对象
  46. } // 如果数据格式是 ['50%', '50%'] 的格式
  47. // fix: 原始数据中可能会包含 'xxx5%xxx' 这样的数据,需要判断下 https://github.com/antvis/f2/issues/590
  48. if ((0, _common.isString)(position[0]) && position[0].indexOf('%') !== -1 && !isNaN(position[0].slice(0, -1))) {
  49. return this.parsePercentPoint(coord, position);
  50. }
  51. var x = self._getNormalizedValue(position[0], xScale);
  52. var y = self._getNormalizedValue(position[1], yScales[0]);
  53. var point = coord.convertPoint({
  54. x: x,
  55. y: y
  56. });
  57. if (self.limitInPlot) {
  58. // limit in chart plotRange
  59. if (x >= 0 && x <= 1 && y >= 0 && y <= 1) {
  60. return point;
  61. }
  62. return null;
  63. }
  64. return point;
  65. }
  66. /**
  67. * render the guide component
  68. * @param {Coord} coord coordinate instance
  69. * @param {Canvas.Group} group the container
  70. */
  71. ;
  72. _proto.render = function render()
  73. /* coord,group */
  74. {};
  75. _proto.repaint = function repaint() {
  76. this.remove();
  77. var coord = this.coord,
  78. container = this.container,
  79. canvas = this.canvas;
  80. if (container && !container.isDestroyed()) {
  81. this.render(coord, container);
  82. canvas.draw();
  83. }
  84. };
  85. _proto.remove = function remove() {
  86. var element = this.element;
  87. element && element.remove(true);
  88. };
  89. _proto.changeVisible = function changeVisible(visible) {
  90. var self = this;
  91. self.visible = visible;
  92. var element = self.element;
  93. if (!element) return;
  94. if (element.set) {
  95. element.set('visible', visible);
  96. } else {
  97. element.style.display = visible ? '' : 'none';
  98. }
  99. };
  100. return GuideBase;
  101. }();
  102. var _default = GuideBase;
  103. exports["default"] = _default;