react-financial-charts
Version:
React charts specific to finance.
161 lines • 7.74 kB
JavaScript
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 * as React from "react";
import { isDefined, isNotDefined, noop } from "../utils";
import { HoverTextNearMouse } from "./components/HoverTextNearMouse";
import { MouseLocationIndicator } from "./components/MouseLocationIndicator";
import { isHoverForInteractiveType, saveNodeType, terminate, } from "./utils";
import { EachFibRetracement } from "./wrapper/EachFibRetracement";
export class FibonacciRetracement extends React.Component {
constructor(props) {
super(props);
this.handleDrawRetracement = (xyValue) => {
const { current } = this.state;
if (isDefined(current) && isDefined(current.x1)) {
this.mouseMoved = true;
this.setState({
current: Object.assign(Object.assign({}, current), { x2: xyValue[0], y2: xyValue[1] }),
});
}
};
this.handleEdge1Drag = (echo, newXYValue, origXYValue) => {
const { retracements } = this.props;
const { index } = echo;
const dx = origXYValue.x1Value - newXYValue.x1Value;
this.setState({
override: {
index,
x1: retracements[index].x1 - dx,
y1: retracements[index].y1,
x2: retracements[index].x2,
y2: retracements[index].y2,
},
});
};
this.handleDrag = (index, xy) => {
this.setState({
override: Object.assign({ index }, xy),
});
};
this.handleEdge2Drag = (echo, newXYValue, origXYValue) => {
const { retracements } = this.props;
const { index } = echo;
const dx = origXYValue.x2Value - newXYValue.x2Value;
this.setState({
override: {
index,
x1: retracements[index].x1,
y1: retracements[index].y1,
x2: retracements[index].x2 - dx,
y2: retracements[index].y2,
},
});
};
this.handleDragComplete = (moreProps) => {
const { retracements } = this.props;
const { override } = this.state;
if (isDefined(override)) {
const { index } = override, rest = __rest(override, ["index"]);
const newRetracements = retracements.map((each, idx) => (idx === index
? Object.assign(Object.assign(Object.assign({}, each), rest), { selected: true }) : each));
this.setState({
override: null,
}, () => {
this.props.onComplete(newRetracements, moreProps);
});
}
};
this.handleStart = (xyValue, moreProps) => {
const { current } = this.state;
if (isNotDefined(current) || isNotDefined(current.x1)) {
this.mouseMoved = false;
this.setState({
current: {
x1: xyValue[0],
y1: xyValue[1],
x2: null,
y2: null,
},
}, () => {
this.props.onStart(moreProps);
});
}
};
this.handleEnd = (xyValue, moreProps, e) => {
const { retracements, appearance, type } = this.props;
const { current } = this.state;
if (this.mouseMoved && isDefined(current) && isDefined(current.x1)) {
const newRetracements = retracements.concat(Object.assign(Object.assign({}, current), { x2: xyValue[0], y2: xyValue[1], selected: true, appearance,
type }));
this.setState({
current: null,
}, () => {
this.props.onComplete(newRetracements, moreProps, e);
});
}
};
this.handleEdge1Drag = this.handleEdge1Drag.bind(this);
this.handleEdge2Drag = this.handleEdge2Drag.bind(this);
this.terminate = terminate.bind(this);
this.getSelectionState = isHoverForInteractiveType("retracements")
.bind(this);
this.saveNodeType = saveNodeType.bind(this);
this.state = {};
}
render() {
const { current, override } = this.state;
const { appearance, currentPositionStroke, currentPositionOpacity, currentPositionStrokeWidth, currentPositionRadius = FibonacciRetracement.defaultProps.currentPositionRadius, retracements, type, } = this.props;
const { enabled, hoverText } = this.props;
const overrideIndex = isDefined(override) ? override.index : null;
const hoverTextWidthDefault = Object.assign(Object.assign({}, FibonacciRetracement.defaultProps.hoverText), hoverText);
const currentRetracement = isDefined(current) && isDefined(current.x2)
? React.createElement(EachFibRetracement, Object.assign({ interactive: false, type: type, appearance: appearance, hoverText: hoverTextWidthDefault }, current))
: null;
return (React.createElement("g", null,
retracements.map((each, idx) => {
const eachAppearance = isDefined(each.appearance)
? Object.assign(Object.assign({}, appearance), each.appearance) : appearance;
const eachHoverText = isDefined(each.hoverText)
? Object.assign(Object.assign({}, hoverTextWidthDefault), each.hoverText) : hoverTextWidthDefault;
return (React.createElement(EachFibRetracement, Object.assign({ key: idx, ref: this.saveNodeType(idx), index: idx, type: each.type, selected: each.selected }, (idx === overrideIndex ? override : each), { hoverText: eachHoverText, appearance: eachAppearance, onDrag: this.handleDrag, onDragComplete: this.handleDragComplete })));
}),
currentRetracement,
React.createElement(MouseLocationIndicator, { enabled: enabled, snap: false, r: currentPositionRadius, stroke: currentPositionStroke, opacity: currentPositionOpacity, strokeWidth: currentPositionStrokeWidth, onMouseDown: this.handleStart, onClick: this.handleEnd, onMouseMove: this.handleDrawRetracement })));
}
}
FibonacciRetracement.defaultProps = {
enabled: true,
type: "RAY",
retracements: [],
onStart: noop,
onComplete: noop,
onSelect: noop,
hoverText: Object.assign(Object.assign({}, HoverTextNearMouse.defaultProps), { enable: true, bgHeight: "auto", bgWidth: "auto", text: "Click to select object", selectedText: "" }),
currentPositionStroke: "#000000",
currentPositionOpacity: 1,
currentPositionStrokeWidth: 3,
currentPositionRadius: 4,
appearance: {
stroke: "#000000",
strokeWidth: 1,
strokeOpacity: 1,
fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
fontSize: 11,
fontFill: "#000000",
edgeStroke: "#000000",
edgeFill: "#FFFFFF",
nsEdgeFill: "#000000",
edgeStrokeWidth: 1,
r: 5,
},
};
//# sourceMappingURL=FibonacciRetracement.js.map