-
Notifications
You must be signed in to change notification settings - Fork 4
/
showChart.js
72 lines (71 loc) · 2.13 KB
/
showChart.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
/**
* Arguments:
* canvas: element of canvas
* labels: [January, feburary, ....] or [1,2,3,4]
* stockData: [1,5,2,1,0,4,2,23]
* buyDecision: The x positions of buying [January, feburary, 20, December]
* sellDecision: Same thing above but for buying
*
* Note: stockData.length MUST equal labels.length
*/
function generateChart(canvas, labels, stockData, buyDecision, sellDecision){
function gen(decisions){
var data = [];
for(var eachLabel of decisions){
data.push({
x: eachLabel,
y: stockData[labels.indexOf(eachLabel)],
r: 5
});
}
return data;
}
if(window.chartInstance){
window.chartInstance.destroy();
}
window.chartInstance = new Chart(canvas.getContext('2d'), {
type: 'line',
data: {
labels,
datasets: [{
label: 'Stock data',
borderColor: 'rgb(0,0,0)',
data: stockData,
lineTension: 0,
fill: false,
radius: 0,
borderWidth: 1
},{
label: 'Buy decision',
borderColor: 'rgb(255,0,0)',
data: gen(buyDecision),
fill: false,
type: 'bubble',
backgroundColor: 'rgb(255,0,0)',
pointStyle: 'triangle',
rotation: 0
},{
label: 'Sell decision',
borderColor: 'rgb(0,0,255)',
data: gen(sellDecision),
fill: false,
type: 'bubble',
backgroundColor: 'rgb(0,0,255)',
pointStyle: 'triangle',
rotation: 180
}]
},
options: {
title: {
display: true,
text: 'Results'
},
maintainAspectRatio: false
}
});
}
window.onload = function(){
var chartDom = document.getElementById('chart');
// Dummy data for show
window.mainChart = generateChart(chartDom, [1,2,3,4,5], [20, 60, 50, 99, 100], [2,3], [5]);
};