-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chart.js
165 lines (157 loc) · 4.69 KB
/
Chart.js
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
165
import { useContext, useLayoutEffect, useState } from "react";
import {
LineChart,
Line,
XAxis,
YAxis,
Tooltip,
CartesianGrid,
Legend,
ResponsiveContainer,
} from "recharts";
import { CryptoContext } from "../context/CryptoContext";
function CustomTooltip({ payload, label, active, currency }) {
if (active) {
return (
<div className="custom-tooltip">
<p className="label">{`${label} : ${new Intl.NumberFormat("en-IN", {
style: "currency",
currency: currency,
minimumFractionDigits: 5,
}).format(payload[0].value)}`}</p>
</div>
);
}
return null;
}
const ChartComponent = ({ data, currency, type }) => {
return (
<ResponsiveContainer height={"90%"}>
<LineChart width={400} height={400} data={data}>
<Line
type="monotone"
dataKey={type}
stroke="#14ffec"
strokeWidth={"1px"}
/>
<CartesianGrid stroke="#323232" />
<XAxis dataKey="date" hide />
<YAxis dataKey={type} hide domain={["auto", "auto"]} />
<Tooltip
content={<CustomTooltip />}
currency={currency}
cursor={false}
wrapperStyle={{ outline: "none" }}
/>
<Legend />
</LineChart>
</ResponsiveContainer>
);
};
const Chart = ({ id }) => {
const [chartData, setChartData] = useState();
let { currency } = useContext(CryptoContext);
const [type, setType] = useState("prices");
const [days, setDays] = useState("7");
useLayoutEffect(() => {
const getChartData = async () => {
try {
const response = await fetch(
`https://api.coingecko.com/api/v3/coins/${id}/market_chart?vs_currency=${currency}&days=${days}&interval=daily`
);
const data = await response.json();
// console.log(data);
let dataChange = data[type].map((item) => {
return {
date: new Date(item[0]).toLocaleDateString(),
[type]: item[1],
};
});
// console.log(dataChange);
setChartData(dataChange);
} catch (error) {
// console.error("Error fetching data:", error);
// Handle the error here
}
};
getChartData(id);
}, [id, type, days]);
return (
<div className="w-full h-[60%] mt-[2.5rem]">
<ChartComponent data={chartData} currency={currency} type={type} />
<div className="flex items-center justify-around">
<div className="flex ">
<button
className={`mx-2 bg-opacity-30 px-1 text-sm py-0.5 rounded ${
type === "prices"
? "bg-cyan text-cyan"
: "bg-gray-200 text-gray-100"
}`}
onClick={() => {
setType("prices");
}}
>
Price
</button>
<button
className={`mx-2 bg-opacity-30 px-1 text-sm py-0.5 rounded ${
type === "market_caps"
? "bg-cyan text-cyan"
: "bg-gray-200 text-gray-100"
}`}
onClick={() => {
setType("market_caps");
}}
>
Market Cap
</button>
<button
className={`mx-2 bg-opacity-30 px-1 text-sm py-0.5 rounded ${
type === "total_volumes"
? "bg-cyan text-cyan"
: "bg-gray-200 text-gray-100"
}`}
onClick={() => {
setType("total_volumes");
}}
>
Total Volume
</button>
</div>
<div className="flex">
<button
className={`mx-2 bg-opacity-30 px-1 text-sm py-0.5 rounded ${
days === "7" ? "bg-cyan text-cyan" : "bg-gray-200 text-gray-100"
}`}
onClick={() => {
setDays("7");
}}
>
7d
</button>
<button
className={`mx-2 bg-opacity-30 px-1 text-sm py-0.5 rounded ${
days === "14" ? "bg-cyan text-cyan" : "bg-gray-200 text-gray-100"
}`}
onClick={() => {
setDays("14");
}}
>
14d
</button>
<button
className={`mx-2 bg-opacity-30 px-1 text-sm py-0.5 rounded ${
days === "30" ? "bg-cyan text-cyan" : "bg-gray-200 text-gray-100"
}`}
onClick={() => {
setDays("30");
}}
>
30d
</button>
</div>
</div>
</div>
);
};
export default Chart;