This repository was archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCurveChart.jsx
164 lines (150 loc) · 4.26 KB
/
CurveChart.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React from "react";
import {
LineChart,
Line,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
ResponsiveContainer,
Label,
} from "recharts";
// Custom label component for the x-axis
const CustomXAxisLabel = ({ viewBox }) => {
const { x, y, width, height } = viewBox;
return (
<g transform={`translate(${x},${y + height - 4})`}>
<text
x={width / 2 + 1}
y={0}
dy={0}
textAnchor="middle"
fill="#666"
fontFamily="monospace"
>
◄ Sell | Buy ►
</text>
</g>
);
};
const formatYAxisTick = (value) => {
return parseFloat(value).toPrecision(2);
};
const CustomTooltip = ({ active, payload, label }) => {
if (active && payload && payload.length) {
let actionText;
let buyPrice, sellPrice;
const price = Number(payload[0].value); // Access price here
const fee = Number(payload[0].payload.fee); // Access fee here
console.log("CustomTooltip", { price, fee });
if (label === 0) {
actionText = "Initial Price";
buyPrice = (price * (100 + fee)) / 100;
sellPrice = (price * (100 - fee)) / 100;
} else if (label < 0) {
actionText = `Sell ${Math.abs(label)}`;
buyPrice = (price * (100 + fee)) / 100;
sellPrice = (price * (100 - fee)) / 100;
} else if (label > 0) {
actionText = `Buy ${label}`;
buyPrice = (price * (100 + fee)) / 100;
sellPrice = (price * (100 - fee)) / 100;
}
return (
<div
style={{
backgroundColor: "#f5f5f5",
border: "1px solid #ccc",
padding: "5px",
}}
>
<p>{actionText}</p>
{buyPrice && (
<p>{`Buy Price: ${parseFloat(buyPrice).toPrecision(3)}`}</p>
)}
{sellPrice && (
<p>{`Sell Price: ${parseFloat(sellPrice).toPrecision(3)}`}</p>
)}
</div>
);
}
return null;
};
const CurveChart = ({
curveType = "exponential",
delta = 20,
fee = 10,
initialPrice = 0.1,
hideYLabels = false, // Add this line
}) => {
console.log("CurveChart render", { curveType, delta, initialPrice });
const generateCurveData = () => {
const data = [];
const numPoints = 10;
for (let i = -numPoints / 2; i <= numPoints / 2; i++) {
let y;
if (curveType === "linear") {
y = Number(Number(initialPrice) + delta * i).toFixed(6);
} else if (curveType === "exponential") {
y = Number(Number(initialPrice) * (1 + delta / 100) ** i).toFixed(6);
}
if (y >= 0) {
data.push({ x: i, y, fee });
}
}
return data;
};
const curveData = generateCurveData();
const yMin = Math.min(...curveData.map((d) => d.y));
const yMax = Math.max(...curveData.map((d) => d.y));
const leftMargin = hideYLabels ? 0 : 20; // Adjust as needed
const rightMargin = hideYLabels ? 0 : 30; // Adjust to be the same as leftMargin when hideYLabels is true
return (
<ResponsiveContainer width="100%" height={300}>
<LineChart
data={curveData}
margin={{
top: 5,
right: rightMargin, // Use the rightMargin variable here
left: leftMargin, // Use the leftMargin variable here
bottom: 30,
}}
>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey="x" tickLine={false} tickFormatter={() => ""}>
<Label
content={<CustomXAxisLabel />}
offset={0}
position="insideBottom"
/>
</XAxis>
{!hideYLabels && ( // Conditionally render the YAxis here
<YAxis domain={[yMin, yMax]} tickFormatter={formatYAxisTick} />
)}
<Tooltip content={<CustomTooltip />} />
<Line
dot={({ cx, cy, index }) => (
<circle
cx={cx}
cy={cy}
r={5}
fill={
index == Math.floor(curveData.length / 2)
? "white"
: index < curveData.length / 2
? "red"
: "#4CBB17"
}
stroke={index === 5 ? "#8884d8" : "none"}
/>
)}
type="monotone"
strokeWidth={3}
dataKey="y"
stroke="#8884d8"
/>
</LineChart>
</ResponsiveContainer>
);
};
export default CurveChart;