|
| 1 | +import Chart from "chart.js/auto"; |
| 2 | + |
| 3 | +let textLineBuffer = ""; |
| 4 | +let textLine; |
| 5 | + |
| 6 | +let defaultColors = ['#8888ff', '#ff8888', '#88ff88']; |
| 7 | + |
| 8 | +/** |
| 9 | + * @name LineBreakTransformer |
| 10 | + * Helper to parse the incoming string messages into lines. |
| 11 | + */ |
| 12 | +class LineBreakTransformer { |
| 13 | + constructor() { |
| 14 | + // A container for holding stream data until a new line. |
| 15 | + this.container = ''; |
| 16 | + } |
| 17 | + |
| 18 | + transform(chunk, linesList) { |
| 19 | + this.container += chunk; |
| 20 | + const lines = this.container.split('\n'); |
| 21 | + this.container = lines.pop(); |
| 22 | + lines.forEach(line => linesList.push(line)); |
| 23 | + } |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +let lineTransformer = new LineBreakTransformer() |
| 28 | + |
| 29 | +export function plotValues(chartObj, serialMessage, bufferSize) { |
| 30 | + /* |
| 31 | + Given a string serialMessage, parse it into the plottable value(s) that |
| 32 | + it contains if any, and plot those values onto the given chartObj. If |
| 33 | + the serialMessage doesn't represent a complete textLine it will be stored |
| 34 | + into a buffer and combined with subsequent serialMessages until a full |
| 35 | + textLine is formed. |
| 36 | + */ |
| 37 | + let currentLines = [] |
| 38 | + lineTransformer.transform(serialMessage, currentLines) |
| 39 | + |
| 40 | + for (textLine of currentLines) { |
| 41 | + |
| 42 | + textLine = textLine.replace("\r", "").replace("\n", "") |
| 43 | + if (textLine.length === 0) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + let valuesToPlot; |
| 48 | + |
| 49 | + // handle possible tuple in textLine |
| 50 | + if (textLine.startsWith("(") && textLine.endsWith(")")) { |
| 51 | + textLine = "[" + textLine.substring(1, textLine.length - 1) + "]"; |
| 52 | + console.log("after tuple conversion: " + textLine); |
| 53 | + } |
| 54 | + |
| 55 | + // handle possible list in textLine |
| 56 | + if (textLine.startsWith("[") && textLine.endsWith("]")) { |
| 57 | + valuesToPlot = JSON.parse(textLine); |
| 58 | + for (let i = 0; i < valuesToPlot.length; i++) { |
| 59 | + valuesToPlot[i] = parseFloat(valuesToPlot[i]) |
| 60 | + } |
| 61 | + |
| 62 | + } else { // handle possible CSV in textLine |
| 63 | + valuesToPlot = textLine.split(",") |
| 64 | + for (let i = 0; i < valuesToPlot.length; i++) { |
| 65 | + valuesToPlot[i] = parseFloat(valuesToPlot[i]) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (valuesToPlot === undefined || valuesToPlot.length === 0) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + while (chartObj.data.labels.length > bufferSize) { |
| 75 | + chartObj.data.labels.shift(); |
| 76 | + for (let i = 0; i < chartObj.data.datasets.length; i++) { |
| 77 | + while (chartObj.data.datasets[i].data.length > bufferSize) { |
| 78 | + chartObj.data.datasets[i].data.shift(); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + chartObj.data.labels.push(""); |
| 83 | + |
| 84 | + for (let i = 0; i < valuesToPlot.length; i++) { |
| 85 | + if (isNaN(valuesToPlot[i])) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + if (i > chartObj.data.datasets.length - 1) { |
| 89 | + let curColor = '#000000'; |
| 90 | + if (i < defaultColors.length) { |
| 91 | + curColor = defaultColors[i]; |
| 92 | + } |
| 93 | + chartObj.data.datasets.push({ |
| 94 | + label: i.toString(), |
| 95 | + data: [], |
| 96 | + borderColor: curColor, |
| 97 | + backgroundColor: curColor |
| 98 | + }); |
| 99 | + } |
| 100 | + chartObj.data.datasets[i].data.push(valuesToPlot[i]); |
| 101 | + } |
| 102 | + |
| 103 | + updatePlotterScales(chartObj); |
| 104 | + chartObj.update(); |
| 105 | + } catch (e) { |
| 106 | + console.log("JSON parse error"); |
| 107 | + // This line isn't a valid data value |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function updatePlotterScales(chartObj) { |
| 113 | + /* |
| 114 | + Update the scale of the plotter so that maximum and minimum values are sure |
| 115 | + to be shown within the plotter instead of going outside the visible range. |
| 116 | + */ |
| 117 | + let allData = [] |
| 118 | + for (let i = 0; i < chartObj.data.datasets.length; i++) { |
| 119 | + allData = allData.concat(chartObj.data.datasets[i].data) |
| 120 | + } |
| 121 | + chartObj.options.scales.y.min = Math.min(...allData) - 10 |
| 122 | + chartObj.options.scales.y.max = Math.max(...allData) + 10 |
| 123 | +} |
| 124 | + |
| 125 | +export async function setupPlotterChart(workflow) { |
| 126 | + /* |
| 127 | + Initialize the plotter chart and configure it. |
| 128 | + */ |
| 129 | + let initialData = [] |
| 130 | + Chart.defaults.backgroundColor = '#444444'; |
| 131 | + Chart.defaults.borderColor = '#000000'; |
| 132 | + Chart.defaults.color = '#000000'; |
| 133 | + Chart.defaults.aspectRatio = 3/2; |
| 134 | + workflow.plotterChart = new Chart( |
| 135 | + document.getElementById('plotter-canvas'), |
| 136 | + { |
| 137 | + type: 'line', |
| 138 | + options: { |
| 139 | + animation: false, |
| 140 | + scales: { |
| 141 | + y: { |
| 142 | + min: -1, |
| 143 | + max: 1, |
| 144 | + grid:{ |
| 145 | + color: "#666" |
| 146 | + }, |
| 147 | + border: { |
| 148 | + color: "#444" |
| 149 | + } |
| 150 | + }, |
| 151 | + x:{ |
| 152 | + grid: { |
| 153 | + display: true, |
| 154 | + color: "#666" |
| 155 | + }, |
| 156 | + border: { |
| 157 | + color: "#444" |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + }, |
| 162 | + data: { |
| 163 | + labels: initialData.map(row => row.timestamp), |
| 164 | + datasets: [ |
| 165 | + { |
| 166 | + label: '0', |
| 167 | + data: initialData.map(row => row.value) |
| 168 | + } |
| 169 | + ] |
| 170 | + } |
| 171 | + } |
| 172 | + ); |
| 173 | + |
| 174 | + // Set up a listener to respond to user changing the grid choice configuration |
| 175 | + // dropdown |
| 176 | + workflow.plotterGridLines.addEventListener('change', (event) => { |
| 177 | + let gridChoice = event.target.value; |
| 178 | + if (gridChoice === "x"){ |
| 179 | + workflow.plotterChart.options.scales.x.grid.display = true; |
| 180 | + workflow.plotterChart.options.scales.y.grid.display = false; |
| 181 | + }else if (gridChoice === "y"){ |
| 182 | + workflow.plotterChart.options.scales.y.grid.display = true; |
| 183 | + workflow.plotterChart.options.scales.x.grid.display = false; |
| 184 | + }else if (gridChoice === "both"){ |
| 185 | + workflow.plotterChart.options.scales.y.grid.display = true; |
| 186 | + workflow.plotterChart.options.scales.x.grid.display = true; |
| 187 | + }else if (gridChoice === "none"){ |
| 188 | + workflow.plotterChart.options.scales.y.grid.display = false; |
| 189 | + workflow.plotterChart.options.scales.x.grid.display = false; |
| 190 | + } |
| 191 | + workflow.plotterChart.update(); |
| 192 | + }); |
| 193 | +} |
0 commit comments