-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
107 lines (82 loc) · 3.05 KB
/
index.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
<!doctype html>
<html>
<head>
<title>WebSocket</title>
<script type="text/javascript" src="d3.js"></script>
<style type="text/css">
body {
font-family: 'Helvetica Neue';
}
.cpu {
width: 640px;
margin: 30px;
background-color: #e5e6db;
border: 1px solid;
}
.cpu rect {
stroke: #fff;
fill: #4d87b7;
}
.cpu text {
fill: #fff;
}
</style>
</head>
<body>
<script type="text/javascript">
window.onload = function() {
/* WebSocket stuff */
var socket = new WebSocket('ws://localhost:9000');
socket.onopen = function() {
console.log('Connected to ' + socket.url);
}
socket.onclose = function() {
console.log('Disconnected from ' + socket.url);
}
socket.onerror = function(e) {
console.log('Ooops... ' + e);
}
socket.onmessage = function(e) {
redraw(JSON.parse(e.data));
}
/* D3 magic */
var bar_height = 40;
/* create a heightless svg, we still don't know how
* many cpu bars to draw */
var chart = d3.select('body').append('svg')
.attr('class', 'cpu')
.attr('height', 0);
var x = d3.scale.linear()
.domain([0, 100])
.range([0, parseInt(chart.style('width'))]);
var redraw = function(data) {
/* adapt svg height to the number of cpus in the
* received message */
chart.transition()
.duration(500)
.attr('height', data.length * bar_height);
var rect = chart.selectAll('rect').data(data);
rect.enter().append('rect')
.attr('y', function(d, i) { return i * bar_height })
.attr('width', x)
.attr('height', bar_height);
rect.transition()
.duration(1000)
.attr('width', x);
var text = chart.selectAll('text').data(data);
text.enter().append('text')
.attr('x', x)
.attr('y', function(d, i) { return i * bar_height })
.attr('dx', -6)
.attr('dy', '1.6em')
.attr('text-anchor', 'end')
.text(String);
text.transition()
.duration(1000)
.attr('x', x)
.text(String);
}
}
</script>
</body>
</html>