-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.html
145 lines (131 loc) · 5.62 KB
/
09.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>09 - Real World</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.0.0/d3.js"></script>
<script>
const container = d3.create('svg')
.attr('width', 1000)
.attr('height', 400)
.attr('font-family', 'sans-serif')
.attr('font-size', 10)
.attr('text-anchor', 'end');
document.body.appendChild(container.node());
const margins = {top: 20, right: 0, bottom: 30, left: 70};
const barsG = container.append('g');
const xAxisG = container.append('g')
.attr('transform', `translate(0, ${container.attr('height') - margins.bottom})`);
const yAxisG = container.append('g')
.attr('transform', `translate(${margins.left}, 0)`);
function draw(data, yAccessor, xAccessor) {
const x = d3.scaleLinear()
.domain([0, d3.max(data, xAccessor)])
.range([margins.left, container.attr('width') - margins.right]);
xAxisG
.transition(d3.transition().duration(750))
.call(d3.axisBottom().scale(x));
const y = d3.scaleBand()
.domain(data.map(yAccessor))
.rangeRound([margins.top, container.attr('height') - margins.bottom])
.padding(0.1);
yAxisG
.transition(d3.transition().duration(750))
.call(d3.axisLeft().scale(y));
const bar = barsG
.selectAll('g')
.data(data, yAccessor)
.join(
enter => enter
.append('g')
.attr('transform', d => `translate(${margins.left}, ${y(yAccessor(d))})`)
.call(g => {
g.append('rect')
.attr('fill', 'steelblue');
g.append('text')
.attr('fill', 'white')
.attr('dy', '.35em')
})
)
.transition(d3.transition().duration(750))
.attr('transform', d => `translate(${margins.left}, ${y(yAccessor(d))})`);
bar.select('rect')
.attr('width', d => x(xAccessor(d)) - x(0))
.attr('height', y.bandwidth());
bar.select('text')
.attr('x', d => x(xAccessor(d)) - x(0) - 3)
.attr('y', y.bandwidth() / 2)
.text(d => xAccessor(d));
}
const token = '<FILL ME IN>';
const symbols = ['AAPL', 'AMZN', 'FB', 'GOOGL', 'MSFT', 'NFLX', 'TSLA', 'AMC', 'GME'];
function stocksUpdateExample() {
const symbolData = {};
function drawSymbols() {
draw(Object.values(symbolData), d => d.symbol, d => d.price);
}
// Initial fetch of all symbols
Promise.all(
symbols.map((symbol) =>
fetch(`https://finnhub.io/api/v1/quote?${new URLSearchParams({symbol, token}).toString()}`)
.then(resp => resp.json())
.then(quote => [symbol, quote])
)
).then(responses => {
responses.forEach(([symbol, quote]) => symbolData[symbol] = {symbol, price: quote.c});
drawSymbols();
});
// Watch updates with a websocket
const socket = new WebSocket(`wss://ws.finnhub.io?token=${token}`);
socket.addEventListener('open', () =>
symbols.forEach(sym =>
socket.send(JSON.stringify({'type':'subscribe', 'symbol': sym}))
)
);
socket.addEventListener('message', event => {
const data = JSON.parse(event.data);
if (data.type === 'trade') {
data.data.forEach(({s, p}) => symbolData[s] = {symbol: s, price: p});
drawSymbols();
}
});
}
function stockDateExample() {
d3.create('div')
.call(e => document.body.appendChild(e.node()))
.selectAll('button')
.data(symbols)
.join('button')
.text(d => d)
.on('click', (_, symbol) => {
const now = new Date();
const nowMinus30D = new Date(now.getTime() - ((24 * 60 * 60 * 1000) * 30));
const params = {
symbol,
resolution: 'D',
from: Math.floor(nowMinus30D.getTime() / 1000),
to: Math.floor(now.getTime()/1000),
token
};
fetch(`https://finnhub.io/api/v1/stock/candle?${new URLSearchParams(params).toString()}`)
.then(resp => resp.json())
.then(json =>
draw(
json.t.map((time, index) => ({
time: new Date(time * 1000).toISOString().split('T')[0],
close: json.c[index]
})),
d => d.time,
d => d.close
)
);
});
}
stocksUpdateExample();
// stockDateExample();
</script>
</body>
</html>