-
Notifications
You must be signed in to change notification settings - Fork 0
/
responsiveScales.html
158 lines (128 loc) · 4.33 KB
/
responsiveScales.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<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;
}
.barea {
fill: mediumSlateBlue;
}
.sarea {
fill: mediumOrchid;
}
</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([1, 1.5]).range([height, 0]);
var sarea = d3.svg.area()
.x(function(d, i) { return x(i); })
.y0(function(d) { return y(d.sellLow); })
.y1(function(d) { return y(d.sellHigh); });
var barea = d3.svg.area()
.x(function(d, i) { return x(i); })
.y0(function(d) { return y(d.buyLow); })
.y1(function(d) { return y(d.buyHigh); });
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(1) + ")")
.call(d3.svg.axis().scale(x).orient("bottom"));
svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis().scale(y).orient("left"));
var spath = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.datum(data)
.attr("class", "sarea")
.attr("d", sarea);
var bpath = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.datum(data)
.attr("class", "barea")
.attr("d", barea);
function updateRates(ratesWsMessage) {
var rates = JSON.parse(ratesWsMessage.data);
var fr = rates["EURUSD"]["fxmp"];
var frr = rates["EURUSD"]["fxmp.rnd"];
data.push(createPoint(fr.updated, frr.buy, fr.buy, fr.sell, frr.sell));
spath
.attr("d", sarea)
.attr("transform", null)
.transition()
.duration(1950)
.ease("linear")
.attr("transform", "translate(" + x(-1) + ",0)");
bpath
.attr("d", barea)
.attr("transform", null)
.transition()
.duration(1950)
.ease("linear")
.attr("transform", "translate(" + x(-1) + ",0)");
data.shift();
//document.getElementById('rate').innerText = JSON.stringify(data);
}
function emptyPoint() {
return createPoint("",0,0,0,0);
}
function createPoint(t, bl, bh, sl, sh) {
return {
time: t,
buyLow: bl,
buyHigh: bh,
sellLow: sl,
sellHigh: sh
}
}
</script>
</body>
</html>