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

Round insulin value in treatment tooltip #6776

Merged
merged 3 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ function init (client, d3) {
var glucose = treatment.glucose;
if (client.settings.units != client.ddata.profile.getUnits()) {
glucose *= (client.settings.units === 'mmol' ? (1 / consts.MMOL_TO_MGDL) : consts.MMOL_TO_MGDL);
var decimals = (client.settings.units === 'mmol' ? 10 : 1);
const decimals = (client.settings.units === 'mmol' ? 10 : 1);

glucose = Math.round(glucose * decimals) / decimals;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not refactor this to use the function as well?

}
Expand All @@ -641,7 +641,7 @@ function init (client, d3) {
(treatment.protein ? '<strong>' + translate('Protein') + ':</strong> ' + treatment.protein + '<br/>' : '') +
(treatment.fat ? '<strong>' + translate('Fat') + ':</strong> ' + treatment.fat + '<br/>' : '') +
(treatment.absorptionTime > 0 ? '<strong>' + translate('Absorption Time') + ':</strong> ' + (Math.round(treatment.absorptionTime / 60.0 * 10) / 10) + 'h' + '<br/>' : '') +
(treatment.insulin ? '<strong>' + translate('Insulin') + ':</strong> ' + treatment.insulin + '<br/>' : '') +
(treatment.insulin ? '<strong>' + translate('Insulin') + ':</strong> ' + utils.toRoundedStr(treatment.insulin, 2) + '<br/>' : '') +
(treatment.enteredinsulin ? '<strong>' + translate('Combo Bolus') + ':</strong> ' + treatment.enteredinsulin + 'U, ' + treatment.splitNow + '% : ' + treatment.splitExt + '%, ' + translate('Duration') + ': ' + treatment.duration + '<br/>' : '') +
(treatment.glucose ? '<strong>' + translate('BG') + ':</strong> ' + glucose + (treatment.glucoseType ? ' (' + translate(treatment.glucoseType) + ')' : '') + '<br/>' : '') +
(treatment.enteredBy ? '<strong>' + translate('Entered By') + ':</strong> ' + treatment.enteredBy + '<br/>' : '') +
Expand Down
2 changes: 1 addition & 1 deletion lib/report_plugins/daytoday.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ daytoday.report = function report_daytoday (datastorage, sorteddaystoshow, optio
}

if (treatment.insulin && options.insulin) {
var dataLabel = client.utils.toFixedMin(treatment.insulin,2)+ 'U';
var dataLabel = client.utils.toRoundedStr(treatment.insulin, 2)+ 'U';
context.append('rect')
.attr('y', yInsulinScale(treatment.insulin))
.attr('height', chartHeight - yInsulinScale(treatment.insulin))
Expand Down
10 changes: 7 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ function init(ctx) {
}
};

utils.toFixedMin = function toFixedMin(value,digits) {
/**
* Round the number to maxDigits places, return a string
* that truncates trailing zeros
*/
utils.toRoundedStr = function toRoundedStr (value, maxDigits) {
if (!value) {
return '0';
}
var mult = Math.pow(10,digits);
var fixed = Math.sign(value) * Math.round(Math.abs(value)*mult) / mult;
const mult = Math.pow(10, maxDigits);
const fixed = Math.sign(value) * Math.round(Math.abs(value)*mult) / mult;
if (isNaN(fixed)) return '0';
return String(fixed);
};
Expand Down
22 changes: 11 additions & 11 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ describe('utils', function ( ) {

it('format numbers short', function () {
var undef;
utils.toFixedMin(3.345, 2).should.equal('3.35');
utils.toFixedMin(5.499999999, 0).should.equal('5');
utils.toFixedMin(5.499999999, 1).should.equal('5.5');
utils.toFixedMin(5.499999999, 3).should.equal('5.5');
utils.toFixedMin(123.45, -2).should.equal('100');
utils.toFixedMin(-0.001, 2).should.equal('0');
utils.toFixedMin(-2.47, 1).should.equal('-2.5');
utils.toFixedMin(-2.44, 1).should.equal('-2.4');
utils.toRoundedStr(3.345, 2).should.equal('3.35');
utils.toRoundedStr(5.499999999, 0).should.equal('5');
utils.toRoundedStr(5.499999999, 1).should.equal('5.5');
utils.toRoundedStr(5.499999999, 3).should.equal('5.5');
utils.toRoundedStr(123.45, -2).should.equal('100');
utils.toRoundedStr(-0.001, 2).should.equal('0');
utils.toRoundedStr(-2.47, 1).should.equal('-2.5');
utils.toRoundedStr(-2.44, 1).should.equal('-2.4');

utils.toFixedMin(undef, 2).should.equal('0');
utils.toFixedMin(null, 2).should.equal('0');
utils.toFixedMin('text', 2).should.equal('0');
utils.toRoundedStr(undef, 2).should.equal('0');
utils.toRoundedStr(null, 2).should.equal('0');
utils.toRoundedStr('text', 2).should.equal('0');
});

it('merge date and time', function () {
Expand Down