简体   繁体   English

为什么这种缩放不起作用?

[英]Why isn't this scaling working?

Currently I am trying to scale a data to a size to fit between two numbers. 目前,我正在尝试将数据缩放到适合两个数字的大小。 My code is as below: 我的代码如下:

function dataScaling(db,rangeMax,rangeMin,value) {

  var min = db[1].FIELD1;
  var max = min;

  for (var i=2; i < db.length; i++) {
    if (db[i].FIELD1 < min) {min = db[i].FIELD1;}
    if (db[i].FIELD1 > max) {max = db[i].FIELD1;}
  }

  var percent = (value - min) / (max - min);
  var answer = percent * (rangeMax - rangeMin) + rangeMin;
  return answer;

}

Now when I run: 现在,当我运行时:

console.log("Min = "+min);
console.log("Maz = "+max);

and if (answer > rangeMax) {console.log("Value is Too BIG: "+answer );} , I get: 并且if (answer > rangeMax) {console.log("Value is Too BIG: "+answer );} ,我得到:

Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:171 Value is Too BIG: 57.38461538461539
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:171 Value is Too BIG: 49.07692307692308
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:171 Value is Too BIG: 29.692307692307693
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000
index.js:166 Min = 0
index.js:167 Maz = 6500000

The very first time and for the second time, I get: 第一次,第二次,我得到:

index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:171 Value is Too BIG: 139.14285714285714
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:171 Value is Too BIG: 96.28571428571429
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:171 Value is Too BIG: 87.71428571428571
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000
index.js:166 Min = 0
index.js:167 Maz = 21000000

It must be noted that the input to dataScaling function ( db ) is constantly changing every time I run the program. 必须注意,每次我运行该程序时, dataScaling函数( db )的输入dataScaling不断变化。 What I don't understand, is the data exceeding the ranges. 我不明白的是,数据超出了范围。 I call the function using this: 我用这个调用函数:

dataScaling(initialArray,20,2,d.FIELD1);

I'm not sure what you're looking for, but I think this is your solution. 我不确定您要寻找什么,但是我认为这是您的解决方案。

 /** * Re-maps a number from one range to another. * @param {number} value Value to be converted. * @param {number} min1 Lower bound of the value's current range. * @param {number} max1 Upper bound of the value's current range. * @param {number} min2 Lower bound of the value's target range. * @param {number} max2 Upper bound of the value's target range. * @returns {number} */ function Map(value, min1, max1, min2, max2) { return ((value - min1) / (max1 - min1)) * (max2 - min2) + min2; } // Lets say this is percentiles let foo = 25; // You want the percentiles to a scale from 0 to 2 let doo = Map(foo, 0, 100, 0, 2) console.log(doo); // From the scale 0-2 back to percentiles. console.log(Map(doo, 0, 2, 0, 100)); 

You never say or check that d.FIELD1 (ie value argument) is within min and max . 您永远不会说或检查d.FIELD1 (即value参数)是否在minmax

BTW, what about db[0].FIELD1 ? 顺便说一句, db[0].FIELD1呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM