From 2f6b9b6c58df932545414fc6eb45e44bd0d9d499 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 10 Mar 2024 21:11:32 -0400 Subject: [PATCH] refactor: extract the scalar plot building --- static/lib.js | 53 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/static/lib.js b/static/lib.js index 338a539..fb3493f 100644 --- a/static/lib.js +++ b/static/lib.js @@ -449,6 +449,9 @@ export class GraphPlot extends HTMLElement { }; } + /** + * @param {any} triple + */ buildSeriesPlot(triple) { const labels = /** @type {Map} */(triple[0]); for (var label in labels) { @@ -483,6 +486,32 @@ export class GraphPlot extends HTMLElement { return trace; } + /** + * @param {any} triple + */ + buildScalarPlot(triple) { + const labels = /** @type {Map} */(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. * @@ -527,33 +556,17 @@ export class GraphPlot extends HTMLElement { // https://plotly.com/javascript/reference/scatter/ loopSeries: for (const triple of subplot.Series) { const trace = this.buildSeriesPlot(triple); - if (trace) { + if (trace) { traces.push(trace); } } } else if (subplot.Scalar) { // https://plotly.com/javascript/reference/bar/ loopScalar: for (const triple of subplot.Scalar) { - const labels = triple[0]; - for (var label in labels) { - var show = this.#filteredLabelSets[label]; - if (show && !show.includes(labels[label])) { - continue loopScalar; - } + const trace = this.buildScalarPlot(triple); + if (trace) { + traces.push(trace); } - 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) { // TODO(jwall): It would be nice if scroll behavior would handle replots better.