Skip to content

Commit 11edd62

Browse files
author
Jim Hague
committed
Add autotrace mode.
When autotrace is selected, input must be 3 columns in table mode, each containing trace name, x and y. Automatically generate a trace for each distinct trace name.
1 parent f7b5b61 commit 11edd62

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

src/module.ts

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class PlotlyPanelCtrl extends MetricsPanelCtrl {
8888
type: 'scatter',
8989
displayModeBar: false,
9090
orientation: 'v',
91+
autotrace: false,
9192
},
9293
layout: {
9394
barmode: 'group',
@@ -563,15 +564,51 @@ class PlotlyPanelCtrl extends MetricsPanelCtrl {
563564

564565
onDataReceived(dataList) {
565566
const finfo: SeriesWrapper[] = [];
567+
const autotrace = this.cfg.settings.autotrace;
566568
let seriesHash = '/';
569+
let autotraceDataSeries = {};
567570
if (dataList && dataList.length > 0) {
571+
if (autotrace) {
572+
// Check input is of expected form.
573+
dataList.forEach((series, sidx) => {
574+
if (series.columns && series.columns.length === 3 && series.type === 'table') {
575+
series.rows.forEach((val) => {
576+
const sname = val[0];
577+
if (!(sname in autotraceDataSeries)) {
578+
autotraceDataSeries[sname] = {
579+
columns: series.columns,
580+
rows: [],
581+
type: 'table',
582+
name: sname,
583+
}
584+
};
585+
autotraceDataSeries[sname].rows.push(val);
586+
});
587+
} else {
588+
console.error('Autotrace needs table input with 3 columns', sidx, series);
589+
throw new Error('Autotrace needs table input with 3 columns');
590+
}
591+
});
592+
593+
let autotraceDataList: any = [];
594+
for (var key in autotraceDataSeries) {
595+
autotraceDataList.push(autotraceDataSeries[key]);
596+
}
597+
this._updateAutoTraces(autotraceDataSeries);
598+
dataList = autotraceDataList;
599+
}
600+
568601
const useRefID = dataList.length === this.panel.targets.length;
569602
dataList.forEach((series, sidx) => {
570603
let refId = '';
571-
if (useRefID) {
572-
refId = _.get(this.panel, 'targets[' + sidx + '].refId');
573-
if (!refId) {
574-
refId = String.fromCharCode('A'.charCodeAt(0) + sidx);
604+
if (autotrace) {
605+
refId = series.name;
606+
} else {
607+
if (useRefID) {
608+
refId = _.get(this.panel, 'targets[' + sidx + '].refId');
609+
if (!refId) {
610+
refId = String.fromCharCode('A'.charCodeAt(0) + sidx);
611+
}
575612
}
576613
}
577614
if (series.columns) {
@@ -661,6 +698,10 @@ class PlotlyPanelCtrl extends MetricsPanelCtrl {
661698

662699
// This will update all trace settings *except* the data
663700
_updateTracesFromConfigs() {
701+
// If we're in autotrace mode, trace creation happens elsewhere.
702+
if (this.cfg.settings.autotrace)
703+
return;
704+
664705
this.dataWarnings = [];
665706

666707
// Make sure we have a trace
@@ -726,16 +767,48 @@ class PlotlyPanelCtrl extends MetricsPanelCtrl {
726767
});
727768
}
728769

770+
// Autotrace. Recreate the traces to those found in the data.
771+
_updateAutoTraces(series) {
772+
this.dataWarnings = [];
773+
774+
delete this.traces;
775+
776+
this.traces = [];
777+
778+
for (var key in series) {
779+
const trace: any = {
780+
name: key,
781+
type: this.cfg.settings.type,
782+
orientation: this.cfg.settings.orientation,
783+
__set: [], // { key:? property:? }
784+
};
785+
786+
// Set the text
787+
this.__addCopyPath(trace, key + '/' + series[key].columns[1].text, 'x');
788+
this.__addCopyPath(trace, key + '/' + series[key].columns[2].text, 'y');
789+
if (trace.orientation === 'v')
790+
this.__addCopyPath(trace, key + '/' + series[key].columns[2].text, 'text');
791+
else
792+
this.__addCopyPath(trace, key + '/' + series[key].columns[1].text, 'text');
793+
794+
this.traces.push(trace);
795+
}
796+
797+
if (this.traces.length < 1) {
798+
this.traces = [_.cloneDeep(PlotlyPanelCtrl.defaultTrace)];
799+
}
800+
}
801+
729802
// Fills in the required data into the trace values
730803
_updateTraceData(force = false): boolean {
731804
if (!this.series) {
732-
// console.log('NO Series data yet!');
805+
// console.log('No series data yet!');
733806
return false;
734807
}
735808

736809
if (force || !this.traces) {
737810
this._updateTracesFromConfigs();
738-
} else if (this.traces.length !== this.cfg.traces.length) {
811+
} else if (!this.cfg.settings.autotrace && this.traces.length !== this.cfg.traces.length) {
739812
console.log(
740813
'trace number mismatch. Found: ' +
741814
this.traces.length +

src/partials/tab_display.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ <h5 class="section-heading">Options</h5>
8989
checked="ctrl.cfg.settings.displayModeBar"
9090
on-change="ctrl.editor.onConfigChanged()"></gf-form-switch>
9191

92+
<gf-form-switch
93+
class="gf-form"
94+
label="Auto trace"
95+
label-class="gf-form-label width-7"
96+
checked="ctrl.cfg.settings.autotrace"
97+
on-change="ctrl.editor.onConfigChanged()"></gf-form-switch>
98+
9299
<gf-form-switch
93100
class="gf-form"
94101
label="Legend"

src/partials/tab_traces.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</div>
55
</div>
66

7-
<div class="edit-tab-with-sidemenu">
7+
<div class="edit-tab-with-sidemenu" ng-if="!ctrl.cfg.settings.autotrace">
88
<aside class="edit-sidemenu-aside" style="min-width: 100px">
99
<ul class="edit-sidemenu">
1010
<li

0 commit comments

Comments
 (0)