-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine.js
More file actions
137 lines (118 loc) · 4.36 KB
/
Line.js
File metadata and controls
137 lines (118 loc) · 4.36 KB
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
import React, { useEffect, useState, useRef } from "react";
import * as echarts from 'echarts';
import Decimal from 'decimal.js';
export default function Linecharts({ name, color }) {
const chart = useRef(null);
const [isLoading, setIsLoading] = useState(true);
const [prices, setPrices] = useState([]);
const [dates, setDates] = useState([]);
const showChart = (ele, x, y) => {
// Dispose of the old chart if there's one
if (echarts.getInstanceByDom(ele)) {
echarts.dispose(ele);
}
let mychart = echarts.init(ele);
let option = {
grid: {
top: "0px",
left: "0px",
right: "0px",
bottom: "0px"
},
xAxis: {
type: 'category',
data: x,
show: false
},
yAxis: {
type: 'value',
show: false,
min: Math.min(...y),
max: Math.max(...y)
},
series: [
{
name: name,
showSymbol: false,
data: y,
type: 'line',
symbolSize: 1,
markLine: { silent: true },
color: color,
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: color
},
{
offset: 1,
color: 'rgba(0, 0, 0, 0)'
}
])
}
}
],
tooltip: {
trigger: 'axis',
backgroundColor: 'rgba(32, 33, 36,.7)',
borderColor: 'rgba(32, 33, 36,0.20)',
borderWidth: 1,
formatter: params => {
const index = params[0].dataIndex;
return `
<div>
<strong> ${name}</strong><br />
${dates[index]}<br />
Price: $${new Decimal(params[0].data).toFixed(7)}
</div>
`;
},
textStyle: {
color: '#fff',
fontSize: '12'
}
}
};
mychart.setOption(option);
}
useEffect(() => {
Promise.all([
fetch('json/price_value.json').then(response => response.json()),
fetch('json/price_date.json').then(response => response.json())
])
.then(([priceData, dateData]) => {
const matchedPriceData = priceData.find(item => item.name_ch === name);
const matchedDateData = dateData.find(item => item.name_ch === name);
if (!matchedPriceData || !matchedDateData) {
console.error('No data found for the provided name:', name);
setIsLoading(false);
return;
}
const pricesArr = [matchedPriceData.price, matchedPriceData.price1, matchedPriceData.price2, matchedPriceData.price3, matchedPriceData.price4].reverse();
const datesArr = [matchedDateData.date_price, matchedDateData.date_price1, matchedDateData.date_price2, matchedDateData.date_price3, matchedDateData.date_price4].reverse();
setPrices(pricesArr);
setDates(datesArr);
setIsLoading(false);
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
setIsLoading(false);
});
}, [name]);
useEffect(() => {
if (!isLoading) {
showChart(chart.current, dates, prices.map(price => Decimal(price).valueOf()));
}
}, [isLoading, dates, prices]);
return (
<div className="bar-chart-container">
{isLoading && (
<div className="loader-container">
<div className="loader"></div>
</div>
)}
<div className="chart" ref={chart}></div>
</div>
);
}