MCPcopy
hub / github.com/apache/echarts / getAcceptableTickPrecision

Function getAcceptableTickPrecision

src/util/number.ts:339–369  ·  view source on GitHub ↗
(
    dataExtent: number[],
    // Typically, `Math.abs(pixelExtent[1] - pixelExtent[0])`.
    pxSpan: number,
    // By default, `1`.
    pxDiffAcceptable: number | NullUndefined
    // Return a precision >= 0
    // This precision can be used in method `round`.
    // Return `NaN` for edge case or illegal inputs. Callers need to handle that.
)

Source from the content-addressed store, hash-verified

337 * not be covered until necessary.
338 */
339export function getAcceptableTickPrecision(
340 dataExtent: number[],
341 // Typically, `Math.abs(pixelExtent[1] - pixelExtent[0])`.
342 pxSpan: number,
343 // By default, `1`.
344 pxDiffAcceptable: number | NullUndefined
345 // Return a precision >= 0
346 // This precision can be used in method `round`.
347 // Return `NaN` for edge case or illegal inputs. Callers need to handle that.
348): number {
349 const dataSpan = mathAbs(dataExtent[1] - dataExtent[0]);
350 if (!isFinite(dataSpan) || dataSpan === 0) {
351 return NaN;
352 }
353 // Formula for choosing an acceptable precision:
354 // Let `pxDiff = abs(dataSpan - round(dataSpan, precision))`.
355 // We require `pxDiff <= dataSpan * pxDiffAcceptable / pxSpan`.
356 // Consider the nature of "round", the max `pxDiff` is: `pow(10, -precision) / 2`,
357 // Hence: `pow(10, -precision) / 2 <= dataSpan * pxDiffAcceptable / pxSpan`
358 // Hence: `precision >= -log10(2 * dataSpan * pxDiffAcceptable / pxSpan)`
359 const dataExp2 = mathLog(2 * mathAbs(pxDiffAcceptable || 1) * mathAbs(dataSpan)) / mathLN10;
360 const pxExp = mathLog(mathAbs(pxSpan)) / mathLN10;
361 // PENDING: Rounding error generally does not matter; do not fix it before `Math.ceil`
362 // until bad case occur.
363 let precision = mathMax(0, mathCeil(-dataExp2 + pxExp));
364 if (!isFinite(precision)) {
365 // If dataSpan is near `0`, the result should not be too big or even `Infinity`.
366 precision = NaN;
367 }
368 return precision;
369}
370
371/**
372 * Get a data of given precision, assuring the sum of percentages

Callers 4

number.test.tsFile · 0.90
setBatchMethod · 0.90
calculateDataWindowMethod · 0.90
scaleCalcAlignFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…