FIDDLE HERE:http://jsfiddle.net/jakelauer/5S8t8/
我需要做什么
循环一系列百分比,加起来达到100%,并将任何低于最小阈值的值设置为某个百分比,然后重新分配剩余的百分比,使它们相对于彼此仍然相同,所以整个范围仍然等于100%。
为什么吗
我正在制作饼图,我需要在饼图上显示每个值,即使它是零值,因此它仍然可以点击。这意味着我需要将低于某个阈值的值设置为更高的值,但我不想丢失较高值的相对比例,并且它们不能超过100%
详情
说我有一系列值:
var range = [190, 2453, 234, 14, 1244, 532, 0, 3, 1999];
我将这些值转换为百分比值,使得范围中的每个值都成为整个范围之和的百分比,从而产生:
[0.02849002849002849, 0.36782126255810466, 0.03508771929824561, 0.002099265257159994, 0.18653471285050233, 0.07977207977207977, 0, 0.000449842555105713, 0.2997450892187734]
我想循环遍历这些百分比,并将它们设置为最小0.02
或2%。这是我到目前为止的代码:
var range = [190, 2453, 234, 14, 1244, 532, 0, 3, 1999];
var minimumPercent = 0.02;
function distributeRangeGivenMinimum(range, min) {
var sum = 0;
for (var i = 0; i < range.length; i++) {
sum += range[i];
}
var percents = [];
for (var i = 0; i < range.length; i++) {
percents.push(range[i] / sum);
}
var amtToSubtract = 0;
var timesUnderMin = 0;
for (var i = 0; i < range.length; i++) {
if (percents[i] < min) {
amtToSubtract += (min - percents[i]);
timesUnderMin++;
percents[i] = min;
}
}
var timesOverMin = percents.length - timesUnderMin;
if (timesOverMin > 0) {
for (var i = 0; i < range.length; i++) {
if (percents[i] > min) {
percents[i] = percents[i] - (amtToSubtract / timesOverMin);
}
}
}
return percents;
}
上述函数的问题在于,虽然它确实完成了重新分配,但如果任何值 高于最小值,则失败(例如,如果其中一个值为0.0201且最小值)在重新分配后,它们最终低于最小值,我不知道如何解决这个问题。
答案 0 :(得分:0)
尝试以下操作(新部分由评论标记):
var range = [190, 2453, 234, 14, 1244, 532, 0, 3, 1999];
var minimumPercent = 0.02;
function distributeRangeGivenMinimum(range, min) {
var sum = 0;
for (var i = 0; i < range.length; i++) {
sum += range[i];
}
var percents = [];
for (var i = 0; i < range.length; i++) {
percents.push(range[i] / sum);
}
var amtToSubtract = 0;
var timesUnderMin = 0;
for (var i = 0; i < range.length; i++) {
if (percents[i] < min) {
amtToSubtract += (min - percents[i]);
timesUnderMin++;
percents[i] = min;
}
}
var timesOverMin = percents.length - timesUnderMin;
// begin new section
do {
var prevTimesOverMin = timesOverMin;
var distSubAmt = amtToSubtract / timesOverMin;
for (var i = 0; i < range.length; i++) {
if (percents[i] > min && percents[i] - distSubAmt < min) {
amtToSubtract -= (percents[i] - min);
timesOverMin--;
percents[i] = min;
}
}
} while (timesOverMin != prevTimesOverMin);
// end new section
if (timesOverMin > 0) {
for (var i = 0; i < range.length; i++) {
if (percents[i] > min) {
percents[i] = percents[i] - (amtToSubtract / timesOverMin);
}
}
}
return percents;
}