浏览代码

✨ spread the index on [0,1] interval

jmacura 4 年之前
父节点
当前提交
0013e383dd
共有 1 个文件被更改,包括 19 次插入9 次删除
  1. 19 9
      src/adjuster/adjuster.service.ts

+ 19 - 9
src/adjuster/adjuster.service.ts

@@ -68,7 +68,7 @@ export class AdjusterService {
           }),
         })
         .toPromise()
-        .then((attractivenessData: any) => {
+        .then((attractivenessData: any[]) => {
           console.log(attractivenessData);
           //this.attractivity = attractivenessData;
           /*let max = 0;
@@ -77,13 +77,21 @@ export class AdjusterService {
               max = a.aggregate;
             }
           });*/
+          // Spread the 'aggregate' value between 0 and 1
+          const min = attractivenessData.reduce((a, b) =>
+            a.aggregate < b.aggregate ? a : b
+          ).aggregate;
           const max = attractivenessData.reduce((a, b) =>
             a.aggregate > b.aggregate ? a : b
           ).aggregate;
-          const normalizer = 1 / max;
+          const coefficient = 1 / (max - min);
+          const constant = -min * coefficient;
           attractivenessData.forEach((a) => {
-            a.aggregate *= normalizer;
+            a.aggregate *= coefficient;
+            a.aggregate += constant;
           });
+          console.log(attractivenessData);
+          // Store relation between region and its data in a hash-table-like structure
           const codeRecordRelations = {};
           attractivenessData.forEach((a) => {
             codeRecordRelations[a.code] = a;
@@ -99,12 +107,14 @@ export class AdjusterService {
               // NOTE: Do NOT add triple equal sign!
               (item) => item['code'] == feature.get('nationalCode')
             );*/
-            if (!featureData && errs < 20) {
-              errs++;
-              console.warn(
-                `No data for feature ${feature.get('nationalCode')}`
-              );
-              console.log(feature);
+            if (!featureData) {
+              if (errs < 20) {
+                errs++;
+                console.warn(
+                  `No data for feature ${feature.get('nationalCode')}`
+                );
+                console.log(feature);
+              }
               return;
             }
             logs++;