forked from mylhei/depth-chart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdepth-chart.html
294 lines (255 loc) · 7.36 KB
/
depth-chart.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML5 Canvas绘制比特币交易深度图</title>
<style type="text/css">
.layout{
position:relative;
height: 600px;
}
.layout canvas{
position:absolute;
}
.layout .canvas{
background: #ffffff;
width: 600px;
height: 300px
}
.layout #x{
top:300px;
left:0;
}
.layout #y{
top:0px;
left:600px;
}
.layout #offdepth {
top: 0;
left: 0;
background: inherit;
}
</style>
</head>
<body>
<div class="layout">
<canvas id="depth" class="canvas" width="600" height="300"></canvas>
<canvas id="offdepth" class="canvas" width="600" height="300"></canvas>
<canvas id="x" width="600" height="24"></canvas>
<canvas id="y" width="48" height="300"></canvas>
</div>
<script>
function drawDepthChart() {
var data = {
"sell":
[
{"price":9000,"amount":8176},
{"price":8999.84,"amount":10245},
{"price":8999.68,"amount":19147},
{"price":8999.52,"amount":4150},
{"price":8999.36,"amount":10420},
{"price":8999.2,"amount":16053},
{"price":8999.04,"amount":8480},
{"price":8998.88,"amount":12751},
{"price":8998.72,"amount":14187},
{"price":8998.56,"amount":7916}
],
"buy":[
{"price":8749,"amount":8379},
{"price":8748.74,"amount":18582},
{"price":8748.48,"amount":9173},
{"price":8748.22,"amount":13327},
{"price":8747.96,"amount":1990},
{"price":8747.7,"amount":3414},
{"price":8747.44,"amount":12062},
{"price":8747.18,"amount":19389},
{"price":8746.92,"amount":8999},
{"price":8746.66,"amount":8675}
]
};
for(i in data['buy']){
var total = 0;
for(n=0; n<= i; n++){
total += data['buy'][n]['amount'];
}
data['buy'][i]['total'] = total;
}
for(i in data['sell']){
var total = 0;
for(n=i; n< data['sell'].length; n++){
total += data['sell'][n]['amount'];
}
data['sell'][i]['total'] = total;
}
var canvas=document.getElementById('depth');
var offCanvas = document.getElementById('offdepth')
window.offContext = offCanvas.getContext('2d')
const buyFillColor = '#09C989'
const sellFillColor = '#F24E4D'
offCanvas.addEventListener('mousemove', ({offsetX, offsetY}) => {
for(let key of valueMap.keys()) {
const x = key[0]
const y = key[1]
const side = key[2]
const obj = valueMap.get(key)
if (offsetX < x) {
// console.log(key, valueMap.get(key))
offContext.strokeStyle = 'rgba(200,200,200,0.5)'
offContext.lineWidth = 1
offContext.setLineDash([2])
offContext.clearRect(0,0,600,300);
offContext.beginPath()
offContext.moveTo(0, y)
offContext.lineTo(600, y)
offContext.stroke()
offContext.closePath()
offContext.beginPath()
offContext.moveTo(x, 0)
offContext.lineTo(x, 300)
offContext.stroke()
offContext.closePath()
offContext.beginPath()
offContext.shadowBlur=20;
offContext.shadowColor="rgba(0,0,0,0.8)";
// 小圆点
offContext.fillStyle = 'rgba(255,255,255, 1)'
offContext.arc(x,y,5,0,2*Math.PI);
offContext.fill()
offContext.closePath()
offContext.beginPath()
// tips框
offContext.fillStyle = 'rgba(255,255,255, 1)'
let widthOffset = 120
let heightOffset = 60
let left = x - widthOffset / 2
let top = y - heightOffset - 10
if (side === 'sell') {
// widthOffset = -widthOffset
// heightOffset = -heightOffset
}
if (left < 0) {
left = 0
}
if (left >= 600 - widthOffset) {
left = 600 - widthOffset
}
if (top <= 0) {
top = y + 10
}
offContext.fillRect(left, top, widthOffset, heightOffset);
offContext.fillStyle = side === 'buy' ? buyFillColor : sellFillColor
offContext.textAlign="left"
offContext.shadowBlur=0;
offContext.font="12px Arial";
const marginLeft = 10
const marginTop = 25
const lineHeight = 20
offContext.fillText(`委托价:${obj.price}`, left + marginLeft, top + marginTop );
offContext.fillText(`累计:${obj.total}`, left + marginLeft, top + marginTop + lineHeight);
offContext.closePath()
break;
}
}
})
var width = canvas.getAttribute('width');
var height = canvas.getAttribute('height');
//计算交易量的最大值,作为纵坐标的最大刻度值
var maxAmount = Math.max( data['sell'][0]['total'], data['buy'][data['buy'].length-1]['total']);
var scaleH = maxAmount / height;
//买卖各占一半宽度
var scaleW = width / 2 / data['sell'].length;
var context=canvas.getContext('2d');
window.context = context
window.valueMap = new Map
var draw = (context, width, height) => {
let tempList = []
// const width = parseFloat(width) || 0
// const height = parseFloat(height) || 0
const gap = 10
context.beginPath()
context.fillStyle = buyFillColor
let x = 0
let y = 0
let maxAmount = Math.max(data['sell'][0]['total'], data['buy'][data['buy'].length - 1]['total'])
let scaleH = maxAmount / height
let scaleW = width / 2 / data['sell'].length
// context.moveTo(width / 2 - gap, height)
// 从中间向上、向左绘制买单图
for (let i in data['buy']) {
x = width / 2 - i * scaleW - gap
y = height - data['buy'][i]['total'] / maxAmount * height
tempList.push({
x,
y,
value: data['buy'][i],
side: 'buy'
})
// valueMap.set([x, y], data['buy'][i])
context.lineTo(x, y)
}
context.lineTo(0, y) // 延伸到最左侧边缘
context.lineTo(0, height)
context.lineTo(width / 2 - gap, height)
context.fill() // 形成一个封装区域 并按fillStyle指定的颜色填充
context.closePath()
// 同上 开始绘制卖单深度图
context.beginPath()
context.fillStyle = sellFillColor
// debugger
context.moveTo(width / 2 + gap, height)
for (let i in data['buy']) {
var index =data['sell'].length - i -1;
x = width/2 + i * scaleW + gap;
y = height - data['sell'][index]['total']/maxAmount * height;
tempList.push({
x,
y,
value: data['buy'][i],
side: 'sell'
})
context.lineTo(x,y);
}
tempList = tempList.sort((a, b) => {
return a.x - b.x
})
// debugger
tempList.forEach(item => {
valueMap.set([item.x, item.y, item.side], item.value)
})
context.lineTo(width + gap, y)
context.lineTo(width + gap, height)
context.lineTo(width / 2 + gap, height)
context.fill()
// context.stroke()
context.closePath()
// X轴刻度
let contextX = document.getElementById('x').getContext('2d')
let contextY = document.getElementById('y').getContext('2d')
contextX.fillStyle = '#000'
for (let i in data['buy']) {
if (i % 3) continue
x = width / 2 - i * scaleW - 30
y = 12
contextX.fillText(data['buy'][i]['price'], x, y)
}
for (let i in data['sell']) {
if (i % 3) continue
x = width / 2 + i * scaleW
y = 12
let index = data['sell'].length - i - 1
contextX.fillText(data['sell'][index]['price'], x, y)
}
// Y轴5等份
var seg = maxAmount / 5
for (let i = 1; i < 6; i++) {
x = 12
y = height - seg * i / maxAmount * height
contextY.fillText(seg * i > 1000 ? (seg * i / 1000 + 'K') : seg * i, x, y)
}
}
draw(context, width, height)
}
drawDepthChart();
</script>
</body>
</html>