-
Notifications
You must be signed in to change notification settings - Fork 0
/
CandlestickChart.js
2207 lines (2124 loc) · 51.7 KB
/
CandlestickChart.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function Candlestick(timestamp, open, close, high, low) {
this.timestamp = parseInt(timestamp)
this.open = parseFloat(open)
this.close = parseFloat(close)
this.high = parseFloat(high)
this.low = parseFloat(low)
}
function CandlestickChart(canvasElementID) {
this.canvas = document.getElementById(canvasElementID)
this.context = this.canvas.getContext("2d")
this.width = parseInt(this.canvas.width)
this.height = parseInt(this.canvas.height)
this.adjustHidpi(this.canvas, this.context)
this.canvas.addEventListener("mousemove", (e) => {
this.mouseMove(e)
})
this.canvas.addEventListener("mouseout", (e) => {
this.mouseOut(e)
})
this.canvas.addEventListener("mousedown", (e) => {
this.mouseDown(e)
})
this.canvas.addEventListener("mouseup", (e) => {
if (this.mousedown) {
this.mouseUp(e)
}
})
this.canvas.style.backgroundColor = "#2f2e43"
this.context.font = "12px sans-serif"
this.gridColor = "#444444"
this.gridTextColor = "#aaaaaa"
this.mouseHoverBackgroundColor = "#eeeeee"
this.mouseHoverTextColor = "#000000"
this.greenColor = "#50ea8e"
this.redColor = "#fc5434"
this.greenHoverColor = "#01c454"
this.redHoverColor = "#fd2906"
this.context.lineWidth = 1
this.candleWidth = 5
this.marginLeft = 30
this.marginRight = 100
this.marginTop = 30
this.marginBottom = 50
this.yValueStart = 0
this.yValueEnd = 0
this.yValueRange = 0
this.yPixelRange = this.height - this.marginTop - this.marginBottom
this.xValueStart = 0
this.xValueEnd = 0
this.xValueRange = 0
this.xPixelRange = this.width - this.marginLeft - this.marginRight
this.cachedCandlesticks = []
this.candlesticks = []
// 预设值,实际会重新计算
this.xGridCells = 16
this.yGridCells = 16
// 鼠标是否在蜡烛图上
this.drawMouseOverlay = false
// 鼠标按下的起始位置
this.mouseDownPosition = { x: 0, y: 0 }
this.mousedown = false
// candlesticks 数组的起始下标
this.startIndex = 50
this.counts = 0
// 鼠标的像素值
this.mousePosition = { x: 0, y: 0 }
// 鼠标对应的某个蜡烛的时间值
this.xMouseHover = 0
// 鼠标对应的某个蜡烛的价格值
this.yMouseHover = 0
this.hoveredCandlestickID = 0
}
CandlestickChart.prototype.adjustHidpi = function (canvas, context) {
const dpr = window.devicePixelRatio || 1
const bsr =
context["webkitBackingStorePixelRatio"] ||
context["mozBackingStorePixelRatio"] ||
context["msBackingStorePixelRatio"] ||
context["oBackingStorePixelRatio"] ||
context["backingStorePixelRatio"] ||
1
// 获取画布缩放的比例
const ratio = dpr / bsr
// 通过css宽高让画布回到原始大小
canvas.style.width = canvas.width + "px"
canvas.style.height = canvas.height + "px"
// 缩放画布
canvas.width = canvas.width * ratio
canvas.height = canvas.height * ratio
// 缩放绘制到画布上的元素的大小。比如100px * 100px 的矩形绘制到画布上是200px * 200px
context.scale(ratio, ratio)
}
CandlestickChart.prototype.addCandlestick = function (candlestick) {
this.cachedCandlesticks.push(candlestick)
}
CandlestickChart.prototype.setCandlesticks = function (
start = this.startIndex,
len = 50
) {
if (start < 0 || start + len > this.cachedCandlesticks.length) {
return
}
this.candlesticks = this.cachedCandlesticks.slice(start, start + len)
}
CandlestickChart.prototype.mouseDown = function (e) {
this.mousedown = true
this.mouseDownPosition = this.getMousePos(e)
}
CandlestickChart.prototype.getMousePos = function (e) {
const rect = this.canvas.getBoundingClientRect()
return { x: e.clientX - rect.left, y: e.clientY - rect.top }
}
CandlestickChart.prototype.mouseUp = function (e) {
this.mousedown = false
if (this.mousePosition.x - this.mouseDownPosition.x > 0) {
this.startIndex = this.startIndex - this.counts
if (this.startIndex < 50) {
getData(this.cachedCandlesticks[0].timestamp - 86400000).then((res) => {
this.cachedCandlesticks = res.concat(this.cachedCandlesticks)
this.startIndex = this.startIndex + 100
})
}
} else {
this.startIndex = this.startIndex + this.counts
}
}
CandlestickChart.prototype.mouseMove = function (e) {
if (!this.mousedown) {
this.drawAuxiliaryLine(e)
} else {
this.drawMouseOverlay = false
this.updateCandlesticks(e)
}
}
CandlestickChart.prototype.updateCandlesticks = function (e) {
this.mousePosition = this.getMousePos(e)
// 判断鼠标是否越界
if (
this.mousePosition.x < this.marginLeft ||
this.mousePosition.x > this.width - this.marginRight + this.candleWidth ||
this.mousePosition.y > this.height - this.marginBottom ||
this.mousePosition.y < this.marginTop
) {
this.mouseUp(e)
return
}
let mouseMoveLength = this.mousePosition.x - this.mouseDownPosition.x
if (mouseMoveLength > 0) {
if (mouseMoveLength >= this.candleWidth) {
this.counts = Math.floor(mouseMoveLength / this.candleWidth)
this.setCandlesticks(this.startIndex - this.counts, 50)
this.draw()
}
} else {
mouseMoveLength = Math.abs(mouseMoveLength)
if (mouseMoveLength >= this.candleWidth) {
this.counts = Math.floor(mouseMoveLength / this.candleWidth)
this.setCandlesticks(this.startIndex + this.counts, 50)
this.draw()
}
}
}
CandlestickChart.prototype.drawAuxiliaryLine = function (e) {
// 获取当前鼠标在canvas中的位置
const getMousePos = (e) => {
const rect = this.canvas.getBoundingClientRect()
return { x: e.clientX - rect.left, y: e.clientY - rect.top }
}
this.mousePosition = getMousePos(e)
// 由于candle是在timestamp两边画的,想要完成坐标转换,pixel <-> value,需要将当前鼠标位置 + this.candleWidth / 2 才是当前 pixel 所对应的candle
// 也就是说 坐标转换,pixel <-> value 的标准是建立在 pixel:0 <-> candle left edge: 0 的规则上。
this.mousePosition.x += this.candleWidth / 2
this.drawMouseOverlay = true
// 判断鼠标是否越界
if (this.mousePosition.x < this.marginLeft) this.drawMouseOverlay = false
if (this.mousePosition.x > this.width - this.marginRight + this.candleWidth)
this.drawMouseOverlay = false
if (this.mousePosition.y > this.height - this.marginBottom)
this.drawMouseOverlay = false
if (this.mousePosition.y < this.marginTop) this.drawMouseOverlay = false
// 获取当前鼠标对应的蜡烛的值坐标
if (this.drawMouseOverlay) {
this.yMouseHover = this.yPixelToValueCoord(this.mousePosition.y)
this.xMouseHover = this.xPixelToValueCoord(this.mousePosition.x)
const candlestickDelta =
this.candlesticks[1].timestamp - this.candlesticks[0].timestamp
this.hoveredCandlestickID = Math.floor(
(this.xMouseHover - this.candlesticks[0].timestamp) / candlestickDelta
)
this.xMouseHover =
Math.floor(this.xMouseHover / candlestickDelta) * candlestickDelta
this.mousePosition.x = this.xValueToPixelCoord(this.xMouseHover)
this.draw()
} else {
this.draw()
}
}
CandlestickChart.prototype.mouseOut = function (e) {
this.drawMouseOverlay = false
this.draw()
}
CandlestickChart.prototype.drawCurveLine = function () {
function getControlPoints(pt0, pt1, pt2, t) {
const x0 = pt0[0]
const y0 = pt0[1]
const x1 = pt1[0]
const y1 = pt1[1]
const x2 = pt2[0]
const y2 = pt2[1]
const d01 = Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2))
const d12 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
const fa = (t * d01) / (d01 + d12)
const fb = t - fa
const p1x = x1 + fa * (x0 - x2)
const p1y = y1 + fa * (y0 - y2)
const p2x = x1 - fb * (x0 - x2)
const p2y = y1 - fb * (y0 - y2)
return [
[p1x, p1y],
[p2x, p2y],
]
}
let cps = []
let pts = []
for (let i = 0; i < this.candlesticks.length; i++) {
pts.push([
this.xValueToPixelCoord(this.candlesticks[i].timestamp),
this.yValueToPixelCoord(this.candlesticks[i].close),
])
}
for (let i = 1; i < this.candlesticks.length - 1; i++) {
cps.push(...getControlPoints(pts[i - 1], pts[i], pts[i + 1], 0.8))
}
// 连接第一个和最后一个曲线
this.context.strokeStyle = "white"
this.context.lineWidth = 3
this.context.beginPath()
this.context.moveTo(pts[0][0], pts[0][1])
this.context.quadraticCurveTo(cps[0][0], cps[0][1], pts[1][0], pts[1][1])
this.context.stroke()
this.context.closePath()
this.context.beginPath()
this.context.moveTo(pts[pts.length - 2][0], pts[pts.length - 2][1])
this.context.quadraticCurveTo(
cps[cps.length - 1][0],
cps[cps.length - 1][1],
pts[pts.length - 1][0],
pts[pts.length - 1][1]
)
this.context.stroke()
this.context.closePath()
for (let i = 1; i < this.candlesticks.length - 2; i++) {
this.context.beginPath()
this.context.moveTo(pts[i][0], pts[i][1])
this.context.bezierCurveTo(
cps[i * 2 - 1][0],
cps[i * 2 - 1][1],
cps[i * 2][0],
cps[i * 2][1],
pts[i + 1][0],
pts[i + 1][1]
)
this.context.stroke()
this.context.closePath()
}
this.context.lineWidth = 1
}
CandlestickChart.prototype.draw = function () {
// 每次鼠标移动事件都需要重新画图
this.context.clearRect(0, 0, this.width, this.height)
// 计算值坐标
this.calculateXValueRange()
this.calculateYValueRange()
// 画坐标系
this.drawGrid()
this.candleWidth = this.xPixelRange / this.candlesticks.length
this.candleWidth--
if (this.candleWidth % 2 == 0) this.candleWidth--
for (let i = 0; i < this.candlesticks.length; i++) {
const candlestick = this.candlesticks[i]
let color =
candlestick.close > candlestick.open ? this.redColor : this.greenColor
if (i == this.hoveredCandlestickID) {
if (color === this.greenColor) color = this.greenHoverColor
else if (color === this.redColor) color = this.redHoverColor
}
// 画灯芯
this.drawLine(
this.xValueToPixelCoord(candlestick.timestamp),
this.yValueToPixelCoord(candlestick.low),
this.xValueToPixelCoord(candlestick.timestamp),
this.yValueToPixelCoord(candlestick.high),
color
)
// 画蜡烛
this.fillRect(
this.xValueToPixelCoord(candlestick.timestamp) -
Math.floor(this.candleWidth / 2),
this.yValueToPixelCoord(candlestick.open),
this.candleWidth,
this.yValueToPixelCoord(candlestick.close) -
this.yValueToPixelCoord(candlestick.open),
color
)
}
// 画曲线
this.drawCurveLine()
// 如果鼠标在某个蜡烛上,需要画出辅助线
if (this.drawMouseOverlay) {
// 画价格线
this.context.setLineDash([5, 5])
this.drawLine(
0,
this.mousePosition.y,
this.width,
this.mousePosition.y,
this.mouseHoverBackgroundColor
)
this.context.setLineDash([])
let str = this.roundPriceValue(this.yMouseHover)
let textWidth = this.context.measureText(str).width
this.fillRect(
this.width - 65,
this.mousePosition.y - 10,
65,
20,
this.mouseHoverBackgroundColor
)
this.context.fillStyle = this.mouseHoverTextColor
this.context.fillText(
str,
this.width - textWidth - 5,
this.mousePosition.y + 5
)
// 时间线
this.context.setLineDash([5, 5])
this.drawLine(
this.mousePosition.x,
0,
this.mousePosition.x,
this.height,
this.mouseHoverBackgroundColor
)
this.context.setLineDash([])
str = this.formatDate(new Date(this.xMouseHover))
textWidth = this.context.measureText(str).width
this.fillRect(
this.mousePosition.x - textWidth / 2 - 5,
this.height - 20,
textWidth + 10,
20,
this.mouseHoverBackgroundColor
)
this.context.fillStyle = this.mouseHoverTextColor
this.context.fillText(
str,
this.mousePosition.x - textWidth / 2,
this.height - 5
)
// 数据
let yPos = this.mousePosition.y - 95
if (yPos < 0) yPos = this.mousePosition.y + 15
const color =
this.candlesticks[this.hoveredCandlestickID].close >
this.candlesticks[this.hoveredCandlestickID].open
? this.redHoverColor
: this.greenHoverColor
this.fillRect(this.mousePosition.x + 15, yPos - 5, 120, 90, color)
this.fillRect(
this.mousePosition.x + 20,
yPos,
110,
80,
this.mouseHoverBackgroundColor
)
this.context.fillStyle = this.mouseHoverTextColor
this.context.fillText(
"开盘价: " + this.candlesticks[this.hoveredCandlestickID].open,
this.mousePosition.x + 30,
yPos + 15
)
this.context.fillText(
"收盘价: " + this.candlesticks[this.hoveredCandlestickID].close,
this.mousePosition.x + 30,
yPos + 35
)
this.context.fillText(
"最高价: " + this.candlesticks[this.hoveredCandlestickID].high,
this.mousePosition.x + 30,
yPos + 55
)
this.context.fillText(
"最低价: " + this.candlesticks[this.hoveredCandlestickID].low,
this.mousePosition.x + 30,
yPos + 75
)
}
}
CandlestickChart.prototype.animation = function () {
// 每次鼠标移动事件都需要重新画图
this.context.clearRect(0, 0, this.width, this.height)
// 计算值坐标
this.calculateXValueRange()
this.calculateYValueRange()
// 画坐标系
this.drawGrid()
this.candleWidth = this.xPixelRange / this.candlesticks.length
this.candleWidth--
if (this.candleWidth % 2 == 0) this.candleWidth--
// 曲线的准备工作,生成 pts 和 cps
function getControlPoints(pt0, pt1, pt2, t) {
const x0 = pt0[0]
const y0 = pt0[1]
const x1 = pt1[0]
const y1 = pt1[1]
const x2 = pt2[0]
const y2 = pt2[1]
const d01 = Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2))
const d12 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
const fa = (t * d01) / (d01 + d12)
const fb = t - fa
const p1x = x1 + fa * (x0 - x2)
const p1y = y1 + fa * (y0 - y2)
const p2x = x1 - fb * (x0 - x2)
const p2y = y1 - fb * (y0 - y2)
return [
[p1x, p1y],
[p2x, p2y],
]
}
let cps = []
let pts = []
for (let i = 0; i < this.candlesticks.length; i++) {
pts.push([
this.xValueToPixelCoord(this.candlesticks[i].timestamp),
this.yValueToPixelCoord(this.candlesticks[i].close),
])
}
for (let i = 1; i < this.candlesticks.length - 1; i++) {
cps.push(...getControlPoints(pts[i - 1], pts[i], pts[i + 1], 0.5))
}
// 画曲线
function drawCurveLine(now) {
if (i - 50 === 0) {
// 连接第一个曲线
this.context.strokeStyle = "white"
this.context.lineWidth = 3
this.context.beginPath()
this.context.moveTo(pts[0][0], pts[0][1])
this.context.quadraticCurveTo(cps[0][0], cps[0][1], pts[1][0], pts[1][1])
this.context.stroke()
this.context.closePath()
}
if (i - 50 === 48) {
this.context.beginPath()
this.context.moveTo(pts[pts.length - 2][0], pts[pts.length - 2][1])
this.context.quadraticCurveTo(
cps[cps.length - 1][0],
cps[cps.length - 1][1],
pts[pts.length - 1][0],
pts[pts.length - 1][1]
)
this.context.stroke()
this.context.closePath()
this.context.lineWidth = 1
}
if (i - 50 > 0 && i - 50 < 48) {
const j = i - 50
this.context.beginPath()
this.context.moveTo(pts[j][0], pts[j][1])
this.context.bezierCurveTo(
cps[j * 2 - 1][0],
cps[j * 2 - 1][1],
cps[j * 2][0],
cps[j * 2][1],
pts[j + 1][0],
pts[j + 1][1]
)
this.context.stroke()
this.context.closePath()
}
i++
if (i >= 50 && i < 99) {
window.requestAnimationFrame(drawCurveLineThis)
}
}
let i = 0
let last = 0
function drawCandle(now) {
if (!last || now - last >= 0) {
last = now
const candlestick = this.candlesticks[i++]
let color =
candlestick.close > candlestick.open ? this.redColor : this.greenColor
// 画灯芯
this.drawLine(
this.xValueToPixelCoord(candlestick.timestamp),
this.yValueToPixelCoord(candlestick.low),
this.xValueToPixelCoord(candlestick.timestamp),
this.yValueToPixelCoord(candlestick.high),
color
)
// 画蜡烛
this.fillRect(
this.xValueToPixelCoord(candlestick.timestamp) -
Math.floor(this.candleWidth / 2),
this.yValueToPixelCoord(candlestick.open),
this.candleWidth,
this.yValueToPixelCoord(candlestick.close) -
this.yValueToPixelCoord(candlestick.open),
color
)
}
if (i < this.candlesticks.length) {
window.requestAnimationFrame(drawCandleThis)
}
if (i >= 50 && i < 98) {
window.requestAnimationFrame(drawCurveLineThis)
}
}
// drawCandle drawCurveLine 里都用到了 CandlestickChart 实例,所以要绑定this
let drawCandleThis = drawCandle.bind(this)
let drawCurveLineThis = drawCurveLine.bind(this)
window.requestAnimationFrame(drawCandleThis)
}
CandlestickChart.prototype.drawGrid = function () {
const yinterval = (this.height - this.marginTop - this.marginBottom) / 6
for (let i = 0; i <= 6; i++) {
const yPiexl = yinterval * i + this.marginTop
const yValue = this.yPixelToValueCoord(yPiexl)
this.drawLine(0, yPiexl, this.width, yPiexl, this.gridColor)
const textWidth = this.context.measureText(
this.roundPriceValue(yValue)
).width
this.context.fillStyle = this.gridTextColor
this.context.fillText(
this.roundPriceValue(yValue),
this.width - textWidth - 5,
this.yValueToPixelCoord(yValue) - 5
)
}
const xValueInterval = this.candlesticks.length / 10
for (let i = 4; i <= this.candlesticks.length; i += xValueInterval) {
const xValue = this.candlesticks[i].timestamp
const xPiexl = this.xValueToPixelCoord(xValue)
this.drawLine(xPiexl, 0, xPiexl, this.height, this.gridColor)
const date = new Date(xValue)
let dateStr = ""
// if (formatAsDate) {
let day = date.getDate()
if (day < 10) day = "0" + day
let month = date.getMonth() + 1
if (month < 10) month = "0" + month
dateStr = day + "." + month
// } else {
// let minutes = date.getMinutes()
// if (minutes < 10) minutes = "0" + minutes
// dateStr = date.getHours() + ":" + minutes
// }
this.context.fillStyle = this.gridTextColor
this.context.fillText(dateStr, xPiexl + 5, this.height - 5)
}
}
CandlestickChart.prototype.calculateYValueRange = function () {
for (let i = 0; i < this.candlesticks.length; i++) {
if (i == 0) {
this.yValueStart = this.candlesticks[i].low
this.yValueEnd = this.candlesticks[i].high
} else {
if (this.candlesticks[i].low < this.yValueStart) {
this.yValueStart = this.candlesticks[i].low
}
if (this.candlesticks[i].high > this.yValueEnd) {
this.yValueEnd = this.candlesticks[i].high
}
}
}
this.yValueRange = this.yValueEnd - this.yValueStart
}
CandlestickChart.prototype.calculateXValueRange = function () {
this.xValueStart = this.candlesticks[0].timestamp
this.xValueEnd = this.candlesticks[this.candlesticks.length - 1].timestamp
this.xValueRange = this.xValueEnd - this.xValueStart
}
CandlestickChart.prototype.drawLine = function (
xStart,
yStart,
xEnd,
yEnd,
color
) {
this.context.beginPath()
// canvas 绘制1px线条时,会在线两边绘制0.5px的区域。偏移0.5,使线条绘制的更清晰
this.context.moveTo(xStart + 0.5, yStart + 0.5)
this.context.lineTo(xEnd + 0.5, yEnd + 0.5)
this.context.strokeStyle = color
this.context.stroke()
}
CandlestickChart.prototype.fillRect = function (x, y, width, height, color) {
this.context.fillStyle = color
this.roundRect(x, y, width, height, 5)
this.context.fill()
}
CandlestickChart.prototype.drawRect = function (x, y, width, height, color) {
this.context.strokeStyle = color
this.roundRect(x, y, width, height, 5)
this.context.stroke()
}
CandlestickChart.prototype.roundRect = function (x, y, width, height, r) {
if (Math.abs(height) < 2 * r) {
r = Math.abs(height) / 2
}
this.context.beginPath()
this.context.moveTo(x + r, y)
this.context.arcTo(x + width, y, x + width, y + height, r)
this.context.arcTo(x + width, y + height, x, y + height, r)
this.context.arcTo(x, y + height, x, y, r)
this.context.arcTo(x, y, x + width, y, r)
this.context.closePath()
}
CandlestickChart.prototype.yValueToPixelCoord = function (yValue) {
return (
this.height -
this.marginBottom -
((yValue - this.yValueStart) * this.yPixelRange) / this.yValueRange
)
}
CandlestickChart.prototype.xValueToPixelCoord = function (xValue) {
return (
this.marginLeft +
((xValue - this.xValueStart) * this.xPixelRange) / this.xValueRange
)
}
CandlestickChart.prototype.yPixelToValueCoord = function (yPiexl) {
return (
this.yValueStart +
((this.height - this.marginBottom - yPiexl) * this.yValueRange) /
this.yPixelRange
)
}
CandlestickChart.prototype.xPixelToValueCoord = function (xPiexl) {
return (
this.xValueStart +
((xPiexl - this.marginLeft) * this.xValueRange) / this.xPixelRange
)
}
CandlestickChart.prototype.formatDate = function (date) {
var day = date.getDate()
if (day < 10) day = "0" + day
var month = date.getMonth() + 1
if (month < 10) month = "0" + month
var hours = date.getHours()
if (hours < 10) hours = "0" + hours
var minutes = date.getMinutes()
if (minutes < 10) minutes = "0" + minutes
return (
day + "." + month + "." + date.getFullYear() + " - " + hours + ":" + minutes
)
}
CandlestickChart.prototype.roundPriceValue = function (value) {
if (value > 1.0) return Math.round(value * 100) / 100
if (value > 0.001) return Math.round(value * 1000) / 1000
if (value > 0.00001) return Math.round(value * 100000) / 100000
if (value > 0.0000001) return Math.round(value * 10000000) / 10000000
else return Math.round(value * 1000000000) / 1000000000
}
function getData(endTime) {
return new Promise((resolve, reject) => {
var xmlhttp = new XMLHttpRequest()
xmlhttp.open(
"GET",
`https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1d&limit=100${
endTime ? "&endTime=" + endTime : ""
}`
)
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var json = JSON.parse(xmlhttp.responseText)
const candles = []
for (var i = 0; i < json.length; ++i) {
candles.push(
new Candlestick(
json[i][0],
json[i][1],
json[i][4],
json[i][2],
json[i][3]
)
)
}
resolve(candles)
}
}
xmlhttp.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
)
xmlhttp.send()
})
}
getData().then((res) => {
const candlestickChart = new CandlestickChart("CandlestickChart")
candlestickChart.cachedCandlesticks = res
candlestickChart.setCandlesticks()
candlestickChart.animation()
})
// const data = [
// [
// 1621382400000,
// "35212.04000000",
// "35939.48000000",
// "23741.41000000",
// "30279.50000000",
// "21674.74565200",
// 1621468799999,
// "674674617.52058151",
// 643176,
// "10645.62653900",
// "331375889.93289458",
// "0",
// ],
// [
// 1621468800000,
// "30319.47000000",
// "35204.63000000",
// "28554.00000000",
// "33359.97000000",
// "13575.74232300",
// 1621555199999,
// "444890787.82981721",
// 540232,
// "6927.14724900",
// "227171978.36678919",
// "0",
// ],
// [
// 1621555200000,
// "33349.96000000",
// "34648.69000000",
// "27697.77000000",
// "30747.21000000",
// "14214.71482700",
// 1621641599999,
// "447729031.88006524",
// 496984,
// "7125.97928800",
// "225112106.09370834",
// "0",
// ],
// [
// 1621641600000,
// "30739.55000000",
// "32046.21000000",
// "29092.28000000",
// "30895.05000000",
// "7428.34857300",
// 1621727999999,
// "229114066.45295256",
// 337564,
// "3708.10559300",
// "114402615.54006333",
// "0",
// ],
// [
// 1621728000000,
// "30894.96000000",
// "31537.24000000",
// "25641.05000000",
// "28573.19000000",
// "12288.74476800",
// 1621814399999,
// "346439078.55764064",
// 434062,
// "5976.72691000",
// "168521983.38021896",
// "0",
// ],
// [
// 1621814400000,
// "28579.78000000",
// "32850.00000000",
// "28347.58000000",
// "31918.73000000",
// "9791.42134200",
// 1621900799999,
// "301373607.22563220",
// 317628,
// "5101.13006900",
// "157008775.48303874",
// "0",
// ],
// [
// 1621900800000,
// "31918.74000000",
// "32700.00000000",
// "29800.00000000",
// "31367.74000000",
// "7433.61187800",
// 1621987199999,
// "230798837.31650623",
// 237067,
// "3926.18625900",
// "121944846.87763443",
// "0",
// ],
// [
// 1621987200000,
// "31367.74000000",
// "33421.61000000",
// "30909.21000000",
// "32280.58000000",
// "6530.81341800",
// 1622073599999,
// "210412614.97246485",
// 219855,
// "3581.93348400",
// "115517558.83943124",
// "0",
// ],
// [
// 1622073600000,
// "32276.13000000",
// "33170.15000000",
// "30616.19000000",
// "31614.95000000",
// "4671.36857700",
// 1622159999999,
// "148971232.21947726",
// 180558,
// "2364.61765200",
// "75472302.59290189",
// "0",
// ],
// [
// 1622160000000,
// "31620.66000000",
// "31900.48000000",
// "28533.77000000",
// "29349.23000000",
// "8752.33477300",
// 1622246399999,
// "261459301.63770837",
// 247402,
// "4415.07783200",
// "131935425.55298808",
// "0",
// ],
// [
// 1622246400000,
// "29348.90000000",
// "30677.47000000",
// "27733.33000000",
// "28510.82000000",
// "5934.73305600",
// 1622332799999,
// "171317195.66081325",
// 183520,
// "3010.15895100",
// "86895856.97528562",
// "0",
// ],
// [
// 1622332800000,
// "28522.92000000",
// "30049.95000000",
// "27538.83000000",
// "29331.47000000",
// "3401.50623200",
// 1622419199999,
// "99249292.44493610",
// 121814,
// "1722.24451400",
// "50278170.66978434",
// "0",
// ],
// [
// 1622419200000,
// "29330.33000000",
// "30688.38000000",
// "28127.30000000",
// "30460.92000000",
// "5215.50761800",
// 1622505599999,
// "154871499.19037037",
// 213798,
// "2705.38307500",
// "80323604.70802540",
// "0",
// ],
// [
// 1622505600000,
// "30466.19000000",
// "30984.02000000",
// "29242.00000000",
// "30043.16000000",
// "4506.82638000",
// 1622591999999,
// "134735266.52168772",
// 208041,
// "2350.07749900",
// "70253865.66712972",
// "0",
// ],
// [
// 1622592000000,
// "30043.14000000",
// "31334.15000000",
// "29400.13000000",
// "30792.11000000",
// "3564.74777100",
// 1622678399999,
// "109079124.65033058",
// 174067,
// "1841.64306000",
// "56369987.01139805",
// "0",
// ],