import { DateProfileGenerator } from '../DateProfileGenerator' ; import { DateMarker , addDays , diffDays } from '../datelib/marker' ; import { DateRange } from '../datelib/date-range' ; export interface DaySeriesSeg { firstIndex : number ; lastIndex : number ; isStart : boolean ; isEnd : boolean ; } export class DaySeriesModel { cnt : number ; dates : DateMarker [ ] ; indices : number [ ] ; constructor ( range , dateProfileGenerator ) { let date = range . start ; let { end } = range ; let indices = [ ] ; let dates = [ ] ; let dayIndex = - 1 ; while ( date < end ) { if ( dateProfileGenerator . isHiddenDay ( date ) ) { indices . push ( dayIndex + 0.5 ) ; } else { dayIndex += 1 ; indices . push ( dayIndex ) ; dates . push ( date ) ; } date = addDays ( date , 1 ) ; } this . dates = dates ; this . indices = indices ; this . cnt = dates . length ; } sliceRange ( range ) { let firstIndex = this . getDateDayIndex ( range . start ) ; let lastIndex = this . getDateDayIndex ( addDays ( range . end , - 1 ) ) ; let clippedFirstIndex = Math . max ( 0 , firstIndex ) ; let clippedLastIndex = Math . min ( this . cnt - 1 , lastIndex ) ; clippedFirstIndex = Math . ceil ( clippedFirstIndex ) ; clippedLastIndex = Math . floor ( clippedLastIndex ) ; if ( clippedFirstIndex <= clippedLastIndex ) { return { firstIndex : clippedFirstIndex , lastIndex : clippedLastIndex , isStart : firstIndex === clippedFirstIndex , isEnd : lastIndex === clippedLastIndex , } ; } return null ; } private getDateDayIndex ( date ) { let { indices } = this ; let dayOffset = Math . floor ( diffDays ( this . dates [ 0 ] , date ) ) ; if ( dayOffset < 0 ) { return indices [ 0 ] - 1 ; } if ( dayOffset >= indices . length ) { return indices [ indices . length - 1 ] + 1 ; } return indices [ dayOffset ] ; } }