UNPKG

@react-financial-charts/interactive

Version:
125 lines 6.46 kB
import * as React from "react"; import { isDefined, isNotDefined } from "@react-financial-charts/core"; import { getValueFromOverride, isHoverForInteractiveType, saveNodeType, terminate } from "./utils"; import { HoverTextNearMouse, MouseLocationIndicator } from "./components"; import { EachLinearRegressionChannel } from "./wrapper"; export class StandardDeviationChannel extends React.Component { constructor(props) { super(props); this.handleEnd = (e, xyValue, moreProps) => { const { current } = this.state; const { appearance, channels } = this.props; if (this.mouseMoved && isDefined(current) && isDefined(current.start)) { const newChannels = [ ...channels.map((d) => (Object.assign(Object.assign({}, d), { selected: false }))), { start: current.start, end: xyValue, selected: true, appearance, }, ]; this.setState({ current: null, }, () => { const { onComplete } = this.props; if (onComplete !== undefined) { onComplete(e, newChannels, moreProps); } }); } }; this.handleStart = (_, xyValue) => { const { current } = this.state; if (isNotDefined(current) || isNotDefined(current.start)) { this.mouseMoved = false; this.setState({ current: { start: xyValue, end: null, }, }, () => { const { onStart } = this.props; if (onStart !== undefined) { onStart(); } }); } }; this.handleDrawLine = (e, xyValue) => { const { current } = this.state; if (isDefined(current) && isDefined(current.start)) { this.mouseMoved = true; this.setState({ current: { start: current.start, end: xyValue, }, }); } }; this.handleDragLineComplete = (e, moreProps) => { const { override } = this.state; const { channels } = this.props; if (isDefined(override)) { const newChannels = channels.map((each, idx) => idx === override.index ? Object.assign(Object.assign({}, each), { start: [override.x1Value, override.y1Value], end: [override.x2Value, override.y2Value], selected: true }) : each); this.setState({ override: null, }, () => { const { onComplete } = this.props; if (onComplete !== undefined) { onComplete(e, newChannels, moreProps); } }); } }; this.handleDragLine = (e, index, newXYValue) => { this.setState({ override: Object.assign({ index }, newXYValue), }); }; this.terminate = terminate.bind(this); this.saveNodeType = saveNodeType.bind(this); this.getSelectionState = isHoverForInteractiveType("channels").bind(this); this.state = {}; } render() { const { appearance, channels, currentPositionOpacity, currentPositionRadius = StandardDeviationChannel.defaultProps.currentPositionRadius, currentPositionStroke, currentPositionStrokeWidth, enabled, hoverText, snapTo, } = this.props; const { current, override } = this.state; const eachDefaultAppearance = Object.assign(Object.assign({}, StandardDeviationChannel.defaultProps.appearance), appearance); const hoverTextDefault = Object.assign(Object.assign({}, StandardDeviationChannel.defaultProps.hoverText), hoverText); const tempLine = isDefined(current) && isDefined(current.end) ? (React.createElement(EachLinearRegressionChannel, { interactive: false, x1Value: current.start[0], x2Value: current.end[0], appearance: eachDefaultAppearance, hoverText: hoverTextDefault })) : null; return (React.createElement("g", null, channels.map((each, idx) => { const eachAppearance = isDefined(each.appearance) ? Object.assign(Object.assign({}, eachDefaultAppearance), each.appearance) : eachDefaultAppearance; const eachHoverText = isDefined(each.hoverText) ? Object.assign(Object.assign({}, hoverTextDefault), each.hoverText) : hoverTextDefault; return (React.createElement(EachLinearRegressionChannel, { key: idx, ref: this.saveNodeType(idx), index: idx, selected: each.selected, x1Value: getValueFromOverride(override, idx, "x1Value", each.start[0]), x2Value: getValueFromOverride(override, idx, "x2Value", each.end[0]), appearance: eachAppearance, snapTo: snapTo, hoverText: eachHoverText, onDrag: this.handleDragLine, onDragComplete: this.handleDragLineComplete, edgeInteractiveCursor: "react-financial-charts-move-cursor" })); }), tempLine, React.createElement(MouseLocationIndicator, { enabled: enabled, snap: true, snapTo: snapTo, r: currentPositionRadius, stroke: currentPositionStroke, opacity: currentPositionOpacity, strokeWidth: currentPositionStrokeWidth, onMouseDown: this.handleStart, onClick: this.handleEnd, onMouseMove: this.handleDrawLine }))); } } StandardDeviationChannel.defaultProps = { snapTo: (d) => d.close, appearance: { stroke: "#000000", fillOpacity: 0.2, strokeOpacity: 1, strokeWidth: 1, fill: "#8AAFE2", edgeStrokeWidth: 2, edgeStroke: "#000000", edgeFill: "#FFFFFF", r: 5, }, currentPositionStroke: "#000000", currentPositionOpacity: 1, currentPositionStrokeWidth: 3, currentPositionRadius: 4, hoverText: Object.assign(Object.assign({}, HoverTextNearMouse.defaultProps), { enable: true, bgHeight: "auto", bgWidth: "auto", text: "Click and drag the edge circles", selectedText: "" }), channels: [], }; //# sourceMappingURL=StandardDeviationChannel.js.map