import { select } from 'd3-selection' ; import { TREND_LINE_DASH_ARRAY , TREND_LINE_STROKE_WIDTH } from './trendConfig' ; import { LinearRegressionResult , RenderTrendLinesOptions } from './types' ; import { getTrendValue } from './trendAnalysis' ; import { ViewPort } from '../../../../utils/dataTypes' ; import { TREND_TYPE } from '../../../../utils/dataConstants' ; const getLinearPathCommand = ( { trendResult , size : { width , height } , viewport : { start , end , yMin , yMax } , } ) => { const startY = Math . round ( height - ( ( getTrendValue ( trendResult , start . getTime ( ) ) - yMin ) * height ) / ( yMax - yMin ) ) ; const endY = Math . round ( height - ( ( getTrendValue ( trendResult , end . getTime ( ) ) - yMin ) * height ) / ( yMax - yMin ) ) ; return `M 0 ${ startY } L ${ width } ${ endY }` ; } ; export const renderTrendLines = ( { container , viewport , size : { width , height } , dataStreams , trendResults , } ) : void => { const linearPathCommands = [ ] ; trendResults . forEach ( trendResult => { const dataStream = dataStreams . find ( elt => elt . id === trendResult . dataStreamId ) ; if ( dataStream ) { switch ( trendResult . type ) { case TREND_TYPE . LINEAR : linearPathCommands . push ( { color : trendResult . color || dataStream . color || 'black' , command : getLinearPathCommand ( { trendResult , size : { width , height } , viewport , } ) , } ) ; break ; default : console . error ( `Unable to render trend line for trend type '${ trendResult . type }'.` ) ; } } } ) ; const linearSelection = select ( container ) . selectAll ( 'path.linear-regression' ) . data ( linearPathCommands ) ; linearSelection . enter ( ) . append ( 'path' ) . attr ( 'class' , 'linear-regression' ) . attr ( 'stroke' , data => data . color ) . attr ( 'stroke-dasharray' , TREND_LINE_DASH_ARRAY ) . attr ( 'stroke-linecap' , 'round' ) . attr ( 'stroke-width' , TREND_LINE_STROKE_WIDTH ) . attr ( 'd' , data => data . command ) ; linearSelection . attr ( 'stroke' , data => data . color ) . attr ( 'd' , data => data . command ) ; linearSelection . exit ( ) . remove ( ) ; } ;