Skip to content

Scale.NiceNum

Returns a "nice" rounded number close to the given value. Used for choosing human-friendly tick intervals and axis bounds

DaxLib.SVG.Scale.NiceNum( rawValue, roundMode )
Parameter Type Required Description
rawValue NUMERIC VAL The raw value to round to a nice number
roundMode BOOLEAN Optional: TRUE for rounding to nearest nice number (for intervals), FALSE for ceiling to next nice number (for range extents). Defaults to TRUE.

NUMERIC A human-friendly number (1, 2, 2.5, 5, or 10 scaled by the appropriate power of 10)

DaxLib.SVG.Scale.NiceNum( 17.3, TRUE )
// Returns 20 (rounds to nearest nice number)

DaxLib.SVG.Scale.NiceNum( 17.3, FALSE )
// Returns 20 (ceiling to next nice number for range extents)

DaxLib.SVG.Scale.NiceNum( 0.73, TRUE )
// Returns 1
function 'DaxLib.SVG.Scale.NiceNum' =
        (
            rawValue: NUMERIC VAL,
            roundMode: BOOLEAN
        ) =>

            VAR _Value = ABS( rawValue )
            VAR _RoundMode = IF( ISBLANK( roundMode ), TRUE(), roundMode )
            VAR _Exponent = IF( _Value = 0, 0, INT( LOG10( _Value ) ) )
            VAR _Fraction = IF( _Value = 0, 0, _Value / POWER( 10, _Exponent ) )
            VAR _NiceFraction =
                IF(
                    _Value = 0,
                    0,
                    IF(
                        _RoundMode,
                        IF( _Fraction < 1.5, 1, IF( _Fraction < 3, 2, IF( _Fraction < 7, 5, 10 ) ) ),
                        IF( _Fraction <= 1, 1, IF( _Fraction <= 2, 2, IF( _Fraction <= 5, 5, 10 ) ) )
                    )
                )

            RETURN
                _NiceFraction * POWER( 10, _Exponent )