@tradingaction/tooltip 2.1.2 → 2.1.4
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.
- package/lib/IndicatorDisplayTooltip.d.ts +10 -1
- package/lib/IndicatorDisplayTooltip.js +65 -25
- package/lib/IndicatorDisplayTooltip.js.map +1 -1
- package/package.json +4 -5
- package/src/BollingerBandTooltip.tsx +0 -99
- package/src/GroupTooltip.tsx +0 -152
- package/src/HoverTooltip.tsx +0 -270
- package/src/IndicatorDisplayTooltip.tsx +0 -466
- package/src/MACDTooltip.tsx +0 -116
- package/src/MovingAverageTooltip.tsx +0 -167
- package/src/OHLCTooltip.tsx +0 -150
- package/src/RSITooltip.tsx +0 -80
- package/src/SingleTooltip.tsx +0 -122
- package/src/SingleValueTooltip.tsx +0 -131
- package/src/StochasticTooltip.tsx +0 -97
- package/src/ToolTipTSpanLabel.tsx +0 -14
- package/src/ToolTipText.tsx +0 -15
- package/src/index.ts +0 -13
package/src/HoverTooltip.tsx
DELETED
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import { max, sum } from "d3-array";
|
|
2
|
-
import * as React from "react";
|
|
3
|
-
import { ChartCanvasContext, first, GenericComponent, isDefined, last } from "@tradingaction/core";
|
|
4
|
-
|
|
5
|
-
const PADDING = 4;
|
|
6
|
-
const X = 8;
|
|
7
|
-
const Y = 8;
|
|
8
|
-
|
|
9
|
-
const roundRect = (
|
|
10
|
-
ctx: CanvasRenderingContext2D,
|
|
11
|
-
x: number,
|
|
12
|
-
y: number,
|
|
13
|
-
width: number,
|
|
14
|
-
height: number,
|
|
15
|
-
radius: number,
|
|
16
|
-
) => {
|
|
17
|
-
ctx.beginPath();
|
|
18
|
-
ctx.moveTo(x + radius, y);
|
|
19
|
-
ctx.lineTo(x + width - radius, y);
|
|
20
|
-
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
|
|
21
|
-
ctx.lineTo(x + width, y + height - radius);
|
|
22
|
-
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
|
|
23
|
-
ctx.lineTo(x + radius, y + height);
|
|
24
|
-
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
|
|
25
|
-
ctx.lineTo(x, y + radius);
|
|
26
|
-
ctx.quadraticCurveTo(x, y, x + radius, y);
|
|
27
|
-
ctx.closePath();
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const defaultBackgroundShapeCanvas = (
|
|
31
|
-
props: HoverTooltipProps,
|
|
32
|
-
{ width, height }: { width: number; height: number },
|
|
33
|
-
ctx: CanvasRenderingContext2D,
|
|
34
|
-
) => {
|
|
35
|
-
const { toolTipFillStyle, toolTipStrokeStyle } = props;
|
|
36
|
-
|
|
37
|
-
ctx.beginPath();
|
|
38
|
-
roundRect(ctx, 0, 0, width, height, 4);
|
|
39
|
-
if (toolTipFillStyle !== undefined) {
|
|
40
|
-
ctx.fillStyle = toolTipFillStyle;
|
|
41
|
-
ctx.shadowColor = "#898";
|
|
42
|
-
ctx.shadowBlur = 4;
|
|
43
|
-
ctx.fill();
|
|
44
|
-
ctx.shadowBlur = 0;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (toolTipStrokeStyle !== undefined) {
|
|
48
|
-
ctx.strokeStyle = toolTipStrokeStyle;
|
|
49
|
-
ctx.stroke();
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const defaultTooltipCanvas = (props: HoverTooltipProps, content: any, ctx: CanvasRenderingContext2D) => {
|
|
54
|
-
const { fontSize = 14, fontFamily, fontFill } = props;
|
|
55
|
-
|
|
56
|
-
const startY = Y + fontSize * 0.9;
|
|
57
|
-
ctx.font = `bold ${fontSize}px ${fontFamily}`;
|
|
58
|
-
if (fontFill !== undefined) {
|
|
59
|
-
ctx.fillStyle = fontFill;
|
|
60
|
-
}
|
|
61
|
-
ctx.textAlign = "left";
|
|
62
|
-
ctx.fillText(content.x, X, startY);
|
|
63
|
-
|
|
64
|
-
const maxLabel = max(content.y, (y: any) => ctx.measureText(y.label as string).width) ?? 0;
|
|
65
|
-
|
|
66
|
-
for (let i = 0; i < content.y.length; i++) {
|
|
67
|
-
const y = content.y[i];
|
|
68
|
-
const textY = (i + 1) * PADDING + startY + fontSize * (i + 1);
|
|
69
|
-
ctx.font = `${fontSize}px ${fontFamily}`;
|
|
70
|
-
ctx.fillStyle = y.stroke ?? fontFill;
|
|
71
|
-
ctx.fillText(y.label, X, textY);
|
|
72
|
-
|
|
73
|
-
if (fontFill !== undefined) {
|
|
74
|
-
ctx.fillStyle = fontFill;
|
|
75
|
-
}
|
|
76
|
-
ctx.fillText(y.value, X * 2 + maxLabel, textY);
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const drawOnCanvas = (
|
|
81
|
-
ctx: CanvasRenderingContext2D,
|
|
82
|
-
props: HoverTooltipProps,
|
|
83
|
-
context: any,
|
|
84
|
-
pointer: any,
|
|
85
|
-
height: number,
|
|
86
|
-
) => {
|
|
87
|
-
const { margin, ratio } = context;
|
|
88
|
-
const { backgroundShapeCanvas, tooltipCanvas, background } = props;
|
|
89
|
-
|
|
90
|
-
const originX = 0.5 * ratio + margin.left;
|
|
91
|
-
const originY = 0.5 * ratio + margin.top;
|
|
92
|
-
|
|
93
|
-
ctx.save();
|
|
94
|
-
|
|
95
|
-
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
96
|
-
ctx.scale(ratio, ratio);
|
|
97
|
-
|
|
98
|
-
ctx.translate(originX, originY);
|
|
99
|
-
|
|
100
|
-
const { x, y, content, centerX, pointWidth, bgSize } = pointer;
|
|
101
|
-
|
|
102
|
-
if (background?.fillStyle !== undefined) {
|
|
103
|
-
ctx.fillStyle = background.fillStyle;
|
|
104
|
-
}
|
|
105
|
-
ctx.beginPath();
|
|
106
|
-
ctx.rect(centerX - pointWidth / 2, 0, pointWidth, height);
|
|
107
|
-
ctx.fill();
|
|
108
|
-
|
|
109
|
-
ctx.translate(x, y);
|
|
110
|
-
|
|
111
|
-
backgroundShapeCanvas(props, bgSize, ctx);
|
|
112
|
-
|
|
113
|
-
tooltipCanvas(props, content, ctx);
|
|
114
|
-
|
|
115
|
-
ctx.restore();
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
const calculateTooltipSize = (props: HoverTooltipProps, content: any, ctx: CanvasRenderingContext2D) => {
|
|
119
|
-
const { fontFamily, fontSize = 12, fontFill } = props;
|
|
120
|
-
|
|
121
|
-
ctx.font = `bold ${fontSize}px ${fontFamily}`;
|
|
122
|
-
if (fontFill !== undefined) {
|
|
123
|
-
ctx.fillStyle = fontFill;
|
|
124
|
-
}
|
|
125
|
-
ctx.textAlign = "left";
|
|
126
|
-
|
|
127
|
-
const measureText = (str: string) => ({
|
|
128
|
-
width: ctx.measureText(str).width,
|
|
129
|
-
height: fontSize + PADDING,
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
const { width, height } = content.y
|
|
133
|
-
.map(({ label, value }: any) => measureText(`${label} ${value}`))
|
|
134
|
-
// Sum all y and x sizes (begin with x label size)
|
|
135
|
-
.reduce((res: any, size: any) => sumSizes(res, size), measureText(String(content.x)));
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
width: width + 2 * X,
|
|
139
|
-
height: height + 2 * Y,
|
|
140
|
-
};
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
const sumSizes = (...sizes: any[]) => {
|
|
144
|
-
return {
|
|
145
|
-
width: Math.max(...sizes.map((size) => size.width)),
|
|
146
|
-
height: sum(sizes, (d) => d.height),
|
|
147
|
-
};
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
const normalizeX = (x: number, bgSize: any, pointWidth: number, width: number) => {
|
|
151
|
-
return x < width / 2 ? x + pointWidth / 2 + PADDING : x - bgSize.width - pointWidth / 2 - PADDING;
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
const normalizeY = (y: number, bgSize: any) => {
|
|
155
|
-
return y - bgSize.height <= 0 ? y + PADDING : y - bgSize.height - PADDING;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
const defaultOrigin = (props: HoverTooltipProps, moreProps: any, bgSize: any, pointWidth: any) => {
|
|
159
|
-
const { chartId, yAccessor } = props;
|
|
160
|
-
|
|
161
|
-
const { mouseXY, xAccessor, currentItem, xScale, chartConfig, width } = moreProps;
|
|
162
|
-
|
|
163
|
-
let y = last(mouseXY);
|
|
164
|
-
|
|
165
|
-
const xValue = xAccessor(currentItem);
|
|
166
|
-
|
|
167
|
-
let x = Math.round(xScale(xValue));
|
|
168
|
-
|
|
169
|
-
if (isDefined(chartId) && isDefined(yAccessor) && isDefined(chartConfig) && isDefined(chartConfig.findIndex)) {
|
|
170
|
-
const yValue = yAccessor(currentItem);
|
|
171
|
-
const chartIndex = chartConfig.findIndex((c: any) => c.id === chartId);
|
|
172
|
-
|
|
173
|
-
y = Math.round(chartConfig[chartIndex].yScale(yValue));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
x = normalizeX(x, bgSize, pointWidth, width);
|
|
177
|
-
y = normalizeY(y, bgSize);
|
|
178
|
-
|
|
179
|
-
return [x, y];
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
export interface HoverTooltipProps {
|
|
183
|
-
readonly background?: {
|
|
184
|
-
fillStyle?: string;
|
|
185
|
-
height?: number;
|
|
186
|
-
strokeStyle?: string;
|
|
187
|
-
width?: number;
|
|
188
|
-
};
|
|
189
|
-
readonly backgroundShapeCanvas: (
|
|
190
|
-
props: HoverTooltipProps,
|
|
191
|
-
{ width, height }: { width: number; height: number },
|
|
192
|
-
ctx: CanvasRenderingContext2D,
|
|
193
|
-
) => void;
|
|
194
|
-
readonly chartId?: number | string;
|
|
195
|
-
readonly fontFamily?: string;
|
|
196
|
-
readonly fontFill?: string;
|
|
197
|
-
readonly fontSize?: number;
|
|
198
|
-
readonly origin?: (
|
|
199
|
-
props: HoverTooltipProps,
|
|
200
|
-
moreProps: any,
|
|
201
|
-
bgSize: { width: number; height: number },
|
|
202
|
-
pointWidth: number,
|
|
203
|
-
) => [number, number];
|
|
204
|
-
readonly tooltip: {
|
|
205
|
-
content: (data: any) => { x: string; y: { label: string; value?: string; stroke?: string }[] };
|
|
206
|
-
};
|
|
207
|
-
readonly toolTipFillStyle?: string;
|
|
208
|
-
readonly toolTipStrokeStyle?: string;
|
|
209
|
-
readonly tooltipCanvas: (props: HoverTooltipProps, content: any, ctx: CanvasRenderingContext2D) => void;
|
|
210
|
-
readonly yAccessor: (data: any) => number;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export class HoverTooltip extends React.Component<HoverTooltipProps> {
|
|
214
|
-
public static defaultProps = {
|
|
215
|
-
background: {
|
|
216
|
-
fillStyle: "rgba(33, 148, 243, 0.1)",
|
|
217
|
-
},
|
|
218
|
-
toolTipFillStyle: "rgba(250, 250, 250, 1)",
|
|
219
|
-
toolTipStrokeStyle: "rgba(33, 148, 243)",
|
|
220
|
-
tooltipCanvas: defaultTooltipCanvas,
|
|
221
|
-
origin: defaultOrigin,
|
|
222
|
-
backgroundShapeCanvas: defaultBackgroundShapeCanvas,
|
|
223
|
-
fontFill: "#000000",
|
|
224
|
-
fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
|
|
225
|
-
fontSize: 14,
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
public static contextType = ChartCanvasContext;
|
|
229
|
-
|
|
230
|
-
public render() {
|
|
231
|
-
return <GenericComponent canvasDraw={this.drawOnCanvas} drawOn={["mousemove", "pan"]} />;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
|
|
235
|
-
const pointer = this.helper(ctx, moreProps);
|
|
236
|
-
if (pointer === undefined) {
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const { height } = moreProps;
|
|
241
|
-
|
|
242
|
-
drawOnCanvas(ctx, this.props, this.context, pointer, height);
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
private readonly helper = (ctx: CanvasRenderingContext2D, moreProps: any) => {
|
|
246
|
-
const { show, xScale, currentItem, plotData, xAccessor, displayXAccessor } = moreProps;
|
|
247
|
-
|
|
248
|
-
const { origin = HoverTooltip.defaultProps.origin, tooltip } = this.props;
|
|
249
|
-
|
|
250
|
-
if (!show || currentItem === undefined) {
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const xValue = xAccessor(currentItem);
|
|
255
|
-
if (xValue === undefined) {
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const content = tooltip.content({ currentItem, xAccessor: displayXAccessor });
|
|
260
|
-
const centerX = xScale(xValue);
|
|
261
|
-
const pointWidth =
|
|
262
|
-
Math.abs(xScale(xAccessor(last(plotData))) - xScale(xAccessor(first(plotData)))) / (plotData.length - 1);
|
|
263
|
-
|
|
264
|
-
const bgSize = calculateTooltipSize(this.props, content, ctx);
|
|
265
|
-
|
|
266
|
-
const [x, y] = origin(this.props, moreProps, bgSize, pointWidth);
|
|
267
|
-
|
|
268
|
-
return { x, y, content, centerX, pointWidth, bgSize };
|
|
269
|
-
};
|
|
270
|
-
}
|