-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (147 loc) · 4 KB
/
index.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
const socket = new WebSocket('ws://localhost:8088/user');
socket.onopen = () => {
console.log('WebSocket connection established.');
};
socket.onerror = (error) => {
console.error('WebSocket error:', error);
};
socket.onclose = () => {
console.log('WebSocket connection closed.');
};
var barCount = 60;
var initialDateStr = '01 Apr 2017 00:00 Z';
var ctx = document.getElementById('chart').getContext('2d');
ctx.canvas.width = 1000;
ctx.canvas.height = 250;
var barData = getRandomData(initialDateStr, barCount);
function lineData() { return barData.map(d => { return { x: d.x, y: d.c} }) };
var chart = new Chart(ctx, {
type: 'candlestick',
data: {
datasets: [{
label: 'CHRT - Chart.js Corporation',
data: barData
}]
}
});
var getRandomInt = function(max) {
return Math.floor(Math.random() * Math.floor(max));
};
function randomNumber(min, max) {
return Math.random() * (max - min) + min;
}
function randomBar(date, lastClose, data) {
//var open = +randomNumber(lastClose * 0.95, lastClose * 1.05).toFixed(2);
//var close = +randomNumber(open * 0.95, open * 1.05).toFixed(2);
//var high = +randomNumber(Math.max(open, close), Math.max(open, close) * 1.1).toFixed(2);
//var low = +randomNumber(Math.min(open, close) * 0.9, Math.min(open, close)).toFixed(2);
var open = 0;
var close = 0;
var high = 0;
var low = 0
return {
x: date.valueOf(),
o: open,
h: high,
l: low,
c: close
};
}
function getRandomData(dateStr, count) {
var date = luxon.DateTime.fromRFC2822(dateStr);
var data = [randomBar(date, 30)];
while (data.length < count) {
date = date.plus({days: 1});
if (date.weekday <= 5) {
data.push(randomBar(date, data[data.length - 1].c));
}
}
return data;
}
var update = function() {
var dataset = chart.config.data.datasets[0];
//console.log(dataset);
// candlestick vs ohlc
var type = document.getElementById('type').value;
dataset.type = type;
console.log(type);
// linear vs log
var scaleType = document.getElementById('scale-type').value;
chart.config.options.scales.y.type = scaleType;
console.log(scaleType);
// color
var colorScheme = document.getElementById('color-scheme').value;
if (colorScheme === 'neon') {
dataset.color = {
up: '#01ff01',
down: '#fe0000',
unchanged: '#999',
};
} else {
delete dataset.color;
}
// border
var border = document.getElementById('border').value;
var defaultOpts = Chart.defaults.elements[type];
if (border === 'true') {
dataset.borderColor = defaultOpts.borderColor;
} else {
dataset.borderColor = {
up: defaultOpts.color.up,
down: defaultOpts.color.down,
unchanged: defaultOpts.color.up
};
}
// mixed charts
var mixed = document.getElementById('mixed').value;
if(mixed === 'true') {
chart.config.data.datasets = [
{
label: 'CHRT - Chart.js Corporation',
data: barData
},
{
label: 'Close price',
type: 'line',
data: lineData()
}
]
}
else {
chart.config.data.datasets = [
{
label: 'CHRT - Chart.js Corporation',
data: barData
}
]
}
chart.update();
};
//document.getElementById('update').addEventListener('click', update);
//document.getElementById('randomizeData').addEventListener('click', function() {
// barData = getRandomData(initialDateStr, barCount);
// update();
//});
var count = 0;
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
//updateDashboard(data);
console.log(data);
var dataset = chart.config.data.datasets[0];
console.log(dataset);
console.log(dataset.data);
console.log(dataset.data.length);
console.log(count);
console.log(dataset.data[count]);
//console.log(dataset.data[count].o);
//console.log(dataset.data[count].c);
//console.log(dataset.data[count].h);
//console.log(dataset.data[count].l);
console.log(data.o + " " + data.c + " " + data.h + " " + data.l );
dataset.data[count].o = data.o;
dataset.data[count].c = data.c;
dataset.data[count].h = data.h;
dataset.data[count].l = data.l;
count++;
update();
};