@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,91 @@
1
+ import * as React from "react";
2
+
3
+ import { getMouseCanvas, GenericChartComponent } from "@ivanmatiaspascual/core";
4
+
5
+ export interface ClickableCircleProps {
6
+ readonly onDragStart?: (e: React.MouseEvent, moreProps: any) => void;
7
+ readonly onDrag?: (e: React.MouseEvent, moreProps: any) => void;
8
+ readonly onDragComplete?: (e: React.MouseEvent, moreProps: any) => void;
9
+ readonly strokeWidth: number;
10
+ readonly strokeStyle: string;
11
+ readonly fillStyle: string;
12
+ readonly r: number;
13
+ readonly cx?: number;
14
+ readonly cy?: number;
15
+ readonly className: string;
16
+ readonly show: boolean;
17
+ readonly interactiveCursorClass?: string;
18
+ readonly xyProvider?: (moreProps: any) => number[];
19
+ }
20
+
21
+ export class ClickableCircle extends React.Component<ClickableCircleProps> {
22
+ public static defaultProps = {
23
+ className: "react-financial-charts-interactive-line-edge",
24
+ show: false,
25
+ };
26
+
27
+ public render() {
28
+ const { interactiveCursorClass, onDragStart, onDrag, onDragComplete, show } = this.props;
29
+
30
+ if (!show) {
31
+ return null;
32
+ }
33
+
34
+ return (
35
+ <GenericChartComponent
36
+ interactiveCursorClass={interactiveCursorClass}
37
+ selected
38
+ isHover={this.isHover}
39
+ onDragStart={onDragStart}
40
+ onDrag={onDrag}
41
+ onDragComplete={onDragComplete}
42
+ canvasDraw={this.drawOnCanvas}
43
+ canvasToDraw={getMouseCanvas}
44
+ drawOn={["pan", "mousemove", "drag"]}
45
+ />
46
+ );
47
+ }
48
+
49
+ private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
50
+ const { strokeStyle, strokeWidth, fillStyle, r } = this.props;
51
+
52
+ ctx.lineWidth = strokeWidth;
53
+ ctx.fillStyle = fillStyle;
54
+ ctx.strokeStyle = strokeStyle;
55
+
56
+ const [x, y] = this.helper(moreProps);
57
+
58
+ ctx.beginPath();
59
+ ctx.arc(x, y, r, 0, 2 * Math.PI, false);
60
+ ctx.fill();
61
+ ctx.stroke();
62
+ };
63
+
64
+ private readonly isHover = (moreProps: any) => {
65
+ const { mouseXY } = moreProps;
66
+ const r = this.props.r + 7;
67
+ const [x, y] = this.helper(moreProps);
68
+
69
+ const [mx, my] = mouseXY;
70
+ const hover = x - r < mx && mx < x + r && y - r < my && my < y + r;
71
+
72
+ return hover;
73
+ };
74
+
75
+ private readonly helper = (moreProps: any) => {
76
+ const { xyProvider, cx, cy } = this.props;
77
+
78
+ if (xyProvider !== undefined) {
79
+ return xyProvider(moreProps);
80
+ }
81
+
82
+ const {
83
+ xScale,
84
+ chartConfig: { yScale },
85
+ } = moreProps;
86
+
87
+ const x = xScale(cx);
88
+ const y = yScale(cy);
89
+ return [x, y];
90
+ };
91
+ }
@@ -0,0 +1,114 @@
1
+ import * as React from "react";
2
+ import { getMouseCanvas, GenericChartComponent } from "@ivanmatiaspascual/core";
3
+ import { isHovering2 } from "./InteractiveStraightLine";
4
+
5
+ export interface ClickableShapeProps {
6
+ readonly fontWeight: string;
7
+ readonly fontFamily: string;
8
+ readonly fontStyle: string;
9
+ readonly fontSize: number;
10
+ readonly strokeStyle: string;
11
+ readonly strokeWidth: number;
12
+ readonly text: string;
13
+ readonly textBox: {
14
+ readonly closeIcon: any;
15
+ readonly left: number;
16
+ readonly padding: any;
17
+ };
18
+ readonly hovering?: boolean;
19
+ readonly interactiveCursorClass?: string;
20
+ readonly show?: boolean;
21
+ readonly onHover?: (e: React.MouseEvent, moreProps: any) => void;
22
+ readonly onUnHover?: (e: React.MouseEvent, moreProps: any) => void;
23
+ readonly onClick?: (e: React.MouseEvent, moreProps: any) => void;
24
+ readonly yValue: number;
25
+ }
26
+
27
+ export class ClickableShape extends React.Component<ClickableShapeProps> {
28
+ public static defaultProps = {
29
+ show: false,
30
+ strokeWidth: 1,
31
+ };
32
+
33
+ private closeIcon: any;
34
+
35
+ public render() {
36
+ const { interactiveCursorClass, onHover, onUnHover, onClick, show } = this.props;
37
+
38
+ if (!show) {
39
+ return null;
40
+ }
41
+
42
+ return (
43
+ <GenericChartComponent
44
+ interactiveCursorClass={interactiveCursorClass}
45
+ isHover={this.isHover}
46
+ onClickWhenHover={onClick}
47
+ canvasDraw={this.drawOnCanvas}
48
+ canvasToDraw={getMouseCanvas}
49
+ onHover={onHover}
50
+ onUnHover={onUnHover}
51
+ drawOn={["pan", "mousemove", "drag"]}
52
+ />
53
+ );
54
+ }
55
+
56
+ private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
57
+ const { strokeStyle, strokeWidth, hovering, textBox } = this.props;
58
+
59
+ const [x, y] = this.helper(this.props, moreProps, ctx);
60
+
61
+ this.closeIcon = { x, y };
62
+ ctx.beginPath();
63
+
64
+ ctx.lineWidth = hovering ? strokeWidth + 1 : strokeWidth;
65
+ ctx.strokeStyle = strokeStyle;
66
+ const halfWidth = textBox.closeIcon.width / 2;
67
+ ctx.moveTo(x - halfWidth, y - halfWidth);
68
+ ctx.lineTo(x + halfWidth, y + halfWidth);
69
+ ctx.moveTo(x - halfWidth, y + halfWidth);
70
+ ctx.lineTo(x + halfWidth, y - halfWidth);
71
+ ctx.stroke();
72
+ };
73
+
74
+ private readonly isHover = (moreProps: any) => {
75
+ const { mouseXY } = moreProps;
76
+ if (this.closeIcon) {
77
+ const { textBox } = this.props;
78
+ const { x, y } = this.closeIcon;
79
+ const halfWidth = textBox.closeIcon.width / 2;
80
+
81
+ const start1 = [x - halfWidth, y - halfWidth];
82
+ const end1 = [x + halfWidth, y + halfWidth];
83
+ const start2 = [x - halfWidth, y + halfWidth];
84
+ const end2 = [x + halfWidth, y - halfWidth];
85
+
86
+ if (isHovering2(start1, end1, mouseXY, 3) || isHovering2(start2, end2, mouseXY, 3)) {
87
+ return true;
88
+ }
89
+ }
90
+ return false;
91
+ };
92
+
93
+ private readonly helper = (props: ClickableShapeProps, moreProps: any, ctx: CanvasRenderingContext2D) => {
94
+ const { yValue, text, textBox } = props;
95
+ const { fontFamily, fontStyle, fontWeight, fontSize } = props;
96
+ ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`;
97
+
98
+ const {
99
+ chartConfig: { yScale },
100
+ } = moreProps;
101
+
102
+ const x =
103
+ textBox.left +
104
+ textBox.padding.left +
105
+ ctx.measureText(text).width +
106
+ textBox.padding.right +
107
+ textBox.closeIcon.padding.left +
108
+ textBox.closeIcon.width / 2;
109
+
110
+ const y = yScale(yValue);
111
+
112
+ return [x, y];
113
+ };
114
+ }
@@ -0,0 +1,189 @@
1
+ import { pairs } from "d3-array";
2
+ import * as React from "react";
3
+ import { generateLine, isHovering2 } from "./InteractiveStraightLine";
4
+ import { isDefined, isNotDefined, getMouseCanvas, GenericChartComponent } from "@ivanmatiaspascual/core";
5
+
6
+ export interface GannFanProps {
7
+ readonly startXY: number[];
8
+ readonly defaultClassName?: string;
9
+ readonly endXY: number[];
10
+ readonly fillStyle: Array<string>;
11
+ readonly fontFamily: string;
12
+ readonly fontSize: number;
13
+ readonly fontFill: string;
14
+ readonly interactiveCursorClass?: string;
15
+ readonly onDragStart?: (e: React.MouseEvent, moreProps: any) => void;
16
+ readonly onDrag?: (e: React.MouseEvent, moreProps: any) => void;
17
+ readonly onDragComplete?: (e: React.MouseEvent, moreProps: any) => void;
18
+ readonly onHover?: (e: React.MouseEvent, moreProps: any) => void;
19
+ readonly onUnHover?: (e: React.MouseEvent, moreProps: any) => void;
20
+ readonly selected: boolean;
21
+ readonly strokeStyle: string;
22
+ readonly strokeWidth: number;
23
+ readonly tolerance: number;
24
+ }
25
+
26
+ export class GannFan extends React.Component<GannFanProps> {
27
+ public static defaultProps = {
28
+ strokeWidth: 1,
29
+ tolerance: 4,
30
+ selected: false,
31
+ };
32
+
33
+ public render() {
34
+ const { selected, interactiveCursorClass } = this.props;
35
+ const { onDragStart, onDrag, onDragComplete, onHover, onUnHover } = this.props;
36
+
37
+ return (
38
+ <GenericChartComponent
39
+ isHover={this.isHover}
40
+ canvasToDraw={getMouseCanvas}
41
+ canvasDraw={this.drawOnCanvas}
42
+ interactiveCursorClass={interactiveCursorClass}
43
+ selected={selected}
44
+ onDragStart={onDragStart}
45
+ onDrag={onDrag}
46
+ onDragComplete={onDragComplete}
47
+ onHover={onHover}
48
+ onUnHover={onUnHover}
49
+ drawOn={["mousemove", "mouseleave", "pan", "drag"]}
50
+ />
51
+ );
52
+ }
53
+
54
+ private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
55
+ const { strokeStyle, strokeWidth, fillStyle, fontFamily, fontSize, fontFill } = this.props;
56
+
57
+ const lines = this.helper(this.props, moreProps);
58
+
59
+ ctx.lineWidth = strokeWidth;
60
+ ctx.strokeStyle = strokeStyle;
61
+ ctx.font = `${fontSize}px ${fontFamily}`;
62
+ ctx.fillStyle = fontFill;
63
+
64
+ lines.forEach((line) => {
65
+ const { x1, y1, x2, y2, label } = line;
66
+
67
+ ctx.beginPath();
68
+ ctx.moveTo(x1, y1);
69
+ ctx.lineTo(x2, y2);
70
+ ctx.stroke();
71
+ ctx.beginPath();
72
+ ctx.fillText(label.text, label.x, label.y);
73
+ });
74
+ const pairsOfLines = pairs(lines);
75
+
76
+ pairsOfLines.forEach(([line1, line2], idx) => {
77
+ ctx.fillStyle = fillStyle[idx];
78
+
79
+ ctx.beginPath();
80
+ ctx.moveTo(line1.x1, line1.y1);
81
+ ctx.lineTo(line1.x2, line1.y2);
82
+ ctx.lineTo(line2.x2, line2.y2);
83
+ ctx.closePath();
84
+ ctx.fill();
85
+ });
86
+ };
87
+
88
+ private readonly isHover = (moreProps: any) => {
89
+ const { tolerance, onHover } = this.props;
90
+ const { mouseXY } = moreProps;
91
+ const [mouseX, mouseY] = mouseXY;
92
+
93
+ let hovering = false;
94
+ if (isDefined(onHover)) {
95
+ const lines = this.helper(this.props, moreProps);
96
+
97
+ // tslint:disable-next-line: prefer-for-of
98
+ for (let i = 0; i < lines.length; i++) {
99
+ const line1 = lines[i];
100
+
101
+ const left = Math.min(line1.x1, line1.x2);
102
+ const right = Math.max(line1.x1, line1.x2);
103
+ const top = Math.min(line1.y1, line1.y2);
104
+ const bottom = Math.max(line1.y1, line1.y2);
105
+
106
+ const isWithinLineBounds = mouseX >= left && mouseX <= right && mouseY >= top && mouseY <= bottom;
107
+
108
+ hovering =
109
+ isWithinLineBounds && isHovering2([line1.x1, line1.y1], [line1.x2, line1.y2], mouseXY, tolerance);
110
+
111
+ if (hovering) {
112
+ break;
113
+ }
114
+ }
115
+ }
116
+ return hovering;
117
+ };
118
+
119
+ private readonly getLineCoordinates = (start: number[], endX: number, endY: number, text: string) => {
120
+ const end = [endX, endY];
121
+ return {
122
+ start,
123
+ end,
124
+ text,
125
+ };
126
+ };
127
+
128
+ private readonly helper = (props: GannFanProps, moreProps: any) => {
129
+ const { startXY, endXY } = props;
130
+
131
+ const {
132
+ xScale,
133
+ chartConfig: { yScale },
134
+ } = moreProps;
135
+ if (isNotDefined(startXY) || isNotDefined(endXY)) {
136
+ return [];
137
+ }
138
+ const [x1, y1] = startXY;
139
+ const [x2, y2] = endXY;
140
+
141
+ const dx = x2 - x1;
142
+ const dy = y2 - y1;
143
+
144
+ if (dx !== 0 && dy !== 0) {
145
+ const halfY = this.getLineCoordinates(startXY, x2, y1 + dy / 2, "2/1");
146
+ const oneThirdY = this.getLineCoordinates(startXY, x2, y1 + dy / 3, "3/1");
147
+ const oneFourthY = this.getLineCoordinates(startXY, x2, y1 + dy / 4, "4/1");
148
+ const oneEighthY = this.getLineCoordinates(startXY, x2, y1 + dy / 8, "8/1");
149
+ const halfX = this.getLineCoordinates(startXY, x1 + dx / 2, y2, "1/2");
150
+ const oneThirdX = this.getLineCoordinates(startXY, x1 + dx / 3, y2, "1/3");
151
+ const oneFourthX = this.getLineCoordinates(startXY, x1 + dx / 4, y2, "1/4");
152
+ const oneEighthX = this.getLineCoordinates(startXY, x1 + dx / 8, y2, "1/8");
153
+ const lines = [
154
+ oneEighthX,
155
+ oneFourthX,
156
+ oneThirdX,
157
+ halfX,
158
+ { start: startXY, end: endXY, text: "1/1" },
159
+ halfY,
160
+ oneThirdY,
161
+ oneFourthY,
162
+ oneEighthY,
163
+ ];
164
+ const lineCoods = lines.map((line) => {
165
+ // tslint:disable-next-line: no-shadowed-variable
166
+ const { x1, y1, x2, y2 } = generateLine({
167
+ type: "RAY",
168
+ start: line.start,
169
+ end: line.end,
170
+ xScale,
171
+ yScale,
172
+ });
173
+ return {
174
+ x1: xScale(x1),
175
+ y1: yScale(y1),
176
+ x2: xScale(x2),
177
+ y2: yScale(y2),
178
+ label: {
179
+ x: xScale(line.end[0]),
180
+ y: yScale(line.end[1]),
181
+ text: line.text,
182
+ },
183
+ };
184
+ });
185
+ return lineCoods;
186
+ }
187
+ return [];
188
+ };
189
+ }
@@ -0,0 +1,174 @@
1
+ import * as React from "react";
2
+ import { isDefined, GenericChartComponent } from "@ivanmatiaspascual/core";
3
+
4
+ const PADDING = 10;
5
+ const MIN_WIDTH = PADDING;
6
+
7
+ export interface HoverTextNearMouseProps {
8
+ readonly fontFamily?: string;
9
+ readonly fontSize?: number;
10
+ readonly fill?: string;
11
+ readonly text?: string;
12
+ readonly bgFill?: string;
13
+ readonly bgOpacity?: number;
14
+ readonly bgWidth: number | string;
15
+ readonly bgHeight: number | string;
16
+ readonly show: boolean;
17
+ }
18
+
19
+ interface HoverTextNearMouseState {
20
+ textHeight?: number;
21
+ textWidth?: number;
22
+ }
23
+
24
+ export class HoverTextNearMouse extends React.Component<HoverTextNearMouseProps, HoverTextNearMouseState> {
25
+ public static defaultProps = {
26
+ fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
27
+ fontSize: 12,
28
+ fill: "#000000",
29
+ bgFill: "#FA9325",
30
+ bgOpacity: 0.5,
31
+ };
32
+
33
+ private readonly textNode = React.createRef<SVGTextElement>();
34
+
35
+ public constructor(props: HoverTextNearMouseProps) {
36
+ super(props);
37
+
38
+ this.state = {
39
+ textWidth: undefined,
40
+ textHeight: undefined,
41
+ };
42
+ }
43
+
44
+ public componentDidMount() {
45
+ this.updateTextSize();
46
+ }
47
+
48
+ public componentDidUpdate() {
49
+ this.updateTextSize();
50
+ }
51
+
52
+ public render() {
53
+ const { text } = this.props;
54
+ if (text) {
55
+ return <GenericChartComponent svgDraw={this.renderSVG} drawOn={["mousemove"]} />;
56
+ } else {
57
+ return null;
58
+ }
59
+ }
60
+
61
+ private readonly renderSVG = (moreProps: any) => {
62
+ const { fontFamily, fontSize, fill, bgFill, bgOpacity } = this.props;
63
+
64
+ const textMetaData = helper(
65
+ {
66
+ ...this.props,
67
+ bgWidth: this.getBgWidth(),
68
+ bgHeight: this.getBgHeight(),
69
+ },
70
+ moreProps,
71
+ );
72
+
73
+ if (textMetaData !== undefined && isDefined(textMetaData)) {
74
+ const { rect, text } = textMetaData;
75
+ return (
76
+ <g>
77
+ <rect fill={bgFill} fillOpacity={bgOpacity} stroke={bgFill} {...rect} />
78
+ <text
79
+ ref={this.textNode}
80
+ fontSize={fontSize}
81
+ fontFamily={fontFamily}
82
+ textAnchor="start"
83
+ alignmentBaseline={"central"}
84
+ fill={fill}
85
+ x={text.x}
86
+ y={text.y}
87
+ >
88
+ {text.text}
89
+ </text>
90
+ </g>
91
+ );
92
+ }
93
+
94
+ return null;
95
+ };
96
+
97
+ private readonly getBgHeight = () => {
98
+ const { bgHeight } = this.props;
99
+ const { textHeight } = this.state;
100
+
101
+ if (bgHeight !== "auto") {
102
+ return bgHeight;
103
+ } else if (textHeight !== undefined) {
104
+ return textHeight + PADDING;
105
+ } else {
106
+ return MIN_WIDTH;
107
+ }
108
+ };
109
+
110
+ private readonly getBgWidth = () => {
111
+ const { bgWidth } = this.props;
112
+ const { textWidth } = this.state;
113
+
114
+ if (bgWidth !== "auto") {
115
+ return bgWidth;
116
+ } else if (textWidth !== undefined) {
117
+ return textWidth + PADDING;
118
+ } else {
119
+ return MIN_WIDTH;
120
+ }
121
+ };
122
+
123
+ private readonly updateTextSize = () => {
124
+ const { bgWidth, bgHeight } = this.props;
125
+ if (bgWidth === "auto" || bgHeight === "auto") {
126
+ const textNode = this.textNode.current;
127
+ if (textNode !== null) {
128
+ const { width, height } = textNode.getBBox();
129
+ if (this.state.textWidth !== width || this.state.textHeight !== height) {
130
+ this.setState({
131
+ textWidth: width,
132
+ textHeight: height,
133
+ });
134
+ }
135
+ }
136
+ }
137
+ };
138
+ }
139
+
140
+ function helper(props: any, moreProps: any) {
141
+ const { show, bgWidth, bgHeight } = props;
142
+
143
+ const {
144
+ mouseXY,
145
+ chartConfig: { height, width },
146
+ show: mouseInsideCanvas,
147
+ } = moreProps;
148
+
149
+ if (show && mouseInsideCanvas) {
150
+ const [x, y] = mouseXY;
151
+
152
+ const cx = x < width / 2 ? x + PADDING : x - bgWidth - PADDING;
153
+
154
+ const cy = y < height / 2 ? y + PADDING : y - bgHeight - PADDING;
155
+
156
+ const rect = {
157
+ x: cx,
158
+ y: cy,
159
+ width: bgWidth,
160
+ height: bgHeight,
161
+ };
162
+
163
+ const text = {
164
+ text: props.text,
165
+ x: cx + PADDING / 2,
166
+ y: cy + bgHeight / 2,
167
+ };
168
+
169
+ return {
170
+ rect,
171
+ text,
172
+ };
173
+ }
174
+ }