/
index.html
156 lines (120 loc) · 3.45 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
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
<!doctype html>
<meta charset='utf-8'>
<title>Arduino Live Chart</title>
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<body>
<script src='http://d3js.org/d3.v3.js'></script>
<script>
var margin = {top: 20, right: 80, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient('bottom');
var yAxis = d3.svg.axis()
.scale(y)
.orient('left');
var line = d3.svg.line()
.interpolate('basis')
.x(function(d) { return x(d.time); })
.y(function(d) { return y(d.value); });
var svg;
function setsvg() {
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 + ')');
}
setsvg();
var json_uri = 'json/datalog';
function drawGraph(error, data) {
if (error) console.log('Error fetching data:', error);
color.domain(d3.keys(data[0]).filter(function(key) { return key !== 'time'; }));
var values = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {time: d.time, value: +d[name]};
})
};
});
x.domain(d3.extent(data, function(d) { return d.time; }));
y.domain([
d3.min(values, function(c) { return d3.min(c.values, function(v) { return v.value; }); }),
d3.max(values, function(c) { return d3.max(c.values, function(v) { return v.value; }); })
]);
svg.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0,' + height + ')')
.call(xAxis);
svg.append('g')
.attr('class', 'y axis')
.call(yAxis)
.append('text')
.attr('transform', 'rotate(-90)')
.attr('y', 6)
.attr('dy', '.71em')
.style('text-anchor', 'end')
.text(' ');
var value = svg.selectAll('.value')
.data(values)
.enter().append('g')
.attr('class', 'value');
value.append('path')
.attr('class', 'line')
.attr('d', function(d) { return line(d.values); })
.style('stroke', function(d) { return color(d.name); });
value.append('text')
.datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
.attr('transform', function(d) { return 'translate(' + x(d.value.time) + ',' + y(d.value.value) + ')'; })
.attr('x', 3)
.attr('dy', '.35em')
.text(function(d) { return d.name; });
}
d3.json(json_uri, drawGraph);
var refresh = 4000;
if (0) window.setInterval(function() {
svg.remove();
setsvg();
d3.json(json_uri, drawGraph);
}, refresh);
if (0) window.setInterval(function() {
d3.json(json_uri, function(error, data) {
var value = svg.selectAll('.value')
.data(data)
.enter().append('g')
.attr('class', 'value');
value.append('path')
.attr('class', 'line')
.attr('d', function(d) { return line(d.values); })
.style('stroke', function(d) { return color(d.name); });
});
}, refresh);
</script>
<p>This example program charts data logged to the PC by a USB-connected Arduino sending streaming JSON.</p>
<p>For documentation see the <a href='https://github.com/billroy/arduino-livechart'>README on GitHub, where you can also find the code.</a></p>
</body><html>