-
Notifications
You must be signed in to change notification settings - Fork 0
/
areaBuySellDiffLocal.html
136 lines (108 loc) · 3.67 KB
/
areaBuySellDiffLocal.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
128
129
130
131
132
133
134
135
136
<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;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.area {
fill: steelBlue;
}
</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 connection = new WebSocket('ws://localhost:9090');
connection.onopen = function () {
console.log("w/s open")
}
connection.onmessage = updateRates;
connection.onclose = function () {
console.log("w/s close")
}
const points = 41;
var data = Array.apply(null, Array(points)).map(emptyPoint);
var margin = {top: 20, right: 20, bottom: 30, left: 50},
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 area = d3.svg.area()
.x(function(d, i) { return x(i); })
.y0(function(d) { return y(d.buy); })
.y1(function(d) { return y(d.sell); });
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", "area")
.attr("d", area);
function updateRates(ratesWsMessage) {
var rates = JSON.parse(ratesWsMessage.data);
var rate = rates["EURUSD"]["fxmp"];
var buy = rate.buy;
var sell = rate.sell;
var time = rates.updated;
data.push(createPoint(time, buy, sell));
path
.attr("d", area)
.attr("transform", null)
.transition()
.duration(500)
.ease("linear")
.attr("transform", "translate(" + x(-1) + ",0)");
data.shift();
document.getElementById('rate').innerText = JSON.stringify(rates);
}
function emptyPoint() {
return createPoint("",0,0);
}
function createPoint(t, b, s) {
return {
time: t,
buy: b,
sell: s
}
}
</script>
</body>
</html>