@@ -6,7 +6,13 @@ import {
66 MetricFormatterShort
77} from '../reports/metric-formatter'
88import { DashboardPeriod } from '../../dashboard-time-periods'
9- import dateFormatter from './date-formatter'
9+ import {
10+ formatMonthYYYY ,
11+ formatDayShort ,
12+ formatTime ,
13+ is12HourClock ,
14+ parseNaiveDate
15+ } from '../../util/date'
1016import classNames from 'classnames'
1117import { ChangeArrow } from '../reports/change-arrow'
1218import { Metric } from '../../../types/query-api'
@@ -50,9 +56,9 @@ type MainGraphResponse = {
5056}
5157type GraphDatum = {
5258 value : number | null
53- timeLabel : string | null
59+ timeLabel : string | null
5460 comparisonValue ?: number | null
55- comparisonTimeLabel ?: string | null
61+ comparisonTimeLabel ?: string | null
5662 change ?: number | null
5763}
5864
@@ -469,55 +475,46 @@ export const MainGraphContainer = React.forwardRef<
469475} )
470476
471477const getXLabel = (
472- xValue : '__blank__' | string ,
478+ // in the format "YYYY-MM-DD" or "YYYY-MM-DD HH:MM:SS"
479+ xValue : string ,
473480 {
474481 shouldShowYear,
475482 period,
476483 interval
477- } : { shouldShowYear : boolean ; interval : string ; period : DashboardPeriod }
478- ) => {
479- if ( xValue == '__blank__' ) return ''
480-
481- if ( interval === 'hour' && period !== 'day' ) {
482- const date = dateFormatter ( {
483- interval : 'day' ,
484- longForm : false ,
485- period : period ,
486- shouldShowYear,
487- isPeriodFull : false
488- } ) ( xValue )
489-
490- const hour = dateFormatter ( {
491- interval : interval ,
492- longForm : false ,
493- period : period ,
494- shouldShowYear,
495- isPeriodFull : false
496- } ) ( xValue )
497-
498- // Returns a combination of date and hour. This is because
499- // small intervals like hour may return multiple days
500- // depending on the queried period.
501- return `${ date } , ${ hour } `
484+ } : {
485+ shouldShowYear : boolean
486+ /* "month" | "week" | "day" | "hour" | "minute" */
487+ interval : string
488+ period : DashboardPeriod
502489 }
503-
504- if ( interval === 'minute' && period !== 'realtime' ) {
505- return dateFormatter ( {
506- interval : 'hour' ,
507- longForm : false ,
508- period : period ,
509- isPeriodFull : false ,
510- shouldShowYear : false
511- } ) ( xValue )
490+ ) => {
491+ const parsedDate = parseNaiveDate ( xValue )
492+ switch ( interval ) {
493+ case 'month' :
494+ return formatMonthYYYY ( parsedDate )
495+ case 'week' :
496+ case 'day' :
497+ return formatDayShort ( parsedDate , shouldShowYear )
498+ case 'hour' : {
499+ const time = formatTime ( parsedDate , {
500+ use12HourClock : is12HourClock ( ) ,
501+ includeMinutes : false
502+ } )
503+ if ( period === 'day' ) {
504+ return time
505+ }
506+ // when viewing a period like Last 24h or Last 7 days, including the date is necessary for clarity
507+ return `${ formatDayShort ( parsedDate , shouldShowYear ) } , ${ time } `
508+ }
509+ case 'minute' :
510+ if ( period === 'realtime' ) return `${ xValue } m`
511+ return formatTime ( parsedDate , {
512+ use12HourClock : is12HourClock ( ) ,
513+ includeMinutes : true
514+ } )
515+ default :
516+ return ''
512517 }
513-
514- return dateFormatter ( {
515- interval : interval ,
516- longForm : false ,
517- period : period ,
518- shouldShowYear,
519- isPeriodFull : false
520- } ) ( xValue )
521518}
522519
523520const remapToGraphData = (
0 commit comments