@react-financial-charts/interactive
Version:
Interactive features for react-financial-charts
128 lines • 6.11 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 "@react-financial-charts/core";
import { HoverTextNearMouse, MouseLocationIndicator } from "./components";
import { isHoverForInteractiveType, saveNodeType, terminate } from "./utils";
import { EachGannFan } from "./wrapper";
export class GannFan extends React.Component {
constructor(props) {
super(props);
this.handleEnd = (e, _, moreProps) => {
const { current } = this.state;
const { fans, appearance } = this.props;
if (this.mouseMoved && isDefined(current) && isDefined(current.startXY)) {
const newfans = [
...fans.map((d) => (Object.assign(Object.assign({}, d), { selected: false }))),
Object.assign(Object.assign({}, current), { selected: true, appearance }),
];
this.setState({
current: null,
}, () => {
const { onComplete } = this.props;
if (onComplete !== undefined) {
onComplete(e, newfans, moreProps);
}
});
}
};
this.handleStart = (_, xyValue) => {
const { current } = this.state;
if (isNotDefined(current) || isNotDefined(current.startXY)) {
this.mouseMoved = false;
this.setState({
current: {
startXY: xyValue,
endXY: null,
},
}, () => {
const { onStart } = this.props;
if (onStart !== undefined) {
onStart();
}
});
}
};
this.handleDrawFan = (_, xyValue) => {
const { current } = this.state;
if (isDefined(current) && isDefined(current.startXY)) {
this.mouseMoved = true;
this.setState({
current: {
startXY: current.startXY,
endXY: xyValue,
},
});
}
};
this.handleDragFanComplete = (e, moreProps) => {
const { override } = this.state;
const { fans } = this.props;
if (isDefined(override)) {
const { index } = override, rest = __rest(override, ["index"]);
const newfans = fans.map((each, idx) => (idx === index ? Object.assign(Object.assign(Object.assign({}, each), rest), { selected: true }) : each));
this.setState({
override: null,
}, () => {
this.props.onComplete(e, newfans, moreProps);
});
}
};
this.handleDragFan = (_, index, newXYValue) => {
this.setState({
override: Object.assign({ index }, newXYValue),
});
};
this.terminate = terminate.bind(this);
this.saveNodeType = saveNodeType.bind(this);
this.getSelectionState = isHoverForInteractiveType("fans").bind(this);
this.state = {};
}
render() {
const { appearance, currentPositionOpacity, currentPositionRadius = GannFan.defaultProps.currentPositionRadius, currentPositionStroke, currentPositionStrokeWidth, enabled, fans, hoverText, } = this.props;
const { current, override } = this.state;
const overrideIndex = isDefined(override) ? override.index : null;
const tempChannel = isDefined(current) && isDefined(current.endXY) ? (React.createElement(EachGannFan, Object.assign({ interactive: false }, current, { appearance: appearance, hoverText: hoverText }))) : null;
return (React.createElement("g", null,
fans.map((each, idx) => {
const eachAppearance = isDefined(each.appearance)
? Object.assign(Object.assign({}, appearance), each.appearance) : appearance;
return (React.createElement(EachGannFan, Object.assign({ key: idx, ref: this.saveNodeType(idx), index: idx, selected: each.selected }, (idx === overrideIndex ? override : each), { appearance: eachAppearance, hoverText: hoverText, onDrag: this.handleDragFan, onDragComplete: this.handleDragFanComplete })));
}),
tempChannel,
React.createElement(MouseLocationIndicator, { enabled: enabled, snap: false, r: currentPositionRadius, stroke: currentPositionStroke, opacity: currentPositionOpacity, strokeWidth: currentPositionStrokeWidth, onMouseDown: this.handleStart, onClick: this.handleEnd, onMouseMove: this.handleDrawFan })));
}
}
GannFan.defaultProps = {
appearance: {
stroke: "#000000",
fillOpacity: 0.2,
strokeOpacity: 1,
strokeWidth: 1,
edgeStroke: "#000000",
edgeFill: "#FFFFFF",
edgeStrokeWidth: 1,
r: 5,
fill: ["#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33", "#a65628", "#f781bf"],
fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
fontSize: 12,
fontFill: "#000000",
},
onSelect: noop,
currentPositionStroke: "#000000",
currentPositionOpacity: 1,
currentPositionStrokeWidth: 3,
currentPositionRadius: 4,
hoverText: Object.assign(Object.assign({}, HoverTextNearMouse.defaultProps), { enable: true, bgHeight: 18, bgWidth: 120, text: "Click to select object" }),
fans: [],
};
//# sourceMappingURL=GannFan.js.map