-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.html
216 lines (204 loc) · 8.2 KB
/
visualization.html
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Gold Price Visualization</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/echarts@5.3.3/dist/echarts.js"
integrity="sha256-E5b0O4AhPTlxRxD4SNUVrMx7/RQDnV2PjWpPIjWoXd4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.4.1/papaparse.min.js"
integrity="sha512-dfX5uYVXzyU8+KHqj8bjo7UkOdg18PaOtpa48djpNbZHwExddghZ+ZmzWT06R5v6NSk3ZUfsH6FNEDepLx9hPQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<style>
.container {
min-height: 88vh;
}
@media (max-width: 576px) {
html, body {
margin: 0;
height: 100%;
overflow: hidden
}
.container {
min-height: 70vh;
}
}
</style>
</head>
<body>
<div class="container position-relative">
<ul class="pt-3 nav nav-tabs d-lg-flex d-none">
<li class="nav-item">
<button class="nav-link active" data-bs-toggle="tab" data-bs-target="#basic_chart" type="button"
role="tab">Gold Savings Account</button>
</li>
<li class="nav-item">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#premium_chart" type="button"
role="tab">Premier Gold Account</button>
</li>
</ul>
<ul class="pt-3 nav nav-pills d-flex d-lg-none row">
<li class="nav-item col">
<button class="nav-link mx-auto active" data-bs-toggle="tab" data-bs-target="#basic_chart" type="button" role="tab">Gold Savings Account</button>
</li>
<li class="nav-item col">
<button class="nav-link mx-auto" data-bs-toggle="tab" data-bs-target="#premium_chart" type="button" role="tab">Premier Gold Account</button>
</li>
</ul>
<div class="tab-content mt-4">
<div id="basic_chart" class="tab-pane fade position-absolute w-100 h-100 show active" role="tabpanel"
tabindex="0"></div>
<div id="premium_chart" class="tab-pane fade position-absolute w-100 h-100" role="tabpanel"
tabindex="0"></div>
</div>
</div>
<script type="text/javascript">
const basic_chart = echarts.init(document.getElementById('basic_chart'));
const premium_chart = echarts.init(document.getElementById('premium_chart'));
let option = {
title: {
text: ''
},
tooltip: {
trigger: 'axis'
},
legend: {},
xAxis: {
type: 'time',
},
yAxis: {
type: 'value',
min: 'dataMin'
},
dataZoom: [
{
type: 'slider',
show: true,
xAxisIndex: [0],
}
],
dataset: {
source: []
},
series: [
{
name: 'Bank Selling',
type: 'line',
step: 'end',
showSymbol: false,
endLabel: {
show: true,
formatter: (params) => {
return parseFloat(params.value[1]).toFixed(2)
}
},
encode: {
x: 'TIME',
y: 'SELLING',
}
}, {
name: 'Bank Buying',
type: 'line',
step: 'end',
showSymbol: false,
endLabel: {
show: true,
formatter: (params) => {
return parseFloat(params.value[2]).toFixed(2)
}
},
encode: {
x: 'TIME',
y: 'BUYING',
}
}
]
};
let cache = {}
let fetch_promise = {}
function load_data(url) {
if (cache[url]) {
console.log('cache')
return Promise.resolve(cache[url]);
} else if (fetch_promise[url]) {
console.log('waiting')
return fetch_promise[url]
} else {
console.log('not cache')
fetch_promise[url] = fetch(url)
.then(res => res.text())
.then(data => {
cache[url] = Papa.parse(data, { skipEmptyLines: true }).data.map(row => Object.values(row));
return cache[url];
})
.catch(error => console.error(error))
.finally(() => fetch_promise[url] = null)
return fetch_promise[url]
}
}
function render_chart(title, data, chart) {
// option.title.text = title;
option.dataset.source = data;
let newData = [...data.slice(-1)[0]]
newData[0] = string_date(new Date())
data.push(newData)
const date = new Date();
if (document.body.clientWidth < 597) {
option.grid = {
right: '20%',
left: '15%',
}
date.setDate(1)
date.setMonth(date.getMonth()-3)
option.dataZoom[0].startValue = string_date(date)
} else {
date.setMonth(date.getMonth()-6)
option.dataZoom[0].startValue = string_date(date)
}
chart.setOption(option);
}
function string_date(date) {
const year = `${date.getFullYear()}`
const month = `${(date.getMonth() + 1).toString().padStart(2, '0')}`
const day = `${date.getDate().toString().padStart(2, '0')}`
const hour = `${date.getHours().toString().padStart(2, '0')}`
const minute = `${date.getMinutes().toString().padStart(2, '0')}`
const second = `${date.getSeconds().toString().padStart(2, '0')}`
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
Promise.all([load_data('data/basic_price.csv'), load_data('data/premium_price.csv')])
.then(([basic_data, premium_data]) => {
render_chart('Gold Savings Account', basic_data, basic_chart)
render_chart('Premier Gold Account', premium_data, premium_chart)
})
document.querySelectorAll('button[data-bs-toggle="tab"]').forEach(
tabEl => tabEl.addEventListener('shown.bs.tab', event => {
if (event.target.textContent==='Premier Gold Account'){
premium_chart.resize()
} else {
basic_chart.resize()
}
})
)
window.onresize = () => {
basic_chart.resize()
premium_chart.resize()
}
// const sleep = ms => new Promise(r => setTimeout(r, ms));
// (async function demo() {
// for (let i = 0; i < 5; i++) {
// console.log(`Waiting ${i} seconds...`);
// await sleep(i * 1000);
// load_basic_data('data/basic_price.csv')
// load_basic_data('data/premium_price.csv')
// }
// console.log('Done');
// })()
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
crossorigin="anonymous"></script>
</body>
</html>