From 26708dc26d361b09763ebd53978597f136cb7277 Mon Sep 17 00:00:00 2001 From: Arjun Choudhary Date: Fri, 25 Nov 2022 16:02:28 +0530 Subject: [PATCH] fix: backported fp rounding fix --- src/js/utils/draw.js | 4 +++- src/js/utils/helpers.js | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/js/utils/draw.js b/src/js/utils/draw.js index 5ef588d..7640eb0 100644 --- a/src/js/utils/draw.js +++ b/src/js/utils/draw.js @@ -4,7 +4,7 @@ import { shortenLargeNumber, getSplineCurvePointsStr, } from "./draw-utils"; -import { getStringWidth, isValidNumber } from "./helpers"; +import { getStringWidth, isValidNumber, round } from "./helpers"; import { DOT_OVERLAY_SIZE_INCR } from "./constants"; export const AXIS_TICK_LENGTH = 6; @@ -485,6 +485,8 @@ export function yLine(y, label, width, options = {}) { x1 += options.offset; x2 += options.offset; + if (typeof label === "number") label = round(label); + return makeHoriLine(y, label, x1, x2, { className: options.className, lineType: options.lineType, diff --git a/src/js/utils/helpers.js b/src/js/utils/helpers.js index 0ce5d71..6062ea3 100644 --- a/src/js/utils/helpers.js +++ b/src/js/utils/helpers.js @@ -141,3 +141,12 @@ export function deepClone(candidate) { return cloned; } +/** + * 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"); +}