Brush.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Brush = void 0;
  6. var _react = _interopRequireWildcard(require("react"));
  7. var _clsx = _interopRequireDefault(require("clsx"));
  8. var _d3Scale = require("victory-vendor/d3-scale");
  9. var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
  10. var _range = _interopRequireDefault(require("lodash/range"));
  11. var _Layer = require("../container/Layer");
  12. var _Text = require("../component/Text");
  13. var _ChartUtils = require("../util/ChartUtils");
  14. var _DataUtils = require("../util/DataUtils");
  15. var _CssPrefixUtils = require("../util/CssPrefixUtils");
  16. var _ReactUtils = require("../util/ReactUtils");
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  18. function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
  19. function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
  20. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  21. function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  22. function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
  23. function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
  24. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  25. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  26. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  27. function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
  28. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
  29. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  30. function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
  31. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  32. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
  33. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  34. function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  35. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  36. function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /**
  37. * @fileOverview Brush
  38. */
  39. var createScale = function createScale(_ref) {
  40. var data = _ref.data,
  41. startIndex = _ref.startIndex,
  42. endIndex = _ref.endIndex,
  43. x = _ref.x,
  44. width = _ref.width,
  45. travellerWidth = _ref.travellerWidth;
  46. if (!data || !data.length) {
  47. return {};
  48. }
  49. var len = data.length;
  50. var scale = (0, _d3Scale.scalePoint)().domain((0, _range["default"])(0, len)).range([x, x + width - travellerWidth]);
  51. var scaleValues = scale.domain().map(function (entry) {
  52. return scale(entry);
  53. });
  54. return {
  55. isTextActive: false,
  56. isSlideMoving: false,
  57. isTravellerMoving: false,
  58. isTravellerFocused: false,
  59. startX: scale(startIndex),
  60. endX: scale(endIndex),
  61. scale: scale,
  62. scaleValues: scaleValues
  63. };
  64. };
  65. var isTouch = function isTouch(e) {
  66. return e.changedTouches && !!e.changedTouches.length;
  67. };
  68. var Brush = exports.Brush = /*#__PURE__*/function (_PureComponent) {
  69. function Brush(props) {
  70. var _this;
  71. _classCallCheck(this, Brush);
  72. _this = _callSuper(this, Brush, [props]);
  73. _defineProperty(_this, "handleDrag", function (e) {
  74. if (_this.leaveTimer) {
  75. clearTimeout(_this.leaveTimer);
  76. _this.leaveTimer = null;
  77. }
  78. if (_this.state.isTravellerMoving) {
  79. _this.handleTravellerMove(e);
  80. } else if (_this.state.isSlideMoving) {
  81. _this.handleSlideDrag(e);
  82. }
  83. });
  84. _defineProperty(_this, "handleTouchMove", function (e) {
  85. if (e.changedTouches != null && e.changedTouches.length > 0) {
  86. _this.handleDrag(e.changedTouches[0]);
  87. }
  88. });
  89. _defineProperty(_this, "handleDragEnd", function () {
  90. _this.setState({
  91. isTravellerMoving: false,
  92. isSlideMoving: false
  93. }, function () {
  94. var _this$props = _this.props,
  95. endIndex = _this$props.endIndex,
  96. onDragEnd = _this$props.onDragEnd,
  97. startIndex = _this$props.startIndex;
  98. onDragEnd === null || onDragEnd === void 0 || onDragEnd({
  99. endIndex: endIndex,
  100. startIndex: startIndex
  101. });
  102. });
  103. _this.detachDragEndListener();
  104. });
  105. _defineProperty(_this, "handleLeaveWrapper", function () {
  106. if (_this.state.isTravellerMoving || _this.state.isSlideMoving) {
  107. _this.leaveTimer = window.setTimeout(_this.handleDragEnd, _this.props.leaveTimeOut);
  108. }
  109. });
  110. _defineProperty(_this, "handleEnterSlideOrTraveller", function () {
  111. _this.setState({
  112. isTextActive: true
  113. });
  114. });
  115. _defineProperty(_this, "handleLeaveSlideOrTraveller", function () {
  116. _this.setState({
  117. isTextActive: false
  118. });
  119. });
  120. _defineProperty(_this, "handleSlideDragStart", function (e) {
  121. var event = isTouch(e) ? e.changedTouches[0] : e;
  122. _this.setState({
  123. isTravellerMoving: false,
  124. isSlideMoving: true,
  125. slideMoveStartX: event.pageX
  126. });
  127. _this.attachDragEndListener();
  128. });
  129. _this.travellerDragStartHandlers = {
  130. startX: _this.handleTravellerDragStart.bind(_this, 'startX'),
  131. endX: _this.handleTravellerDragStart.bind(_this, 'endX')
  132. };
  133. _this.state = {};
  134. return _this;
  135. }
  136. _inherits(Brush, _PureComponent);
  137. return _createClass(Brush, [{
  138. key: "componentWillUnmount",
  139. value: function componentWillUnmount() {
  140. if (this.leaveTimer) {
  141. clearTimeout(this.leaveTimer);
  142. this.leaveTimer = null;
  143. }
  144. this.detachDragEndListener();
  145. }
  146. }, {
  147. key: "getIndex",
  148. value: function getIndex(_ref2) {
  149. var startX = _ref2.startX,
  150. endX = _ref2.endX;
  151. var scaleValues = this.state.scaleValues;
  152. var _this$props2 = this.props,
  153. gap = _this$props2.gap,
  154. data = _this$props2.data;
  155. var lastIndex = data.length - 1;
  156. var min = Math.min(startX, endX);
  157. var max = Math.max(startX, endX);
  158. var minIndex = Brush.getIndexInRange(scaleValues, min);
  159. var maxIndex = Brush.getIndexInRange(scaleValues, max);
  160. return {
  161. startIndex: minIndex - minIndex % gap,
  162. endIndex: maxIndex === lastIndex ? lastIndex : maxIndex - maxIndex % gap
  163. };
  164. }
  165. }, {
  166. key: "getTextOfTick",
  167. value: function getTextOfTick(index) {
  168. var _this$props3 = this.props,
  169. data = _this$props3.data,
  170. tickFormatter = _this$props3.tickFormatter,
  171. dataKey = _this$props3.dataKey;
  172. var text = (0, _ChartUtils.getValueByDataKey)(data[index], dataKey, index);
  173. return (0, _isFunction["default"])(tickFormatter) ? tickFormatter(text, index) : text;
  174. }
  175. }, {
  176. key: "attachDragEndListener",
  177. value: function attachDragEndListener() {
  178. window.addEventListener('mouseup', this.handleDragEnd, true);
  179. window.addEventListener('touchend', this.handleDragEnd, true);
  180. window.addEventListener('mousemove', this.handleDrag, true);
  181. }
  182. }, {
  183. key: "detachDragEndListener",
  184. value: function detachDragEndListener() {
  185. window.removeEventListener('mouseup', this.handleDragEnd, true);
  186. window.removeEventListener('touchend', this.handleDragEnd, true);
  187. window.removeEventListener('mousemove', this.handleDrag, true);
  188. }
  189. }, {
  190. key: "handleSlideDrag",
  191. value: function handleSlideDrag(e) {
  192. var _this$state = this.state,
  193. slideMoveStartX = _this$state.slideMoveStartX,
  194. startX = _this$state.startX,
  195. endX = _this$state.endX;
  196. var _this$props4 = this.props,
  197. x = _this$props4.x,
  198. width = _this$props4.width,
  199. travellerWidth = _this$props4.travellerWidth,
  200. startIndex = _this$props4.startIndex,
  201. endIndex = _this$props4.endIndex,
  202. onChange = _this$props4.onChange;
  203. var delta = e.pageX - slideMoveStartX;
  204. if (delta > 0) {
  205. delta = Math.min(delta, x + width - travellerWidth - endX, x + width - travellerWidth - startX);
  206. } else if (delta < 0) {
  207. delta = Math.max(delta, x - startX, x - endX);
  208. }
  209. var newIndex = this.getIndex({
  210. startX: startX + delta,
  211. endX: endX + delta
  212. });
  213. if ((newIndex.startIndex !== startIndex || newIndex.endIndex !== endIndex) && onChange) {
  214. onChange(newIndex);
  215. }
  216. this.setState({
  217. startX: startX + delta,
  218. endX: endX + delta,
  219. slideMoveStartX: e.pageX
  220. });
  221. }
  222. }, {
  223. key: "handleTravellerDragStart",
  224. value: function handleTravellerDragStart(id, e) {
  225. var event = isTouch(e) ? e.changedTouches[0] : e;
  226. this.setState({
  227. isSlideMoving: false,
  228. isTravellerMoving: true,
  229. movingTravellerId: id,
  230. brushMoveStartX: event.pageX
  231. });
  232. this.attachDragEndListener();
  233. }
  234. }, {
  235. key: "handleTravellerMove",
  236. value: function handleTravellerMove(e) {
  237. var _this$state2 = this.state,
  238. brushMoveStartX = _this$state2.brushMoveStartX,
  239. movingTravellerId = _this$state2.movingTravellerId,
  240. endX = _this$state2.endX,
  241. startX = _this$state2.startX;
  242. var prevValue = this.state[movingTravellerId];
  243. var _this$props5 = this.props,
  244. x = _this$props5.x,
  245. width = _this$props5.width,
  246. travellerWidth = _this$props5.travellerWidth,
  247. onChange = _this$props5.onChange,
  248. gap = _this$props5.gap,
  249. data = _this$props5.data;
  250. var params = {
  251. startX: this.state.startX,
  252. endX: this.state.endX
  253. };
  254. var delta = e.pageX - brushMoveStartX;
  255. if (delta > 0) {
  256. delta = Math.min(delta, x + width - travellerWidth - prevValue);
  257. } else if (delta < 0) {
  258. delta = Math.max(delta, x - prevValue);
  259. }
  260. params[movingTravellerId] = prevValue + delta;
  261. var newIndex = this.getIndex(params);
  262. var startIndex = newIndex.startIndex,
  263. endIndex = newIndex.endIndex;
  264. var isFullGap = function isFullGap() {
  265. var lastIndex = data.length - 1;
  266. if (movingTravellerId === 'startX' && (endX > startX ? startIndex % gap === 0 : endIndex % gap === 0) || endX < startX && endIndex === lastIndex || movingTravellerId === 'endX' && (endX > startX ? endIndex % gap === 0 : startIndex % gap === 0) || endX > startX && endIndex === lastIndex) {
  267. return true;
  268. }
  269. return false;
  270. };
  271. this.setState(_defineProperty(_defineProperty({}, movingTravellerId, prevValue + delta), "brushMoveStartX", e.pageX), function () {
  272. if (onChange) {
  273. if (isFullGap()) {
  274. onChange(newIndex);
  275. }
  276. }
  277. });
  278. }
  279. }, {
  280. key: "handleTravellerMoveKeyboard",
  281. value: function handleTravellerMoveKeyboard(direction, id) {
  282. var _this2 = this;
  283. // scaleValues are a list of coordinates. For example: [65, 250, 435, 620, 805, 990].
  284. var _this$state3 = this.state,
  285. scaleValues = _this$state3.scaleValues,
  286. startX = _this$state3.startX,
  287. endX = _this$state3.endX;
  288. // currentScaleValue refers to which coordinate the current traveller should be placed at.
  289. var currentScaleValue = this.state[id];
  290. var currentIndex = scaleValues.indexOf(currentScaleValue);
  291. if (currentIndex === -1) {
  292. return;
  293. }
  294. var newIndex = currentIndex + direction;
  295. if (newIndex === -1 || newIndex >= scaleValues.length) {
  296. return;
  297. }
  298. var newScaleValue = scaleValues[newIndex];
  299. // Prevent travellers from being on top of each other or overlapping
  300. if (id === 'startX' && newScaleValue >= endX || id === 'endX' && newScaleValue <= startX) {
  301. return;
  302. }
  303. this.setState(_defineProperty({}, id, newScaleValue), function () {
  304. _this2.props.onChange(_this2.getIndex({
  305. startX: _this2.state.startX,
  306. endX: _this2.state.endX
  307. }));
  308. });
  309. }
  310. }, {
  311. key: "renderBackground",
  312. value: function renderBackground() {
  313. var _this$props6 = this.props,
  314. x = _this$props6.x,
  315. y = _this$props6.y,
  316. width = _this$props6.width,
  317. height = _this$props6.height,
  318. fill = _this$props6.fill,
  319. stroke = _this$props6.stroke;
  320. return /*#__PURE__*/_react["default"].createElement("rect", {
  321. stroke: stroke,
  322. fill: fill,
  323. x: x,
  324. y: y,
  325. width: width,
  326. height: height
  327. });
  328. }
  329. }, {
  330. key: "renderPanorama",
  331. value: function renderPanorama() {
  332. var _this$props7 = this.props,
  333. x = _this$props7.x,
  334. y = _this$props7.y,
  335. width = _this$props7.width,
  336. height = _this$props7.height,
  337. data = _this$props7.data,
  338. children = _this$props7.children,
  339. padding = _this$props7.padding;
  340. var chartElement = _react.Children.only(children);
  341. if (!chartElement) {
  342. return null;
  343. }
  344. return /*#__PURE__*/_react["default"].cloneElement(chartElement, {
  345. x: x,
  346. y: y,
  347. width: width,
  348. height: height,
  349. margin: padding,
  350. compact: true,
  351. data: data
  352. });
  353. }
  354. }, {
  355. key: "renderTravellerLayer",
  356. value: function renderTravellerLayer(travellerX, id) {
  357. var _data$startIndex,
  358. _data$endIndex,
  359. _this3 = this;
  360. var _this$props8 = this.props,
  361. y = _this$props8.y,
  362. travellerWidth = _this$props8.travellerWidth,
  363. height = _this$props8.height,
  364. traveller = _this$props8.traveller,
  365. ariaLabel = _this$props8.ariaLabel,
  366. data = _this$props8.data,
  367. startIndex = _this$props8.startIndex,
  368. endIndex = _this$props8.endIndex;
  369. var x = Math.max(travellerX, this.props.x);
  370. var travellerProps = _objectSpread(_objectSpread({}, (0, _ReactUtils.filterProps)(this.props, false)), {}, {
  371. x: x,
  372. y: y,
  373. width: travellerWidth,
  374. height: height
  375. });
  376. var ariaLabelBrush = ariaLabel || "Min value: ".concat((_data$startIndex = data[startIndex]) === null || _data$startIndex === void 0 ? void 0 : _data$startIndex.name, ", Max value: ").concat((_data$endIndex = data[endIndex]) === null || _data$endIndex === void 0 ? void 0 : _data$endIndex.name);
  377. return /*#__PURE__*/_react["default"].createElement(_Layer.Layer, {
  378. tabIndex: 0,
  379. role: "slider",
  380. "aria-label": ariaLabelBrush,
  381. "aria-valuenow": travellerX,
  382. className: "recharts-brush-traveller",
  383. onMouseEnter: this.handleEnterSlideOrTraveller,
  384. onMouseLeave: this.handleLeaveSlideOrTraveller,
  385. onMouseDown: this.travellerDragStartHandlers[id],
  386. onTouchStart: this.travellerDragStartHandlers[id],
  387. onKeyDown: function onKeyDown(e) {
  388. if (!['ArrowLeft', 'ArrowRight'].includes(e.key)) {
  389. return;
  390. }
  391. e.preventDefault();
  392. e.stopPropagation();
  393. _this3.handleTravellerMoveKeyboard(e.key === 'ArrowRight' ? 1 : -1, id);
  394. },
  395. onFocus: function onFocus() {
  396. _this3.setState({
  397. isTravellerFocused: true
  398. });
  399. },
  400. onBlur: function onBlur() {
  401. _this3.setState({
  402. isTravellerFocused: false
  403. });
  404. },
  405. style: {
  406. cursor: 'col-resize'
  407. }
  408. }, Brush.renderTraveller(traveller, travellerProps));
  409. }
  410. }, {
  411. key: "renderSlide",
  412. value: function renderSlide(startX, endX) {
  413. var _this$props9 = this.props,
  414. y = _this$props9.y,
  415. height = _this$props9.height,
  416. stroke = _this$props9.stroke,
  417. travellerWidth = _this$props9.travellerWidth;
  418. var x = Math.min(startX, endX) + travellerWidth;
  419. var width = Math.max(Math.abs(endX - startX) - travellerWidth, 0);
  420. return /*#__PURE__*/_react["default"].createElement("rect", {
  421. className: "recharts-brush-slide",
  422. onMouseEnter: this.handleEnterSlideOrTraveller,
  423. onMouseLeave: this.handleLeaveSlideOrTraveller,
  424. onMouseDown: this.handleSlideDragStart,
  425. onTouchStart: this.handleSlideDragStart,
  426. style: {
  427. cursor: 'move'
  428. },
  429. stroke: "none",
  430. fill: stroke,
  431. fillOpacity: 0.2,
  432. x: x,
  433. y: y,
  434. width: width,
  435. height: height
  436. });
  437. }
  438. }, {
  439. key: "renderText",
  440. value: function renderText() {
  441. var _this$props10 = this.props,
  442. startIndex = _this$props10.startIndex,
  443. endIndex = _this$props10.endIndex,
  444. y = _this$props10.y,
  445. height = _this$props10.height,
  446. travellerWidth = _this$props10.travellerWidth,
  447. stroke = _this$props10.stroke;
  448. var _this$state4 = this.state,
  449. startX = _this$state4.startX,
  450. endX = _this$state4.endX;
  451. var offset = 5;
  452. var attrs = {
  453. pointerEvents: 'none',
  454. fill: stroke
  455. };
  456. return /*#__PURE__*/_react["default"].createElement(_Layer.Layer, {
  457. className: "recharts-brush-texts"
  458. }, /*#__PURE__*/_react["default"].createElement(_Text.Text, _extends({
  459. textAnchor: "end",
  460. verticalAnchor: "middle",
  461. x: Math.min(startX, endX) - offset,
  462. y: y + height / 2
  463. }, attrs), this.getTextOfTick(startIndex)), /*#__PURE__*/_react["default"].createElement(_Text.Text, _extends({
  464. textAnchor: "start",
  465. verticalAnchor: "middle",
  466. x: Math.max(startX, endX) + travellerWidth + offset,
  467. y: y + height / 2
  468. }, attrs), this.getTextOfTick(endIndex)));
  469. }
  470. }, {
  471. key: "render",
  472. value: function render() {
  473. var _this$props11 = this.props,
  474. data = _this$props11.data,
  475. className = _this$props11.className,
  476. children = _this$props11.children,
  477. x = _this$props11.x,
  478. y = _this$props11.y,
  479. width = _this$props11.width,
  480. height = _this$props11.height,
  481. alwaysShowText = _this$props11.alwaysShowText;
  482. var _this$state5 = this.state,
  483. startX = _this$state5.startX,
  484. endX = _this$state5.endX,
  485. isTextActive = _this$state5.isTextActive,
  486. isSlideMoving = _this$state5.isSlideMoving,
  487. isTravellerMoving = _this$state5.isTravellerMoving,
  488. isTravellerFocused = _this$state5.isTravellerFocused;
  489. if (!data || !data.length || !(0, _DataUtils.isNumber)(x) || !(0, _DataUtils.isNumber)(y) || !(0, _DataUtils.isNumber)(width) || !(0, _DataUtils.isNumber)(height) || width <= 0 || height <= 0) {
  490. return null;
  491. }
  492. var layerClass = (0, _clsx["default"])('recharts-brush', className);
  493. var isPanoramic = _react["default"].Children.count(children) === 1;
  494. var style = (0, _CssPrefixUtils.generatePrefixStyle)('userSelect', 'none');
  495. return /*#__PURE__*/_react["default"].createElement(_Layer.Layer, {
  496. className: layerClass,
  497. onMouseLeave: this.handleLeaveWrapper,
  498. onTouchMove: this.handleTouchMove,
  499. style: style
  500. }, this.renderBackground(), isPanoramic && this.renderPanorama(), this.renderSlide(startX, endX), this.renderTravellerLayer(startX, 'startX'), this.renderTravellerLayer(endX, 'endX'), (isTextActive || isSlideMoving || isTravellerMoving || isTravellerFocused || alwaysShowText) && this.renderText());
  501. }
  502. }], [{
  503. key: "renderDefaultTraveller",
  504. value: function renderDefaultTraveller(props) {
  505. var x = props.x,
  506. y = props.y,
  507. width = props.width,
  508. height = props.height,
  509. stroke = props.stroke;
  510. var lineY = Math.floor(y + height / 2) - 1;
  511. return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("rect", {
  512. x: x,
  513. y: y,
  514. width: width,
  515. height: height,
  516. fill: stroke,
  517. stroke: "none"
  518. }), /*#__PURE__*/_react["default"].createElement("line", {
  519. x1: x + 1,
  520. y1: lineY,
  521. x2: x + width - 1,
  522. y2: lineY,
  523. fill: "none",
  524. stroke: "#fff"
  525. }), /*#__PURE__*/_react["default"].createElement("line", {
  526. x1: x + 1,
  527. y1: lineY + 2,
  528. x2: x + width - 1,
  529. y2: lineY + 2,
  530. fill: "none",
  531. stroke: "#fff"
  532. }));
  533. }
  534. }, {
  535. key: "renderTraveller",
  536. value: function renderTraveller(option, props) {
  537. var rectangle;
  538. if ( /*#__PURE__*/_react["default"].isValidElement(option)) {
  539. rectangle = /*#__PURE__*/_react["default"].cloneElement(option, props);
  540. } else if ((0, _isFunction["default"])(option)) {
  541. rectangle = option(props);
  542. } else {
  543. rectangle = Brush.renderDefaultTraveller(props);
  544. }
  545. return rectangle;
  546. }
  547. }, {
  548. key: "getDerivedStateFromProps",
  549. value: function getDerivedStateFromProps(nextProps, prevState) {
  550. var data = nextProps.data,
  551. width = nextProps.width,
  552. x = nextProps.x,
  553. travellerWidth = nextProps.travellerWidth,
  554. updateId = nextProps.updateId,
  555. startIndex = nextProps.startIndex,
  556. endIndex = nextProps.endIndex;
  557. if (data !== prevState.prevData || updateId !== prevState.prevUpdateId) {
  558. return _objectSpread({
  559. prevData: data,
  560. prevTravellerWidth: travellerWidth,
  561. prevUpdateId: updateId,
  562. prevX: x,
  563. prevWidth: width
  564. }, data && data.length ? createScale({
  565. data: data,
  566. width: width,
  567. x: x,
  568. travellerWidth: travellerWidth,
  569. startIndex: startIndex,
  570. endIndex: endIndex
  571. }) : {
  572. scale: null,
  573. scaleValues: null
  574. });
  575. }
  576. if (prevState.scale && (width !== prevState.prevWidth || x !== prevState.prevX || travellerWidth !== prevState.prevTravellerWidth)) {
  577. prevState.scale.range([x, x + width - travellerWidth]);
  578. var scaleValues = prevState.scale.domain().map(function (entry) {
  579. return prevState.scale(entry);
  580. });
  581. return {
  582. prevData: data,
  583. prevTravellerWidth: travellerWidth,
  584. prevUpdateId: updateId,
  585. prevX: x,
  586. prevWidth: width,
  587. startX: prevState.scale(nextProps.startIndex),
  588. endX: prevState.scale(nextProps.endIndex),
  589. scaleValues: scaleValues
  590. };
  591. }
  592. return null;
  593. }
  594. }, {
  595. key: "getIndexInRange",
  596. value: function getIndexInRange(valueRange, x) {
  597. var len = valueRange.length;
  598. var start = 0;
  599. var end = len - 1;
  600. while (end - start > 1) {
  601. var middle = Math.floor((start + end) / 2);
  602. if (valueRange[middle] > x) {
  603. end = middle;
  604. } else {
  605. start = middle;
  606. }
  607. }
  608. return x >= valueRange[end] ? end : start;
  609. }
  610. }]);
  611. }(_react.PureComponent);
  612. _defineProperty(Brush, "displayName", 'Brush');
  613. _defineProperty(Brush, "defaultProps", {
  614. height: 40,
  615. travellerWidth: 5,
  616. gap: 1,
  617. fill: '#fff',
  618. stroke: '#666',
  619. padding: {
  620. top: 1,
  621. right: 1,
  622. bottom: 1,
  623. left: 1
  624. },
  625. leaveTimeOut: 1000,
  626. alwaysShowText: false
  627. });