-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle.html
127 lines (102 loc) · 3.49 KB
/
single.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
<html>
<head>
<title>Hackathon Streaming RBS FX Rates</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="http://js.pusher.com/2.2/pusher.min.js" type="text/javascript"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<style>
svg {
font: 10px sans-serif;
}
.line {
fill: none;
stroke: #0F0;
stroke-width: 1.5px;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="/">
<span class="glyphicon glyphicon-align-right"></span> FXMP Stream
</a>
</div>
</div>
</nav>
<pre id='rate' style="font-size:10px">You will see data in less than 10s...</pre>
<script type="text/javascript">
var pusher = new Pusher('ec68fe8cadf14e52b659');
var channel = pusher.subscribe('fxRateStream');
const points = 41;
var data = Array.apply(null, Array(points)).map(emptyPoint);
var margin = {top: 20, right: 20, bottom: 20, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear().domain([0, points - 1]).range([0, width]);
var y = d3.scale.linear().domain([0, 2]).range([height, 0]);
var line = d3.svg.line()
.x(function(d, i) { return x(i); })
.y(function(d, i) { return y(d.buy); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + y(0) + ")")
.call(d3.svg.axis().scale(x).orient("bottom"));
svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis().scale(y).orient("left"));
var path = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
channel.bind('fxEvent', function(rates) {
var time = rates.expiryDateTime;
var gbpEur = rates.fx["GBP/EUR"];
var buy = gbpEur.buy;
var sell = gbpEur.sell;
data.push(createPoint(time, buy, sell));
path
.attr("d", line)
.attr("transform", null)
.transition()
.duration(500)
.ease("linear")
.attr("transform", "translate(" + x(-1) + ",0)");
data.shift();
document.getElementById('rate').innerText = JSON.stringify(data);
});
function emptyPoint() {
return createPoint("",0,0);
}
function createPoint(t, b, s) {
return {
time: t,
buy: b,
sell: s
}
}
</script>
</body>
</html>