This is an automated email from the ASF dual-hosted git repository. sushuang pushed a commit to branch PR/plainheart_fix/alignTicks-precision in repository https://gitbox.apache.org/repos/asf/echarts.git
commit bdec91e39cffe42ff3f8883e8aa01d3cbf969905 Author: 100pah <[email protected]> AuthorDate: Wed Feb 18 01:12:32 2026 +0800 refactor: Remove the default value of number round due to its unreasonable and error-prone for small float number. --- src/chart/gauge/GaugeView.ts | 4 ++-- src/chart/helper/Line.ts | 4 +++- src/coord/Axis.ts | 8 ++++---- src/export/api/number.ts | 2 +- src/util/number.ts | 25 +++++++++++++++++++------ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/chart/gauge/GaugeView.ts b/src/chart/gauge/GaugeView.ts index 1df6458a1..d7465e8a8 100644 --- a/src/chart/gauge/GaugeView.ts +++ b/src/chart/gauge/GaugeView.ts @@ -22,7 +22,7 @@ import * as graphic from '../../util/graphic'; import { setStatesStylesFromModel, toggleHoverEmphasis } from '../../util/states'; import {createTextStyle, setLabelValueAnimation, animateLabelValue} from '../../label/labelStyle'; import ChartView from '../../view/Chart'; -import {parsePercent, round, linearMap} from '../../util/number'; +import {parsePercent, round, linearMap, DEFAULT_PRECISION_FOR_ROUNDING_ERROR} from '../../util/number'; import GaugeSeriesModel, { GaugeDataItemOption } from './GaugeSeries'; import GlobalModel from '../../model/Global'; import ExtensionAPI from '../../core/ExtensionAPI'; @@ -272,7 +272,7 @@ class GaugeView extends ChartView { const distance = labelModel.get('distance') + splitLineDistance; const label = formatLabel( - round(i / splitNumber * (maxVal - minVal) + minVal), + round(i / splitNumber * (maxVal - minVal) + minVal, DEFAULT_PRECISION_FOR_ROUNDING_ERROR), labelModel.get('formatter') ); const autoColor = getColor(i / splitNumber); diff --git a/src/chart/helper/Line.ts b/src/chart/helper/Line.ts index 07748206a..647b5c1d3 100644 --- a/src/chart/helper/Line.ts +++ b/src/chart/helper/Line.ts @@ -303,7 +303,9 @@ class Line extends graphic.Group { defaultText: (rawVal == null ? lineData.getName(idx) : isFinite(rawVal) - ? round(rawVal) + // PENDING: the `rawVal` is not supposed to be rounded. But this rounding was introduced + // in the early stages, so changing it would likely be breaking. + ? round(rawVal, 10) : rawVal) + '' }); const label = this.getTextContent() as InnerLineLabel; diff --git a/src/coord/Axis.ts b/src/coord/Axis.ts index 6f38a191b..af3721ee1 100644 --- a/src/coord/Axis.ts +++ b/src/coord/Axis.ts @@ -28,12 +28,12 @@ import { createAxisLabelsComputingContext, } from './axisTickLabelBuilder'; import Scale, { ScaleGetTicksOpt } from '../scale/Scale'; -import { DimensionName, NullUndefined, ScaleDataValue, ScaleTick } from '../util/types'; +import { DimensionName, ScaleDataValue, ScaleTick } from '../util/types'; import OrdinalScale from '../scale/Ordinal'; import Model from '../model/Model'; import { AxisBaseOption, CategoryAxisBaseOption, OptionAxisType } from './axisCommonTypes'; import { AxisBaseModel } from './AxisBaseModel'; -import { isIntervalOrTimeScale, isOrdinalScale } from '../scale/helper'; +import { isOrdinalScale } from '../scale/helper'; const NORMALIZED_EXTENT = [0, 1] as [number, number]; @@ -351,8 +351,8 @@ function fixOnBandTicksCoords( function littleThan(a: number, b: number): boolean { // Avoid rounding error cause calculated tick coord different with extent. // It may cause an extra unnecessary tick added. - a = round(a); - b = round(b); + a = round(a, 10); + b = round(b, 10); return inverse ? a > b : a < b; } } diff --git a/src/export/api/number.ts b/src/export/api/number.ts index 40b130d25..835ca1563 100644 --- a/src/export/api/number.ts +++ b/src/export/api/number.ts @@ -19,7 +19,7 @@ export { linearMap, - round, + roundLegacy as round, asc, getPrecision, getPrecisionSafe, diff --git a/src/util/number.ts b/src/util/number.ts index c7484e04c..e5fa52256 100644 --- a/src/util/number.ts +++ b/src/util/number.ts @@ -36,6 +36,9 @@ const RADIAN_EPSILON = 1e-4; // the ES3~ES6 spec (0 <= n <= 20) for backward and cross-platform compatibility. const TO_FIXED_SUPPORTED_PRECISION_MAX = 20; +// For rounding error like `2.9999999999999996`, with respect to IEEE754 64bit float. +export const DEFAULT_PRECISION_FOR_ROUNDING_ERROR = 14; + function _trim(str: string): string { return str.replace(/^\s+|\s+$/g, ''); } @@ -190,14 +193,14 @@ export function parsePositionSizeOption(option: unknown, percentBase: number, pe * Since: ` quantityExponent(val) = floor(log10(abs(val))) ` * Hence: ` precision ~= floor(EXP52B10 - 1 - quantityExponent(val)) */ -export function round(x: number | string, precision?: number): number; +export function round(x: number | string, precision: number): number; export function round(x: number | string, precision: number, returnStr: false): number; export function round(x: number | string, precision: number, returnStr: true): string; -export function round(x: number | string, precision?: number, returnStr?: boolean): string | number { - if (precision == null) { - // FIXME: the default precision should not be provided, since there is no universally adaptable - // precision. The caller need to input a precision according to the scenarios. - precision = 10; +export function round(x: number | string, precision: number, returnStr?: boolean): string | number { + if (__DEV__) { + // NOTICE: We should not provided a default precision, since there is no universally adaptable + // precision. The caller need to input a precision according to the scenarios. + zrUtil.assert(precision != null); } if (isNaN(precision)) { // precision utils (such as getAcceptableTickPrecision) may return NaN. @@ -210,6 +213,16 @@ export function round(x: number | string, precision?: number, returnStr?: boolea return (returnStr ? x : +x); } +export function roundLegacy(x: number | string, precision?: number): number; +export function roundLegacy(x: number | string, precision: number, returnStr: false): number; +export function roundLegacy(x: number | string, precision: number, returnStr: true): string; +export function roundLegacy(x: number | string, precision?: number, returnStr?: boolean): string | number { + if (precision == null) { + precision = 10; + } + return round(x, precision, returnStr as any); +} + /** * Inplacd asc sort arr. * The input arr will be modified. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
