@tradingaction/coordinates 2.0.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 (51) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +5 -0
  3. package/lib/CrossHairCursor.d.ts +22 -0
  4. package/lib/CrossHairCursor.js +76 -0
  5. package/lib/CrossHairCursor.js.map +1 -0
  6. package/lib/CurrentCoordinate.d.ts +31 -0
  7. package/lib/CurrentCoordinate.js +54 -0
  8. package/lib/CurrentCoordinate.js.map +1 -0
  9. package/lib/Cursor.d.ts +31 -0
  10. package/lib/Cursor.js +122 -0
  11. package/lib/Cursor.js.map +1 -0
  12. package/lib/EdgeCoordinate.d.ts +32 -0
  13. package/lib/EdgeCoordinate.js +151 -0
  14. package/lib/EdgeCoordinate.js.map +1 -0
  15. package/lib/EdgeCoordinateV2.d.ts +3 -0
  16. package/lib/EdgeCoordinateV2.js +138 -0
  17. package/lib/EdgeCoordinateV2.js.map +1 -0
  18. package/lib/EdgeCoordinateV3.d.ts +3 -0
  19. package/lib/EdgeCoordinateV3.js +176 -0
  20. package/lib/EdgeCoordinateV3.js.map +1 -0
  21. package/lib/EdgeIndicator.d.ts +55 -0
  22. package/lib/EdgeIndicator.js +85 -0
  23. package/lib/EdgeIndicator.js.map +1 -0
  24. package/lib/MouseCoordinateX.d.ts +49 -0
  25. package/lib/MouseCoordinateX.js +85 -0
  26. package/lib/MouseCoordinateX.js.map +1 -0
  27. package/lib/MouseCoordinateXV2.d.ts +61 -0
  28. package/lib/MouseCoordinateXV2.js +92 -0
  29. package/lib/MouseCoordinateXV2.js.map +1 -0
  30. package/lib/MouseCoordinateY.d.ts +68 -0
  31. package/lib/MouseCoordinateY.js +91 -0
  32. package/lib/MouseCoordinateY.js.map +1 -0
  33. package/lib/PriceCoordinate.d.ts +53 -0
  34. package/lib/PriceCoordinate.js +81 -0
  35. package/lib/PriceCoordinate.js.map +1 -0
  36. package/lib/index.d.ts +8 -0
  37. package/lib/index.js +9 -0
  38. package/lib/index.js.map +1 -0
  39. package/package.json +49 -0
  40. package/src/CrossHairCursor.tsx +116 -0
  41. package/src/CurrentCoordinate.tsx +95 -0
  42. package/src/Cursor.tsx +174 -0
  43. package/src/EdgeCoordinate.tsx +239 -0
  44. package/src/EdgeCoordinateV2.tsx +204 -0
  45. package/src/EdgeCoordinateV3.tsx +284 -0
  46. package/src/EdgeIndicator.tsx +161 -0
  47. package/src/MouseCoordinateX.tsx +127 -0
  48. package/src/MouseCoordinateXV2.tsx +161 -0
  49. package/src/MouseCoordinateY.tsx +141 -0
  50. package/src/PriceCoordinate.tsx +121 -0
  51. package/src/index.ts +8 -0
@@ -0,0 +1,161 @@
1
+ import * as React from "react";
2
+ import { getMouseCanvas, GenericChartComponent } from "@tradingaction/core";
3
+
4
+ interface MouseCoordinateXV2Props {
5
+ readonly at?: "bottom" | "top";
6
+ readonly bg: {
7
+ readonly fill: string | ((moreProps: any, ctx: CanvasRenderingContext2D) => string);
8
+ readonly stroke: string | ((moreProps: any, ctx: CanvasRenderingContext2D) => string);
9
+ readonly strokeWidth: number | ((moreProps: any) => number);
10
+ readonly padding: {
11
+ left: number;
12
+ right: number;
13
+ top: number;
14
+ bottom: number;
15
+ };
16
+ };
17
+ readonly drawCoordinate: (ctx: CanvasRenderingContext2D, shape: any, props: any, moreProps: any) => void;
18
+ readonly displayFormat: (value: number) => string;
19
+ readonly dx: number;
20
+ readonly dy: number;
21
+ readonly orient?: "bottom" | "top";
22
+ readonly text: {
23
+ readonly fontStyle: string;
24
+ readonly fontWeight: string;
25
+ readonly fontFamily: string;
26
+ readonly fontSize: number;
27
+ readonly fill: string | ((moreProps: any, ctx: CanvasRenderingContext2D) => string);
28
+ };
29
+ readonly xPosition: (props: MouseCoordinateXV2Props, moreProps: any) => number;
30
+ }
31
+
32
+ export class MouseCoordinateXV2 extends React.Component<MouseCoordinateXV2Props> {
33
+ public static defaultProps = {
34
+ xPosition: defaultXPosition,
35
+ drawCoordinate: defaultDrawCoordinate,
36
+ at: "bottom",
37
+ orient: "bottom",
38
+ text: {
39
+ fontStyle: "",
40
+ fontWeight: "",
41
+ fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
42
+ fontSize: 13,
43
+ fill: "rgb(35, 35, 35)",
44
+ },
45
+ bg: {
46
+ fill: "rgb(255, 255, 255)",
47
+ stroke: "rgb(35, 35, 35)",
48
+ strokeWidth: 1,
49
+ padding: {
50
+ left: 7,
51
+ right: 7,
52
+ top: 4,
53
+ bottom: 4,
54
+ },
55
+ },
56
+ dx: 7,
57
+ dy: 7,
58
+ };
59
+
60
+ public render() {
61
+ return (
62
+ <GenericChartComponent
63
+ clip={false}
64
+ canvasDraw={this.drawOnCanvas}
65
+ canvasToDraw={getMouseCanvas}
66
+ drawOn={["mousemove", "pan", "drag"]}
67
+ />
68
+ );
69
+ }
70
+
71
+ private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
72
+ const { show, currentItem } = moreProps;
73
+ const { drawCoordinate } = this.props;
74
+
75
+ if (show && currentItem != null) {
76
+ const shape = getXCoordinateInfo(ctx, this.props, moreProps);
77
+
78
+ drawCoordinate(ctx, shape, this.props, moreProps);
79
+ }
80
+ };
81
+ }
82
+
83
+ function defaultXPosition(props: MouseCoordinateXV2Props, moreProps: any) {
84
+ const { currentItem, xAccessor } = moreProps;
85
+
86
+ return xAccessor(currentItem);
87
+ }
88
+
89
+ function getXCoordinateInfo(ctx: CanvasRenderingContext2D, props: MouseCoordinateXV2Props, moreProps: any) {
90
+ const { at, displayFormat, text, xPosition } = props;
91
+
92
+ const xValue = xPosition(props, moreProps);
93
+
94
+ const {
95
+ xScale,
96
+ chartConfig: { height },
97
+ } = moreProps;
98
+
99
+ ctx.font = `${text.fontStyle} ${text.fontWeight} ${text.fontSize}px ${text.fontFamily}`;
100
+
101
+ const t = displayFormat(xValue);
102
+ const textWidth = ctx.measureText(t).width;
103
+
104
+ const y = at === "bottom" ? height : 0;
105
+ const x = Math.round(xScale(xValue));
106
+
107
+ return {
108
+ x,
109
+ y,
110
+ textWidth,
111
+ text: t,
112
+ };
113
+ }
114
+
115
+ function defaultDrawCoordinate(
116
+ ctx: CanvasRenderingContext2D,
117
+ shape: any,
118
+ props: MouseCoordinateXV2Props,
119
+ moreProps: any,
120
+ ) {
121
+ const { x, y, textWidth, text } = shape;
122
+
123
+ const {
124
+ orient,
125
+ dx,
126
+ dy,
127
+ bg: { padding, fill, stroke, strokeWidth },
128
+ text: { fontSize, fill: textFill },
129
+ } = props;
130
+
131
+ ctx.textAlign = "center";
132
+
133
+ const sign = orient === "top" ? -1 : 1;
134
+ const halfWidth = Math.round(textWidth / 2 + padding.right);
135
+ const height = Math.round(fontSize + padding.top + padding.bottom);
136
+
137
+ ctx.strokeStyle = typeof stroke === "function" ? stroke(moreProps, ctx) : stroke;
138
+ ctx.fillStyle = typeof fill === "function" ? fill(moreProps, ctx) : fill;
139
+ ctx.lineWidth = typeof strokeWidth === "function" ? strokeWidth(moreProps) : strokeWidth;
140
+
141
+ ctx.beginPath();
142
+
143
+ ctx.moveTo(x, y);
144
+ ctx.lineTo(x + dx, y + sign * dy);
145
+ ctx.lineTo(x + halfWidth, y + sign * dy);
146
+ ctx.lineTo(x + halfWidth, y + sign * (dy + height));
147
+ ctx.lineTo(x - halfWidth, y + sign * (dy + height));
148
+ ctx.lineTo(x - halfWidth, y + sign * dy);
149
+ ctx.lineTo(x - dx, y + sign * dy);
150
+ ctx.closePath();
151
+ ctx.stroke();
152
+ ctx.fill();
153
+
154
+ ctx.beginPath();
155
+ ctx.fillStyle = typeof textFill === "function" ? textFill(moreProps, ctx) : textFill;
156
+
157
+ ctx.textBaseline = orient === "top" ? "alphabetic" : "hanging";
158
+ const pad = orient === "top" ? padding.bottom : padding.top;
159
+
160
+ ctx.fillText(text, x, y + sign * (dy + pad + 2));
161
+ }
@@ -0,0 +1,141 @@
1
+ import * as React from "react";
2
+ import { getMouseCanvas, GenericChartComponent, isNotDefined } from "@tradingaction/core";
3
+ import { drawOnCanvas } from "./EdgeCoordinateV3";
4
+
5
+ export interface MouseCoordinateYProps {
6
+ readonly arrowWidth?: number;
7
+ readonly at?: "left" | "right";
8
+ readonly displayFormat: (value: number) => string;
9
+ readonly dx?: number;
10
+ readonly fontFamily?: string;
11
+ readonly fontSize?: number;
12
+ readonly fill?: string;
13
+ readonly fitToText?: boolean;
14
+ readonly opacity?: number;
15
+ readonly orient?: "left" | "right";
16
+ readonly rectWidth?: number;
17
+ readonly rectHeight?: number;
18
+ readonly stroke?: string;
19
+ readonly strokeOpacity?: number;
20
+ readonly strokeWidth?: number;
21
+ readonly textFill?: string;
22
+ readonly yAccessor?: (data: any) => number | undefined;
23
+ readonly yAxisPad?: number;
24
+ }
25
+
26
+ export class MouseCoordinateY extends React.Component<MouseCoordinateYProps> {
27
+ public static defaultProps = {
28
+ arrowWidth: 0,
29
+ at: "right",
30
+ dx: 0,
31
+ fill: "#4C525E",
32
+ fitToText: false,
33
+ fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
34
+ fontSize: 13,
35
+ opacity: 1,
36
+ orient: "right",
37
+ rectWidth: 50,
38
+ rectHeight: 20,
39
+ strokeOpacity: 1,
40
+ strokeWidth: 1,
41
+ textFill: "#FFFFFF",
42
+ yAxisPad: 0,
43
+ };
44
+
45
+ public render() {
46
+ return (
47
+ <GenericChartComponent
48
+ clip={false}
49
+ canvasDraw={this.drawOnCanvas}
50
+ canvasToDraw={getMouseCanvas}
51
+ drawOn={["mousemove", "pan", "drag"]}
52
+ />
53
+ );
54
+ }
55
+
56
+ private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
57
+ const props = this.helper(this.props, moreProps);
58
+ if (props === undefined) {
59
+ return;
60
+ }
61
+
62
+ drawOnCanvas(ctx, props);
63
+ };
64
+
65
+ private readonly helper = (props: MouseCoordinateYProps, moreProps: any) => {
66
+ const {
67
+ chartConfig: { yScale },
68
+ chartId,
69
+ currentItem,
70
+ currentCharts,
71
+ mouseXY,
72
+ show,
73
+ } = moreProps;
74
+
75
+ if (!show) {
76
+ return undefined;
77
+ }
78
+
79
+ if (isNotDefined(mouseXY)) {
80
+ return undefined;
81
+ }
82
+
83
+ if (currentCharts.indexOf(chartId) < 0) {
84
+ return undefined;
85
+ }
86
+
87
+ const { displayFormat, yAccessor } = props;
88
+
89
+ if (yAccessor && !currentItem) {
90
+ return undefined;
91
+ }
92
+
93
+ const y = yAccessor ? yScale(yAccessor(currentItem)) : mouseXY[1];
94
+
95
+ const coordinate = displayFormat(yScale.invert(y));
96
+
97
+ return getYCoordinate(y, coordinate, props, moreProps);
98
+ };
99
+ }
100
+
101
+ export function getYCoordinate(y: number, coordinate: string, props: any, moreProps: any) {
102
+ const { width } = moreProps;
103
+
104
+ const { orient, at, rectWidth, rectHeight, dx, stroke, strokeOpacity, strokeWidth } = props;
105
+ const { fill, opacity, fitToText, fontFamily, fontSize, textFill, arrowWidth } = props;
106
+
107
+ const x1 = 0;
108
+ const x2 = width;
109
+ const edgeAt = at === "right" ? width : 0;
110
+
111
+ const type = "horizontal";
112
+ const hideLine = true;
113
+
114
+ const coordinateProps = {
115
+ coordinate,
116
+ show: true,
117
+ fitToText,
118
+ type,
119
+ orient,
120
+ edgeAt,
121
+ hideLine,
122
+ fill,
123
+ opacity,
124
+ fontFamily,
125
+ fontSize,
126
+ textFill,
127
+ stroke,
128
+ strokeOpacity,
129
+ strokeWidth,
130
+ rectWidth,
131
+ rectHeight,
132
+ arrowWidth,
133
+ dx,
134
+ x1,
135
+ x2,
136
+ y1: y,
137
+ y2: y,
138
+ };
139
+
140
+ return coordinateProps;
141
+ }
@@ -0,0 +1,121 @@
1
+ import { format } from "d3-format";
2
+ import * as React from "react";
3
+ import { getAxisCanvas, GenericChartComponent, functor, strokeDashTypes } from "@tradingaction/core";
4
+ import { drawOnCanvas } from "./EdgeCoordinateV3";
5
+
6
+ interface PriceCoordinateProps {
7
+ readonly arrowWidth?: number;
8
+ readonly at?: "bottom" | "top" | "left" | "right";
9
+ readonly displayFormat: (n: number) => string;
10
+ readonly dx?: number;
11
+ readonly fontFamily?: string;
12
+ readonly fontSize?: number;
13
+ readonly fill?: string | ((price: number) => string);
14
+ readonly lineOpacity?: number;
15
+ readonly lineStroke?: string;
16
+ readonly opacity?: number;
17
+ readonly orient?: "bottom" | "top" | "left" | "right";
18
+ readonly price: number;
19
+ readonly rectWidth?: number;
20
+ readonly rectHeight?: number;
21
+ readonly strokeDasharray?: strokeDashTypes;
22
+ readonly stroke?: string;
23
+ readonly strokeOpacity?: number;
24
+ readonly strokeWidth?: number;
25
+ readonly textFill?: string | ((price: number) => string);
26
+ readonly yAxisPad?: number;
27
+ }
28
+
29
+ export class PriceCoordinate extends React.Component<PriceCoordinateProps> {
30
+ public static defaultProps = {
31
+ displayFormat: format(".2f"),
32
+ yAxisPad: 0,
33
+ rectWidth: 50,
34
+ rectHeight: 20,
35
+ orient: "left",
36
+ at: "left",
37
+ price: 0,
38
+ dx: 0,
39
+ arrowWidth: 0,
40
+ fill: "#BAB8b8",
41
+ opacity: 1,
42
+ lineOpacity: 0.2,
43
+ lineStroke: "#000000",
44
+ fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
45
+ fontSize: 13,
46
+ textFill: "#FFFFFF",
47
+ strokeOpacity: 1,
48
+ strokeWidth: 1,
49
+ strokeDasharray: "Solid",
50
+ };
51
+
52
+ public render() {
53
+ return (
54
+ <GenericChartComponent
55
+ clip={false}
56
+ canvasDraw={this.drawOnCanvas}
57
+ canvasToDraw={getAxisCanvas}
58
+ drawOn={["pan"]}
59
+ />
60
+ );
61
+ }
62
+
63
+ private readonly drawOnCanvas = (ctx: CanvasRenderingContext2D, moreProps: any) => {
64
+ const props = this.helper(this.props, moreProps);
65
+
66
+ drawOnCanvas(ctx, props);
67
+ };
68
+
69
+ private readonly helper = (props: PriceCoordinateProps, moreProps: any) => {
70
+ const {
71
+ chartConfig: { yScale },
72
+ width,
73
+ } = moreProps;
74
+ const [lowerYValue, upperYValue] = yScale.domain();
75
+
76
+ const { price, stroke, strokeDasharray, strokeOpacity, strokeWidth } = props;
77
+ const { orient, at, rectWidth, rectHeight, displayFormat, dx } = props;
78
+ const { fill, opacity, fontFamily, fontSize, textFill, arrowWidth, lineOpacity, lineStroke } = props;
79
+
80
+ const x1 = 0;
81
+ const x2 = width;
82
+ const edgeAt = at === "right" ? width : 0;
83
+
84
+ const type = "horizontal";
85
+
86
+ const y = yScale(price);
87
+ const show = price <= upperYValue && price >= lowerYValue;
88
+
89
+ const coordinate = displayFormat(yScale.invert(y));
90
+ const hideLine = false;
91
+
92
+ const coordinateProps = {
93
+ coordinate,
94
+ show,
95
+ type,
96
+ orient,
97
+ edgeAt,
98
+ hideLine,
99
+ lineOpacity,
100
+ lineStroke,
101
+ lineStrokeDasharray: strokeDasharray,
102
+ stroke,
103
+ strokeOpacity,
104
+ strokeWidth,
105
+ fill: functor(fill)(price),
106
+ textFill: functor(textFill)(price),
107
+ opacity,
108
+ fontFamily,
109
+ fontSize,
110
+ rectWidth,
111
+ rectHeight,
112
+ arrowWidth,
113
+ dx,
114
+ x1,
115
+ x2,
116
+ y1: y,
117
+ y2: y,
118
+ };
119
+ return coordinateProps;
120
+ };
121
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./EdgeIndicator";
2
+ export * from "./CrossHairCursor";
3
+ export * from "./CurrentCoordinate";
4
+ export * from "./Cursor";
5
+ export * from "./MouseCoordinateX";
6
+ export { MouseCoordinateXV2 } from "./MouseCoordinateXV2";
7
+ export { MouseCoordinateY } from "./MouseCoordinateY";
8
+ export * from "./PriceCoordinate";