(
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.
)
| 337 | * not be covered until necessary. |
| 338 | */ |
| 339 | export 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 |
no outgoing calls
no test coverage detected
searching dependent graphs…