fix: floating point rounding fixes
This commit is contained in:
parent
e2d7ce8b21
commit
5d2c141f14
@ -1,5 +1,5 @@
|
|||||||
import { getBarHeightAndYAttr, truncateString, shortenLargeNumber, getSplineCurvePointsStr } from './draw-utils';
|
import { getBarHeightAndYAttr, truncateString, shortenLargeNumber, getSplineCurvePointsStr } from './draw-utils';
|
||||||
import { getStringWidth, isValidNumber } from './helpers';
|
import { getStringWidth, isValidNumber, round } from './helpers';
|
||||||
import { DOT_OVERLAY_SIZE_INCR } from './constants';
|
import { DOT_OVERLAY_SIZE_INCR } from './constants';
|
||||||
|
|
||||||
export const AXIS_TICK_LENGTH = 6;
|
export const AXIS_TICK_LENGTH = 6;
|
||||||
@ -389,6 +389,8 @@ export function yLine(y, label, width, options={}) {
|
|||||||
x1 += options.offset;
|
x1 += options.offset;
|
||||||
x2 += options.offset;
|
x2 += options.offset;
|
||||||
|
|
||||||
|
if (typeof label === "number") label = round(label);
|
||||||
|
|
||||||
return makeHoriLine(y, label, x1, x2, {
|
return makeHoriLine(y, label, x1, x2, {
|
||||||
className: options.className,
|
className: options.className,
|
||||||
lineType: options.lineType,
|
lineType: options.lineType,
|
||||||
|
|||||||
@ -98,7 +98,7 @@ export function getPositionByAngle(angle, radius) {
|
|||||||
* @param {object} candidate Candidate to test
|
* @param {object} candidate Candidate to test
|
||||||
* @param {Boolean} nonNegative flag to treat negative number as invalid
|
* @param {Boolean} nonNegative flag to treat negative number as invalid
|
||||||
*/
|
*/
|
||||||
export function isValidNumber(candidate, nonNegative=false) {
|
export function isValidNumber(candidate, nonNegative = false) {
|
||||||
if (Number.isNaN(candidate)) return false;
|
if (Number.isNaN(candidate)) return false;
|
||||||
else if (candidate === undefined) return false;
|
else if (candidate === undefined) return false;
|
||||||
else if (!Number.isFinite(candidate)) return false;
|
else if (!Number.isFinite(candidate)) return false;
|
||||||
@ -115,3 +115,12 @@ export function round(d) {
|
|||||||
// https://www.jacklmoore.com/notes/rounding-in-javascript/
|
// https://www.jacklmoore.com/notes/rounding-in-javascript/
|
||||||
return Number(Math.round(d + 'e4') + 'e-4');
|
return Number(Math.round(d + 'e4') + 'e-4');
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Round a number to the closes precision, max max precision 4
|
||||||
|
* @param {Number} d Any Number
|
||||||
|
*/
|
||||||
|
export function round(d) {
|
||||||
|
// https://floating-point-gui.de/
|
||||||
|
// https://www.jacklmoore.com/notes/rounding-in-javascript/
|
||||||
|
return Number(Math.round(d + 'e4') + 'e-4');
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user