refactor: extract the scalar plot building

This commit is contained in:
Jeremy Wall 2024-03-10 21:11:32 -04:00
parent 75264dc095
commit 2f6b9b6c58

View File

@ -449,6 +449,9 @@ export class GraphPlot extends HTMLElement {
}; };
} }
/**
* @param {any} triple
*/
buildSeriesPlot(triple) { buildSeriesPlot(triple) {
const labels = /** @type {Map<String, String>} */(triple[0]); const labels = /** @type {Map<String, String>} */(triple[0]);
for (var label in labels) { for (var label in labels) {
@ -483,6 +486,32 @@ export class GraphPlot extends HTMLElement {
return trace; return trace;
} }
/**
* @param {any} triple
*/
buildScalarPlot(triple) {
const labels = /** @type {Map<String,String>} */(triple[0]);
for (var label in labels) {
var show = this.#filteredLabelSets[label];
if (show && !show.includes(labels[label])) {
return null;
}
}
const meta = /** @type {PlotMeta} */(triple[1]);
const series = triple[2];
const trace = /** @type GraphTrace */({
type: "bar",
x: [],
y: [],
yhoverformat: meta["d3_tick_format"],
});
var name = this.formatName(meta, labels);
if (name) { trace.name = name; }
trace.y.push(series.value);
trace.x.push(trace.name);
return trace;
}
/** /**
* Update the graph with new data. * Update the graph with new data.
* *
@ -527,33 +556,17 @@ export class GraphPlot extends HTMLElement {
// https://plotly.com/javascript/reference/scatter/ // https://plotly.com/javascript/reference/scatter/
loopSeries: for (const triple of subplot.Series) { loopSeries: for (const triple of subplot.Series) {
const trace = this.buildSeriesPlot(triple); const trace = this.buildSeriesPlot(triple);
if (trace) { if (trace) {
traces.push(trace); traces.push(trace);
} }
} }
} else if (subplot.Scalar) { } else if (subplot.Scalar) {
// https://plotly.com/javascript/reference/bar/ // https://plotly.com/javascript/reference/bar/
loopScalar: for (const triple of subplot.Scalar) { loopScalar: for (const triple of subplot.Scalar) {
const labels = triple[0]; const trace = this.buildScalarPlot(triple);
for (var label in labels) { if (trace) {
var show = this.#filteredLabelSets[label]; traces.push(trace);
if (show && !show.includes(labels[label])) {
continue loopScalar;
}
} }
const meta = triple[1];
const series = triple[2];
const trace = /** @type GraphTrace */({
type: "bar",
x: [],
y: [],
yhoverformat: meta["d3_tick_format"],
});
var name = this.formatName(meta, labels);
if (name) { trace.name = name; }
trace.y.push(series.value);
trace.x.push(trace.name);
traces.push(trace);
} }
} else if (subplot.Stream) { } else if (subplot.Stream) {
// TODO(jwall): It would be nice if scroll behavior would handle replots better. // TODO(jwall): It would be nice if scroll behavior would handle replots better.