-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.htm
More file actions
149 lines (134 loc) · 5.28 KB
/
index.htm
File metadata and controls
149 lines (134 loc) · 5.28 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-url-parser/2.3.1/purl.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.3.13/d3.min.js"></script>
<style>
#chart {
background: #333;
text-align: center;
white-space: nowrap;
overflow: auto;
padding: 10px;
}
#chart div {
box-sizing: border-box;
}
.box {
display: inline-block;
width: 11px;
margin-right: 3px;
position: relative;
vertical-align: bottom;
border: 1px solid #ccc;
border-width: 1px 0;
}
.box:before {
content: '';
position: absolute;
height: 100%;
left: 49%;
border-left: 1px solid #ccc;
}
.range {
position: absolute;
width: 100%;
border: 1px solid #390;
background: #6f0;
}
.range.fall {
border-color: #c00;
background: #f33;
}
</style>
</head>
<body>
<button id="authcode">Manually Login</button>
<button id="refresh" disabled="disabled">Refresh</button>
<input type="text" id="token" />
<button id="barhistory" disabled="disabled">Load chart</button>
<div id="quoteText"></div>
<div id="chart"></div>
<script type="text/javascript">
// enable refresh button
if (sessionStorage.refresh_token) {
$('#refresh').removeAttr('disabled');
$.get('gettoken.php?refresh=' + sessionStorage.refresh_token)
.done(function (data) {
$('#token').val(data.access_token);
$('#barhistory').removeAttr('disabled');
});
} else {
// pass authorization code to php to generate access_token
if ($.url().param("code")) {
$.get('gettoken.php?code=' + $.url().param("code"))
.done(function (data) {
$('#token').val(data.access_token);
if (typeof(Storage) !== "undefined")
sessionStorage.refresh_token = data.refresh_token;
$('#refresh').removeAttr('disabled');
$('#barhistory').removeAttr('disabled');
})
.fail(function () {
// php will redirect to TS login page
location.href = "gettoken.php";
});
}
}
$('#authcode').click(function () {
// php will redirect to TS login page
location.href = "gettoken.php";
});
// pass refresh_token to php to generate access_token
$('#refresh').click(function () {
$.get('gettoken.php?refresh=' + sessionStorage.refresh_token)
.done(function (data) {
$('#token').val(data.access_token);
});
});
$('#barhistory').click(function () {
$.get('gettoken.php?refresh=' + sessionStorage.refresh_token + '&history=MSFT')
.done(function (data) {
$('#chart').empty();
var bars = [];
$.each(data.split("\n"), function (index, value) {
if (value !== "END") {
var bar = $.parseJSON(value);
bar.TimeStamp = new Date(parseInt(bar.TimeStamp.substring(6)));
bars.push(bar);
}
});
(function (dataset) {
// influenced by: http://phrogz.net/js/d3-playground/#StockPrice_HTML
var min = Math.min.apply(null, dataset.map(function (d) { return d.Low; })),
max = Math.max.apply(null, dataset.map(function (d) { return d.High; })),
vscale = (document.querySelector('#chart').offsetHeight + 350);
var vol = dataset.map(function (d) { return d.TotalVolume; }),
volMin = Math.min.apply(Math, vol),
volDiff = Math.max.apply(Math, vol) - volMin;
var boxes = d3.select('#chart').selectAll('div.box').data(dataset);
boxes.enter()
.append('div').attr('class', 'box')
.append('div').attr('class', 'range');
boxes
.sort(function (a, b) { return (a.TimeStamp < b.TimeStamp)
? - 1 : (a.TimeStamp > b.TimeStamp)
? 1 : 0 })
.attr('title', function (d) {
var date = d.TimeStamp.getFullYear() + '-' + (d.TimeStamp.getMonth() + 1) + '-' + d.TimeStamp.getDate();
return date + ' open: ' + d.Open + ', close: ' + d.Close + ' (' + d.Low + '-' + d.High + ')';
})
.style('height', function (d) { return (d.High - d.Low) * vscale + 'px'; })
.style('margin-bottom', function (d) { return (d.Low - min) * vscale + 'px'; })
.select('.range')
.classed('fall', function (d) { return d.Open > d.Close; })
.style('height', function (d) { return Math.abs(d.Open - d.Close) * vscale + 'px'; })
.style('bottom', function (d) { return (Math.min(d.Close, d.Open) - d.Low) * vscale + 'px'; })
.style('opacity', function (d) { return (d.TotalVolume - volMin) / volDiff });
})(bars);
});
});
</script>
</body>
</html>