目前我正在尝试将数据缩放到适合两个数字的大小。我的代码如下:
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;
}
现在我跑的时候:
console.log("Min = "+min);
console.log("Maz = "+max);
和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
第一次,第二次,我得到:
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
必须注意的是,dataScaling
函数(db
)的输入在每次运行程序时都会不断变化。我不明白的是,数据超出了范围。我用这个函数调用函数:
dataScaling(initialArray,20,2,d.FIELD1);
答案 0 :(得分:2)
我不确定你在寻找什么,但我认为这是你的解决方案。
/**
* 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));
&#13;
答案 1 :(得分:0)
您永远不会说或检查d.FIELD1
(即value
参数)是否在min
和max
内。
BTW,db[0].FIELD1
怎么办?