forked from react-financial/react-financial-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InteractiveYCoordinate.tsx
205 lines (193 loc) · 6.96 KB
/
InteractiveYCoordinate.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { format } from "d3-format";
import * as PropTypes from "prop-types";
import * as React from "react";
import { isDefined, strokeDashTypes } from "@react-financial-charts/core";
import { HoverTextNearMouse } from "./components";
import { getValueFromOverride, isHoverForInteractiveType, saveNodeType, terminate } from "./utils";
import { EachInteractiveYCoordinate } from "./wrapper";
interface InteractiveYCoordinateProps {
readonly onChoosePosition: (e: React.MouseEvent, newText: any, moreProps: any) => void;
readonly onDragComplete: (e: React.MouseEvent, newAlertList: any[], moreProps: any, draggedAlert: any) => void;
readonly onSelect?: (e: React.MouseEvent, interactives: any[], moreProps: any) => void;
readonly onDelete?: (e: React.MouseEvent, yCoordinate: any, moreProps: any) => void;
readonly defaultPriceCoordinate: {
readonly bgFill: string;
readonly bgOpacity: number;
readonly stroke: string;
readonly strokeDasharray: strokeDashTypes;
readonly strokeOpacity: number;
readonly strokeWidth: number;
readonly textFill: string;
readonly fontFamily: string;
readonly fontWeight: string;
readonly fontStyle: string;
readonly fontSize: number;
readonly text: string;
readonly textBox: {
readonly height: number;
readonly left: number;
readonly padding: {
left: number;
right: number;
};
readonly closeIcon: {
padding: {
left: number;
right: number;
};
width: number;
};
};
readonly edge: {
readonly stroke: string;
readonly strokeOpacity: number;
readonly strokeWidth: number;
readonly fill: string;
readonly fillOpacity: number;
};
};
readonly hoverText: object;
readonly yCoordinateList: any[];
readonly enabled: boolean;
}
interface InteractiveYCoordinateState {
current?: any;
override?: any;
}
export class InteractiveYCoordinate extends React.Component<InteractiveYCoordinateProps, InteractiveYCoordinateState> {
public static defaultProps = {
defaultPriceCoordinate: {
bgFill: "#FFFFFF",
bgOpacity: 1,
stroke: "#6574CD",
strokeOpacity: 1,
strokeDasharray: "ShortDash2",
strokeWidth: 1,
textFill: "#6574CD",
fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
fontSize: 12,
fontStyle: "normal",
fontWeight: "normal",
text: "Alert",
textBox: {
height: 24,
left: 20,
padding: { left: 10, right: 5 },
closeIcon: {
padding: { left: 5, right: 8 },
width: 8,
},
},
edge: {
stroke: "#6574CD",
strokeOpacity: 1,
strokeWidth: 1,
fill: "#FFFFFF",
fillOpacity: 1,
orient: "right",
at: "right",
arrowWidth: 10,
dx: 0,
rectWidth: 50,
rectHeight: 20,
displayFormat: format(".2f"),
},
},
hoverText: {
...HoverTextNearMouse.defaultProps,
enable: true,
bgHeight: 18,
bgWidth: 175,
text: "Click and drag the edge circles",
},
yCoordinateList: [],
};
public static contextTypes = {
subscribe: PropTypes.func.isRequired,
unsubscribe: PropTypes.func.isRequired,
generateSubscriptionId: PropTypes.func.isRequired,
chartId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
};
// @ts-ignore
private getSelectionState: any;
private saveNodeType: any;
// @ts-ignore
private terminate: any;
public constructor(props: InteractiveYCoordinateProps) {
super(props);
this.terminate = terminate.bind(this);
this.saveNodeType = saveNodeType.bind(this);
this.getSelectionState = isHoverForInteractiveType("yCoordinateList").bind(this);
this.state = {};
}
public render() {
const { yCoordinateList } = this.props;
const { override } = this.state;
return (
<g>
{yCoordinateList.map((each, idx) => {
const props = each;
return (
<EachInteractiveYCoordinate
key={each.id}
ref={this.saveNodeType(idx)}
index={idx}
{...props}
selected={each.selected}
yValue={getValueFromOverride(override, idx, "yValue", each.yValue)}
onDelete={this.handleDelete}
onDrag={this.handleDrag}
onDragComplete={this.handleDragComplete}
edgeInteractiveCursor="react-financial-charts-move-cursor"
/>
);
})}
</g>
);
}
private readonly handleDelete = (e: React.MouseEvent, index: number | undefined, moreProps: any) => {
const { onDelete, yCoordinateList } = this.props;
if (onDelete !== undefined && index !== undefined) {
onDelete(e, yCoordinateList[index], moreProps);
}
};
private readonly handleDragComplete = (e: React.MouseEvent, moreProps: any) => {
const { override } = this.state;
if (isDefined(override)) {
const { yCoordinateList } = this.props;
const newAlertList = yCoordinateList.map((each, idx) => {
const selected = idx === override.index;
return selected
? {
...each,
yValue: override.yValue,
selected,
}
: {
...each,
selected,
};
});
const draggedAlert = newAlertList[override.index];
this.setState(
{
override: null,
},
() => {
const { onDragComplete } = this.props;
if (onDragComplete !== undefined) {
onDragComplete(e, newAlertList, moreProps, draggedAlert);
}
},
);
}
};
private readonly handleDrag = (_: React.MouseEvent, index: any, yValue: any) => {
this.setState({
override: {
index,
yValue,
},
});
};
}