base.js 2.9 KB

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