浏览代码

Filters - first step

Deltakosh 7 年之前
父节点
当前提交
ac550bb7f3
共有 3 个文件被更改,包括 51 次插入5 次删除
  1. 6 3
      gui/src/3D/charting/barGraph.ts
  2. 14 2
      gui/src/3D/charting/chart.ts
  3. 31 0
      gui/src/3D/charting/dataSeries.ts

+ 6 - 3
gui/src/3D/charting/barGraph.ts

@@ -121,7 +121,10 @@ export class BarGraph extends Chart {
         // Scan data
         let min = Number.MAX_VALUE;
         let max = Number.MIN_VALUE;
-        this._dataSource.data.forEach(entry => {
+
+        const data = this._dataFilters ? this._dataSource.getFilteredData(this._dataFilters) : this._dataSource.data;
+
+        data.forEach(entry => {
             if (min > entry.value) {
                 min = entry.value;
             }
@@ -134,9 +137,9 @@ export class BarGraph extends Chart {
         let ratio = this.maxBarHeight / (max - min);
 
         // We will generate one bar per entry
-        let left = -(this._dataSource.data.length / 2) * (this.barWidth + this.margin) + 1.5 * this._margin;
+        let left = -(data.length / 2) * (this.barWidth + this.margin) + 1.5 * this._margin;
         let index = 0;
-        this._dataSource.data.forEach(entry => {
+        data.forEach(entry => {
 
             var barMesh = this._createBarMesh(this.name + "_box_" + index++, scene);
 

+ 14 - 2
gui/src/3D/charting/chart.ts

@@ -1,10 +1,11 @@
-import { DataSeries } from ".";
 import { Nullable, TransformNode, Scene } from "babylonjs";
+import { DataSeries } from ".";
 
 /** base class for all chart controls*/
 export abstract class Chart {
     protected _dataSource: Nullable<DataSeries>;
     protected _rootNode: TransformNode;
+    protected _dataFilters: {[key: string]: string};
 
     /** Gets or sets the data source used by the graph */
     public get dataSource(): Nullable<DataSeries> {
@@ -21,6 +22,17 @@ export abstract class Chart {
         this.refresh();
     }
 
+    /** Gets the filters applied to data source */
+    public get dataFilters(): {[key: string]: string} {
+        return this._dataFilters;
+    }
+
+    public set dataFilters(filters: {[key: string]: string}) {
+        this._dataFilters = filters;
+
+        this.refresh();
+    }
+
     /** Gets the root node associated with this graph */
     public get rootNode(): TransformNode {
         return this._rootNode;
@@ -30,7 +42,7 @@ export abstract class Chart {
     public name: string; 
 
     /**
-     * Creates a new BarGraph
+     * Creates a new Chart
      * @param name defines the name of the graph
      */
     constructor(name: string, scene?: Scene) {

+ 31 - 0
gui/src/3D/charting/dataSeries.ts

@@ -14,6 +14,37 @@ export class DataSeries {
     /** Gets or sets the list of values (data to display) */
     public data: Array<any>;  
 
+    /**
+     * Apply a list of filters to the data and return a list
+     * @param filters defines the filters to apply
+     * @returns an array containing the filtered data
+     */
+    public getFilteredData(filters: {[key: string]: string}): Array<any> {
+        let filteredData = new Array<any>();
+
+        this.data.forEach(element => {
+            let isValid = false;
+            for (var filter in filters) {
+                if (!filters.hasOwnProperty(filter)) {
+                    continue;
+                }
+
+                var filterValue = filters[filter];
+                isValid = (element[filter] === filterValue);
+
+                if (!isValid) {
+                    break;
+                }
+            }
+
+            if (isValid) {
+                filteredData.push(element);
+            }
+        });
+
+        return filteredData;
+    }
+
     public static CreateFakeData(): DataSeries {
         var series = new DataSeries();
         series.label = "Product #1";