Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

426 строки
15 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends = Object.assign || 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; };
  6. var _createClass = function () { 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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  7. var _react = require('react');
  8. var _react2 = _interopRequireDefault(_react);
  9. var _abstractSeries = require('./series/abstract-series');
  10. var _abstractSeries2 = _interopRequireDefault(_abstractSeries);
  11. var _scalesUtils = require('../utils/scales-utils');
  12. var _propTypes = require('prop-types');
  13. var _propTypes2 = _interopRequireDefault(_propTypes);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  16. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  17. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  18. function getLocs(evt) {
  19. var xLoc = evt.type === 'touchstart' ? evt.pageX : evt.offsetX;
  20. var yLoc = evt.type === 'touchstart' ? evt.pageY : evt.offsetY;
  21. return { xLoc: xLoc, yLoc: yLoc };
  22. }
  23. var Highlight = function (_AbstractSeries) {
  24. _inherits(Highlight, _AbstractSeries);
  25. function Highlight() {
  26. var _ref;
  27. var _temp, _this, _ret;
  28. _classCallCheck(this, Highlight);
  29. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  30. args[_key] = arguments[_key];
  31. }
  32. return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Highlight.__proto__ || Object.getPrototypeOf(Highlight)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  33. dragging: false,
  34. brushArea: { top: 0, right: 0, bottom: 0, left: 0 },
  35. brushing: false,
  36. startLocX: 0,
  37. startLocY: 0,
  38. dragArea: null
  39. }, _temp), _possibleConstructorReturn(_this, _ret);
  40. }
  41. _createClass(Highlight, [{
  42. key: '_getDrawArea',
  43. value: function _getDrawArea(xLoc, yLoc) {
  44. var _state = this.state,
  45. startLocX = _state.startLocX,
  46. startLocY = _state.startLocY;
  47. var _props = this.props,
  48. enableX = _props.enableX,
  49. enableY = _props.enableY,
  50. highlightWidth = _props.highlightWidth,
  51. highlightHeight = _props.highlightHeight,
  52. innerWidth = _props.innerWidth,
  53. innerHeight = _props.innerHeight,
  54. marginLeft = _props.marginLeft,
  55. marginRight = _props.marginRight,
  56. marginBottom = _props.marginBottom,
  57. marginTop = _props.marginTop;
  58. var plotHeight = innerHeight + marginTop + marginBottom;
  59. var plotWidth = innerWidth + marginLeft + marginRight;
  60. var touchWidth = highlightWidth || plotWidth;
  61. var touchHeight = highlightHeight || plotHeight;
  62. return {
  63. bottom: enableY ? Math.max(startLocY, yLoc) : touchHeight,
  64. right: enableX ? Math.max(startLocX, xLoc) : touchWidth,
  65. left: enableX ? Math.min(xLoc, startLocX) : 0,
  66. top: enableY ? Math.min(yLoc, startLocY) : 0
  67. };
  68. }
  69. }, {
  70. key: '_getDragArea',
  71. value: function _getDragArea(xLoc, yLoc) {
  72. var _props2 = this.props,
  73. enableX = _props2.enableX,
  74. enableY = _props2.enableY;
  75. var _state2 = this.state,
  76. startLocX = _state2.startLocX,
  77. startLocY = _state2.startLocY,
  78. dragArea = _state2.dragArea;
  79. return {
  80. bottom: dragArea.bottom + (enableY ? yLoc - startLocY : 0),
  81. left: dragArea.left + (enableX ? xLoc - startLocX : 0),
  82. right: dragArea.right + (enableX ? xLoc - startLocX : 0),
  83. top: dragArea.top + (enableY ? yLoc - startLocY : 0)
  84. };
  85. }
  86. }, {
  87. key: '_clickedOutsideDrag',
  88. value: function _clickedOutsideDrag(xLoc, yLoc) {
  89. var _props3 = this.props,
  90. enableX = _props3.enableX,
  91. enableY = _props3.enableY;
  92. var _state3 = this.state,
  93. dragArea = _state3.dragArea,
  94. _state3$brushArea = _state3.brushArea,
  95. left = _state3$brushArea.left,
  96. right = _state3$brushArea.right,
  97. top = _state3$brushArea.top,
  98. bottom = _state3$brushArea.bottom;
  99. var clickedOutsideDragX = dragArea && (xLoc < left || xLoc > right);
  100. var clickedOutsideDragY = dragArea && (yLoc < top || yLoc > bottom);
  101. if (enableX && enableY) {
  102. return clickedOutsideDragX || clickedOutsideDragY;
  103. }
  104. if (enableX) {
  105. return clickedOutsideDragX;
  106. }
  107. if (enableY) {
  108. return clickedOutsideDragY;
  109. }
  110. return true;
  111. }
  112. }, {
  113. key: '_convertAreaToCoordinates',
  114. value: function _convertAreaToCoordinates(brushArea) {
  115. // NOTE only continuous scales are supported for brushing/getting coordinates back
  116. var _props4 = this.props,
  117. enableX = _props4.enableX,
  118. enableY = _props4.enableY,
  119. marginLeft = _props4.marginLeft,
  120. marginTop = _props4.marginTop;
  121. var xScale = (0, _scalesUtils.getAttributeScale)(this.props, 'x');
  122. var yScale = (0, _scalesUtils.getAttributeScale)(this.props, 'y');
  123. // Ensure that users wishes are being respected about which scales are evaluated
  124. // this is specifically enabled to ensure brushing on mixed categorical and linear
  125. // charts will run as expected
  126. if (enableX && enableY) {
  127. return {
  128. bottom: yScale.invert(brushArea.bottom),
  129. left: xScale.invert(brushArea.left - marginLeft),
  130. right: xScale.invert(brushArea.right - marginLeft),
  131. top: yScale.invert(brushArea.top)
  132. };
  133. }
  134. if (enableY) {
  135. return {
  136. bottom: yScale.invert(brushArea.bottom - marginTop),
  137. top: yScale.invert(brushArea.top - marginTop)
  138. };
  139. }
  140. if (enableX) {
  141. return {
  142. left: xScale.invert(brushArea.left - marginLeft),
  143. right: xScale.invert(brushArea.right - marginLeft)
  144. };
  145. }
  146. return {};
  147. }
  148. }, {
  149. key: 'startBrushing',
  150. value: function startBrushing(e) {
  151. var _this2 = this;
  152. var _props5 = this.props,
  153. onBrushStart = _props5.onBrushStart,
  154. onDragStart = _props5.onDragStart,
  155. drag = _props5.drag;
  156. var dragArea = this.state.dragArea;
  157. var _getLocs = getLocs(e.nativeEvent),
  158. xLoc = _getLocs.xLoc,
  159. yLoc = _getLocs.yLoc;
  160. var startArea = function startArea(dragging, resetDrag) {
  161. var emptyBrush = {
  162. bottom: yLoc,
  163. left: xLoc,
  164. right: xLoc,
  165. top: yLoc
  166. };
  167. _this2.setState({
  168. dragging: dragging,
  169. brushArea: dragArea && !resetDrag ? dragArea : emptyBrush,
  170. brushing: !dragging,
  171. startLocX: xLoc,
  172. startLocY: yLoc
  173. });
  174. };
  175. var clickedOutsideDrag = this._clickedOutsideDrag(xLoc, yLoc);
  176. if (drag && !dragArea || !drag || clickedOutsideDrag) {
  177. startArea(false, clickedOutsideDrag);
  178. if (onBrushStart) {
  179. onBrushStart(e);
  180. }
  181. return;
  182. }
  183. if (drag && dragArea) {
  184. startArea(true, clickedOutsideDrag);
  185. if (onDragStart) {
  186. onDragStart(e);
  187. }
  188. }
  189. }
  190. }, {
  191. key: 'stopBrushing',
  192. value: function stopBrushing(e) {
  193. var _state4 = this.state,
  194. brushing = _state4.brushing,
  195. dragging = _state4.dragging,
  196. brushArea = _state4.brushArea;
  197. // Quickly short-circuit if the user isn't brushing in our component
  198. if (!brushing && !dragging) {
  199. return;
  200. }
  201. var _props6 = this.props,
  202. onBrushEnd = _props6.onBrushEnd,
  203. onDragEnd = _props6.onDragEnd,
  204. drag = _props6.drag;
  205. var noHorizontal = Math.abs(brushArea.right - brushArea.left) < 5;
  206. var noVertical = Math.abs(brushArea.top - brushArea.bottom) < 5;
  207. // Invoke the callback with null if the selected area was < 5px
  208. var isNulled = noVertical || noHorizontal;
  209. // Clear the draw area
  210. this.setState({
  211. brushing: false,
  212. dragging: false,
  213. brushArea: drag ? brushArea : { top: 0, right: 0, bottom: 0, left: 0 },
  214. startLocX: 0,
  215. startLocY: 0,
  216. dragArea: drag && !isNulled && brushArea
  217. });
  218. if (brushing && onBrushEnd) {
  219. onBrushEnd(!isNulled ? this._convertAreaToCoordinates(brushArea) : null);
  220. }
  221. if (drag && onDragEnd) {
  222. onDragEnd(!isNulled ? this._convertAreaToCoordinates(brushArea) : null);
  223. }
  224. }
  225. }, {
  226. key: 'onBrush',
  227. value: function onBrush(e) {
  228. var _props7 = this.props,
  229. onBrush = _props7.onBrush,
  230. onDrag = _props7.onDrag,
  231. drag = _props7.drag;
  232. var _state5 = this.state,
  233. brushing = _state5.brushing,
  234. dragging = _state5.dragging;
  235. var _getLocs2 = getLocs(e.nativeEvent),
  236. xLoc = _getLocs2.xLoc,
  237. yLoc = _getLocs2.yLoc;
  238. if (brushing) {
  239. var brushArea = this._getDrawArea(xLoc, yLoc);
  240. this.setState({ brushArea: brushArea });
  241. if (onBrush) {
  242. onBrush(this._convertAreaToCoordinates(brushArea));
  243. }
  244. }
  245. if (drag && dragging) {
  246. var _brushArea = this._getDragArea(xLoc, yLoc);
  247. this.setState({ brushArea: _brushArea });
  248. if (onDrag) {
  249. onDrag(this._convertAreaToCoordinates(_brushArea));
  250. }
  251. }
  252. }
  253. }, {
  254. key: 'render',
  255. value: function render() {
  256. var _this3 = this;
  257. var _props8 = this.props,
  258. color = _props8.color,
  259. className = _props8.className,
  260. highlightHeight = _props8.highlightHeight,
  261. highlightWidth = _props8.highlightWidth,
  262. highlightX = _props8.highlightX,
  263. highlightY = _props8.highlightY,
  264. innerWidth = _props8.innerWidth,
  265. innerHeight = _props8.innerHeight,
  266. marginLeft = _props8.marginLeft,
  267. marginRight = _props8.marginRight,
  268. marginTop = _props8.marginTop,
  269. marginBottom = _props8.marginBottom,
  270. opacity = _props8.opacity;
  271. var _state$brushArea = this.state.brushArea,
  272. left = _state$brushArea.left,
  273. right = _state$brushArea.right,
  274. top = _state$brushArea.top,
  275. bottom = _state$brushArea.bottom;
  276. var leftPos = 0;
  277. if (highlightX) {
  278. var xScale = (0, _scalesUtils.getAttributeScale)(this.props, 'x');
  279. leftPos = xScale(highlightX);
  280. }
  281. var topPos = 0;
  282. if (highlightY) {
  283. var yScale = (0, _scalesUtils.getAttributeScale)(this.props, 'y');
  284. topPos = yScale(highlightY);
  285. }
  286. var plotWidth = marginLeft + marginRight + innerWidth;
  287. var plotHeight = marginTop + marginBottom + innerHeight;
  288. var touchWidth = highlightWidth || plotWidth;
  289. var touchHeight = highlightHeight || plotHeight;
  290. return _react2.default.createElement(
  291. 'g',
  292. {
  293. transform: 'translate(' + leftPos + ', ' + topPos + ')',
  294. className: className + ' rv-highlight-container'
  295. },
  296. _react2.default.createElement('rect', {
  297. className: 'rv-mouse-target',
  298. fill: 'black',
  299. opacity: '0',
  300. x: '0',
  301. y: '0',
  302. width: Math.max(touchWidth, 0),
  303. height: Math.max(touchHeight, 0),
  304. onMouseDown: function onMouseDown(e) {
  305. return _this3.startBrushing(e);
  306. },
  307. onMouseMove: function onMouseMove(e) {
  308. return _this3.onBrush(e);
  309. },
  310. onMouseUp: function onMouseUp(e) {
  311. return _this3.stopBrushing(e);
  312. },
  313. onMouseLeave: function onMouseLeave(e) {
  314. return _this3.stopBrushing(e);
  315. }
  316. // preventDefault() so that mouse event emulation does not happen
  317. , onTouchEnd: function onTouchEnd(e) {
  318. e.preventDefault();
  319. _this3.stopBrushing(e);
  320. },
  321. onTouchCancel: function onTouchCancel(e) {
  322. e.preventDefault();
  323. _this3.stopBrushing(e);
  324. },
  325. onContextMenu: function onContextMenu(e) {
  326. return e.preventDefault();
  327. },
  328. onContextMenuCapture: function onContextMenuCapture(e) {
  329. return e.preventDefault();
  330. }
  331. }),
  332. _react2.default.createElement('rect', {
  333. className: 'rv-highlight',
  334. pointerEvents: 'none',
  335. opacity: opacity,
  336. fill: color,
  337. x: left,
  338. y: top,
  339. width: Math.min(Math.max(0, right - left), touchWidth),
  340. height: Math.min(Math.max(0, bottom - top), touchHeight)
  341. })
  342. );
  343. }
  344. }]);
  345. return Highlight;
  346. }(_abstractSeries2.default);
  347. Highlight.displayName = 'HighlightOverlay';
  348. Highlight.defaultProps = {
  349. color: 'rgb(77, 182, 172)',
  350. className: '',
  351. enableX: true,
  352. enableY: true,
  353. opacity: 0.3
  354. };
  355. Highlight.propTypes = _extends({}, _abstractSeries2.default.propTypes, {
  356. enableX: _propTypes2.default.bool,
  357. enableY: _propTypes2.default.bool,
  358. highlightHeight: _propTypes2.default.number,
  359. highlightWidth: _propTypes2.default.number,
  360. highlightX: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  361. highlightY: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  362. onBrushStart: _propTypes2.default.func,
  363. onDragStart: _propTypes2.default.func,
  364. onBrush: _propTypes2.default.func,
  365. onDrag: _propTypes2.default.func,
  366. onBrushEnd: _propTypes2.default.func,
  367. onDragEnd: _propTypes2.default.func
  368. });
  369. exports.default = Highlight;