@tradingaction/tooltip 2.0.13

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 (55) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +5 -0
  3. package/lib/BollingerBandTooltip.d.ts +40 -0
  4. package/lib/BollingerBandTooltip.js +48 -0
  5. package/lib/BollingerBandTooltip.js.map +1 -0
  6. package/lib/GroupTooltip.d.ts +41 -0
  7. package/lib/GroupTooltip.js +80 -0
  8. package/lib/GroupTooltip.js.map +1 -0
  9. package/lib/HoverTooltip.d.ts +57 -0
  10. package/lib/HoverTooltip.js +174 -0
  11. package/lib/HoverTooltip.js.map +1 -0
  12. package/lib/MACDTooltip.d.ts +46 -0
  13. package/lib/MACDTooltip.js +58 -0
  14. package/lib/MACDTooltip.js.map +1 -0
  15. package/lib/MovingAverageTooltip.d.ts +65 -0
  16. package/lib/MovingAverageTooltip.js +71 -0
  17. package/lib/MovingAverageTooltip.js.map +1 -0
  18. package/lib/OHLCTooltip.d.ts +65 -0
  19. package/lib/OHLCTooltip.js +78 -0
  20. package/lib/OHLCTooltip.js.map +1 -0
  21. package/lib/RSITooltip.d.ts +32 -0
  22. package/lib/RSITooltip.js +36 -0
  23. package/lib/RSITooltip.js.map +1 -0
  24. package/lib/SingleTooltip.d.ts +29 -0
  25. package/lib/SingleTooltip.js +82 -0
  26. package/lib/SingleTooltip.js.map +1 -0
  27. package/lib/SingleValueTooltip.d.ts +46 -0
  28. package/lib/SingleValueTooltip.js +65 -0
  29. package/lib/SingleValueTooltip.js.map +1 -0
  30. package/lib/StochasticTooltip.d.ts +43 -0
  31. package/lib/StochasticTooltip.js +44 -0
  32. package/lib/StochasticTooltip.js.map +1 -0
  33. package/lib/ToolTipTSpanLabel.d.ts +8 -0
  34. package/lib/ToolTipTSpanLabel.js +23 -0
  35. package/lib/ToolTipTSpanLabel.js.map +1 -0
  36. package/lib/ToolTipText.d.ts +9 -0
  37. package/lib/ToolTipText.js +24 -0
  38. package/lib/ToolTipText.js.map +1 -0
  39. package/lib/index.d.ts +12 -0
  40. package/lib/index.js +13 -0
  41. package/lib/index.js.map +1 -0
  42. package/package.json +51 -0
  43. package/src/BollingerBandTooltip.tsx +99 -0
  44. package/src/GroupTooltip.tsx +152 -0
  45. package/src/HoverTooltip.tsx +270 -0
  46. package/src/MACDTooltip.tsx +116 -0
  47. package/src/MovingAverageTooltip.tsx +167 -0
  48. package/src/OHLCTooltip.tsx +150 -0
  49. package/src/RSITooltip.tsx +80 -0
  50. package/src/SingleTooltip.tsx +122 -0
  51. package/src/SingleValueTooltip.tsx +131 -0
  52. package/src/StochasticTooltip.tsx +97 -0
  53. package/src/ToolTipTSpanLabel.tsx +14 -0
  54. package/src/ToolTipText.tsx +15 -0
  55. package/src/index.ts +12 -0
@@ -0,0 +1,80 @@
1
+ import { functor, isDefined, GenericChartComponent } 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 RSITooltipProps {
8
+ readonly className?: string;
9
+ readonly displayFormat: (value: number) => string;
10
+ readonly displayInit?: string;
11
+ readonly displayValuesFor: (props: RSITooltipProps, 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<SVGGElement, MouseEvent>) => void;
18
+ readonly origin: number[] | ((width: number, height: number) => number[]);
19
+ readonly options: {
20
+ windowSize: number;
21
+ };
22
+ readonly textFill?: string;
23
+ readonly yAccessor: (data: any) => number | undefined;
24
+ }
25
+
26
+ export class RSITooltip extends React.Component<RSITooltipProps> {
27
+ public static defaultProps = {
28
+ displayFormat: format(".2f"),
29
+ displayInit: "n/a",
30
+ displayValuesFor: (_: RSITooltipProps, props: any) => props.currentItem,
31
+ origin: [0, 0],
32
+ className: "react-financial-charts-tooltip",
33
+ };
34
+
35
+ public render() {
36
+ return <GenericChartComponent clip={false} svgDraw={this.renderSVG} drawOn={["mousemove"]} />;
37
+ }
38
+
39
+ private readonly renderSVG = (moreProps: any) => {
40
+ const {
41
+ onClick,
42
+ displayInit,
43
+ fontFamily,
44
+ fontSize,
45
+ fontWeight,
46
+ yAccessor,
47
+ displayFormat,
48
+ className,
49
+ options,
50
+ labelFill,
51
+ labelFontWeight,
52
+ textFill,
53
+ displayValuesFor,
54
+ } = this.props;
55
+
56
+ const {
57
+ chartConfig: { width, height },
58
+ } = moreProps;
59
+
60
+ const currentItem = displayValuesFor(this.props, moreProps);
61
+ const rsi = isDefined(currentItem) && yAccessor(currentItem);
62
+ const value = (rsi && displayFormat(rsi)) || displayInit;
63
+
64
+ const { origin: originProp } = this.props;
65
+ const origin = functor(originProp);
66
+ const [x, y] = origin(width, height);
67
+
68
+ const tooltipLabel = `RSI (${options.windowSize}): `;
69
+ return (
70
+ <g className={className} transform={`translate(${x}, ${y})`} onClick={onClick}>
71
+ <ToolTipText x={0} y={0} fontFamily={fontFamily} fontSize={fontSize} fontWeight={fontWeight}>
72
+ <ToolTipTSpanLabel fill={labelFill} fontWeight={labelFontWeight}>
73
+ {tooltipLabel}
74
+ </ToolTipTSpanLabel>
75
+ <tspan fill={textFill}>{value}</tspan>
76
+ </ToolTipText>
77
+ </g>
78
+ );
79
+ };
80
+ }
@@ -0,0 +1,122 @@
1
+ import * as React from "react";
2
+ import { ToolTipText } from "./ToolTipText";
3
+ import { ToolTipTSpanLabel } from "./ToolTipTSpanLabel";
4
+
5
+ export type layouts = "horizontal" | "horizontalRows" | "horizontalInline" | "vertical" | "verticalRows";
6
+
7
+ export interface SingleTooltipProps {
8
+ readonly origin: [number, number];
9
+ readonly yLabel: string;
10
+ readonly yValue: string;
11
+ readonly onClick?: (event: React.MouseEvent, details: any) => void;
12
+ readonly fontFamily?: string;
13
+ readonly fontSize?: number;
14
+ readonly fontWeight?: number;
15
+ readonly labelFill: string;
16
+ readonly valueFill: string;
17
+ readonly forChart: number | string;
18
+ readonly options: any;
19
+ readonly layout: layouts;
20
+ readonly withShape: boolean;
21
+ }
22
+
23
+ export class SingleTooltip extends React.Component<SingleTooltipProps> {
24
+ public static defaultProps = {
25
+ labelFill: "#4682B4",
26
+ valueFill: "#000000",
27
+ withShape: false,
28
+ };
29
+
30
+ /*
31
+ * Renders the value next to the label.
32
+ */
33
+ public renderValueNextToLabel() {
34
+ const { origin, yLabel, yValue, labelFill, valueFill, withShape, fontSize, fontFamily, fontWeight } =
35
+ this.props;
36
+
37
+ return (
38
+ <g transform={`translate(${origin[0]}, ${origin[1]})`} onClick={this.handleClick}>
39
+ {withShape ? <rect x="0" y="-6" width="6" height="6" fill={valueFill} /> : null}
40
+ <ToolTipText
41
+ x={withShape ? 8 : 0}
42
+ y={0}
43
+ fontFamily={fontFamily}
44
+ fontSize={fontSize}
45
+ fontWeight={fontWeight}
46
+ >
47
+ <ToolTipTSpanLabel fill={labelFill}>{yLabel}: </ToolTipTSpanLabel>
48
+ <tspan fill={valueFill}>{yValue}</tspan>
49
+ </ToolTipText>
50
+ </g>
51
+ );
52
+ }
53
+
54
+ /*
55
+ * Renders the value beneath the label.
56
+ */
57
+ public renderValueBeneathLabel() {
58
+ const { origin, yLabel, yValue, labelFill, valueFill, withShape, fontSize, fontFamily, fontWeight } =
59
+ this.props;
60
+
61
+ return (
62
+ <g transform={`translate(${origin[0]}, ${origin[1]})`} onClick={this.handleClick}>
63
+ {withShape ? <line x1={0} y1={2} x2={0} y2={28} stroke={valueFill} strokeWidth="4px" /> : null}
64
+ <ToolTipText x={5} y={11} fontFamily={fontFamily} fontSize={fontSize} fontWeight={fontWeight}>
65
+ <ToolTipTSpanLabel fill={labelFill}>{yLabel}</ToolTipTSpanLabel>
66
+ <tspan x="5" dy="15" fill={valueFill}>
67
+ {yValue}
68
+ </tspan>
69
+ </ToolTipText>
70
+ </g>
71
+ );
72
+ }
73
+
74
+ /*
75
+ * Renders the value next to the label.
76
+ * The parent component must have a "text"-element.
77
+ */
78
+ public renderInline() {
79
+ const { yLabel, yValue, labelFill, valueFill, fontSize, fontFamily, fontWeight } = this.props;
80
+
81
+ return (
82
+ <tspan onClick={this.handleClick} fontFamily={fontFamily} fontSize={fontSize} fontWeight={fontWeight}>
83
+ <ToolTipTSpanLabel fill={labelFill}>{yLabel}:&nbsp;</ToolTipTSpanLabel>
84
+ <tspan fill={valueFill}>{yValue}&nbsp;&nbsp;</tspan>
85
+ </tspan>
86
+ );
87
+ }
88
+
89
+ public render() {
90
+ const { layout } = this.props;
91
+ let comp: JSX.Element | null = null;
92
+
93
+ switch (layout) {
94
+ case "horizontal":
95
+ comp = this.renderValueNextToLabel();
96
+ break;
97
+ case "horizontalRows":
98
+ comp = this.renderValueBeneathLabel();
99
+ break;
100
+ case "horizontalInline":
101
+ comp = this.renderInline();
102
+ break;
103
+ case "vertical":
104
+ comp = this.renderValueNextToLabel();
105
+ break;
106
+ case "verticalRows":
107
+ comp = this.renderValueBeneathLabel();
108
+ break;
109
+ default:
110
+ comp = this.renderValueNextToLabel();
111
+ }
112
+
113
+ return comp;
114
+ }
115
+
116
+ private readonly handleClick = (event: React.MouseEvent) => {
117
+ const { onClick, forChart, options } = this.props;
118
+ if (onClick !== undefined) {
119
+ onClick(event, { chartId: forChart, ...options });
120
+ }
121
+ };
122
+ }
@@ -0,0 +1,131 @@
1
+ import { functor, identity, GenericChartComponent, noop, 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
+ interface SingleValueTooltipIcon {
8
+ unicode: string;
9
+ onClick: () => void;
10
+ size?: any;
11
+ fillColor?: string;
12
+ }
13
+
14
+ export interface SingleValueTooltipProps {
15
+ readonly xDisplayFormat?: (value: number) => string;
16
+ readonly yDisplayFormat?: (value: number) => string;
17
+ readonly xInitDisplay?: string;
18
+ readonly yInitDisplay?: string;
19
+ readonly xLabel?: string;
20
+ readonly yLabel: string;
21
+ readonly labelFill?: string;
22
+ readonly labelFontWeight?: number;
23
+ readonly valueFill?: string;
24
+ readonly origin?: [number, number] | ((width: number, height: number) => [number, number]);
25
+ readonly className?: string;
26
+ readonly fontFamily?: string;
27
+ readonly fontSize?: number;
28
+ readonly fontWeight?: number;
29
+ readonly onClick?: (event: React.MouseEvent<SVGGElement, MouseEvent>) => void;
30
+ readonly displayValuesFor?: (props: SingleValueTooltipProps, moreProps: any) => any;
31
+ readonly xAccessor?: (d: any) => number;
32
+ readonly yAccessor?: (d: any) => number;
33
+ readonly icons?: SingleValueTooltipIcon[];
34
+ }
35
+
36
+ export class SingleValueTooltip extends React.Component<SingleValueTooltipProps> {
37
+ public static defaultProps = {
38
+ className: "react-financial-charts-tooltip",
39
+ displayValuesFor: (_: any, props: any) => props.currentItem,
40
+ labelFill: "#4682B4",
41
+ origin: [0, 0],
42
+ valueFill: "#000000",
43
+ xAccessor: noop,
44
+ xDisplayFormat: identity as (value: number) => string,
45
+ xInitDisplay: "n/a",
46
+ yAccessor: identity as (d: any) => number,
47
+ yDisplayFormat: format(".2f") as (value: number) => string,
48
+ yInitDisplay: "n/a",
49
+ };
50
+
51
+ public render() {
52
+ return <GenericChartComponent clip={false} svgDraw={this.renderSVG} drawOn={["mousemove"]} />;
53
+ }
54
+
55
+ private readonly renderSVG = (moreProps: any) => {
56
+ const {
57
+ onClick,
58
+ fontFamily,
59
+ fontSize,
60
+ fontWeight,
61
+ labelFill,
62
+ labelFontWeight,
63
+ valueFill,
64
+ className,
65
+ displayValuesFor = SingleValueTooltip.defaultProps.displayValuesFor,
66
+ origin: originProp,
67
+ xDisplayFormat = SingleValueTooltip.defaultProps.xDisplayFormat,
68
+ yDisplayFormat = SingleValueTooltip.defaultProps.yDisplayFormat,
69
+ xLabel,
70
+ yLabel,
71
+ xAccessor = SingleValueTooltip.defaultProps.xAccessor,
72
+ yAccessor = SingleValueTooltip.defaultProps.yAccessor,
73
+ xInitDisplay,
74
+ yInitDisplay,
75
+ } = this.props;
76
+
77
+ const {
78
+ chartConfig: { width, height },
79
+ fullData,
80
+ } = moreProps;
81
+
82
+ const currentItem = displayValuesFor(this.props, moreProps) ?? last(fullData);
83
+
84
+ let xDisplayValue = xInitDisplay;
85
+ let yDisplayValue = yInitDisplay;
86
+ if (currentItem !== undefined) {
87
+ const xItem = xAccessor(currentItem);
88
+ if (xItem !== undefined) {
89
+ xDisplayValue = xDisplayFormat(xItem);
90
+ }
91
+
92
+ const yItem = yAccessor(currentItem);
93
+ if (yItem !== undefined) {
94
+ yDisplayValue = yDisplayFormat(yItem);
95
+ }
96
+ }
97
+
98
+ const origin = functor(originProp);
99
+
100
+ const [x, y] = origin(width, height);
101
+
102
+ const handleIconClick = (e: React.MouseEvent<SVGTSpanElement, MouseEvent>, icon: SingleValueTooltipIcon) => {
103
+ if (typeof icon.onClick == "function") {
104
+ e.stopPropagation();
105
+ icon.onClick();
106
+ }
107
+ };
108
+
109
+ return (
110
+ <>
111
+ <g className={className} transform={`translate(${x}, ${y})`} onClick={onClick}>
112
+ <ToolTipText x={0} y={0} fontFamily={fontFamily} fontSize={fontSize} fontWeight={fontWeight}>
113
+ {xLabel ? (
114
+ <ToolTipTSpanLabel x={0} dy="5" fill={labelFill}>{`${xLabel}: `}</ToolTipTSpanLabel>
115
+ ) : null}
116
+ {xLabel ? <tspan fill={valueFill}>{`${xDisplayValue} `}</tspan> : null}
117
+ <ToolTipTSpanLabel
118
+ fill={labelFill}
119
+ fontWeight={labelFontWeight}
120
+ >{`${yLabel} `}</ToolTipTSpanLabel>
121
+ <tspan fill={valueFill}>{yDisplayValue}</tspan>
122
+
123
+ {this.props.icons?.map((i) => {
124
+ return <tspan fill={i.fillColor || '#000'} fontSize={i.size} onClick={(e) => handleIconClick(e, i)}>&nbsp;{i.unicode}</tspan>;
125
+ })}
126
+ </ToolTipText>
127
+ </g>
128
+ </>
129
+ );
130
+ };
131
+ }
@@ -0,0 +1,97 @@
1
+ import { functor, GenericChartComponent } 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 StochasticTooltipProps {
8
+ readonly origin: number[] | ((width: number, height: number) => [number, number]);
9
+ readonly className?: string;
10
+ readonly fontFamily?: string;
11
+ readonly fontSize?: number;
12
+ readonly fontWeight?: number;
13
+ readonly labelFill?: string;
14
+ readonly onClick?: (event: React.MouseEvent) => void;
15
+ readonly yAccessor: (currentItem: any) => { K: number; D: number };
16
+ readonly options: {
17
+ windowSize: number;
18
+ kWindowSize: number;
19
+ dWindowSize: number;
20
+ };
21
+ readonly appearance: {
22
+ stroke: {
23
+ dLine: string;
24
+ kLine: string;
25
+ };
26
+ };
27
+ readonly displayFormat: (value: number) => string;
28
+ readonly displayInit?: string;
29
+ readonly displayValuesFor?: (props: StochasticTooltipProps, moreProps: any) => any;
30
+ readonly label: string;
31
+ }
32
+
33
+ export class StochasticTooltip extends React.Component<StochasticTooltipProps> {
34
+ public static defaultProps = {
35
+ className: "react-financial-charts-tooltip",
36
+ displayFormat: format(".2f"),
37
+ displayInit: "n/a",
38
+ displayValuesFor: (_: any, props: any) => props.currentItem,
39
+ label: "STO",
40
+ origin: [0, 0],
41
+ };
42
+
43
+ public render() {
44
+ return <GenericChartComponent clip={false} svgDraw={this.renderSVG} drawOn={["mousemove"]} />;
45
+ }
46
+
47
+ private readonly renderSVG = (moreProps: any) => {
48
+ const {
49
+ onClick,
50
+ fontFamily,
51
+ fontSize,
52
+ fontWeight,
53
+ yAccessor,
54
+ displayFormat,
55
+ origin: originProp,
56
+ label,
57
+ className,
58
+ displayInit,
59
+ displayValuesFor = StochasticTooltip.defaultProps.displayValuesFor,
60
+ options,
61
+ appearance,
62
+ labelFill,
63
+ } = this.props;
64
+ const {
65
+ chartConfig: { width, height },
66
+ fullData,
67
+ } = moreProps;
68
+
69
+ const currentItem = displayValuesFor(this.props, moreProps) ?? fullData[fullData.length - 1];
70
+
71
+ const stochastic = currentItem && yAccessor(currentItem);
72
+
73
+ const K = (stochastic?.K && displayFormat(stochastic.K)) ?? displayInit;
74
+ const D = (stochastic?.D && displayFormat(stochastic.D)) ?? displayInit;
75
+
76
+ const origin = functor(originProp);
77
+
78
+ const [x, y] = origin(width, height);
79
+
80
+ const { stroke } = appearance;
81
+
82
+ return (
83
+ <g className={className} transform={`translate(${x}, ${y})`} onClick={onClick}>
84
+ <ToolTipText x={0} y={0} fontFamily={fontFamily} fontSize={fontSize} fontWeight={fontWeight}>
85
+ <ToolTipTSpanLabel fill={labelFill}>{`${label} %K(`}</ToolTipTSpanLabel>
86
+ <tspan fill={stroke.kLine}>{`${options.windowSize}, ${options.kWindowSize}`}</tspan>
87
+ <ToolTipTSpanLabel fill={labelFill}>): </ToolTipTSpanLabel>
88
+ <tspan fill={stroke.kLine}>{K}</tspan>
89
+ <ToolTipTSpanLabel fill={labelFill}> %D (</ToolTipTSpanLabel>
90
+ <tspan fill={stroke.dLine}>{options.dWindowSize}</tspan>
91
+ <ToolTipTSpanLabel fill={labelFill}>): </ToolTipTSpanLabel>
92
+ <tspan fill={stroke.dLine}>{D}</tspan>
93
+ </ToolTipText>
94
+ </g>
95
+ );
96
+ };
97
+ }
@@ -0,0 +1,14 @@
1
+ import * as React from "react";
2
+
3
+ export class ToolTipTSpanLabel extends React.PureComponent<React.SVGProps<SVGTSpanElement>> {
4
+ public static defaultProps = {
5
+ className: "react-financial-charts-tooltip-label",
6
+ fill: "#4682B4",
7
+ };
8
+
9
+ public render() {
10
+ const { children, ...rest } = this.props;
11
+
12
+ return <tspan {...rest}>{children}</tspan>;
13
+ }
14
+ }
@@ -0,0 +1,15 @@
1
+ import * as React from "react";
2
+
3
+ export class ToolTipText extends React.PureComponent<React.SVGProps<SVGTextElement>> {
4
+ public static defaultProps = {
5
+ className: "react-financial-charts-tooltip",
6
+ fontFamily: "-apple-system, system-ui, 'Helvetica Neue', Ubuntu, sans-serif",
7
+ fontSize: 11,
8
+ };
9
+
10
+ public render() {
11
+ const { children, ...rest } = this.props;
12
+
13
+ return <text {...rest}>{children}</text>;
14
+ }
15
+ }
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ export * from "./BollingerBandTooltip";
2
+ export * from "./GroupTooltip";
3
+ export * from "./HoverTooltip";
4
+ export * from "./MACDTooltip";
5
+ export * from "./MovingAverageTooltip";
6
+ export * from "./OHLCTooltip";
7
+ export * from "./RSITooltip";
8
+ export * from "./SingleTooltip";
9
+ export * from "./SingleValueTooltip";
10
+ export * from "./StochasticTooltip";
11
+ export * from "./ToolTipText";
12
+ export * from "./ToolTipTSpanLabel";