You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2021/11/10 11:49:57 UTC

[incubator-streampipes] branch dev updated: Enabling groupby for time series charts in data-explorer

This is an automated email from the ASF dual-hosted git repository.

zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new a140671  Enabling groupby for time series charts in data-explorer
     new 393eecf  Merge pull request #58 from patrickraoulphilipp/dev
a140671 is described below

commit a1406716af37faad341f6073b5cacfefd5d76e32
Author: Patrick Philipp <ph...@fzi.de>
AuthorDate: Tue Nov 9 11:55:05 2021 +0100

    Enabling groupby for time series charts in data-explorer
---
 .../time-series-chart-widget.component.ts          | 176 +++++++++++++++------
 1 file changed, 125 insertions(+), 51 deletions(-)

diff --git a/ui/src/app/data-explorer/components/widgets/time-series-chart/time-series-chart-widget.component.ts b/ui/src/app/data-explorer/components/widgets/time-series-chart/time-series-chart-widget.component.ts
index 7e9b1eb..fe49b81 100644
--- a/ui/src/app/data-explorer/components/widgets/time-series-chart/time-series-chart-widget.component.ts
+++ b/ui/src/app/data-explorer/components/widgets/time-series-chart/time-series-chart-widget.component.ts
@@ -31,6 +31,8 @@ export class TimeSeriesChartWidgetComponent extends BaseDataExplorerWidget<TimeS
 
   presetColors: string[] = ['#39B54A', '#1B1464', '#f44336', '#4CAF50', '#FFEB3B', '#FFFFFF', '#000000'];
 
+  groupKeeper: {} = {};
+
   data: any[] = undefined;
   advancedSettingsActive = false;
   showBackgroundColorProperty = true;
@@ -128,38 +130,69 @@ export class TimeSeriesChartWidgetComponent extends BaseDataExplorerWidget<TimeS
     const indexXkey = 0;
 
     const tmpLineChartTraces: any[] = [];
-    columnsContainingNumbersPlusBooleans.forEach(field => {
-      const headerName = data.headers[this.getColumnIndex(field, data)];
-      tmpLineChartTraces[field.fullDbName + sourceIndex.toString()] = {
-        type: 'scatter',
-        mode: 'Line',
-        name: headerName,
-        connectgaps: false,
-        x: [],
-        y: []
-      };
-    });
 
-    data.allDataSeries[0].rows.forEach(row => {
-      this.dataExplorerWidget.visualizationConfig.selectedTimeSeriesChartProperties.forEach(field => {
-        if (field.sourceIndex === data.sourceIndex) {
-          const columnIndex = this.getColumnIndex(field, data);
-
-          let value = row[columnIndex];
-          if (this.fieldProvider.booleanFields.find(f => field.fullDbName === f.fullDbName
-            && f.sourceIndex === data.sourceIndex) !== undefined) {
-            value = value === true ? 1 : 0;
+    data.allDataSeries.map((group, index) => {
+      group.rows.forEach(row => {
+
+        this.dataExplorerWidget.visualizationConfig.selectedTimeSeriesChartProperties.forEach(field => {
+          if (field.sourceIndex === data.sourceIndex) {
+
+            const name = field.runtimeName + sourceIndex.toString();
+
+            if (group['tags'] != null) {
+              Object.entries(group['tags']).forEach(
+                ([key, val]) => {
+                  if (name in this.groupKeeper) {
+                    if (this.groupKeeper[name].indexOf(val) === - 1) {
+                      this.groupKeeper[name].push(val);
+                    }
+                  } else {
+                    this.groupKeeper[name] = [val];
+                  }
+                });
+            }
+
+            const columnIndex = this.getColumnIndex(field, data);
+
+            let value = row[columnIndex];
+            if (this.fieldProvider.booleanFields.find(f => field.fullDbName === f.fullDbName
+              && f.sourceIndex === data.sourceIndex) !== undefined) {
+              value = value === true ? 1 : 0;
+            }
+
+            if (!(field.fullDbName + sourceIndex.toString() + index.toString() in tmpLineChartTraces)) {
+              const headerName = data.headers[this.getColumnIndex(field, data)];
+              tmpLineChartTraces[field.fullDbName + sourceIndex.toString() + index.toString()] = {
+                type: 'scatter',
+                mode: 'Line',
+                name: headerName,
+                connectgaps: false,
+                x: [],
+                y: []
+              };
+            }
+
+            tmpLineChartTraces[field.fullDbName + sourceIndex.toString() + index.toString()].x.push(new Date(row[indexXkey]));
+            tmpLineChartTraces[field.fullDbName + sourceIndex.toString() + index.toString()].y.push(value);
           }
-
-          tmpLineChartTraces[field.fullDbName + sourceIndex.toString()].x.push(new Date(row[indexXkey]));
-          tmpLineChartTraces[field.fullDbName + sourceIndex.toString()].y.push(value);
-        }
+        });
       });
     });
 
     return Object.values(tmpLineChartTraces);
   }
 
+  lightenColor(color: string, percent: number) {
+    const num = parseInt(color.replace('#', ''), 16);
+    const amt = Math.round(2.55 * percent);
+    const R = (num >> 16) + amt;
+    const B = (num >> 8 & 0x00FF) + amt;
+    const G = (num & 0x0000FF) + amt;
+    const result = '#' + (0x1000000 + (R < 255 ? R < 1 ? 0 : R : 255) * 0x10000 +
+                  (B < 255 ? B < 1 ? 0 : B : 255) * 0x100 + (G < 255 ? G < 1 ? 0 : G : 255)).toString(16).slice(1);
+    return result;
+  }
+
   setStartX(startX: string) {
     this.selectedStartX = startX;
   }
@@ -172,46 +205,82 @@ export class TimeSeriesChartWidgetComponent extends BaseDataExplorerWidget<TimeS
     this.graph.layout.paper_bgcolor = this.dataExplorerWidget.baseAppearanceConfig.backgroundColor;
     this.graph.layout.plot_bgcolor = this.dataExplorerWidget.baseAppearanceConfig.backgroundColor;
     this.graph.layout.font.color = this.dataExplorerWidget.baseAppearanceConfig.textColor;
+
+    const keeper = {};
+    let pastGroups = 0;
+    let index = 0;
+
     if (this.data) {
-      this.dataExplorerWidget.visualizationConfig.selectedTimeSeriesChartProperties.map((field, index) => {
-        if (this.data[index] !== undefined) {
-          this.data[index]['marker'] = { 'color': '' };
+      this.dataExplorerWidget.visualizationConfig.selectedTimeSeriesChartProperties.map((field, findex) => {
 
-          const name = field.runtimeName + field.sourceIndex.toString();
+        const name = field.runtimeName + field.sourceIndex.toString();
 
-          if (!(name in this.dataExplorerWidget.visualizationConfig.chosenColor)) {
-            this.dataExplorerWidget.visualizationConfig.chosenColor[name] = this.presetColors[index];
-          }
+        let localGroups = [];
+        if (name in this.groupKeeper) {
+          localGroups = this.groupKeeper[name];
+        }
 
-          if (!(name in this.dataExplorerWidget.visualizationConfig.displayName)) {
-            this.dataExplorerWidget.visualizationConfig.displayName[name] = field.fullDbName;
-          }
+        let repeat = 1;
+        if (localGroups.length > 0) {
+          repeat = localGroups.length;
+        }
 
-          if (!(name in this.dataExplorerWidget.visualizationConfig.displayType)) {
-            this.dataExplorerWidget.visualizationConfig.displayType[name] = 'lines';
-          }
+        for (let it = 0; it < repeat; it++) {
 
-          this.data[index].marker.color = this.dataExplorerWidget.visualizationConfig.chosenColor[name];
-          this.data[index].name = this.dataExplorerWidget.visualizationConfig.displayName[name];
+          index = pastGroups;
 
-          let displayType = 'scatter';
-          let displayMode = 'lines';
+          if (this.data[index] !== undefined) {
+            this.data[index]['marker'] = { 'color': '' };
 
-          const setType = this.dataExplorerWidget.visualizationConfig.displayType[name];
+            if (!(name in this.dataExplorerWidget.visualizationConfig.chosenColor)) {
+              this.dataExplorerWidget.visualizationConfig.chosenColor[name] = this.presetColors[index];
+            }
 
-          if (setType !== 'bar') {
-            displayMode = setType;
-          } else {
-            displayType = 'bar';
-          }
+            if (!(name in this.dataExplorerWidget.visualizationConfig.displayName)) {
+              this.dataExplorerWidget.visualizationConfig.displayName[name] = field.fullDbName;
+            }
+
+            if (!(name in this.dataExplorerWidget.visualizationConfig.displayType)) {
+              this.dataExplorerWidget.visualizationConfig.displayType[name] = 'lines';
+            }
+
+            let color = this.dataExplorerWidget.visualizationConfig.chosenColor[name];
+
+            if (name in keeper) {
+              color = this.lightenColor(keeper[name], 11.0);
+              keeper[name] = color;
+            } else {
+              keeper[name] = this.dataExplorerWidget.visualizationConfig.chosenColor[name];
+            }
+
+            let displayName = this.dataExplorerWidget.visualizationConfig.displayName[name];
+            if (localGroups.length > 0) {
+              const tag = localGroups[it];
+              displayName = displayName + ' ' + tag;
+            }
+
+            this.data[index].marker.color = color;
+            this.data[index].name = displayName;
+
+            let displayType = 'scatter';
+            let displayMode = 'lines';
 
-          this.data[index].type = displayType;
-          this.data[index].mode = displayMode;
+            const setType = this.dataExplorerWidget.visualizationConfig.displayType[name];
 
+            if (setType !== 'bar') {
+              displayMode = setType;
+            } else {
+              displayType = 'bar';
+            }
+
+            this.data[index].type = displayType;
+            this.data[index].mode = displayMode;
+
+            pastGroups += 1;
+          }
         }
       });
     }
-
   }
 
   refreshView() {
@@ -233,10 +302,15 @@ export class TimeSeriesChartWidgetComponent extends BaseDataExplorerWidget<TimeS
     this.data = [];
 
     this.setShownComponents(true, false, false);
+    this.groupKeeper = {};
     spQueryResults.map((spQueryResult, index) => {
-      this.data = this.data.concat(this.transformData(spQueryResult, spQueryResult.sourceIndex));
+      const res = this.transformData(spQueryResult, spQueryResult.sourceIndex);
+      res.forEach(item => {
+        this.data = this.data.concat(item);
+      });
     });
     this.setShownComponents(false, true, false);
+
   }
 
   handleUpdatedFields(addedFields: DataExplorerField[],