UNPKG

@react-financial-charts/interactive

Version:
89 lines 4.72 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { getCurrentItem } from "@react-financial-charts/core/lib/utils/ChartDataUtil"; import * as React from "react"; import { isHover, saveNodeType } from "../utils"; import { HoverTextNearMouse, ClickableCircle } from "../components"; import { edge1Provider, edge2Provider, LinearRegressionChannelWithArea, } from "../components/LinearRegressionChannelWithArea"; export class EachLinearRegressionChannel extends React.Component { constructor(props) { super(props); this.handleHover = (_, moreProps) => { if (this.state.hover !== moreProps.hovering) { this.setState({ hover: moreProps.hovering, }); } }; this.handleEdge2Drag = (e, moreProps) => { const { index, onDrag, snapTo, x1Value } = this.props; if (onDrag === undefined) { return; } const [x2Value] = getNewXY(moreProps, snapTo); onDrag(e, index, { x1Value, x2Value, }); }; this.handleEdge1Drag = (e, moreProps) => { const { index, onDrag, snapTo, x2Value } = this.props; if (onDrag === undefined) { return; } const [x1Value] = getNewXY(moreProps, snapTo); onDrag(e, index, { x1Value, x2Value, }); }; this.isHover = isHover.bind(this); this.saveNodeType = saveNodeType.bind(this); this.state = { hover: false, }; } render() { const { x1Value, x2Value, appearance, edgeInteractiveCursor, hoverText, interactive, selected, onDragComplete, } = this.props; const { stroke, strokeWidth, fill, r, edgeStrokeWidth, edgeFill, edgeStroke } = appearance; const { hover } = this.state; const hoverHandler = interactive ? { onHover: this.handleHover, onUnHover: this.handleHover } : {}; const { enable: hoverTextEnabled, selectedText: hoverTextSelected, text: hoverTextUnselected } = hoverText, restHoverTextProps = __rest(hoverText, ["enable", "selectedText", "text"]); return (React.createElement("g", null, React.createElement(LinearRegressionChannelWithArea, Object.assign({ ref: this.saveNodeType("area"), selected: selected || hover }, hoverHandler, { x1Value: x1Value, x2Value: x2Value, fillStyle: fill, strokeStyle: stroke, strokeWidth: hover || selected ? strokeWidth + 1 : strokeWidth })), React.createElement(ClickableCircle, { ref: this.saveNodeType("edge1"), show: selected || hover, xyProvider: edge1Provider(this.props), r: r, fillStyle: edgeFill, strokeStyle: edgeStroke, strokeWidth: edgeStrokeWidth, interactiveCursorClass: edgeInteractiveCursor, onDrag: this.handleEdge1Drag, onDragComplete: onDragComplete }), React.createElement(ClickableCircle, { ref: this.saveNodeType("edge2"), show: selected || hover, xyProvider: edge2Provider(this.props), r: r, fillStyle: edgeFill, strokeStyle: edgeStroke, strokeWidth: edgeStrokeWidth, interactiveCursorClass: edgeInteractiveCursor, onDrag: this.handleEdge2Drag, onDragComplete: onDragComplete }), React.createElement(HoverTextNearMouse, Object.assign({ show: hoverTextEnabled && hover }, restHoverTextProps, { text: selected ? hoverTextSelected : hoverTextUnselected })))); } } EachLinearRegressionChannel.defaultProps = { appearance: { stroke: "#000000", strokeWidth: 1, fill: "rgba(138, 175, 226, 0.7)", edgeStrokeWidth: 2, edgeStroke: "#000000", edgeFill: "#FFFFFF", r: 5, }, interactive: true, selected: false, hoverText: Object.assign(Object.assign({}, HoverTextNearMouse.defaultProps), { enable: true, bgHeight: 18, bgWidth: 175, text: "Click and drag the edge circles" }), }; export function getNewXY(moreProps, snapTo) { const { xScale, xAccessor, plotData, mouseXY } = moreProps; const currentItem = getCurrentItem(xScale, xAccessor, mouseXY, plotData); const x = xAccessor(currentItem); const y = snapTo(currentItem); return [x, y]; } //# sourceMappingURL=EachLinearRegressionChannel.js.map