-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectrum-rf.html
224 lines (184 loc) · 6.82 KB
/
spectrum-rf.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<style>
body {
font: 12px Arial;
}
.grid .tick {
stroke: lightgrey;
opacity: 0.7;
}
.grid path {
stroke-width: 0;
}
.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;
}
.svg-container {
display: inline-block;
position: relative;
width: 100%;
padding-bottom: 50%; /* aspect ratio */
vertical-align: top;
overflow: hidden;
}
.svg-content-responsive {
display: inline-block;
position: absolute;
top: 10px;
left: 20px;
bottom: 10px;
}
</style>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<button onclick="updateData()">Reload measurement</button>
Center frequency: <input id="freq" type="number"> Gain: <input id="gain" type="number">
<button onclick="setconfig()">Set config</button>
<div id="chart">
</div>
<script>
function setconfig() {
var config = {};
config.center_freq = document.getElementById("freq").value * 1e6;
config.gain = document.getElementById("gain").value * 1;
d3.json("/cmd", function (error, data) {
console.log(error, data)
updateData();
})
.header("Content-Type", "application/json")
.send("POST", JSON.stringify(config));
}
// Size of chart plotting area
var height = 300;
var width = 670;
// Holds the data received
var dataarray;
// Holds the ranges
var x, y;
// Define the line
var valueline = d3.svg.line()
.x(function (d) { return x(d.x); })
.y(function (d) { return y(d.y); });
function convertJsonToArray(data) {
dataarray = []; // clear existing data
for (var i = 0; i < data["rf-spectrum"].length; i++) {
var obj = {};
obj.x = data["rf-xf"][i] / 1e6;
obj.y = data["rf-spectrum"][i];
dataarray.push(obj);
}
}
// ** Update data section (Called from the onclick)
function updateData() {
// Get the data again
d3.json("/meas?field=rf-spectrum&field=rf-xf&field=gain&field=center_freq", function (error, data) {
convertJsonToArray(data);
// Select the section we want to apply our changes to
var svg = d3.select("div#chart").transition();
// Make the changes
svg.select(".line") // change the line
.duration(200)
.attr("d", valueline(dataarray));
var i = document.getElementById("freq");
i.value = data.center_freq / 1e6;
i = document.getElementById("gain");
i.value = data.gain;
});
}
function updateConfig() {
d3.json("/meas?field=gain&field=center_freq", function (error, data) {
var i = document.getElementById("freq");
i.value = data.center_freq / 1e6;
i = document.getElementById("gain");
i.value = data.gain;
})
}
updateConfig();
d3.json("/meas?field=rf-spectrum&field=rf-xf", function (error, data) {
convertJsonToArray(data);
// Set the ranges
x = d3.scale.linear().domain(d3.extent(dataarray, function (d) { return d.x; })).range([0, width]);
y = d3.scale.linear().domain([-100, 0]).range([height, 0]);
var zoom = d3.behavior.zoom()
.x(x)
.y(y)
.on("zoom", zoomed);
// Adds the svg canvas
svg = d3.select("div#chart")
.append("div")
.classed("svg-container", true) //container class to make it responsive
.append("svg")
//responsive SVG needs these 2 attributes and no width and height attr
.attr("preserveAspectRatio", "xMinYMin meet")
.attr("viewBox", "-40 -10 " + (width + 60) + " " + height + 40)
//class to make it responsive
.classed("svg-content-responsive", true)
.call(zoom);
var make_x_axis = function () {
return d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(10);
};
var make_y_axis = function () {
return d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10);
};
var xAxis = make_x_axis();
var yAxis = make_y_axis();
svg.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0, " + height + ")")
.call(xAxis);
svg.append("svg:g")
.attr("class", "y axis")
.call(yAxis);
svg.append("svg:g")
.attr("class", "x grid")
.attr("transform", "translate(0," + height + ")")
.call(make_x_axis().tickSize(-height, 0, 0).tickFormat(""));
svg.append("g")
.attr("class", "y grid")
.call(make_y_axis().tickSize(-width, 0, 0).tickFormat(""));
var clip = svg.append("svg:clipPath")
.attr("id", "clip")
.append("svg:rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height);
var chartBody = svg.append("svg:g")
.attr("clip-path", "url(#clip)");
chartBody.append("svg:path")
.datum(data)
.attr("class", "line")
.attr("d", valueline(dataarray));
function zoomed() {
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
svg.select(".x.grid").call(make_x_axis().tickSize(-height, 0, 0).tickFormat(""));
svg.select(".y.grid").call(make_y_axis().tickSize(-width, 0, 0).tickFormat(""));
svg.select(".line").attr("class", "line").attr("d", valueline(dataarray));
}
});
</script>
</body>
</html>