blob: 6b0e0e237203e12c2b3484b94f914dd71da979b3 [file] [log] [blame]
James Kuszmaul5f5e1232020-12-22 20:58:00 -08001// This file provides an index of all standard plot configs.
2// In the future, this may be split into more pieces (e.g., to have
3// year-specific indices). For now, the pattern for creating a new plot
4// is that you provide an exported function (a la the plot*() functions imported
5// below) which, when called, will generate the plot in the provided div.
6// This file handles providing a master list of all known plots so that
7// the user can just open a single web-page and select the plot that they want
8// from a drop-down. A given plot will not be loaded until it has been selected
9// once, at which point it will stay loaded. This means that if the user wants
10// to switch between several plot configs, they don't incur any performance
11// penalty associated with swapping.
12// By default, no plot is selected, but the plot= URL parameter may be used
13// to specify a specific plot, so that people can create links to a specific
14// plot.
15// The plot*() functions are called *after* we have already received a valid
16// config from the web server, so config handlers do not need to be used.
17//
18// The exact setup of this will be in flux as we add more configs and figure out
19// what setups work best--we will likely end up with separate index files for
20// each robot year, and may even end up allowing plots to be specified solely
21// using JSON rather than requiring people to write a script just to create
22// a plot.
23import * as configuration from 'org_frc971/aos/configuration_generated';
24import * as proxy from 'org_frc971/aos/network/www/proxy';
25import {plotImu} from 'org_frc971/frc971/wpilib/imu_plotter';
26import {plotDemo} from 'org_frc971/aos/network/www/demo_plot';
James Kuszmaul48671362020-12-24 13:54:16 -080027import {plotData} from 'org_frc971/frc971/analysis/plot_data_utils';
James Kuszmaul5f5e1232020-12-22 20:58:00 -080028
29import Connection = proxy.Connection;
30import Configuration = configuration.aos.Configuration;
31
32const rootDiv = document.createElement('div');
33document.body.appendChild(rootDiv);
34
35const helpDiv = document.createElement('div');
36rootDiv.appendChild(helpDiv);
37helpDiv.innerHTML =
James Kuszmaul461b0682020-12-22 22:20:21 -080038 'Help: click + drag to pan, double click to reset, scroll to zoom, ' +
39 'right-click + drag to zoom to rectangle, Esc to cancel. ' +
James Kuszmaul5f5e1232020-12-22 20:58:00 -080040 'Hold the x/y keys to only pan/zoom along the x/y axes.';
41
42class PlotState {
43 public readonly div: HTMLElement;
44 private initialized = false;
45 constructor(
46 parentDiv: HTMLElement,
47 private readonly initializer:
48 (conn: Connection, element: Element) => void) {
49 this.div = document.createElement('div');
50 parentDiv.appendChild(this.div);
51 this.hide();
52 }
53 initialize(conn: Connection): void {
54 if (!this.initialized) {
55 this.initializer(conn, this.div);
56 this.initialized = true;
57 }
58 }
59 hide(): void {
60 this.div.style.display = "none";
61 }
62 show(): void {
63 this.div.style.display = "block";
64 }
65}
66
67const plotSelect = document.createElement('select');
68rootDiv.appendChild(plotSelect);
69
70const plotDiv = document.createElement('div');
71plotDiv.style.top = (plotSelect.getBoundingClientRect().bottom + 10).toString();
72plotDiv.style.left = '0';
73plotDiv.style.position = 'absolute';
74rootDiv.appendChild(plotDiv);
75
76// The master list of all the plots that we provide. For a given config, it
77// is possible that not all of these plots will be usable depending on the
78// presence of certain channels.
79const plotIndex = new Map<string, PlotState>([
80 ['Demo', new PlotState(plotDiv, plotDemo)],
James Kuszmaul48671362020-12-24 13:54:16 -080081 ['IMU', new PlotState(plotDiv, plotImu)],
82 ['C++ Plotter', new PlotState(plotDiv, plotData)],
James Kuszmaul5f5e1232020-12-22 20:58:00 -080083]);
84
85const invalidSelectValue = 'null';
86function getDefaultPlot(): string {
87 const urlParams = (new URL(document.URL)).searchParams;
88 const urlParamKey = 'plot';
89 if (!urlParams.has(urlParamKey)) {
90 return invalidSelectValue;
91 }
92 const desiredPlot = urlParams.get(urlParamKey);
93 if (!plotIndex.has(desiredPlot)) {
94 return invalidSelectValue;
95 }
96 return desiredPlot;
97}
98
99const conn = new Connection();
100
101conn.connect();
102
103conn.addConfigHandler((config: Configuration) => {
104 plotSelect.add(new Option("Select Plot", invalidSelectValue));
105 for (const name of plotIndex.keys()) {
106 plotSelect.add(new Option(name, name));
107 }
108 plotSelect.addEventListener('input', () => {
109 for (const plot of plotIndex.values()) {
110 plot.hide();
111 }
112 if (plotSelect.value == invalidSelectValue) {
113 return;
114 }
115 plotIndex.get(plotSelect.value).initialize(conn);
116 plotIndex.get(plotSelect.value).show();
117 });
118 plotSelect.value = getDefaultPlot();
119 // Force the event to occur once at the start.
120 plotSelect.dispatchEvent(new Event('input'));
121});