11import React , { useEffect , useRef , useState } from 'react' ;
22import { Bar , BarChart , CartesianGrid , LabelList , Tooltip , XAxis , YAxis } from 'recharts' ;
33import { useTranslation } from 'react-i18next' ;
4+ import { endOfWeek } from 'date-fns' ;
5+ import { MdFlag } from 'react-icons/md' ;
6+ import Icon from '../../Icon' ;
7+ import TooltipWrapper from '../../TooltipWrapper' ;
48import { formatDate , getDistributionYAxisTicks } from '../../../util/charts-utils' ;
5- import { DateRange , formatOverviewDate , OverviewUnit } from '../../../util/overview-date-utils' ;
9+ import { DateRange , formatOverviewDate , isLongPeriod , OverviewUnit , WEEK_OPTIONS } from '../../../util/overview-date-utils' ;
610import { OVERVIEW_AXIS_STROKE , OVERVIEW_BLUE , OVERVIEW_GREEN , OVERVIEW_TICK_FILL } from '../../../util/overview-colors' ;
711import { createStackedBarShape } from './barShapes' ;
812import { useOverviewChartData } from './useOverviewChartData' ;
@@ -22,9 +26,10 @@ type Props = {
2226
2327const OverviewBarChart = ( { range, unit } : Props ) => {
2428 const { t } = useTranslation ( ) ;
25- const buckets = useOverviewChartData ( range , unit ) ;
29+ const { buckets, period } = useOverviewChartData ( range , unit ) ;
2630 const ref = useRef < HTMLDivElement > ( null ) ;
2731 const [ width , setWidth ] = useState < number > ( 0 ) ;
32+ const isHourly = period === 'hour' ;
2833
2934 useEffect ( ( ) => {
3035 const handleResize = ( ) => setWidth ( ref . current ?. clientWidth ?? 0 ) ;
@@ -37,7 +42,17 @@ const OverviewBarChart = ({ range, unit }: Props) => {
3742 const yAxisMax = roundUpToTen ( maxTotal ) ;
3843 const yAxisTicks = getDistributionYAxisTicks ( yAxisMax ) ;
3944
40- const tickFormatter = ( value : number ) => formatDate ( new Date ( value ) , unit === 'day' ? 'HH:mm' : 'dd.MM' ) ;
45+ const tickFormatter = ( value : number ) => formatDate ( new Date ( value ) , isHourly ? 'HH:mm' : 'dd.MM' ) ;
46+
47+ const tooltipLabelFormatter = ( value : number ) => {
48+ const date = new Date ( value ) ;
49+ if ( isHourly ) return formatDate ( date , 'HH:mm' ) ;
50+ if ( period === 'week' ) {
51+ const weekEnd = endOfWeek ( date , WEEK_OPTIONS ) ;
52+ return `${ formatDate ( date , 'dd.MM.yyyy' ) } - ${ formatDate ( weekEnd , 'dd.MM.yyyy' ) } ` ;
53+ }
54+ return formatDate ( date , 'dd-MM-yyyy' ) ;
55+ } ;
4156
4257 const totalCount = buckets . reduce ( ( sum , bucket ) => sum + bucket . burokratt + bucket . csa , 0 ) ;
4358
@@ -50,7 +65,16 @@ const OverviewBarChart = ({ range, unit }: Props) => {
5065 return (
5166 < div ref = { ref } className = "overview-bar-chart" >
5267 < div className = "overview-bar-chart__header" >
53- < h2 className = "overview-bar-chart__title" > { title } </ h2 >
68+ < h2 className = "overview-bar-chart__title" >
69+ { title }
70+ { isLongPeriod ( range ) && (
71+ < span className = "overview-bar-chart__title-warning" >
72+ < TooltipWrapper enabled text = { t ( 'overview.longPeriodWarning' ) } >
73+ < Icon icon = { < MdFlag /> } size = "small" />
74+ </ TooltipWrapper >
75+ </ span >
76+ ) }
77+ </ h2 >
5478 < div className = "overview-bar-chart__legend" >
5579 { LEGEND_ITEMS . map ( ( { key, color } ) => (
5680 < div key = { key } className = "overview-bar-chart__legend-item" >
@@ -60,7 +84,7 @@ const OverviewBarChart = ({ range, unit }: Props) => {
6084 ) ) }
6185 </ div >
6286 </ div >
63- < BarChart width = { width } height = { width / 3.76 } data = { buckets } barSize = { unit === 'day' ? 8 : 20 } margin = { { top : 20 , right : 20 , bottom : 30 } } >
87+ < BarChart width = { width } height = { width / 3.76 } data = { buckets } barSize = { isHourly ? 8 : 20 } margin = { { top : 20 , right : 20 , bottom : 30 } } >
6488 < CartesianGrid
6589 vertical = { false }
6690 horizontalValues = { yAxisTicks . filter ( ( tick ) => tick > 0 ) }
@@ -73,7 +97,7 @@ const OverviewBarChart = ({ range, unit }: Props) => {
7397 type = "number"
7498 domain = { [ 'dataMin' , 'dataMax' ] }
7599 scale = "time"
76- padding = { { left : unit === 'day' ? 8 : 14 , right : unit === 'day' ? 8 : 14 } }
100+ padding = { { left : isHourly ? 8 : 14 , right : isHourly ? 8 : 14 } }
77101 axisLine = { { stroke : OVERVIEW_AXIS_STROKE } }
78102 tickLine = { false }
79103 tick = { { fill : OVERVIEW_TICK_FILL , fontSize : 12 } }
@@ -88,7 +112,7 @@ const OverviewBarChart = ({ range, unit }: Props) => {
88112 />
89113 < Tooltip
90114 cursor = { { fill : 'rgba(151, 153, 164, 0.12)' } }
91- labelFormatter = { ( value ) => formatDate ( new Date ( value as number ) , unit === 'day' ? 'HH:mm' : 'dd-MM-yyyy' ) }
115+ labelFormatter = { ( value ) => tooltipLabelFormatter ( value as number ) }
92116 />
93117 < Bar dataKey = "burokratt" stackId = "total" fill = { OVERVIEW_BLUE } shape = { createStackedBarShape ( 'burokratt' ) } />
94118 < Bar dataKey = "csa" stackId = "total" fill = { OVERVIEW_GREEN } shape = { createStackedBarShape ( 'csa' ) } >
0 commit comments