@ivanmatiaspascual/interactive 1.3.1-sp1-beta.0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/CHANGELOG.md +190 -0
  2. package/LICENSE +24 -0
  3. package/README.md +5 -0
  4. package/lib/Brush.d.ts +35 -0
  5. package/lib/Brush.js +103 -0
  6. package/lib/Brush.js.map +1 -0
  7. package/lib/ClickCallback.d.ts +18 -0
  8. package/lib/ClickCallback.js +12 -0
  9. package/lib/ClickCallback.js.map +1 -0
  10. package/lib/DrawingObjectSelector.d.ts +18 -0
  11. package/lib/DrawingObjectSelector.js +73 -0
  12. package/lib/DrawingObjectSelector.js.map +1 -0
  13. package/lib/EquidistantChannel.d.ts +74 -0
  14. package/lib/EquidistantChannel.js +149 -0
  15. package/lib/EquidistantChannel.js.map +1 -0
  16. package/lib/FibonacciRetracement.d.ts +83 -0
  17. package/lib/FibonacciRetracement.js +163 -0
  18. package/lib/FibonacciRetracement.js.map +1 -0
  19. package/lib/GannFan.d.ts +78 -0
  20. package/lib/GannFan.js +128 -0
  21. package/lib/GannFan.js.map +1 -0
  22. package/lib/InteractiveText.d.ts +70 -0
  23. package/lib/InteractiveText.js +89 -0
  24. package/lib/InteractiveText.js.map +1 -0
  25. package/lib/InteractiveYCoordinate.d.ts +128 -0
  26. package/lib/InteractiveYCoordinate.js +106 -0
  27. package/lib/InteractiveYCoordinate.js.map +1 -0
  28. package/lib/StandardDeviationChannel.d.ts +74 -0
  29. package/lib/StandardDeviationChannel.js +125 -0
  30. package/lib/StandardDeviationChannel.js.map +1 -0
  31. package/lib/TrendLine.d.ts +77 -0
  32. package/lib/TrendLine.js +125 -0
  33. package/lib/TrendLine.js.map +1 -0
  34. package/lib/ZoomButtons.d.ts +37 -0
  35. package/lib/ZoomButtons.js +85 -0
  36. package/lib/ZoomButtons.js.map +1 -0
  37. package/lib/components/ChannelWithArea.d.ts +30 -0
  38. package/lib/components/ChannelWithArea.js +126 -0
  39. package/lib/components/ChannelWithArea.js.map +1 -0
  40. package/lib/components/ClickableCircle.d.ts +26 -0
  41. package/lib/components/ClickableCircle.js +48 -0
  42. package/lib/components/ClickableCircle.js.map +1 -0
  43. package/lib/components/ClickableShape.d.ts +33 -0
  44. package/lib/components/ClickableShape.js +64 -0
  45. package/lib/components/ClickableShape.js.map +1 -0
  46. package/lib/components/GannFan.d.ts +32 -0
  47. package/lib/components/GannFan.js +134 -0
  48. package/lib/components/GannFan.js.map +1 -0
  49. package/lib/components/HoverTextNearMouse.d.ts +35 -0
  50. package/lib/components/HoverTextNearMouse.js +113 -0
  51. package/lib/components/HoverTextNearMouse.js.map +1 -0
  52. package/lib/components/InteractiveStraightLine.d.ts +57 -0
  53. package/lib/components/InteractiveStraightLine.js +220 -0
  54. package/lib/components/InteractiveStraightLine.js.map +1 -0
  55. package/lib/components/InteractiveText.d.ts +37 -0
  56. package/lib/components/InteractiveText.js +84 -0
  57. package/lib/components/InteractiveText.js.map +1 -0
  58. package/lib/components/InteractiveYCoordinate.d.ts +46 -0
  59. package/lib/components/InteractiveYCoordinate.js +109 -0
  60. package/lib/components/InteractiveYCoordinate.js.map +1 -0
  61. package/lib/components/LinearRegressionChannelWithArea.d.ts +31 -0
  62. package/lib/components/LinearRegressionChannelWithArea.js +104 -0
  63. package/lib/components/LinearRegressionChannelWithArea.js.map +1 -0
  64. package/lib/components/MouseLocationIndicator.d.ts +34 -0
  65. package/lib/components/MouseLocationIndicator.js +79 -0
  66. package/lib/components/MouseLocationIndicator.js.map +1 -0
  67. package/lib/components/Text.d.ts +17 -0
  68. package/lib/components/Text.js +26 -0
  69. package/lib/components/Text.js.map +1 -0
  70. package/lib/components/index.d.ts +11 -0
  71. package/lib/components/index.js +12 -0
  72. package/lib/components/index.js.map +1 -0
  73. package/lib/index.d.ts +12 -0
  74. package/lib/index.js +13 -0
  75. package/lib/index.js.map +1 -0
  76. package/lib/utils.d.ts +7 -0
  77. package/lib/utils.js +84 -0
  78. package/lib/utils.js.map +1 -0
  79. package/lib/wrapper/EachEquidistantChannel.d.ts +59 -0
  80. package/lib/wrapper/EachEquidistantChannel.js +172 -0
  81. package/lib/wrapper/EachEquidistantChannel.js.map +1 -0
  82. package/lib/wrapper/EachFibRetracement.d.ts +77 -0
  83. package/lib/wrapper/EachFibRetracement.js +197 -0
  84. package/lib/wrapper/EachFibRetracement.js.map +1 -0
  85. package/lib/wrapper/EachGannFan.d.ts +81 -0
  86. package/lib/wrapper/EachGannFan.js +161 -0
  87. package/lib/wrapper/EachGannFan.js.map +1 -0
  88. package/lib/wrapper/EachInteractiveYCoordinate.d.ts +50 -0
  89. package/lib/wrapper/EachInteractiveYCoordinate.js +80 -0
  90. package/lib/wrapper/EachInteractiveYCoordinate.js.map +1 -0
  91. package/lib/wrapper/EachLinearRegressionChannel.d.ts +75 -0
  92. package/lib/wrapper/EachLinearRegressionChannel.js +89 -0
  93. package/lib/wrapper/EachLinearRegressionChannel.js.map +1 -0
  94. package/lib/wrapper/EachText.d.ts +58 -0
  95. package/lib/wrapper/EachText.js +79 -0
  96. package/lib/wrapper/EachText.js.map +1 -0
  97. package/lib/wrapper/EachTrendLine.d.ts +76 -0
  98. package/lib/wrapper/EachTrendLine.js +141 -0
  99. package/lib/wrapper/EachTrendLine.js.map +1 -0
  100. package/lib/wrapper/index.d.ts +7 -0
  101. package/lib/wrapper/index.js +8 -0
  102. package/lib/wrapper/index.js.map +1 -0
  103. package/package.json +55 -0
  104. package/src/Brush.tsx +177 -0
  105. package/src/ClickCallback.tsx +37 -0
  106. package/src/DrawingObjectSelector.tsx +97 -0
  107. package/src/EquidistantChannel.tsx +268 -0
  108. package/src/FibonacciRetracement.tsx +298 -0
  109. package/src/GannFan.tsx +233 -0
  110. package/src/InteractiveText.tsx +185 -0
  111. package/src/InteractiveYCoordinate.tsx +205 -0
  112. package/src/StandardDeviationChannel.tsx +269 -0
  113. package/src/TrendLine.tsx +283 -0
  114. package/src/ZoomButtons.tsx +172 -0
  115. package/src/components/ChannelWithArea.tsx +199 -0
  116. package/src/components/ClickableCircle.tsx +91 -0
  117. package/src/components/ClickableShape.tsx +114 -0
  118. package/src/components/GannFan.tsx +189 -0
  119. package/src/components/HoverTextNearMouse.tsx +174 -0
  120. package/src/components/InteractiveStraightLine.tsx +317 -0
  121. package/src/components/InteractiveText.tsx +153 -0
  122. package/src/components/InteractiveYCoordinate.tsx +205 -0
  123. package/src/components/LinearRegressionChannelWithArea.tsx +169 -0
  124. package/src/components/MouseLocationIndicator.tsx +128 -0
  125. package/src/components/Text.tsx +47 -0
  126. package/src/components/index.ts +11 -0
  127. package/src/index.ts +12 -0
  128. package/src/utils.ts +101 -0
  129. package/src/wrapper/EachEquidistantChannel.tsx +302 -0
  130. package/src/wrapper/EachFibRetracement.tsx +342 -0
  131. package/src/wrapper/EachGannFan.tsx +289 -0
  132. package/src/wrapper/EachInteractiveYCoordinate.tsx +198 -0
  133. package/src/wrapper/EachLinearRegressionChannel.tsx +202 -0
  134. package/src/wrapper/EachText.tsx +183 -0
  135. package/src/wrapper/EachTrendLine.tsx +297 -0
  136. package/src/wrapper/index.ts +7 -0
@@ -0,0 +1,283 @@
1
+ import * as React from "react";
2
+ import { isDefined, isNotDefined, noop, strokeDashTypes } from "@ivanmatiaspascual/core";
3
+ import { getValueFromOverride, isHoverForInteractiveType, saveNodeType, terminate } from "./utils";
4
+ import { HoverTextNearMouse, MouseLocationIndicator, InteractiveStraightLine } from "./components";
5
+ import { EachTrendLine } from "./wrapper";
6
+
7
+ export interface TrendLineProps {
8
+ readonly snap: boolean;
9
+ readonly enabled: boolean;
10
+ readonly snapTo?: (datum: any) => number | number[];
11
+ readonly shouldDisableSnap?: (e: React.MouseEvent) => boolean;
12
+ readonly onStart: (e: React.MouseEvent, moreProps: any) => void;
13
+ readonly onComplete?: (e: React.MouseEvent, newTrends: any[], moreProps: any) => void;
14
+ readonly onSelect?: (e: React.MouseEvent, interactives: any[], moreProps: any) => void;
15
+ readonly currentPositionStroke?: string;
16
+ readonly currentPositionStrokeWidth?: number;
17
+ readonly currentPositionstrokeOpacity?: number;
18
+ readonly currentPositionRadius?: number;
19
+ readonly type:
20
+ | "XLINE" // extends from -Infinity to +Infinity
21
+ | "RAY" // extends to +/-Infinity in one direction
22
+ | "LINE"; // extends between the set bounds
23
+ readonly hoverText: object;
24
+ readonly trends: any[];
25
+ readonly appearance: {
26
+ readonly strokeStyle: string;
27
+ readonly strokeWidth: number;
28
+ readonly strokeDasharray: strokeDashTypes;
29
+ readonly edgeStrokeWidth: number;
30
+ readonly edgeFill: string;
31
+ readonly edgeStroke: string;
32
+ };
33
+ }
34
+
35
+ interface TrendLineState {
36
+ current?: any;
37
+ override?: any;
38
+ trends?: any;
39
+ }
40
+
41
+ export class TrendLine extends React.Component<TrendLineProps, TrendLineState> {
42
+ public static defaultProps = {
43
+ type: "XLINE",
44
+ onStart: noop,
45
+ onSelect: noop,
46
+ currentPositionStroke: "#000000",
47
+ currentPositionstrokeOpacity: 1,
48
+ currentPositionStrokeWidth: 3,
49
+ currentPositionRadius: 0,
50
+ shouldDisableSnap: (e: React.MouseEvent) => e.button === 2 || e.shiftKey,
51
+ hoverText: {
52
+ ...HoverTextNearMouse.defaultProps,
53
+ enable: true,
54
+ bgHeight: "auto",
55
+ bgWidth: "auto",
56
+ text: "Click to select object",
57
+ selectedText: "",
58
+ },
59
+ trends: [],
60
+ appearance: {
61
+ strokeStyle: "#000000",
62
+ strokeWidth: 1,
63
+ strokeDasharray: "Solid",
64
+ edgeStrokeWidth: 1,
65
+ edgeFill: "#FFFFFF",
66
+ edgeStroke: "#000000",
67
+ r: 6,
68
+ },
69
+ };
70
+
71
+ // @ts-ignore
72
+ private getSelectionState: any;
73
+ private mouseMoved: any;
74
+ private saveNodeType: any;
75
+ // @ts-ignore
76
+ private terminate: any;
77
+
78
+ public constructor(props: TrendLineProps) {
79
+ super(props);
80
+
81
+ this.terminate = terminate.bind(this);
82
+ this.saveNodeType = saveNodeType.bind(this);
83
+
84
+ this.getSelectionState = isHoverForInteractiveType("trends").bind(this);
85
+
86
+ this.state = {};
87
+ }
88
+
89
+ public render() {
90
+ const {
91
+ appearance,
92
+ currentPositionstrokeOpacity,
93
+ currentPositionRadius = TrendLine.defaultProps.currentPositionRadius,
94
+ currentPositionStroke,
95
+ currentPositionStrokeWidth,
96
+ enabled,
97
+ hoverText,
98
+ shouldDisableSnap,
99
+ snap,
100
+ snapTo,
101
+ trends,
102
+ type,
103
+ } = this.props;
104
+
105
+ const { current, override } = this.state;
106
+
107
+ const tempLine =
108
+ isDefined(current) && isDefined(current.end) ? (
109
+ <InteractiveStraightLine
110
+ type={type}
111
+ x1Value={current.start[0]}
112
+ y1Value={current.start[1]}
113
+ x2Value={current.end[0]}
114
+ y2Value={current.end[1]}
115
+ strokeStyle={appearance.strokeStyle}
116
+ strokeWidth={appearance.strokeWidth}
117
+ />
118
+ ) : null;
119
+
120
+ return (
121
+ <g>
122
+ {trends.map((each, idx) => {
123
+ const eachAppearance = isDefined(each.appearance)
124
+ ? { ...appearance, ...each.appearance }
125
+ : appearance;
126
+
127
+ const hoverTextWithDefault = {
128
+ ...TrendLine.defaultProps.hoverText,
129
+ ...hoverText,
130
+ };
131
+
132
+ return (
133
+ <EachTrendLine
134
+ key={idx}
135
+ ref={this.saveNodeType(idx)}
136
+ index={idx}
137
+ type={each.type}
138
+ selected={each.selected}
139
+ x1Value={getValueFromOverride(override, idx, "x1Value", each.start[0])}
140
+ y1Value={getValueFromOverride(override, idx, "y1Value", each.start[1])}
141
+ x2Value={getValueFromOverride(override, idx, "x2Value", each.end[0])}
142
+ y2Value={getValueFromOverride(override, idx, "y2Value", each.end[1])}
143
+ strokeStyle={eachAppearance.strokeStyle}
144
+ strokeWidth={eachAppearance.strokeWidth}
145
+ strokeOpacity={eachAppearance.strokeOpacity}
146
+ strokeDasharray={eachAppearance.strokeDasharray}
147
+ edgeStroke={eachAppearance.edgeStroke}
148
+ edgeFill={eachAppearance.edgeFill}
149
+ edgeStrokeWidth={eachAppearance.edgeStrokeWidth}
150
+ r={eachAppearance.r}
151
+ hoverText={hoverTextWithDefault}
152
+ onDrag={this.handleDragLine}
153
+ onDragComplete={this.handleDragLineComplete}
154
+ edgeInteractiveCursor="react-financial-charts-move-cursor"
155
+ lineInteractiveCursor="react-financial-charts-move-cursor"
156
+ />
157
+ );
158
+ })}
159
+ {tempLine}
160
+ <MouseLocationIndicator
161
+ enabled={enabled}
162
+ snap={snap}
163
+ shouldDisableSnap={shouldDisableSnap}
164
+ snapTo={snapTo}
165
+ r={currentPositionRadius}
166
+ stroke={currentPositionStroke}
167
+ opacity={currentPositionstrokeOpacity}
168
+ strokeWidth={currentPositionStrokeWidth}
169
+ onMouseDown={this.handleStart}
170
+ onClick={this.handleEnd}
171
+ onMouseMove={this.handleDrawLine}
172
+ />
173
+ </g>
174
+ );
175
+ }
176
+
177
+ private readonly handleEnd = (e: React.MouseEvent, xyValue: any, moreProps: any) => {
178
+ const { current } = this.state;
179
+ const { trends, appearance, type } = this.props;
180
+
181
+ if (this.mouseMoved && isDefined(current) && isDefined(current.start)) {
182
+ const newTrends = [
183
+ ...trends.map((d) => ({ ...d, selected: false })),
184
+ {
185
+ start: current.start,
186
+ end: xyValue,
187
+ selected: true,
188
+ appearance,
189
+ type,
190
+ },
191
+ ];
192
+ this.setState(
193
+ {
194
+ current: null,
195
+ trends: newTrends,
196
+ },
197
+ () => {
198
+ const { onComplete } = this.props;
199
+ if (onComplete !== undefined) {
200
+ onComplete(e, newTrends, moreProps);
201
+ }
202
+ },
203
+ );
204
+ }
205
+ };
206
+
207
+ private readonly handleStart = (e: React.MouseEvent, xyValue: any, moreProps: any) => {
208
+ const { current } = this.state;
209
+
210
+ if (isNotDefined(current) || isNotDefined(current.start)) {
211
+ this.mouseMoved = false;
212
+
213
+ this.setState(
214
+ {
215
+ current: {
216
+ start: xyValue,
217
+ end: null,
218
+ },
219
+ },
220
+ () => {
221
+ const { onStart } = this.props;
222
+ if (onStart !== undefined) {
223
+ onStart(e, moreProps);
224
+ }
225
+ },
226
+ );
227
+ }
228
+ };
229
+
230
+ private readonly handleDrawLine = (_: React.MouseEvent, xyValue: any) => {
231
+ const { current } = this.state;
232
+ if (isDefined(current) && isDefined(current.start)) {
233
+ this.mouseMoved = true;
234
+ this.setState({
235
+ current: {
236
+ start: current.start,
237
+ end: xyValue,
238
+ },
239
+ });
240
+ }
241
+ };
242
+
243
+ private readonly handleDragLineComplete = (e: React.MouseEvent, moreProps: any) => {
244
+ const { override } = this.state;
245
+ if (isDefined(override)) {
246
+ const { trends } = this.props;
247
+ const newTrends = trends.map((each, idx) =>
248
+ idx === override.index
249
+ ? {
250
+ ...each,
251
+ start: [override.x1Value, override.y1Value],
252
+ end: [override.x2Value, override.y2Value],
253
+ selected: true,
254
+ }
255
+ : {
256
+ ...each,
257
+ selected: false,
258
+ },
259
+ );
260
+
261
+ this.setState(
262
+ {
263
+ override: null,
264
+ },
265
+ () => {
266
+ const { onComplete } = this.props;
267
+ if (onComplete !== undefined) {
268
+ onComplete(e, newTrends, moreProps);
269
+ }
270
+ },
271
+ );
272
+ }
273
+ };
274
+
275
+ private readonly handleDragLine = (_: React.MouseEvent, index: number | undefined, newXYValue: any) => {
276
+ this.setState({
277
+ override: {
278
+ index,
279
+ ...newXYValue,
280
+ },
281
+ });
282
+ };
283
+ }
@@ -0,0 +1,172 @@
1
+ import { last } from "@ivanmatiaspascual/core";
2
+ import { interpolateNumber } from "d3-interpolate";
3
+ import * as PropTypes from "prop-types";
4
+ import * as React from "react";
5
+
6
+ export interface ZoomButtonsProps {
7
+ readonly fill: string;
8
+ readonly fillOpacity: number;
9
+ readonly heightFromBase: number;
10
+ readonly onReset?: () => void;
11
+ readonly r: number;
12
+ readonly stroke: string;
13
+ readonly strokeWidth: number;
14
+ readonly textFill: string;
15
+ readonly zoomMultiplier: number;
16
+ }
17
+
18
+ export class ZoomButtons extends React.Component<ZoomButtonsProps> {
19
+ public static defaultProps = {
20
+ fill: "#ffffff",
21
+ fillOpacity: 0.75,
22
+ heightFromBase: 32,
23
+ r: 16,
24
+ stroke: "#e0e3eb",
25
+ strokeWidth: 1,
26
+ textFill: "#000000",
27
+ zoomMultiplier: 1.5,
28
+ };
29
+
30
+ public static contextTypes = {
31
+ xScale: PropTypes.func.isRequired,
32
+ chartConfig: PropTypes.object.isRequired,
33
+ plotData: PropTypes.array.isRequired,
34
+ xAccessor: PropTypes.func.isRequired,
35
+ xAxisZoom: PropTypes.func.isRequired,
36
+ };
37
+
38
+ private interval?: number;
39
+
40
+ public render() {
41
+ const { chartConfig } = this.context;
42
+
43
+ const { width, height } = chartConfig;
44
+
45
+ const { heightFromBase, r, fill, fillOpacity, onReset, stroke, strokeWidth, textFill } = this.props;
46
+
47
+ const centerX = Math.round(width / 2);
48
+ const y = height - heightFromBase;
49
+
50
+ const zoomOutX = centerX - 16 - r * 2;
51
+ const zoomInX = centerX - 8;
52
+ const resetX = centerX + 16 + r * 2;
53
+
54
+ return (
55
+ <g className="react-financial-charts-zoom-buttons">
56
+ <circle
57
+ className="react-financial-charts-button"
58
+ cx={zoomOutX - r / 2}
59
+ cy={y + r / 2}
60
+ fill={fill}
61
+ fillOpacity={fillOpacity}
62
+ stroke={stroke}
63
+ strokeWidth={strokeWidth}
64
+ r={r}
65
+ />
66
+ <g transform={`translate (${zoomOutX - 20}, ${y - 8 + r / 4})`}>
67
+ <path d="M19,13H5V11H19V13Z" fill={textFill} />
68
+ </g>
69
+ <circle
70
+ className="react-financial-charts-button"
71
+ cx={zoomInX - r / 2}
72
+ cy={y + r / 2}
73
+ fill={fill}
74
+ fillOpacity={fillOpacity}
75
+ stroke={stroke}
76
+ strokeWidth={strokeWidth}
77
+ r={r}
78
+ />
79
+ <g transform={`translate (${zoomInX - 20}, ${y - 8 + r / 4})`}>
80
+ <path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6z" fill={textFill} />
81
+ </g>
82
+ <circle
83
+ className="react-financial-charts-button"
84
+ cx={resetX - r / 2}
85
+ cy={y + r / 2}
86
+ fill={fill}
87
+ fillOpacity={fillOpacity}
88
+ stroke={stroke}
89
+ strokeWidth={strokeWidth}
90
+ r={r}
91
+ />
92
+ <g transform={`translate (${resetX - r}, ${y - 4 + r / 4})`}>
93
+ <path
94
+ d="M2.35 2.35A7.958 7.958 0 018 0a8 8 0 110 16c-3.73 0-6.84-2.55-7.73-6h2.08c.82 2.33 3.04 4 5.65 4A6 6 0 108 2c-1.66 0-3.14.69-4.22 1.78L7 7H0V0l2.35 2.35z"
95
+ fill={textFill}
96
+ />
97
+ </g>
98
+ <circle
99
+ className="react-financial-charts-enable-interaction out"
100
+ onClick={this.handleZoomOut}
101
+ cx={zoomOutX - r / 2}
102
+ cy={y + r / 2}
103
+ r={r}
104
+ fill="none"
105
+ />
106
+ <circle
107
+ className="react-financial-charts-enable-interaction in"
108
+ onClick={this.handleZoomIn}
109
+ cx={zoomInX - r / 2}
110
+ cy={y + r / 2}
111
+ r={r}
112
+ fill="none"
113
+ />
114
+ <circle
115
+ className="react-financial-charts-enable-interaction reset"
116
+ onClick={onReset}
117
+ cx={resetX - r / 2}
118
+ cy={y + r / 2}
119
+ r={r}
120
+ fill="none"
121
+ />
122
+ </g>
123
+ );
124
+ }
125
+
126
+ private readonly handleZoomIn = () => {
127
+ if (this.interval) {
128
+ return;
129
+ }
130
+
131
+ this.zoom(-1);
132
+ };
133
+
134
+ private readonly handleZoomOut = () => {
135
+ if (this.interval) {
136
+ return;
137
+ }
138
+
139
+ this.zoom(1);
140
+ };
141
+
142
+ private readonly zoom = (direction: number) => {
143
+ const { xAxisZoom, xScale, plotData, xAccessor } = this.context;
144
+
145
+ const cx = xScale(xAccessor(last(plotData)));
146
+
147
+ const { zoomMultiplier } = this.props;
148
+
149
+ const c = direction > 0 ? 1 * zoomMultiplier : 1 / zoomMultiplier;
150
+
151
+ const [start, end] = xScale.domain();
152
+ const [newStart, newEnd] = xScale
153
+ .range()
154
+ .map((x: number) => cx + (x - cx) * c)
155
+ .map(xScale.invert);
156
+
157
+ const left = interpolateNumber(start, newStart);
158
+ const right = interpolateNumber(end, newEnd);
159
+
160
+ const foo = [0.25, 0.3, 0.5, 0.6, 0.75, 1].map((i) => {
161
+ return [left(i), right(i)];
162
+ });
163
+
164
+ this.interval = window.setInterval(() => {
165
+ xAxisZoom(foo.shift());
166
+ if (foo.length === 0) {
167
+ clearInterval(this.interval);
168
+ delete this.interval;
169
+ }
170
+ }, 10);
171
+ };
172
+ }
@@ -0,0 +1,199 @@
1
+ import * as React from "react";
2
+ import { isDefined, isNotDefined, getMouseCanvas, GenericChartComponent } from "@ivanmatiaspascual/core";
3
+ import { generateLine, isHovering } from "./InteractiveStraightLine";
4
+
5
+ export interface ChannelWithAreaProps {
6
+ readonly startXY?: number[];
7
+ readonly endXY?: number[];
8
+ readonly dy?: number;
9
+ readonly interactiveCursorClass?: string;
10
+ readonly strokeStyle: string;
11
+ readonly strokeWidth: number;
12
+ readonly fillStyle: string;
13
+ readonly type:
14
+ | "XLINE" // extends from -Infinity to +Infinity
15
+ | "RAY" // extends to +/-Infinity in one direction
16
+ | "LINE"; // extends between the set bounds
17
+ readonly onDragStart?: (e: React.MouseEvent, moreProps: any) => void;
18
+ readonly onDrag?: (e: React.MouseEvent, moreProps: any) => void;
19
+ readonly onDragComplete?: (e: React.MouseEvent, moreProps: any) => void;
20
+ readonly onHover?: (e: React.MouseEvent, moreProps: any) => void;
21
+ readonly onUnHover?: (e: React.MouseEvent, moreProps: any) => void;
22
+ readonly defaultClassName?: string;
23
+ readonly tolerance: number;
24
+ readonly selected: boolean;
25
+ }
26
+
27
+ export class ChannelWithArea extends React.Component<ChannelWithAreaProps> {
28
+ public static defaultProps = {
29
+ type: "LINE",
30
+ strokeWidth: 1,
31
+ tolerance: 4,
32
+ selected: false,
33
+ };
34
+
35
+ public render() {
36
+ const { selected, interactiveCursorClass } = this.props;
37
+ const { onDragStart, onDrag, onDragComplete, onHover, onUnHover } = this.props;
38
+
39
+ return (
40
+ <GenericChartComponent
41
+ isHover={this.isHover}
42
+ canvasToDraw={getMouseCanvas}
43
+ canvasDraw={this.drawOnCanvas}
44
+ interactiveCursorClass={interactiveCursorClass}
45
+ selected={selected}
46
+ onDragStart={onDragStart}
47
+ onDrag={onDrag}
48
+ onDragComplete={onDragComplete}
49
+ onHover={onHover}
50
+ onUnHover={onUnHover}
51
+ drawOn={["mousemove", "mouseleave", "pan", "drag"]}
52
+ />
53
+ );
54
+ }
55
+
56
+ private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
57
+ const { strokeStyle, strokeWidth, fillStyle } = this.props;
58
+ const { line1, line2 } = helper(this.props, moreProps);
59
+
60
+ if (line1 !== undefined) {
61
+ const { x1, y1, x2, y2 } = line1;
62
+
63
+ ctx.lineWidth = strokeWidth;
64
+ ctx.strokeStyle = strokeStyle;
65
+
66
+ ctx.beginPath();
67
+ ctx.moveTo(x1, y1);
68
+ ctx.lineTo(x2, y2);
69
+ ctx.stroke();
70
+
71
+ if (line2 !== undefined) {
72
+ const { y1: line2Y1, y2: line2Y2 } = line2;
73
+
74
+ ctx.beginPath();
75
+ ctx.moveTo(x1, line2Y1);
76
+ ctx.lineTo(x2, line2Y2);
77
+ ctx.stroke();
78
+
79
+ ctx.fillStyle = fillStyle;
80
+ ctx.beginPath();
81
+ ctx.moveTo(x1, y1);
82
+
83
+ ctx.lineTo(x2, y2);
84
+ ctx.lineTo(x2, line2Y2);
85
+ ctx.lineTo(x1, line2Y1);
86
+
87
+ ctx.closePath();
88
+ ctx.fill();
89
+ }
90
+ }
91
+ };
92
+
93
+ private readonly isHover = (moreProps: any) => {
94
+ const { tolerance, onHover } = this.props;
95
+
96
+ if (onHover !== undefined) {
97
+ const { line1, line2 } = helper(this.props, moreProps);
98
+
99
+ if (line1 !== undefined && line2 !== undefined) {
100
+ const {
101
+ mouseXY,
102
+ xScale,
103
+ chartConfig: { yScale },
104
+ } = moreProps;
105
+
106
+ const line1Hovering = isHovering({
107
+ x1Value: line1.x1,
108
+ y1Value: line1.y1,
109
+ x2Value: line1.x2,
110
+ y2Value: line1.y2,
111
+ type: "LINE",
112
+ mouseXY,
113
+ tolerance,
114
+ xScale,
115
+ yScale,
116
+ });
117
+
118
+ const line2Hovering = isHovering({
119
+ x1Value: line2.x1,
120
+ y1Value: line2.y1,
121
+ x2Value: line2.x2,
122
+ y2Value: line2.y2,
123
+ type: "LINE",
124
+ mouseXY,
125
+ tolerance,
126
+ xScale,
127
+ yScale,
128
+ });
129
+
130
+ return line1Hovering || line2Hovering;
131
+ }
132
+ }
133
+ return false;
134
+ };
135
+ }
136
+
137
+ function getLines(props: ChannelWithAreaProps, moreProps: any) {
138
+ const { startXY, endXY, dy, type } = props;
139
+ const { xScale } = moreProps;
140
+
141
+ if (isNotDefined(startXY) || isNotDefined(endXY)) {
142
+ return {};
143
+ }
144
+
145
+ const line1 = generateLine({
146
+ type,
147
+ start: startXY,
148
+ end: endXY,
149
+ xScale,
150
+ yScale: undefined,
151
+ });
152
+
153
+ const line2 = isDefined(dy)
154
+ ? {
155
+ ...line1,
156
+ y1: line1.y1 + dy,
157
+ y2: line1.y2 + dy,
158
+ }
159
+ : undefined;
160
+
161
+ return {
162
+ line1,
163
+ line2,
164
+ };
165
+ }
166
+
167
+ function helper(props: ChannelWithAreaProps, moreProps: any) {
168
+ const lines = getLines(props, moreProps);
169
+ const {
170
+ xScale,
171
+ chartConfig: { yScale },
172
+ } = moreProps;
173
+
174
+ const line1 =
175
+ lines.line1 !== undefined
176
+ ? {
177
+ x1: xScale(lines.line1.x1),
178
+ y1: yScale(lines.line1.y1),
179
+ x2: xScale(lines.line1.x2),
180
+ y2: yScale(lines.line1.y2),
181
+ }
182
+ : undefined;
183
+
184
+ const line2 =
185
+ lines.line2 !== undefined
186
+ ? {
187
+ x1: line1!.x1,
188
+ y1: yScale(lines.line2.y1),
189
+ x2: line1!.x2,
190
+ y2: yScale(lines.line2.y2),
191
+ }
192
+ : undefined;
193
+
194
+ return {
195
+ lines,
196
+ line1,
197
+ line2,
198
+ };
199
+ }