@ -12,7 +12,7 @@ import type {
TooltipPosition
TooltipPosition
} from 'chart.js' ;
} from 'chart.js' ;
import type { AnnotationOptions } from 'chartjs-plugin-annotation' ;
import type { AnnotationOptions } from 'chartjs-plugin-annotation' ;
import { format } from 'date-fns' ;
import { addDays , format , startOfWeek } from 'date-fns' ;
import {
import {
DATE_FORMAT ,
DATE_FORMAT ,
@ -21,9 +21,10 @@ import {
getBackgroundColor ,
getBackgroundColor ,
getDateFormatString ,
getDateFormatString ,
getLocale ,
getLocale ,
getTextColor
getTextColor ,
parseDate
} from './helper' ;
} from './helper' ;
import { ColorScheme , GroupBy } from './types' ;
import { ColorScheme , DateRange , GroupBy } from './types' ;
export function formatGroupedDate ( {
export function formatGroupedDate ( {
date ,
date ,
@ -60,17 +61,221 @@ export function getChartElementsOptions(
} ;
} ;
}
}
/ * *
* Returns the unit of the ticks of the time axis , so that the labels fit the
* length of the date range : days up to a month , months up to a year and years
* above
* /
/ * *
* Returns the daily items of the trading days ( Monday to Friday ) and , for a
* date range with a fixed end ( the current year ) , the remaining trading days
* without a value , so that the chart shows the whole period
* /
function getWeekKey ( aDate? : Date ) {
return aDate
? format ( startOfWeek ( aDate , { weekStartsOn : 1 } ) , DATE_FORMAT )
: '' ;
}
export function getTradingDayItems < T extends { date : string } > ( {
dateRange ,
items ,
maxItems = 400 ,
now = new Date ( )
} : {
dateRange? : DateRange ;
items : T [ ] ;
/** Above, one point per week (the last trading day) is kept */
maxItems? : number ;
now? : Date ;
} ) : ( T | { date : string ; value : null } ) [ ] {
const isWeekend = ( aDate : Date ) = > {
return aDate . getDay ( ) === 0 || aDate . getDay ( ) === 6 ;
} ;
const result : ( T | { date : string ; value : null } ) [ ] = items . filter (
( { date } ) = > {
const parsedDate = parseDate ( date ) ;
return ! parsedDate || ! isWeekend ( parsedDate ) ;
}
) ;
if ( result . length > maxItems ) {
const weekly = result . filter ( ( { date } , index ) = > {
const next = result [ index + 1 ] ;
return (
index === 0 ||
! next ||
getWeekKey ( parseDate ( date ) ) !== getWeekKey ( parseDate ( next . date ) )
) ;
} ) ;
result . splice ( 0 , result . length , . . . weekly ) ;
}
let end : Date | undefined ;
let start : Date | undefined ;
if ( dateRange === 'ytd' ) {
end = new Date ( now . getFullYear ( ) , 11 , 31 ) ;
start = new Date ( now . getFullYear ( ) , 0 , 1 ) ;
} else if ( /^\d{4}$/ . test ( dateRange ? ? '' ) ) {
end = new Date ( Number ( dateRange ) , 11 , 31 ) ;
start = new Date ( Number ( dateRange ) , 0 , 1 ) ;
}
const firstDate = result [ 0 ] ? parseDate ( result [ 0 ] . date ) : undefined ;
if ( start && firstDate && firstDate < start ) {
// The baseline (the close of the previous year) is shown at the start
result [ 0 ] = { . . . result [ 0 ] , date : format ( start , DATE_FORMAT ) } ;
}
const lastDate = items . at ( - 1 ) ? . date ;
const last = lastDate ? parseDate ( lastDate ) : undefined ;
if ( end && last ) {
for (
let date = addDays ( last , 1 ) ;
date . getTime ( ) <= end . getTime ( ) ;
date = addDays ( date , 1 )
) {
if ( ! isWeekend ( date ) ) {
result . push ( { date : format ( date , DATE_FORMAT ) , value : null } ) ;
}
}
}
return result ;
}
export function getTimeAxisUnit (
aDateRange? : DateRange
) : 'day' | 'hour' | 'month' | 'year' | false {
switch ( aDateRange ) {
case undefined :
return 'year' ;
case '1d' :
return 'hour' ;
case 'mtd' :
case 'wtd' :
return 'day' ;
case '1y' :
case '5y' :
case 'max' :
// Chosen by the chart according to the length of the period
return false ;
default :
// 'ytd' and single years
return 'month' ;
}
}
const TIME_SERIES_LABEL_FORMATS = {
day : 'EEE d' ,
hour : 'HH:mm' ,
month : 'MMM' ,
year : 'yyyy'
} as const ;
/ * *
* Returns the labels of an axis with equidistant points ( without the closed
* markets ) : a label at the first point of a period ( hour , day , month or
* year ) , thinned out to at most a few labels
* /
export function getTimeSeriesTickCallback (
aUnit : 'day' | 'hour' | 'month' | 'year' | false ,
aMaxLabels = 7
) {
let cache : { key : string ; labels : Map < number , string > } | undefined ;
return (
_value : number | string ,
index : number ,
ticks : { value : number } [ ]
) = > {
const key = ` ${ ticks . length } - ${ ticks [ 0 ] ? . value } - ${ ticks . at ( - 1 ) ? . value } ` ;
if ( cache ? . key !== key ) {
const span = ( ticks . at ( - 1 ) ? . value ? ? 0 ) - ( ticks [ 0 ] ? . value ? ? 0 ) ;
const unit =
aUnit ||
( span > 3 * 365 * 24 * 60 * 60 * 1000
? 'year'
: span > 45 * 24 * 60 * 60 * 1000
? 'month'
: 'day' ) ;
const getPeriod = ( aTime : number ) = > {
const date = new Date ( aTime ) ;
switch ( unit ) {
case 'hour' :
return ` ${ date . toDateString ( ) } ${ date . getHours ( ) } ` ;
case 'month' :
return ` ${ date . getFullYear ( ) } - ${ date . getMonth ( ) } ` ;
default :
return ` ${ date . getFullYear ( ) } ` ;
}
} ;
// The first point of each period
const starts : number [ ] = [ ] ;
ticks . forEach ( ( { value } , tickIndex ) = > {
const previous = ticks [ tickIndex - 1 ] ? . value ;
if (
tickIndex === 0 ||
( unit === 'day'
? // A trading day starts after the closed market (the post-market
// can last after midnight)
value - previous > 2 * 60 * 60 * 1000
: getPeriod ( value ) !== getPeriod ( previous ) )
) {
starts . push ( tickIndex ) ;
}
} ) ;
const step = Math . max ( 1 , Math . ceil ( starts . length / aMaxLabels ) ) ;
const labels = new Map < number , string > ( ) ;
starts . forEach ( ( tickIndex , startIndex ) = > {
if ( startIndex % step === 0 ) {
labels . set (
tickIndex ,
format ( ticks [ tickIndex ] . value , TIME_SERIES_LABEL_FORMATS [ unit ] )
) ;
}
} ) ;
cache = { key , labels } ;
}
return cache . labels . get ( index ) ? ? '' ;
} ;
}
export function getTimeAxisOptions ( {
export function getTimeAxisOptions ( {
borderWidth = 1 ,
borderWidth = 1 ,
colorScheme ,
colorScheme ,
display = true ,
display = true ,
locale = getLocale ( )
locale = getLocale ( ) ,
type = 'time' ,
unit = 'year' ,
withTime = false
} : {
} : {
borderWidth? : number ;
borderWidth? : number ;
colorScheme? : ColorScheme ;
colorScheme? : ColorScheme ;
display? : boolean ;
display? : boolean ;
locale? : string ;
locale? : string ;
} ) : ScaleOptions < 'time' > {
type ? : 'time' | 'timeseries' ;
unit ? : 'day' | 'hour' | 'month' | 'year' | false ;
/** Whether the points are intraday (the tooltip shows the time) */
withTime? : boolean ;
} ) : ScaleOptions < 'time' | 'timeseries' > {
return {
return {
display ,
display ,
border : {
border : {
@ -80,11 +285,36 @@ export function getTimeAxisOptions({
grid : {
grid : {
display : false
display : false
} ,
} ,
ticks :
type === 'timeseries'
? {
// A label at the first point of an hour, day, month or year
autoSkip : false ,
callback : getTimeSeriesTickCallback ( unit ) ,
maxRotation : 0 ,
source : 'data'
}
: {
autoSkip : true ,
maxRotation : 0 ,
maxTicksLimit : 6
} ,
time : {
time : {
tooltipFormat : getDateFormatString ( locale ) ,
tooltipFormat : withTime
unit : 'year'
? unit === 'hour'
? 'HH:mm'
: ` ${ getDateFormatString ( locale ) } HH:mm `
: getDateFormatString ( locale ) ,
displayFormats : {
day : 'EEE d' ,
hour : 'HH:mm' ,
month : unit === 'month' ? 'MMM' : 'MMM yy' ,
week : 'd MMM' ,
year : 'yyyy'
} ,
unit
} ,
} ,
type : 'time'
type
} ;
} ;
}
}