import { scaleLinear , line } from 'd3' ; export const getDimensions = ( props , state ) : NBaseLineChart . Dimensions => { if ( state . chartRef ) { const parentNode = state . chartRef ?. parentNode ; const boundingRect = parentNode ?. getBoundingClientRect ( ) || { width : 0 , height : 0 , } ; const dimensions = { width : props . width || boundingRect . width + 10 , height : props . height || boundingRect . height , } ; return dimensions ; } return state . dimensions ; } ; export const setDimensions = ( props , state , actions ) : void => { const { x , y } = props . dataKeys ; const d = getDimensions ( props , state ) ; const { width , height } = state . dimensions ; if ( d && ( d . width !== width || d . height !== height ) ) { actions . setDimensions ( d ) ; actions . setMouse ( { datum : { [ x ] : 0 , [ y ] : 0 } } ) ; actions . setMouse ( { datum : null } ) ; if ( props . onResize ) { props . onResize ( d ) ; } } } ; export const computeValues = ( props , state , actions , cb ? ) : void => { const { data , dataKeys , margin , domains } = props ; const d = getDimensions ( props , state ) ; if ( d ) { const height = d . height - margin ! . bottom - margin ! . top ; const width = d . width - margin ! . right - margin ! . left ; const [ xData , yData ] = data . reduce ( ( out , datum ) => { const x = datum [ dataKeys . x ] ; const y = datum [ dataKeys . y ] || 0 ; return [ [ ... out [ 0 ] , x ] , [ ... out [ 1 ] , y ] , ] ; } , [ [ ] , [ ] ] ) ; const _domains = { x : domains ?. x || [ Math . min ( ... xData ) , Math . max ( ... xData ) ] , y : domains ?. y || [ Math . min ( ... yData ) , Math . max ( ... yData ) ] , } ; const x = scaleLinear ( ) . domain ( _domains . x ) . range ( [ 0 , width ] ) ; const y = scaleLinear ( ) . domain ( _domains . y ) . range ( [ height - 80 , 0 ] ) ; const _line = line < NBaseLineChart . Datum > ( ) . x ( ( datum ) => x ( datum [ dataKeys . x ] ) ) . y ( ( datum ) => y ( datum [ dataKeys . y ] ) ) ; actions . setChart ( { x , y , line : _line , domains : _domains } , cb ) ; } } ;