-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathplotter.js
More file actions
198 lines (178 loc) · 6.7 KB
/
Copy pathplotter.js
File metadata and controls
198 lines (178 loc) · 6.7 KB
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
import Chart from "chart.js/auto";
let textLineBuffer = "";
let textLine;
let defaultColors = ['#8888ff', '#ff8888', '#88ff88'];
/**
* @name LineBreakTransformer
* Helper to parse the incoming string messages into lines.
*/
class LineBreakTransformer {
constructor() {
// A container for holding stream data until a new line.
this.container = '';
}
transform(chunk, linesList) {
this.container += chunk;
const lines = this.container.split('\n');
this.container = lines.pop();
lines.forEach(line => linesList.push(line));
}
}
let lineTransformer = new LineBreakTransformer()
export function plotValues(chartObj, serialMessage, bufferSize) {
/*
Given a string serialMessage, parse it into the plottable value(s) that
it contains if any, and plot those values onto the given chartObj. If
the serialMessage doesn't represent a complete textLine it will be stored
into a buffer and combined with subsequent serialMessages until a full
textLine is formed.
*/
let currentLines = []
lineTransformer.transform(serialMessage, currentLines)
for (textLine of currentLines) {
textLine = textLine.replace("\r", "").replace("\n", "")
if (textLine.length === 0) {
continue;
}
let valuesToPlot;
// handle possible tuple in textLine
if (textLine.startsWith("(") && textLine.endsWith(")")) {
textValues = textLine.substring(1, textLine.length - 1).trim();
// Python tuples can end with a comma, but JS arrays cannot
if (textValues.endsWith(",")) {
textValues = textValues.substring(0, textValues.length - 1);
}
textLine = "[" + textValues + "]";
console.log("after tuple conversion: " + textLine);
}
// handle possible list in textLine
if (textLine.startsWith("[") && textLine.endsWith("]")) {
valuesToPlot = JSON.parse(textLine);
for (let i = 0; i < valuesToPlot.length; i++) {
valuesToPlot[i] = parseFloat(valuesToPlot[i])
}
} else { // handle possible CSV in textLine
valuesToPlot = textLine.split(",")
for (let i = 0; i < valuesToPlot.length; i++) {
valuesToPlot[i] = parseFloat(valuesToPlot[i])
}
}
if (valuesToPlot === undefined || valuesToPlot.length === 0) {
continue;
}
try {
while (chartObj.data.labels.length > bufferSize) {
chartObj.data.labels.shift();
for (let i = 0; i < chartObj.data.datasets.length; i++) {
while (chartObj.data.datasets[i].data.length > bufferSize) {
chartObj.data.datasets[i].data.shift();
}
}
}
chartObj.data.labels.push("");
for (let i = 0; i < valuesToPlot.length; i++) {
if (isNaN(valuesToPlot[i])) {
continue;
}
if (i > chartObj.data.datasets.length - 1) {
let curColor = '#000000';
if (i < defaultColors.length) {
curColor = defaultColors[i];
}
chartObj.data.datasets.push({
label: i.toString(),
data: [],
borderColor: curColor,
backgroundColor: curColor
});
}
chartObj.data.datasets[i].data.push(valuesToPlot[i]);
}
updatePlotterScales(chartObj);
chartObj.update();
} catch (e) {
console.log("JSON parse error");
// This line isn't a valid data value
}
}
}
function updatePlotterScales(chartObj) {
/*
Update the scale of the plotter so that maximum and minimum values are sure
to be shown within the plotter instead of going outside the visible range.
*/
let allData = []
for (let i = 0; i < chartObj.data.datasets.length; i++) {
allData = allData.concat(chartObj.data.datasets[i].data)
}
chartObj.options.scales.y.min = Math.min(...allData) - 10
chartObj.options.scales.y.max = Math.max(...allData) + 10
}
export async function setupPlotterChart(workflow) {
/*
Initialize the plotter chart and configure it.
*/
let initialData = []
Chart.defaults.backgroundColor = '#444444';
Chart.defaults.borderColor = '#000000';
Chart.defaults.color = '#000000';
Chart.defaults.aspectRatio = 3/2;
workflow.plotterChart = new Chart(
document.getElementById('plotter-canvas'),
{
type: 'line',
options: {
animation: false,
scales: {
y: {
min: -1,
max: 1,
grid:{
color: "#666"
},
border: {
color: "#444"
}
},
x:{
grid: {
display: true,
color: "#666"
},
border: {
color: "#444"
}
}
}
},
data: {
labels: initialData.map(row => row.timestamp),
datasets: [
{
label: '0',
data: initialData.map(row => row.value)
}
]
}
}
);
// Set up a listener to respond to user changing the grid choice configuration
// dropdown
workflow.plotterGridLines.addEventListener('change', (event) => {
let gridChoice = event.target.value;
if (gridChoice === "x"){
workflow.plotterChart.options.scales.x.grid.display = true;
workflow.plotterChart.options.scales.y.grid.display = false;
}else if (gridChoice === "y"){
workflow.plotterChart.options.scales.y.grid.display = true;
workflow.plotterChart.options.scales.x.grid.display = false;
}else if (gridChoice === "both"){
workflow.plotterChart.options.scales.y.grid.display = true;
workflow.plotterChart.options.scales.x.grid.display = true;
}else if (gridChoice === "none"){
workflow.plotterChart.options.scales.y.grid.display = false;
workflow.plotterChart.options.scales.x.grid.display = false;
}
workflow.plotterChart.update();
});
}