blob: 5d1f4a0a19702cc3956ea3cd09d52458b0ed176d [file] [log] [blame]
James Kuszmaul48671362020-12-24 13:54:16 -08001// Provides a plot which handles plotting the plot defined by a
2// frc971.analysis.Plot message.
3import * as configuration from 'org_frc971/aos/configuration_generated';
4import * as plot_data from 'org_frc971/frc971/analysis/plot_data_generated';
5import {MessageHandler, TimestampedMessage} from 'org_frc971/aos/network/www/aos_plotter';
6import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
James Kuszmaul0d7df892021-04-09 22:19:49 -07007import {Plot, Point} from 'org_frc971/aos/network/www/plotter';
James Kuszmaul48671362020-12-24 13:54:16 -08008import * as proxy from 'org_frc971/aos/network/www/proxy';
9
10import Connection = proxy.Connection;
11import Schema = configuration.reflection.Schema;
12import PlotFb = plot_data.frc971.analysis.Plot;
13
14export 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 Kuszmaul48671362020-12-24 13:54:16 -080032 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 Schuhc2e9c502021-11-25 21:23:24 -080050 figureDiv.style.width = figure.position().width().toString() + "px";
51 figureDiv.style.height = figure.position().height().toString() + "px";
52 figureDiv.style.position = 'relative';
James Kuszmaul48671362020-12-24 13:54:16 -080053 div.appendChild(figureDiv);
Austin Schuhc2e9c502021-11-25 21:23:24 -080054 const plot = new Plot(figureDiv);
James Kuszmaul48671362020-12-24 13:54:16 -080055
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 Kuszmaule32fa932021-05-11 21:38:16 -070063 plot.getAxisLabels().setYLabel(figure.ylabel());
James Kuszmaul48671362020-12-24 13:54:16 -080064 }
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 Kuszmaul0d7df892021-04-09 22:19:49 -070078 const points = [];
James Kuszmaul48671362020-12-24 13:54:16 -080079 for (let kk = 0; kk < lineFb.pointsLength(); ++kk) {
James Kuszmaul0d7df892021-04-09 22:19:49 -070080 const point = lineFb.points(kk);
81 points.push(new Point(point.x(), point.y()));
James Kuszmaul48671362020-12-24 13:54:16 -080082 }
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}