base.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var Util = require('../../util/common');
  2. var KEYWORDS_PERCENT = {
  3. min: 0,
  4. median: 0.5,
  5. max: 1
  6. };
  7. var GuideBase =
  8. /*#__PURE__*/
  9. function () {
  10. var _proto = GuideBase.prototype;
  11. _proto._initDefaultCfg = function _initDefaultCfg() {};
  12. function GuideBase(cfg) {
  13. this._initDefaultCfg();
  14. Util.deepMix(this, cfg);
  15. }
  16. _proto._getNormalizedValue = function _getNormalizedValue(val, scale) {
  17. var rst;
  18. if (Util.isNil(KEYWORDS_PERCENT[val])) {
  19. rst = scale.scale(val);
  20. } else {
  21. rst = KEYWORDS_PERCENT[val];
  22. }
  23. return rst;
  24. };
  25. _proto.parsePercentPoint = function parsePercentPoint(coord, position) {
  26. var xPercent = parseFloat(position[0]) / 100;
  27. var yPercent = parseFloat(position[1]) / 100;
  28. var start = coord.start;
  29. var end = coord.end;
  30. var width = Math.abs(start.x - end.x);
  31. var height = Math.abs(start.y - end.y);
  32. var x = width * xPercent + Math.min(start.x, end.x);
  33. var y = height * yPercent + Math.min(start.y, end.y);
  34. return {
  35. x: x,
  36. y: y
  37. };
  38. };
  39. _proto.parsePoint = function parsePoint(coord, position) {
  40. var self = this;
  41. var xScale = self.xScale;
  42. var yScales = self.yScales;
  43. if (Util.isFunction(position)) {
  44. position = position(xScale, yScales); // position 必须是对象
  45. } // 如果数据格式是 ['50%', '50%'] 的格式
  46. if (Util.isString(position[0]) && position[0].indexOf('%') !== -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. _proto.render = function render()
  70. /* coord,group */
  71. {};
  72. _proto.repaint = function repaint() {
  73. this.remove();
  74. var coord = this.coord,
  75. container = this.container,
  76. canvas = this.canvas;
  77. if (container && !container.isDestroyed()) {
  78. this.render(coord, container);
  79. canvas.draw();
  80. }
  81. };
  82. _proto.remove = function remove() {
  83. var element = this.element;
  84. element && element.remove(true);
  85. };
  86. _proto.changeVisible = function changeVisible(visible) {
  87. var self = this;
  88. self.visible = visible;
  89. var element = self.element;
  90. if (!element) return;
  91. if (element.set) {
  92. element.set('visible', visible);
  93. } else {
  94. element.style.display = visible ? '' : 'none';
  95. }
  96. };
  97. return GuideBase;
  98. }();
  99. module.exports = GuideBase;