@@ -7,19 +7,27 @@ interface Data extends DataLabel {
77}
88
99interface GroupBarChartProps {
10+ yAxisLabel : string ;
1011 title : string ;
1112 data : Data [ ] ;
1213}
1314
14- const margin = { top : 10 , right : 10 , bottom : 40 , left : 50 } ;
15+ const margin = { top : 30 , right : 50 , bottom : 40 , left : 80 } ;
1516const height = 300 - margin . top - margin . bottom ;
1617const barWidth = 0.05 * window . innerWidth < 40 ? 40 : 0.05 * window . innerWidth ;
1718const barGap = 5 ;
1819
19- const GroupBarChart = ( { title, data } : GroupBarChartProps ) => {
20+ const GroupBarChart = ( { title, data, yAxisLabel } : GroupBarChartProps ) => {
2021 const svgRef = useRef < SVGSVGElement > ( null ) ;
2122 const containerRef = useRef < HTMLDivElement > ( null ) ;
2223 const [ containerWidth , setContainerWidth ] = useState ( 800 ) ; // Default width
24+
25+ //const legendWidth = 140;
26+ // const plotWidth =
27+ // containerWidth -
28+ // margin.left -
29+ // margin.right ;
30+
2331 const fx = useMemo (
2432 ( ) =>
2533 d3
@@ -82,6 +90,7 @@ const GroupBarChart = ({ title, data }: GroupBarChartProps) => {
8290 . text (
8391 "@import url('https://fonts.googleapis.com/css2?family=Avenir:wght@600');"
8492 ) ;
93+
8594 // Append x-axis
8695 const xAxis = svg
8796 . append ( 'g' )
@@ -143,14 +152,24 @@ const GroupBarChart = ({ title, data }: GroupBarChartProps) => {
143152 `translate(${ Math . max ( containerWidth , data . length * barWidth ) - margin . left - margin . right + 20 } , 0)`
144153 ) ;
145154
146- // Append title to the legend
147- legend
148- . append ( 'text' )
149- . attr ( 'x' , 0 )
150- . attr ( 'y' , 0 )
155+ // Append title to the svg
156+ svg . append ( 'text' )
157+ . attr ( 'x' , ( containerWidth - margin . left - margin . right ) / 2 )
158+ . attr ( 'y' , - 10 )
159+ . attr ( 'text-anchor' , 'middle' )
160+ . style ( 'font-size' , '12px' )
151161 . style ( 'font-weight' , 'bold' )
152162 . text ( title ) ;
153163
164+ // Add y-axis label
165+ svg . append ( 'text' )
166+ . attr ( 'transform' , 'rotate(-90)' )
167+ . attr ( 'y' , - 50 )
168+ . attr ( 'x' , - height / 2 )
169+ . attr ( 'text-anchor' , 'middle' )
170+ . attr ( 'font-size' , '12px' )
171+ . text ( yAxisLabel ) ;
172+
154173 // Append legend color boxes and text labels
155174 const legendItems = legend
156175 . selectAll ( '.legend-item' )
0 commit comments