Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Progress] Fix wrong error on validating sum total on multiple progress #758

Merged
merged 9 commits into from
Jun 10, 2019
173 changes: 106 additions & 67 deletions src/definitions/modules/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,40 @@ $.fn.progress = function(parameters) {
return left + right;
}, 0) : 0;
},
/**
* Derive precision for multiple progress with total and values.
*
* This helper dervices a precision that is sufficiently large to show minimum value of multiple progress.
*
* Example1
* - total: 1122
* - values: [325, 111, 74, 612]
* - min ratio: 74/1122 = 0.0659...
* - required precision: 100
*
* Example2
* - total: 10541
* - values: [3235, 1111, 74, 6121]
* - min ratio: 74/10541 = 0.0070...
* - required precision: 1000
*
* @param min A minimum value within multiple values
* @param total A total amount of multiple values
* @returns {number} A precison. Could be 1, 10, 100, ... 1e+10.
*/
derivePrecision: function(min, total) {
var precisionPower = 0
var precision = 1;
var ratio = min / total;
while (precisionPower < 10) {
ratio = ratio * precision;
if (ratio > 1) {
break;
}
precision = Math.pow(10, precisionPower++);
}
return precision;
},
forceArray: function (element) {
return Array.isArray(element)
? element
Expand Down Expand Up @@ -411,52 +445,43 @@ $.fn.progress = function(parameters) {
barWidth: function(values) {
module.debug("set bar width with ", values);
values = module.helper.forceArray(values);
var total = module.helper.sum(values);
if(total > 100) {
module.error(error.tooHigh, total);
}
else if (total < 0) {
module.error(error.tooLow, total);
}
else {
var firstNonZeroIndex = -1;
var lastNonZeroIndex = -1;
var valuesSum = module.helper.sum(values);
var barCounts = $bars.length;
var isMultiple = barCounts > 1;
var percents = values.map(function(value, index) {
var allZero = (index === barCounts - 1 && valuesSum === 0);
var $bar = $($bars[index]);
if (value === 0 && isMultiple && !allZero) {
$bar.css('display', 'none');
} else {
if (isMultiple && allZero) {
$bar.css('background', 'transparent');
}
if (firstNonZeroIndex == -1) {
firstNonZeroIndex = index;
}
lastNonZeroIndex = index;
$bar.css({
display: 'block',
width: value + '%'
});
var firstNonZeroIndex = -1;
var lastNonZeroIndex = -1;
var valuesSum = module.helper.sum(values);
var barCounts = $bars.length;
var isMultiple = barCounts > 1;
var percents = values.map(function(value, index) {
var allZero = (index === barCounts - 1 && valuesSum === 0);
var $bar = $($bars[index]);
if (value === 0 && isMultiple && !allZero) {
$bar.css('display', 'none');
} else {
if (isMultiple && allZero) {
$bar.css('background', 'transparent');
}
return parseInt(value, 10);
});
values.forEach(function(_, index) {
var $bar = $($bars[index]);
if (firstNonZeroIndex == -1) {
firstNonZeroIndex = index;
}
lastNonZeroIndex = index;
$bar.css({
borderTopLeftRadius: index == firstNonZeroIndex ? '' : 0,
borderBottomLeftRadius: index == firstNonZeroIndex ? '' : 0,
borderTopRightRadius: index == lastNonZeroIndex ? '' : 0,
borderBottomRightRadius: index == lastNonZeroIndex ? '' : 0
display: 'block',
width: value + '%'
});
}
return parseInt(value, 10);
});
values.forEach(function(_, index) {
var $bar = $($bars[index]);
$bar.css({
borderTopLeftRadius: index == firstNonZeroIndex ? '' : 0,
borderBottomLeftRadius: index == firstNonZeroIndex ? '' : 0,
borderTopRightRadius: index == lastNonZeroIndex ? '' : 0,
borderBottomRightRadius: index == lastNonZeroIndex ? '' : 0
});
$module
.attr('data-percent', percents)
;
}
});
$module
.attr('data-percent', percents)
;
},
duration: function(duration) {
duration = duration || settings.duration;
Expand All @@ -478,34 +503,48 @@ $.fn.progress = function(parameters) {
: percent
;
});
// round display percentage
percents = percents.map(function(percent){
return (settings.precision > 0)
? Math.round(percent * (10 * settings.precision)) / (10 * settings.precision)
: Math.round(percent)
;
});
module.percent = percents;
if( !module.has.total() ) {
module.value = percents.map(function(percent){
return (settings.precision > 0)
? Math.round( (percent / 100) * module.total * (10 * settings.precision)) / (10 * settings.precision)
: Math.round( (percent / 100) * module.total * 10) / 10
;
// Sum before rouding since sum of rounded may have error though sum of actual is fine
var total = module.helper.sum(percents);
if (total > 100) {
module.error(error.tooHigh, total);
} else if (total < 0) {
module.error(error.tooLow, total);
} else {
var autoPrecision = settings.precision > 0
? settings.precision
: percents.length > 1 && module.has.total()
? module.helper.derivePrecision(Math.min.apply(null, module.value), module.total)
: undefined;

// round display percentage
percents = percents.map(function (percent) {
return (autoPrecision > 0)
? Math.round(percent * (10 * autoPrecision)) / (10 * autoPrecision)
: Math.round(percent)
;
});
if(settings.limitValues) {
module.value = module.value.map(function(value) {
return (value > 100)
? 100
: (module.value < 0)
? 0
: module.value;
module.percent = percents;
if (!module.has.total()) {
module.value = percents.map(function (percent) {
return (autoPrecision > 0)
? Math.round((percent / 100) * module.total * (10 * autoPrecision)) / (10 * autoPrecision)
: Math.round((percent / 100) * module.total * 10) / 10
;
});
if (settings.limitValues) {
module.value = module.value.map(function (value) {
return (value > 100)
? 100
: (module.value < 0)
? 0
: module.value;
});
}
}
module.set.barWidth(percents);
module.set.labelInterval();
module.set.labels();
}
module.set.barWidth(percents);
module.set.labelInterval();
module.set.labels();
settings.onChange.call(element, percents, module.value, module.total);
},
labelInterval: function() {
Expand Down Expand Up @@ -550,7 +589,7 @@ $.fn.progress = function(parameters) {
: module.helper.sum(module.percent)
;
if(percent === 100) {
if(settings.autoSuccess && !(module.is.warning() || module.is.error() || module.is.success())) {
if(settings.autoSuccess && $bars.length === 1 && !(module.is.warning() || module.is.error() || module.is.success())) {
module.set.success();
module.debug('Automatically triggering success at 100%');
}
Expand Down