James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 1 | // Provides a plot which handles plotting the plot defined by a |
| 2 | // frc971.analysis.Plot message. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 3 | import {Plot as PlotFb} from 'org_frc971/frc971/analysis/plot_data_generated'; |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 4 | import {MessageHandler, TimestampedMessage} from 'org_frc971/aos/network/www/aos_plotter'; |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 5 | import {ByteBuffer} from 'flatbuffers'; |
James Kuszmaul | 0d7df89 | 2021-04-09 22:19:49 -0700 | [diff] [blame] | 6 | import {Plot, Point} from 'org_frc971/aos/network/www/plotter'; |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 7 | import {Connection} from 'org_frc971/aos/network/www/proxy'; |
James Kuszmaul | 136aa2b | 2022-04-02 14:50:56 -0700 | [diff] [blame] | 8 | import {Schema} from 'flatbuffers_reflection/reflection_generated'; |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 9 | |
| 10 | export function plotData(conn: Connection, parentDiv: Element) { |
| 11 | // Set up a selection box to allow the user to choose between plots to show. |
| 12 | const plotSelect = document.createElement('select'); |
| 13 | parentDiv.appendChild(plotSelect); |
| 14 | const plots = new Map<string, HTMLElement>(); |
| 15 | const invalidSelectValue = 'null'; |
| 16 | plotSelect.addEventListener('input', () => { |
| 17 | for (const plot of plots.values()) { |
| 18 | plot.style.display = 'none'; |
| 19 | } |
| 20 | if (plotSelect.value == invalidSelectValue) { |
| 21 | return; |
| 22 | } |
| 23 | plots.get(plotSelect.value).style.display = 'block'; |
| 24 | }); |
| 25 | plotSelect.add(new Option('Select Plot', invalidSelectValue)); |
| 26 | |
| 27 | const plotDiv = document.createElement('div'); |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 28 | parentDiv.appendChild(plotDiv); |
| 29 | |
| 30 | conn.addReliableHandler( |
| 31 | '/analysis', 'frc971.analysis.Plot', (data: Uint8Array, time: number) => { |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 32 | const plotFb = PlotFb.getRootAsPlot(new ByteBuffer(data)); |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 33 | const name = (!plotFb.title()) ? 'Plot ' + plots.size : plotFb.title(); |
| 34 | const div = document.createElement('div'); |
| 35 | div.style.display = 'none'; |
| 36 | plots.set(name, div); |
| 37 | plotDiv.appendChild(div); |
| 38 | plotSelect.add(new Option(name, name)); |
| 39 | |
| 40 | const linkedXAxes: Plot[] = []; |
| 41 | |
| 42 | for (let ii = 0; ii < plotFb.figuresLength(); ++ii) { |
| 43 | const figure = plotFb.figures(ii); |
| 44 | const figureDiv = document.createElement('div'); |
Austin Schuh | c2e9c50 | 2021-11-25 21:23:24 -0800 | [diff] [blame] | 45 | figureDiv.style.width = figure.position().width().toString() + "px"; |
| 46 | figureDiv.style.height = figure.position().height().toString() + "px"; |
| 47 | figureDiv.style.position = 'relative'; |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 48 | div.appendChild(figureDiv); |
Austin Schuh | c2e9c50 | 2021-11-25 21:23:24 -0800 | [diff] [blame] | 49 | const plot = new Plot(figureDiv); |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 50 | |
| 51 | if (figure.title()) { |
| 52 | plot.getAxisLabels().setTitle(figure.title()); |
| 53 | } |
| 54 | if (figure.xlabel()) { |
| 55 | plot.getAxisLabels().setXLabel(figure.xlabel()); |
| 56 | } |
| 57 | if (figure.ylabel()) { |
James Kuszmaul | e32fa93 | 2021-05-11 21:38:16 -0700 | [diff] [blame] | 58 | plot.getAxisLabels().setYLabel(figure.ylabel()); |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 59 | } |
| 60 | if (figure.shareXAxis()) { |
| 61 | for (const other of linkedXAxes) { |
| 62 | plot.linkXAxis(other); |
| 63 | } |
| 64 | linkedXAxes.push(plot); |
| 65 | } |
| 66 | |
| 67 | for (let jj = 0; jj < figure.linesLength(); ++jj) { |
| 68 | const lineFb = figure.lines(jj); |
| 69 | const line = plot.getDrawer().addLine(); |
| 70 | if (lineFb.label()) { |
| 71 | line.setLabel(lineFb.label()); |
| 72 | } |
James Kuszmaul | 0d7df89 | 2021-04-09 22:19:49 -0700 | [diff] [blame] | 73 | const points = []; |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 74 | for (let kk = 0; kk < lineFb.pointsLength(); ++kk) { |
James Kuszmaul | 0d7df89 | 2021-04-09 22:19:49 -0700 | [diff] [blame] | 75 | const point = lineFb.points(kk); |
| 76 | points.push(new Point(point.x(), point.y())); |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 77 | } |
| 78 | if (lineFb.color()) { |
| 79 | line.setColor( |
| 80 | [lineFb.color().r(), lineFb.color().g(), lineFb.color().b()]); |
| 81 | } |
James Kuszmaul | 19217a4 | 2022-06-17 10:54:29 -0700 | [diff] [blame^] | 82 | if (lineFb.style()) { |
| 83 | if (lineFb.style().pointSize() !== null) { |
| 84 | line.setPointSize(lineFb.style().pointSize()); |
| 85 | } |
| 86 | if (lineFb.style().drawLine() !== null) { |
| 87 | line.setDrawLine(lineFb.style().drawLine()); |
| 88 | } |
| 89 | } |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame] | 90 | line.setPoints(points); |
| 91 | } |
| 92 | } |
| 93 | }); |
| 94 | } |