@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,317 @@
1
+ import {
2
+ getStrokeDasharrayCanvas,
3
+ getMouseCanvas,
4
+ GenericChartComponent,
5
+ noop,
6
+ strokeDashTypes,
7
+ } from "@ivanmatiaspascual/core";
8
+ import * as React from "react";
9
+
10
+ export interface StraightLineProps {
11
+ readonly x1Value: any;
12
+ readonly x2Value: any;
13
+ readonly y1Value: any;
14
+ readonly y2Value: any;
15
+ readonly interactiveCursorClass?: string;
16
+ readonly strokeStyle: string;
17
+ readonly strokeWidth?: number;
18
+ readonly strokeDasharray?: strokeDashTypes;
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 onEdge1Drag?: any; // func
24
+ readonly onEdge2Drag?: any; // func
25
+ readonly onDragStart?: (e: React.MouseEvent, moreProps: any) => void;
26
+ readonly onDrag?: (e: React.MouseEvent, moreProps: any) => void;
27
+ readonly onDragComplete?: (e: React.MouseEvent, moreProps: any) => void;
28
+ readonly onHover?: (e: React.MouseEvent, moreProps: any) => void;
29
+ readonly onUnHover?: (e: React.MouseEvent, moreProps: any) => void;
30
+ readonly defaultClassName?: string;
31
+ readonly r?: number;
32
+ readonly edgeFill?: string;
33
+ readonly edgeStroke?: string;
34
+ readonly edgeStrokeWidth?: number;
35
+ readonly withEdge?: boolean;
36
+ readonly tolerance?: number;
37
+ readonly selected?: boolean;
38
+ }
39
+
40
+ export class InteractiveStraightLine extends React.Component<StraightLineProps> {
41
+ public static defaultProps = {
42
+ onEdge1Drag: noop,
43
+ onEdge2Drag: noop,
44
+ edgeStrokeWidth: 3,
45
+ edgeStroke: "#000000",
46
+ edgeFill: "#FFFFFF",
47
+ r: 10,
48
+ withEdge: false,
49
+ strokeWidth: 1,
50
+ strokeDasharray: "Solid",
51
+ children: noop,
52
+ tolerance: 7,
53
+ selected: false,
54
+ };
55
+
56
+ public render() {
57
+ const { selected, interactiveCursorClass } = this.props;
58
+ const { onDragStart, onDrag, onDragComplete, onHover, onUnHover } = this.props;
59
+
60
+ return (
61
+ <GenericChartComponent
62
+ isHover={this.isHover}
63
+ canvasToDraw={getMouseCanvas}
64
+ canvasDraw={this.drawOnCanvas}
65
+ interactiveCursorClass={interactiveCursorClass}
66
+ selected={selected}
67
+ onDragStart={onDragStart}
68
+ onDrag={onDrag}
69
+ onDragComplete={onDragComplete}
70
+ onHover={onHover}
71
+ onUnHover={onUnHover}
72
+ drawOn={["mousemove", "pan", "drag"]}
73
+ />
74
+ );
75
+ }
76
+
77
+ private readonly isHover = (moreProps: any) => {
78
+ const { tolerance, onHover } = this.props;
79
+
80
+ if (onHover !== undefined) {
81
+ const { x1Value, x2Value, y1Value, y2Value, type } = this.props;
82
+ const { mouseXY, xScale } = moreProps;
83
+ const {
84
+ chartConfig: { yScale },
85
+ } = moreProps;
86
+
87
+ const hovering = isHovering({
88
+ x1Value,
89
+ y1Value,
90
+ x2Value,
91
+ y2Value,
92
+ mouseXY,
93
+ type,
94
+ tolerance,
95
+ xScale,
96
+ yScale,
97
+ });
98
+
99
+ return hovering;
100
+ }
101
+ return false;
102
+ };
103
+
104
+ private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
105
+ const {
106
+ strokeWidth = InteractiveStraightLine.defaultProps.strokeWidth,
107
+ strokeDasharray,
108
+ strokeStyle,
109
+ } = this.props;
110
+ const { x1, y1, x2, y2 } = helper(this.props, moreProps);
111
+
112
+ ctx.lineWidth = strokeWidth;
113
+ ctx.strokeStyle = strokeStyle;
114
+
115
+ const lineDash = getStrokeDasharrayCanvas(strokeDasharray);
116
+
117
+ ctx.setLineDash(lineDash);
118
+
119
+ ctx.beginPath();
120
+ ctx.moveTo(x1, y1);
121
+ ctx.lineTo(x2, y2);
122
+ ctx.stroke();
123
+ };
124
+ }
125
+
126
+ export function isHovering2(start: any, end: any, [mouseX, mouseY]: any, tolerance: any) {
127
+ const m = getSlope(start, end);
128
+
129
+ if (m !== undefined) {
130
+ const b = getYIntercept(m, end);
131
+ const y = m * mouseX + b;
132
+ return (
133
+ mouseY < y + tolerance &&
134
+ mouseY > y - tolerance &&
135
+ mouseX > Math.min(start[0], end[0]) - tolerance &&
136
+ mouseX < Math.max(start[0], end[0]) + tolerance
137
+ );
138
+ } else {
139
+ return (
140
+ mouseY >= Math.min(start[1], end[1]) &&
141
+ mouseY <= Math.max(start[1], end[1]) &&
142
+ mouseX < start[0] + tolerance &&
143
+ mouseX > start[0] - tolerance
144
+ );
145
+ }
146
+ }
147
+
148
+ export function isHovering({ x1Value, y1Value, x2Value, y2Value, mouseXY, type, tolerance, xScale, yScale }: any) {
149
+ const line = generateLine({
150
+ type,
151
+ start: [x1Value, y1Value],
152
+ end: [x2Value, y2Value],
153
+ xScale,
154
+ yScale,
155
+ });
156
+
157
+ const start = [xScale(line.x1), yScale(line.y1)];
158
+ const end = [xScale(line.x2), yScale(line.y2)];
159
+
160
+ const m = getSlope(start, end);
161
+ const [mouseX, mouseY] = mouseXY;
162
+
163
+ if (m !== undefined) {
164
+ const b = getYIntercept(m, end);
165
+ const y = m * mouseX + b;
166
+
167
+ return (
168
+ mouseY < y + tolerance &&
169
+ mouseY > y - tolerance &&
170
+ mouseX > Math.min(start[0], end[0]) - tolerance &&
171
+ mouseX < Math.max(start[0], end[0]) + tolerance
172
+ );
173
+ } else {
174
+ return (
175
+ mouseY >= Math.min(start[1], end[1]) &&
176
+ mouseY <= Math.max(start[1], end[1]) &&
177
+ mouseX < start[0] + tolerance &&
178
+ mouseX > start[0] - tolerance
179
+ );
180
+ }
181
+ }
182
+
183
+ function helper(props: any, moreProps: any) {
184
+ const { x1Value, x2Value, y1Value, y2Value, type } = props;
185
+
186
+ const {
187
+ xScale,
188
+ chartConfig: { yScale },
189
+ } = moreProps;
190
+
191
+ const modLine = generateLine({
192
+ type,
193
+ start: [x1Value, y1Value],
194
+ end: [x2Value, y2Value],
195
+ xScale,
196
+ yScale,
197
+ });
198
+
199
+ const x1 = xScale(modLine.x1);
200
+ const y1 = yScale(modLine.y1);
201
+ const x2 = xScale(modLine.x2);
202
+ const y2 = yScale(modLine.y2);
203
+
204
+ return {
205
+ x1,
206
+ y1,
207
+ x2,
208
+ y2,
209
+ };
210
+ }
211
+
212
+ export function getSlope(start: any, end: any) {
213
+ const m /* slope */ = end[0] === start[0] ? undefined : (end[1] - start[1]) / (end[0] - start[0]);
214
+ return m;
215
+ }
216
+ export function getYIntercept(m: any, end: any) {
217
+ const b /* y intercept */ = -1 * m * end[0] + end[1];
218
+ return b;
219
+ }
220
+
221
+ export function generateLine({ type, start, end, xScale, yScale }: any) {
222
+ const m /* slope */ = getSlope(start, end);
223
+ const b /* y intercept */ = getYIntercept(m, start);
224
+
225
+ switch (type) {
226
+ case "XLINE":
227
+ return getXLineCoordinates({
228
+ start,
229
+ end,
230
+ xScale,
231
+ yScale,
232
+ m,
233
+ b,
234
+ });
235
+ case "RAY":
236
+ return getRayCoordinates({
237
+ start,
238
+ end,
239
+ xScale,
240
+ yScale,
241
+ m,
242
+ b,
243
+ });
244
+ default:
245
+ case "LINE":
246
+ return getLineCoordinates({
247
+ start,
248
+ end,
249
+ });
250
+ }
251
+ }
252
+
253
+ function getXLineCoordinates({ start, end, xScale, yScale, m, b }: any) {
254
+ const [xBegin, xFinish] = xScale.domain();
255
+ const [yBegin, yFinish] = yScale.domain();
256
+
257
+ if (end[0] === start[0]) {
258
+ return {
259
+ x1: end[0],
260
+ y1: yBegin,
261
+ x2: end[0],
262
+ y2: yFinish,
263
+ };
264
+ }
265
+ const [x1, x2] = end[0] > start[0] ? [xBegin, xFinish] : [xFinish, xBegin];
266
+
267
+ return {
268
+ x1,
269
+ y1: m * x1 + b,
270
+ x2,
271
+ y2: m * x2 + b,
272
+ };
273
+ }
274
+
275
+ function getRayCoordinates({ start, end, xScale, yScale, m, b }: any) {
276
+ const [xBegin, xFinish] = xScale.domain();
277
+ const [yBegin, yFinish] = yScale.domain();
278
+
279
+ const x1 = start[0];
280
+ if (end[0] === start[0]) {
281
+ return {
282
+ x1,
283
+ y1: start[1],
284
+ x2: x1,
285
+ y2: end[1] > start[1] ? yFinish : yBegin,
286
+ };
287
+ }
288
+
289
+ const x2 = end[0] > start[0] ? xFinish : xBegin;
290
+
291
+ return {
292
+ x1,
293
+ y1: m * x1 + b,
294
+ x2,
295
+ y2: m * x2 + b,
296
+ };
297
+ }
298
+
299
+ function getLineCoordinates({ start, end }: any) {
300
+ const [x1, y1] = start;
301
+ const [x2, y2] = end;
302
+ if (end[0] === start[0]) {
303
+ return {
304
+ x1,
305
+ y1: start[1],
306
+ x2: x1,
307
+ y2: end[1],
308
+ };
309
+ }
310
+
311
+ return {
312
+ x1,
313
+ y1,
314
+ x2,
315
+ y2,
316
+ };
317
+ }
@@ -0,0 +1,153 @@
1
+ import * as React from "react";
2
+ import { getMouseCanvas, GenericChartComponent } from "@ivanmatiaspascual/core";
3
+
4
+ export interface InteractiveTextProps {
5
+ readonly bgFillStyle: string;
6
+ readonly bgStrokeWidth: number;
7
+ readonly bgStroke: string;
8
+ readonly defaultClassName?: string;
9
+ readonly fontFamily: string;
10
+ readonly fontSize: number;
11
+ readonly fontWeight: number | string;
12
+ readonly fontStyle: string;
13
+ readonly onDragStart?: (e: React.MouseEvent, moreProps: any) => void;
14
+ readonly onDrag?: (e: React.MouseEvent, moreProps: any) => void;
15
+ readonly onDragComplete?: (e: React.MouseEvent, moreProps: any) => void;
16
+ readonly onHover?: (e: React.MouseEvent, moreProps: any) => void;
17
+ readonly onUnHover?: (e: React.MouseEvent, moreProps: any) => void;
18
+ readonly position?: any;
19
+ readonly interactiveCursorClass?: string;
20
+ readonly selected: boolean;
21
+ readonly text: string;
22
+ readonly textFill: string;
23
+ readonly tolerance: number;
24
+ }
25
+
26
+ export class InteractiveText extends React.Component<InteractiveTextProps> {
27
+ public static defaultProps = {
28
+ type: "SD", // standard dev
29
+ fontWeight: "normal", // standard dev
30
+ tolerance: 4,
31
+ selected: false,
32
+ };
33
+
34
+ private calculateTextWidth = true;
35
+ private textWidth?: number;
36
+
37
+ public componentDidUpdate(previousProps: InteractiveTextProps) {
38
+ this.calculateTextWidth =
39
+ previousProps.text !== this.props.text ||
40
+ previousProps.fontStyle !== this.props.fontStyle ||
41
+ previousProps.fontWeight !== this.props.fontWeight ||
42
+ previousProps.fontSize !== this.props.fontSize ||
43
+ previousProps.fontFamily !== this.props.fontFamily;
44
+ }
45
+
46
+ public render() {
47
+ const { selected, interactiveCursorClass } = this.props;
48
+ const { onHover, onUnHover } = this.props;
49
+ const { onDragStart, onDrag, onDragComplete } = this.props;
50
+
51
+ return (
52
+ <GenericChartComponent
53
+ isHover={this.isHover}
54
+ canvasToDraw={getMouseCanvas}
55
+ canvasDraw={this.drawOnCanvas}
56
+ interactiveCursorClass={interactiveCursorClass}
57
+ selected={selected}
58
+ onDragStart={onDragStart}
59
+ onDrag={onDrag}
60
+ onDragComplete={onDragComplete}
61
+ onHover={onHover}
62
+ onUnHover={onUnHover}
63
+ drawOn={["mousemove", "mouseleave", "pan", "drag"]}
64
+ />
65
+ );
66
+ }
67
+
68
+ private readonly isHover = (moreProps: any) => {
69
+ const { onHover } = this.props;
70
+
71
+ if (onHover !== undefined && this.textWidth !== undefined && !this.calculateTextWidth) {
72
+ const { rect } = this.helper(moreProps, this.textWidth);
73
+ const {
74
+ mouseXY: [x, y],
75
+ } = moreProps;
76
+
77
+ if (x >= rect.x && y >= rect.y && x <= rect.x + rect.width && y <= rect.y + rect.height) {
78
+ return true;
79
+ }
80
+ }
81
+ return false;
82
+ };
83
+
84
+ private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
85
+ const {
86
+ bgFillStyle,
87
+ bgStrokeWidth,
88
+ bgStroke,
89
+ textFill,
90
+ fontFamily,
91
+ fontSize,
92
+ fontStyle,
93
+ fontWeight,
94
+ text,
95
+ } = this.props;
96
+
97
+ if (this.calculateTextWidth) {
98
+ ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`;
99
+ const { width } = ctx.measureText(text);
100
+ this.textWidth = width;
101
+ this.calculateTextWidth = false;
102
+ }
103
+
104
+ const { selected } = this.props;
105
+
106
+ const { x, y, rect } = this.helper(moreProps, this.textWidth ?? 0);
107
+
108
+ ctx.fillStyle = bgFillStyle;
109
+
110
+ ctx.beginPath();
111
+ ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
112
+
113
+ if (selected) {
114
+ ctx.strokeStyle = bgStroke;
115
+ ctx.lineWidth = bgStrokeWidth;
116
+ ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);
117
+ }
118
+
119
+ ctx.fillStyle = textFill;
120
+ ctx.textBaseline = "middle";
121
+ ctx.textAlign = "center";
122
+ ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`;
123
+
124
+ ctx.beginPath();
125
+ ctx.fillText(text, x, y);
126
+ };
127
+
128
+ private readonly helper = (moreProps: any, textWidth: number) => {
129
+ const { position, fontSize } = this.props;
130
+
131
+ const {
132
+ xScale,
133
+ chartConfig: { yScale },
134
+ } = moreProps;
135
+
136
+ const [xValue, yValue] = position;
137
+ const x = xScale(xValue);
138
+ const y = yScale(yValue);
139
+
140
+ const rect = {
141
+ x: x - textWidth / 2 - fontSize,
142
+ y: y - fontSize,
143
+ width: textWidth + fontSize * 2,
144
+ height: fontSize * 2,
145
+ };
146
+
147
+ return {
148
+ x,
149
+ y,
150
+ rect,
151
+ };
152
+ };
153
+ }
@@ -0,0 +1,205 @@
1
+ import * as React from "react";
2
+ import { drawOnCanvas } from "@ivanmatiaspascual/coordinates/lib/EdgeCoordinateV3";
3
+ import { getYCoordinate } from "@ivanmatiaspascual/coordinates/lib/MouseCoordinateY";
4
+ import {
5
+ getStrokeDasharrayCanvas,
6
+ getMouseCanvas,
7
+ GenericChartComponent,
8
+ strokeDashTypes,
9
+ } from "@ivanmatiaspascual/core";
10
+
11
+ export interface InteractiveYCoordinateProps {
12
+ readonly bgFillStyle: string;
13
+ readonly strokeStyle: string;
14
+ readonly strokeWidth: number;
15
+ readonly strokeDasharray: strokeDashTypes;
16
+ readonly textFill: string;
17
+ readonly fontFamily: string;
18
+ readonly fontSize: number;
19
+ readonly fontWeight: number | string;
20
+ readonly fontStyle: string;
21
+ readonly text: string;
22
+ readonly edge: object;
23
+ readonly textBox: {
24
+ readonly closeIcon: any;
25
+ readonly left: number;
26
+ readonly height: number;
27
+ readonly padding: any;
28
+ };
29
+ readonly yValue: number;
30
+ readonly onDragStart?: (e: React.MouseEvent, moreProps: any) => void;
31
+ readonly onDrag?: (e: React.MouseEvent, moreProps: any) => void;
32
+ readonly onDragComplete?: (e: React.MouseEvent, moreProps: any) => void;
33
+ readonly onHover?: (e: React.MouseEvent, moreProps: any) => void;
34
+ readonly onUnHover?: (e: React.MouseEvent, moreProps: any) => void;
35
+ readonly defaultClassName?: string;
36
+ readonly interactiveCursorClass?: string;
37
+ readonly tolerance: number;
38
+ readonly selected: boolean;
39
+ readonly hovering: boolean;
40
+ }
41
+
42
+ export class InteractiveYCoordinate extends React.Component<InteractiveYCoordinateProps> {
43
+ public static defaultProps = {
44
+ fontWeight: "normal", // standard dev
45
+ strokeWidth: 1,
46
+ tolerance: 4,
47
+ selected: false,
48
+ hovering: false,
49
+ };
50
+
51
+ private width = 0;
52
+
53
+ public render() {
54
+ const { interactiveCursorClass } = this.props;
55
+ const { onHover, onUnHover } = this.props;
56
+ const { onDragStart, onDrag, onDragComplete } = this.props;
57
+
58
+ return (
59
+ <GenericChartComponent
60
+ clip={false}
61
+ isHover={this.isHover}
62
+ canvasToDraw={getMouseCanvas}
63
+ canvasDraw={this.drawOnCanvas}
64
+ interactiveCursorClass={interactiveCursorClass}
65
+ enableDragOnHover
66
+ onDragStart={onDragStart}
67
+ onDrag={onDrag}
68
+ onDragComplete={onDragComplete}
69
+ onHover={onHover}
70
+ onUnHover={onUnHover}
71
+ drawOn={["mousemove", "mouseleave", "pan", "drag"]}
72
+ />
73
+ );
74
+ }
75
+
76
+ private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
77
+ const {
78
+ bgFillStyle,
79
+ textFill,
80
+ fontFamily,
81
+ fontSize,
82
+ fontStyle,
83
+ fontWeight,
84
+ strokeStyle,
85
+ strokeWidth,
86
+ strokeDasharray,
87
+ text,
88
+ textBox,
89
+ edge,
90
+ selected,
91
+ hovering,
92
+ } = this.props;
93
+
94
+ const values = this.helper(moreProps);
95
+ if (values == null) {
96
+ return;
97
+ }
98
+
99
+ const { x1, x2, y, rect } = values;
100
+
101
+ ctx.strokeStyle = strokeStyle;
102
+
103
+ ctx.beginPath();
104
+ if (selected || hovering) {
105
+ ctx.lineWidth = strokeWidth + 1;
106
+ } else {
107
+ ctx.lineWidth = strokeWidth;
108
+ }
109
+ ctx.textBaseline = "middle";
110
+ ctx.textAlign = "start";
111
+ ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`;
112
+
113
+ this.width =
114
+ textBox.padding.left +
115
+ ctx.measureText(text).width +
116
+ textBox.padding.right +
117
+ textBox.closeIcon.padding.left +
118
+ textBox.closeIcon.width +
119
+ textBox.closeIcon.padding.right;
120
+
121
+ ctx.setLineDash(getStrokeDasharrayCanvas(strokeDasharray));
122
+ ctx.moveTo(x1, y);
123
+ ctx.lineTo(rect.x, y);
124
+
125
+ ctx.moveTo(rect.x + this.width, y);
126
+ ctx.lineTo(x2, y);
127
+ ctx.stroke();
128
+
129
+ ctx.setLineDash([]);
130
+
131
+ ctx.fillStyle = bgFillStyle;
132
+
133
+ ctx.fillRect(rect.x, rect.y, this.width, rect.height);
134
+ ctx.strokeRect(rect.x, rect.y, this.width, rect.height);
135
+
136
+ ctx.fillStyle = textFill;
137
+
138
+ ctx.beginPath();
139
+ ctx.fillText(text, rect.x + 10, y);
140
+ const newEdge = {
141
+ ...edge,
142
+ textFill,
143
+ fontFamily,
144
+ fontSize,
145
+ };
146
+
147
+ // @ts-ignore
148
+ const yValue = edge.displayFormat(this.props.yValue);
149
+ const yCoord = getYCoordinate(y, yValue, newEdge, moreProps);
150
+ drawOnCanvas(ctx, yCoord);
151
+ };
152
+
153
+ private readonly isHover = (moreProps: any) => {
154
+ const { onHover } = this.props;
155
+
156
+ if (onHover !== undefined) {
157
+ const values = this.helper(moreProps);
158
+ if (values == null) {
159
+ return false;
160
+ }
161
+
162
+ const { x1, x2, y, rect } = values;
163
+ const {
164
+ mouseXY: [mouseX, mouseY],
165
+ } = moreProps;
166
+
167
+ if (
168
+ mouseX >= rect.x &&
169
+ mouseX <= rect.x + this.width &&
170
+ mouseY >= rect.y &&
171
+ mouseY <= rect.y + rect.height
172
+ ) {
173
+ return true;
174
+ }
175
+ if (x1 <= mouseX && x2 >= mouseX && Math.abs(mouseY - y) < 4) {
176
+ return true;
177
+ }
178
+ }
179
+ return false;
180
+ };
181
+
182
+ private readonly helper = (moreProps: any) => {
183
+ const { yValue, textBox } = this.props;
184
+
185
+ const {
186
+ chartConfig: { width, yScale, height },
187
+ } = moreProps;
188
+
189
+ const y = Math.round(yScale(yValue));
190
+
191
+ if (y >= 0 && y <= height) {
192
+ const rect = {
193
+ x: textBox.left,
194
+ y: y - textBox.height / 2,
195
+ height: textBox.height,
196
+ };
197
+ return {
198
+ x1: 0,
199
+ x2: width,
200
+ y,
201
+ rect,
202
+ };
203
+ }
204
+ };
205
+ }