@tradingaction/tooltip 2.1.2 → 2.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tradingaction/tooltip",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "Tooltips for react-financial-charts",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -8,8 +8,7 @@
8
8
  "main": "./lib/index.js",
9
9
  "typings": "./lib/index.d.ts",
10
10
  "files": [
11
- "lib",
12
- "src"
11
+ "lib"
13
12
  ],
14
13
  "sideEffects": false,
15
14
  "author": "Reactive Markets",
@@ -38,7 +37,7 @@
38
37
  "watch": "tsc -p tsconfig.json --watch --preserveWatchOutput"
39
38
  },
40
39
  "dependencies": {
41
- "@tradingaction/core": "^2.1.1",
40
+ "@tradingaction/core": "^2.1.2",
42
41
  "d3-array": "^2.9.1",
43
42
  "d3-format": "^2.0.0",
44
43
  "d3-time-format": "^3.0.0"
@@ -47,5 +46,5 @@
47
46
  "react": "^16.0.0 || ^17.0.0 || ^18.0.0",
48
47
  "react-dom": "^16.0.0 || ^17.0.0 || ^18.0.0"
49
48
  },
50
- "gitHead": "7491d9931380aafbdf99dfbdb06e1e2705c99fb9"
49
+ "gitHead": "8b8fc8c751cebbed2ff486c0cea46e561a477054"
51
50
  }
@@ -1,99 +0,0 @@
1
- import { functor, GenericChartComponent, last } from "@tradingaction/core";
2
- import { format } from "d3-format";
3
- import * as React from "react";
4
- import { ToolTipText } from "./ToolTipText";
5
- import { ToolTipTSpanLabel } from "./ToolTipTSpanLabel";
6
-
7
- export interface BollingerBandTooltipProps {
8
- readonly className?: string;
9
- readonly displayFormat: (value: number) => string;
10
- readonly displayInit?: string;
11
- readonly displayValuesFor?: (props: BollingerBandTooltipProps, moreProps: any) => any;
12
- readonly fontFamily?: string;
13
- readonly fontSize?: number;
14
- readonly fontWeight?: number;
15
- readonly labelFill?: string;
16
- readonly labelFontWeight?: number;
17
- readonly onClick?: (event: React.MouseEvent) => void;
18
- readonly options: {
19
- movingAverageType: string;
20
- multiplier: number;
21
- sourcePath: string;
22
- windowSize: number;
23
- };
24
- readonly origin?: [number, number] | ((width: number, height: number) => [number, number]);
25
- readonly textFill?: string;
26
- readonly yAccessor?: (data: any) => { bottom: number; middle: number; top: number };
27
- }
28
-
29
- export class BollingerBandTooltip extends React.Component<BollingerBandTooltipProps> {
30
- public static defaultProps = {
31
- className: "react-financial-charts-tooltip react-financial-charts-bollingerband-tooltip",
32
- displayFormat: format(".2f"),
33
- displayValuesFor: (_: any, props: any) => props.currentItem,
34
- displayInit: "n/a",
35
- origin: [8, 8],
36
- yAccessor: (data: any) => data.bb,
37
- };
38
-
39
- public render() {
40
- return <GenericChartComponent clip={false} svgDraw={this.renderSVG} drawOn={["mousemove"]} />;
41
- }
42
-
43
- private readonly renderSVG = (moreProps: any) => {
44
- const {
45
- onClick,
46
- displayFormat,
47
- yAccessor = BollingerBandTooltip.defaultProps.yAccessor,
48
- options,
49
- origin: originProp,
50
- textFill,
51
- labelFill,
52
- labelFontWeight,
53
- className,
54
- displayValuesFor = BollingerBandTooltip.defaultProps.displayValuesFor,
55
- displayInit,
56
- fontFamily,
57
- fontSize,
58
- fontWeight,
59
- } = this.props;
60
-
61
- const {
62
- chartConfig: { width, height },
63
- fullData,
64
- } = moreProps;
65
-
66
- const currentItem = displayValuesFor(this.props, moreProps) ?? last(fullData);
67
-
68
- let top = displayInit;
69
- let middle = displayInit;
70
- let bottom = displayInit;
71
-
72
- if (currentItem !== undefined) {
73
- const item = yAccessor(currentItem);
74
- if (item !== undefined) {
75
- top = displayFormat(item.top);
76
- middle = displayFormat(item.middle);
77
- bottom = displayFormat(item.bottom);
78
- }
79
- }
80
-
81
- const origin = functor(originProp);
82
- const [x, y] = origin(width, height);
83
-
84
- const { sourcePath, windowSize, multiplier, movingAverageType } = options;
85
- const tooltipLabel = `BB(${sourcePath}, ${windowSize}, ${multiplier}, ${movingAverageType}): `;
86
- const tooltipValue = `${top}, ${middle}, ${bottom}`;
87
-
88
- return (
89
- <g transform={`translate(${x}, ${y})`} className={className} onClick={onClick}>
90
- <ToolTipText x={0} y={0} fontFamily={fontFamily} fontSize={fontSize} fontWeight={fontWeight}>
91
- <ToolTipTSpanLabel fill={labelFill} fontWeight={labelFontWeight}>
92
- {tooltipLabel}
93
- </ToolTipTSpanLabel>
94
- <tspan fill={textFill}>{tooltipValue}</tspan>
95
- </ToolTipText>
96
- </g>
97
- );
98
- };
99
- }
@@ -1,152 +0,0 @@
1
- import { GenericChartComponent, last } from "@tradingaction/core";
2
- import { format } from "d3-format";
3
- import * as React from "react";
4
- import { layouts, SingleTooltip } from "./SingleTooltip";
5
- import { ToolTipText } from "./ToolTipText";
6
-
7
- export interface GroupTooltipProps {
8
- readonly className?: string;
9
- readonly fontFamily?: string;
10
- readonly fontSize?: number;
11
- readonly fontWeight?: number;
12
- readonly displayFormat: (value: number) => string;
13
- readonly displayInit?: string;
14
- readonly displayValuesFor: (props: GroupTooltipProps, moreProps: any) => any;
15
- readonly layout: layouts;
16
- readonly onClick?: (event: React.MouseEvent, details: any) => void;
17
- readonly options: {
18
- labelFill?: string;
19
- yLabel: string;
20
- yAccessor: (data: any) => number;
21
- valueFill?: string;
22
- withShape?: boolean;
23
- }[];
24
- readonly origin: [number, number];
25
- readonly position?: "topRight" | "bottomLeft" | "bottomRight";
26
- readonly verticalSize?: number; // "verticalSize" only be used, if layout is "vertical", "verticalRows".
27
- readonly width?: number; // "width" only be used, if layout is "horizontal" or "horizontalRows".
28
- }
29
-
30
- export class GroupTooltip extends React.Component<GroupTooltipProps> {
31
- public static defaultProps = {
32
- className: "react-financial-charts-tooltip react-financial-charts-group-tooltip",
33
- layout: "horizontal",
34
- displayFormat: format(".2f"),
35
- displayInit: "",
36
- displayValuesFor: (_: any, props: any) => props.currentItem,
37
- origin: [0, 0],
38
- width: 60,
39
- verticalSize: 13,
40
- };
41
-
42
- public render() {
43
- return <GenericChartComponent clip={false} svgDraw={this.renderSVG} drawOn={["mousemove"]} />;
44
- }
45
-
46
- private readonly getPosition = (moreProps: any) => {
47
- const { position } = this.props;
48
- const { height, width } = moreProps.chartConfig;
49
-
50
- const dx = 20;
51
- const dy = 40;
52
- let textAnchor: "start" | "middle" | "end" | "inherit" | undefined;
53
- let xyPos: (number | null)[] | null = null;
54
-
55
- if (position !== undefined) {
56
- switch (position) {
57
- case "topRight":
58
- xyPos = [width - dx, null];
59
- textAnchor = "end";
60
- break;
61
- case "bottomLeft":
62
- xyPos = [null, height - dy];
63
- break;
64
- case "bottomRight":
65
- xyPos = [width - dx, height - dy];
66
- textAnchor = "end";
67
- break;
68
- default:
69
- xyPos = [null, null];
70
- }
71
- } else {
72
- xyPos = [null, null];
73
- }
74
-
75
- return { xyPos, textAnchor };
76
- };
77
-
78
- private readonly renderSVG = (moreProps: any) => {
79
- const { chartId, fullData } = moreProps;
80
-
81
- const {
82
- className,
83
- displayInit = GroupTooltip.defaultProps.displayInit,
84
- displayValuesFor,
85
- onClick,
86
- width = 60,
87
- verticalSize = 13,
88
- fontFamily,
89
- fontSize,
90
- fontWeight,
91
- layout,
92
- origin,
93
- displayFormat,
94
- options,
95
- } = this.props;
96
-
97
- const currentItem = displayValuesFor(this.props, moreProps) ?? last(fullData);
98
-
99
- const { xyPos, textAnchor } = this.getPosition(moreProps);
100
-
101
- const xPos = xyPos != null && xyPos[0] != null ? xyPos[0] : origin[0];
102
- const yPos = xyPos != null && xyPos[1] != null ? xyPos[1] : origin[1];
103
-
104
- const singleTooltip = options.map((each, idx) => {
105
- const yValue = currentItem && each.yAccessor(currentItem);
106
- const yDisplayValue = yValue ? displayFormat(yValue) : displayInit;
107
-
108
- const orig: () => [number, number] = () => {
109
- if (layout === "horizontal" || layout === "horizontalRows") {
110
- return [width * idx, 0];
111
- }
112
- if (layout === "vertical") {
113
- return [0, verticalSize * idx];
114
- }
115
- if (layout === "verticalRows") {
116
- return [0, verticalSize * 2.3 * idx];
117
- }
118
- return [0, 0];
119
- };
120
-
121
- return (
122
- <SingleTooltip
123
- key={idx}
124
- layout={layout}
125
- origin={orig()}
126
- yLabel={each.yLabel}
127
- yValue={yDisplayValue}
128
- options={each}
129
- forChart={chartId}
130
- onClick={onClick}
131
- fontFamily={fontFamily}
132
- fontSize={fontSize}
133
- labelFill={each.labelFill}
134
- valueFill={each.valueFill}
135
- withShape={each.withShape}
136
- />
137
- );
138
- });
139
-
140
- return (
141
- <g transform={`translate(${xPos}, ${yPos})`} className={className} textAnchor={textAnchor}>
142
- {layout === "horizontalInline" ? (
143
- <ToolTipText x={0} y={0} fontFamily={fontFamily} fontSize={fontSize} fontWeight={fontWeight}>
144
- {singleTooltip}
145
- </ToolTipText>
146
- ) : (
147
- singleTooltip
148
- )}
149
- </g>
150
- );
151
- };
152
- }
@@ -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
- }