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