upsetjs import { NumericScaleFactory , NumericScaleTick , TickOptions , genTicks , checkValues } from './numeric' ; function toStr ( v ) { return v . toLocaleString ( ) ; } internal export function niceFactors ( max , maxCount = 11 ) { const digits = Math . max ( 0 , Math . floor ( Math . log10 ( max ) - 0.5 ) ) ; const factor = Math . pow ( 10 , digits ) ; const factors = [ 1 , 2 , 5 ] ; const r = factors . map ( ( f ) => f * factor ) ; if ( digits > 0 ) { r . unshift ( factors [ factors . length - 1 ] * Math . pow ( 10 , digits - 1 ) ) ; } r . push ( factors [ 0 ] * Math . pow ( 10 , digits + 1 ) ) ; const lower = Math . ceil ( max / maxCount ) ; return r . filter ( ( d ) => d >= lower && d <= max ) ; } internal export function range ( max , inc = 1 ) { const values = [ ] ; for ( let v = 0 ; v <= max ; v += inc ) { values . push ( v ) ; } return values ; } function distributeTicks ( max , maxCount , scale , heightPerTick ) { if ( maxCount <= 0 ) { return [ ] ; } const factors = niceFactors ( max , maxCount ) ; for ( let i = 0 ; i < factors . length ; i ++ ) { const values = range ( max , factors [ i ] ) ; const r = checkValues ( values , scale , heightPerTick , max , toStr ) ; if ( r ) { return r ; } } return genTicks ( [ 0 , max ] , toStr ) ; } export const linearScale = ( max , range , options ) => { const size = range [ 1 ] - range [ 0 ] ; const domain = max ; const scale = ( v ) => { const cv = Math . max ( 0 , Math . min ( v , domain ) ) ; const n = cv / domain ; return range [ 0 ] + n * size ; } ; scale . ticks = ( count = 10 ) => { if ( options . orientation === 'vertical' ) { const heightPerTick = Math . ceil ( options . fontSizeHint * 1.4 ) ; return distributeTicks ( max , count + 1 , scale , ( ) => heightPerTick ) ; } const widthPerChar = options . fontSizeHint / 1.4 ; return distributeTicks ( max , count + 1 , scale , ( v ) => Math . ceil ( toStr ( v ) . length * widthPerChar ) ) ; } ; scale . tickFormat = ( ) => toStr ; return scale ; } ;