@tradingaction/axes 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.
- package/LICENSE +24 -0
- package/README.md +5 -0
- package/lib/Axis.d.ts +59 -0
- package/lib/Axis.js +244 -0
- package/lib/Axis.js.map +1 -0
- package/lib/AxisZoomCapture.d.ts +47 -0
- package/lib/AxisZoomCapture.js +135 -0
- package/lib/AxisZoomCapture.js.map +1 -0
- package/lib/XAxis.d.ts +71 -0
- package/lib/XAxis.js +103 -0
- package/lib/XAxis.js.map +1 -0
- package/lib/YAxis.d.ts +71 -0
- package/lib/YAxis.js +104 -0
- package/lib/YAxis.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +4 -0
- package/lib/index.js.map +1 -0
- package/package.json +53 -0
- package/src/Axis.tsx +407 -0
- package/src/AxisZoomCapture.tsx +232 -0
- package/src/XAxis.tsx +164 -0
- package/src/YAxis.tsx +161 -0
- package/src/index.ts +3 -0
package/src/XAxis.tsx
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { ChartContext, strokeDashTypes } from "@tradingaction/core";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { Axis } from "./Axis";
|
|
4
|
+
|
|
5
|
+
export interface XAxisProps<T extends number | Date> {
|
|
6
|
+
readonly axisAt?: number | "top" | "bottom" | "middle";
|
|
7
|
+
readonly className?: string;
|
|
8
|
+
readonly domainClassName?: string;
|
|
9
|
+
readonly fontFamily?: string;
|
|
10
|
+
readonly fontSize?: number;
|
|
11
|
+
readonly fontWeight?: number;
|
|
12
|
+
readonly getMouseDelta?: (startXY: [number, number], mouseXY: [number, number]) => number;
|
|
13
|
+
readonly gridLinesStrokeStyle?: string;
|
|
14
|
+
readonly gridLinesStrokeWidth?: number;
|
|
15
|
+
readonly gridLinesStrokeDasharray?: strokeDashTypes;
|
|
16
|
+
readonly innerTickSize?: number;
|
|
17
|
+
readonly onContextMenu?: (e: React.MouseEvent, mousePosition: [number, number]) => void;
|
|
18
|
+
readonly onDoubleClick?: (e: React.MouseEvent, mousePosition: [number, number]) => void;
|
|
19
|
+
readonly orient?: "top" | "bottom";
|
|
20
|
+
readonly outerTickSize?: number;
|
|
21
|
+
readonly showDomain?: boolean;
|
|
22
|
+
readonly showGridLines?: boolean;
|
|
23
|
+
readonly showTicks?: boolean;
|
|
24
|
+
readonly showTickLabel?: boolean;
|
|
25
|
+
readonly strokeStyle?: string;
|
|
26
|
+
readonly strokeWidth?: number;
|
|
27
|
+
readonly tickFormat?: (value: T) => string;
|
|
28
|
+
readonly tickPadding?: number;
|
|
29
|
+
readonly tickSize?: number;
|
|
30
|
+
readonly tickLabelFill?: string;
|
|
31
|
+
readonly ticks?: number;
|
|
32
|
+
readonly tickStrokeStyle?: string;
|
|
33
|
+
readonly tickStrokeWidth?: number;
|
|
34
|
+
readonly tickStrokeDasharray?: strokeDashTypes;
|
|
35
|
+
readonly tickValues?: number[];
|
|
36
|
+
readonly xZoomHeight?: number;
|
|
37
|
+
readonly zoomEnabled?: boolean;
|
|
38
|
+
readonly zoomCursorClassName?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class XAxis<T extends number | Date> extends React.Component<XAxisProps<T>> {
|
|
42
|
+
public static defaultProps = {
|
|
43
|
+
axisAt: "bottom",
|
|
44
|
+
className: "react-financial-charts-x-axis",
|
|
45
|
+
domainClassName: "react-financial-charts-axis-domain",
|
|
46
|
+
fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
|
|
47
|
+
fontSize: 12,
|
|
48
|
+
fontWeight: 400,
|
|
49
|
+
getMouseDelta: (startXY: [number, number], mouseXY: [number, number]) => startXY[0] - mouseXY[0],
|
|
50
|
+
gridLinesStrokeStyle: "#E2E4EC",
|
|
51
|
+
gridLinesStrokeWidth: 1,
|
|
52
|
+
orient: "bottom",
|
|
53
|
+
outerTickSize: 0,
|
|
54
|
+
innerTickSize: 4,
|
|
55
|
+
showDomain: true,
|
|
56
|
+
showGridLines: false,
|
|
57
|
+
showTicks: true,
|
|
58
|
+
showTickLabel: true,
|
|
59
|
+
strokeStyle: "#000000",
|
|
60
|
+
strokeWidth: 1,
|
|
61
|
+
tickPadding: 4,
|
|
62
|
+
tickLabelFill: "#000000",
|
|
63
|
+
tickStrokeStyle: "#000000",
|
|
64
|
+
xZoomHeight: 25,
|
|
65
|
+
zoomEnabled: true,
|
|
66
|
+
zoomCursorClassName: "react-financial-charts-ew-resize-cursor",
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
public static contextType = ChartContext;
|
|
70
|
+
|
|
71
|
+
public render() {
|
|
72
|
+
const {
|
|
73
|
+
getMouseDelta = XAxis.defaultProps.getMouseDelta,
|
|
74
|
+
outerTickSize = XAxis.defaultProps.outerTickSize,
|
|
75
|
+
showTicks,
|
|
76
|
+
strokeStyle = XAxis.defaultProps.strokeStyle,
|
|
77
|
+
strokeWidth = XAxis.defaultProps.strokeWidth,
|
|
78
|
+
zoomEnabled,
|
|
79
|
+
...rest
|
|
80
|
+
} = this.props;
|
|
81
|
+
|
|
82
|
+
const { ...moreProps } = this.helper();
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<Axis
|
|
86
|
+
{...rest}
|
|
87
|
+
{...moreProps}
|
|
88
|
+
getMouseDelta={getMouseDelta}
|
|
89
|
+
outerTickSize={outerTickSize}
|
|
90
|
+
showTicks={showTicks}
|
|
91
|
+
strokeStyle={strokeStyle}
|
|
92
|
+
strokeWidth={strokeWidth}
|
|
93
|
+
zoomEnabled={zoomEnabled && showTicks}
|
|
94
|
+
axisZoomCallback={this.axisZoomCallback}
|
|
95
|
+
/>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private readonly axisZoomCallback = (newXDomain: number[]) => {
|
|
100
|
+
const { xAxisZoom } = this.context;
|
|
101
|
+
|
|
102
|
+
xAxisZoom(newXDomain);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
private readonly helper = () => {
|
|
106
|
+
const { axisAt, xZoomHeight = XAxis.defaultProps.xZoomHeight, orient, ticks } = this.props;
|
|
107
|
+
const {
|
|
108
|
+
chartConfig: { width, height },
|
|
109
|
+
} = this.context;
|
|
110
|
+
|
|
111
|
+
let axisLocation;
|
|
112
|
+
const x = 0;
|
|
113
|
+
const w = width;
|
|
114
|
+
const h = xZoomHeight;
|
|
115
|
+
|
|
116
|
+
switch (axisAt) {
|
|
117
|
+
case "top":
|
|
118
|
+
axisLocation = 0;
|
|
119
|
+
break;
|
|
120
|
+
case "bottom":
|
|
121
|
+
axisLocation = height;
|
|
122
|
+
break;
|
|
123
|
+
case "middle":
|
|
124
|
+
axisLocation = height / 2;
|
|
125
|
+
break;
|
|
126
|
+
default:
|
|
127
|
+
axisLocation = axisAt;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const y = orient === "top" ? -xZoomHeight : 0;
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
transform: [0, axisLocation],
|
|
134
|
+
range: [0, width],
|
|
135
|
+
getScale: this.getXScale,
|
|
136
|
+
bg: { x, y, h, w },
|
|
137
|
+
ticks: ticks ?? this.getXTicks(width),
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
private readonly getXTicks = (width: number) => {
|
|
142
|
+
if (width < 400) {
|
|
143
|
+
return 2;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (width < 500) {
|
|
147
|
+
return 6;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return 8;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
private readonly getXScale = (moreProps: any) => {
|
|
154
|
+
const { xScale: scale, width } = moreProps;
|
|
155
|
+
|
|
156
|
+
if (scale.invert) {
|
|
157
|
+
const trueRange = [0, width];
|
|
158
|
+
const trueDomain = trueRange.map(scale.invert);
|
|
159
|
+
return scale.copy().domain(trueDomain).range(trueRange);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return scale;
|
|
163
|
+
};
|
|
164
|
+
}
|
package/src/YAxis.tsx
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { ChartContext, strokeDashTypes } from "@tradingaction/core";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { Axis } from "./Axis";
|
|
4
|
+
|
|
5
|
+
export interface YAxisProps {
|
|
6
|
+
readonly axisAt?: number | "left" | "right" | "middle";
|
|
7
|
+
readonly className?: string;
|
|
8
|
+
readonly domainClassName?: string;
|
|
9
|
+
readonly fontFamily?: string;
|
|
10
|
+
readonly fontSize?: number;
|
|
11
|
+
readonly fontWeight?: number;
|
|
12
|
+
readonly getMouseDelta?: (startXY: [number, number], mouseXY: [number, number]) => number;
|
|
13
|
+
readonly gridLinesStrokeStyle?: string;
|
|
14
|
+
readonly gridLinesStrokeWidth?: number;
|
|
15
|
+
readonly gridLinesStrokeDasharray?: strokeDashTypes;
|
|
16
|
+
readonly innerTickSize?: number;
|
|
17
|
+
readonly onContextMenu?: (e: React.MouseEvent, mousePosition: [number, number]) => void;
|
|
18
|
+
readonly onDoubleClick?: (e: React.MouseEvent, mousePosition: [number, number]) => void;
|
|
19
|
+
readonly orient?: "left" | "right";
|
|
20
|
+
readonly outerTickSize?: number;
|
|
21
|
+
readonly showDomain?: boolean;
|
|
22
|
+
readonly showGridLines?: boolean;
|
|
23
|
+
readonly showTicks?: boolean;
|
|
24
|
+
readonly showTickLabel?: boolean;
|
|
25
|
+
readonly strokeStyle?: string;
|
|
26
|
+
readonly strokeWidth?: number;
|
|
27
|
+
readonly tickFormat?: (value: number) => string;
|
|
28
|
+
readonly tickPadding?: number;
|
|
29
|
+
readonly tickSize?: number;
|
|
30
|
+
readonly tickLabelFill?: string;
|
|
31
|
+
readonly ticks?: number;
|
|
32
|
+
readonly tickStrokeStyle?: string;
|
|
33
|
+
readonly tickStrokeWidth?: number;
|
|
34
|
+
readonly tickStrokeDasharray?: strokeDashTypes;
|
|
35
|
+
readonly tickValues?: number[];
|
|
36
|
+
readonly yZoomWidth?: number;
|
|
37
|
+
readonly zoomEnabled?: boolean;
|
|
38
|
+
readonly zoomCursorClassName?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class YAxis extends React.Component<YAxisProps> {
|
|
42
|
+
public static defaultProps = {
|
|
43
|
+
axisAt: "right",
|
|
44
|
+
className: "react-financial-charts-y-axis",
|
|
45
|
+
domainClassName: "react-financial-charts-axis-domain",
|
|
46
|
+
fontFamily: "-apple-system, system-ui, Roboto, 'Helvetica Neue', Ubuntu, sans-serif",
|
|
47
|
+
fontSize: 12,
|
|
48
|
+
fontWeight: 400,
|
|
49
|
+
getMouseDelta: (startXY: [number, number], mouseXY: [number, number]) => startXY[1] - mouseXY[1],
|
|
50
|
+
gridLinesStrokeStyle: "#E2E4EC",
|
|
51
|
+
gridLinesStrokeWidth: 1,
|
|
52
|
+
innerTickSize: 4,
|
|
53
|
+
outerTickSize: 0,
|
|
54
|
+
orient: "right",
|
|
55
|
+
showDomain: true,
|
|
56
|
+
showGridLines: false,
|
|
57
|
+
showTicks: true,
|
|
58
|
+
showTickLabel: true,
|
|
59
|
+
strokeStyle: "#000000",
|
|
60
|
+
strokeWidth: 1,
|
|
61
|
+
tickPadding: 4,
|
|
62
|
+
tickLabelFill: "#000000",
|
|
63
|
+
tickStrokeStyle: "#000000",
|
|
64
|
+
yZoomWidth: 40,
|
|
65
|
+
zoomEnabled: true,
|
|
66
|
+
zoomCursorClassName: "react-financial-charts-ns-resize-cursor",
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
public static contextType = ChartContext;
|
|
70
|
+
|
|
71
|
+
public render() {
|
|
72
|
+
const {
|
|
73
|
+
getMouseDelta = YAxis.defaultProps.getMouseDelta,
|
|
74
|
+
outerTickSize = YAxis.defaultProps.outerTickSize,
|
|
75
|
+
strokeStyle = YAxis.defaultProps.strokeStyle,
|
|
76
|
+
strokeWidth = YAxis.defaultProps.strokeWidth,
|
|
77
|
+
...rest
|
|
78
|
+
} = this.props;
|
|
79
|
+
|
|
80
|
+
const { zoomEnabled, ...moreProps } = this.helper();
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<Axis
|
|
84
|
+
{...rest}
|
|
85
|
+
{...moreProps}
|
|
86
|
+
edgeClip
|
|
87
|
+
getMouseDelta={getMouseDelta}
|
|
88
|
+
outerTickSize={outerTickSize}
|
|
89
|
+
strokeStyle={strokeStyle}
|
|
90
|
+
strokeWidth={strokeWidth}
|
|
91
|
+
zoomEnabled={this.props.zoomEnabled && zoomEnabled}
|
|
92
|
+
axisZoomCallback={this.axisZoomCallback}
|
|
93
|
+
/>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private readonly axisZoomCallback = (newYDomain: number[]) => {
|
|
98
|
+
const { chartId, yAxisZoom } = this.context;
|
|
99
|
+
|
|
100
|
+
yAxisZoom(chartId, newYDomain);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
private readonly helper = () => {
|
|
104
|
+
const { axisAt, ticks, yZoomWidth = YAxis.defaultProps.yZoomWidth, orient } = this.props;
|
|
105
|
+
const {
|
|
106
|
+
chartConfig: { width, height },
|
|
107
|
+
} = this.context;
|
|
108
|
+
|
|
109
|
+
let axisLocation;
|
|
110
|
+
const y = 0;
|
|
111
|
+
const w = yZoomWidth;
|
|
112
|
+
const h = height;
|
|
113
|
+
|
|
114
|
+
switch (axisAt) {
|
|
115
|
+
case "left":
|
|
116
|
+
axisLocation = 0;
|
|
117
|
+
break;
|
|
118
|
+
case "right":
|
|
119
|
+
axisLocation = width;
|
|
120
|
+
break;
|
|
121
|
+
case "middle":
|
|
122
|
+
axisLocation = width / 2;
|
|
123
|
+
break;
|
|
124
|
+
default:
|
|
125
|
+
axisLocation = axisAt;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const x = orient === "left" ? -yZoomWidth : 0;
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
transform: [axisLocation, 0],
|
|
132
|
+
range: [0, height],
|
|
133
|
+
getScale: this.getYScale,
|
|
134
|
+
bg: { x, y, h, w },
|
|
135
|
+
ticks: ticks ?? this.getYTicks(height),
|
|
136
|
+
zoomEnabled: this.context.chartConfig.yPan,
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
private readonly getYTicks = (height: number) => {
|
|
141
|
+
if (height < 300) {
|
|
142
|
+
return 2;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (height < 500) {
|
|
146
|
+
return 6;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return 8;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
private readonly getYScale = (moreProps: any) => {
|
|
153
|
+
const { yScale: scale, flipYScale, height } = moreProps.chartConfig;
|
|
154
|
+
if (scale.invert) {
|
|
155
|
+
const trueRange = flipYScale ? [0, height] : [height, 0];
|
|
156
|
+
const trueDomain = trueRange.map(scale.invert);
|
|
157
|
+
return scale.copy().domain(trueDomain).range(trueRange);
|
|
158
|
+
}
|
|
159
|
+
return scale;
|
|
160
|
+
};
|
|
161
|
+
}
|
package/src/index.ts
ADDED