1+ import { get } from 'lodash-es'
12import { ReactElement } from 'react'
23import { Bar , BarChart , CartesianGrid , Tooltip , XAxis , YAxis } from 'recharts'
34import ChartTick from './ChartTick'
@@ -7,6 +8,7 @@ import { TimePeriod } from '../../utils/useTimePeriod'
78import { LabelledTimePeriod } from '../TimePeriodPicker'
89import { DataKey } from 'recharts/types/util/types'
910import { ChartCard } from './ChartCard'
11+ import { useYAxis } from './useYAxis'
1012
1113export type BarChartCardBar < T > = {
1214 dataKey : DataKey < T >
@@ -37,6 +39,21 @@ export function BarChartCard<T>({
3739 height = 300 ,
3840 tooltip
3941} : BarChartCardProps < T > ) {
42+ const { yAxisProps } = useYAxis ( {
43+ data,
44+ transformer : ( d ) => {
45+ return d . flatMap ( ( item ) =>
46+ bars . map ( ( bar ) => {
47+ if ( typeof bar . dataKey === 'function' ) {
48+ return bar . dataKey ( item )
49+ }
50+ const value = get ( item , String ( bar . dataKey ) )
51+ return typeof value === 'number' ? value : 0
52+ } )
53+ )
54+ }
55+ } )
56+
4057 return (
4158 < ChartCard
4259 title = { title }
@@ -45,8 +62,9 @@ export function BarChartCard<T>({
4562 timePeriod = { timePeriod }
4663 onTimePeriodChange = { onTimePeriodChange }
4764 height = { height }
65+ hasData = { data . length > 0 }
4866 >
49- < BarChart data = { data } margin = { { bottom : 20 , left : 10 , top : 10 } } >
67+ < BarChart data = { data } margin = { { top : 8 , left : 16 , bottom : 20 , right : 8 } } >
5068 < CartesianGrid strokeDasharray = '4' stroke = '#444' vertical = { false } />
5169
5270 < XAxis
@@ -60,15 +78,7 @@ export function BarChartCard<T>({
6078 ) }
6179 />
6280
63- < YAxis
64- allowDecimals = { false }
65- tick = { (
66- < ChartTick
67- transform = { ( x , y ) => `translate(${ x ! - 4 } ,${ y ! - 12 } )` }
68- formatter = { ( tick ) => tick . toLocaleString ( ) }
69- />
70- ) }
71- />
81+ < YAxis { ...yAxisProps } />
7282
7383 < Tooltip
7484 content = { tooltip }
0 commit comments