@@ -14,7 +14,6 @@ import { RootState } from '@/redux/store';
1414import {
1515 PlotXYVar ,
1616 VoyagesOptionProps ,
17- FilterObjectsState ,
1817 CurrentPageInitialState ,
1918 LanguageKey ,
2019 IRootFilterLineAndBarRequest ,
@@ -37,9 +36,6 @@ function BarGraph() {
3736 isSuccess,
3837 isLoading,
3938 } = useGetOptionsQuery ( datas ) ;
40- const { varName } = useSelector (
41- ( state : RootState ) => state . rangeSlider as FilterObjectsState ,
42- ) ;
4339
4440 const { currentPage } = useSelector (
4541 ( state : RootState ) => state . getScrollPage as CurrentPageInitialState ,
@@ -69,10 +65,9 @@ function BarGraph() {
6965 ) ;
7066 const [ yAxes , setYAxes ] = useState < string [ ] > ( [
7167 VOYAGE_BARGRAPH_OPTIONS . y_vars [ 0 ] . label [ lang ] ,
72- VOYAGE_BARGRAPH_OPTIONS . y_vars [ 0 ] . agg_fn ,
7368 ] ) ;
7469 const [ chips , setChips ] = useState < string [ ] > ( [
75- VOYAGE_BARGRAPH_OPTIONS . y_vars [ 0 ] . var_name ,
70+ ` ${ VOYAGE_BARGRAPH_OPTIONS . y_vars [ 0 ] . var_name } __AGG__ ${ VOYAGE_BARGRAPH_OPTIONS . y_vars [ 0 ] . agg_fn } ` ,
7671 ] ) ;
7772 const [ barGraphOptions , setBarOptions ] = useState < VoyagesOptionProps > ( {
7873 x_vars : VOYAGE_BARGRAPH_OPTIONS . x_vars [ 0 ] . var_name ,
@@ -119,12 +114,10 @@ function BarGraph() {
119114 groupby : {
120115 by : barGraphOptions . x_vars ,
121116 agg_series : chips . map ( ( chip ) => {
122- const yVar = VOYAGE_BARGRAPH_OPTIONS . y_vars . find (
123- ( y ) => y . var_name === chip ,
124- ) ;
117+ const [ varName , aggFn ] = chip . split ( '__AGG__' ) ;
125118 return {
126- vals : chip ,
127- agg_fn : yVar ?. agg_fn || 'sum' ,
119+ vals : varName ,
120+ agg_fn : aggFn || 'sum' ,
128121 } ;
129122 } ) ,
130123 } ,
@@ -142,24 +135,48 @@ function BarGraph() {
142135 isError,
143136 } = useFetchLineAndBarcharts ( dataSend ) ;
144137
138+ // CHANGED: Complete rewrite of the data processing logic
145139 useEffect ( ( ) => {
146140 VoyageBargraphOptions ( ) ;
147141 if ( ! loading && ! isError && response ) {
148- const values = Object . values ( response ) ;
149- const cleanXVar = barGraphOptions . x_vars . replace ( / _ _ b i n s _ _ \d + $ / , '' ) ;
150142 const data : Data [ ] = [ ] ;
151- for ( const [ index , [ key , value ] ] of Object . entries ( response ) . entries ( ) ) {
152- if ( key !== cleanXVar && Array . isArray ( value ) ) {
143+
144+ // Find x-axis data - handle both regular and binned variables
145+ const cleanXVar = barGraphOptions . x_vars . replace ( / _ _ b i n s _ _ \d + $ / , '' ) ;
146+ const xVarKey = Object . keys ( response ) . find (
147+ ( key ) => key === barGraphOptions . x_vars || key === cleanXVar ,
148+ ) ;
149+
150+ const xData = xVarKey ? response [ xVarKey ] : [ ] ;
151+
152+ // Create a bar for each selected y-variable
153+ chips . forEach ( ( chip ) => {
154+ const [ varName , aggFn ] = chip . split ( '__AGG__' ) ;
155+ const yDataKey = Object . keys ( response ) . find ( ( key ) => {
156+ // Skip the x-axis key
157+ if ( key === xVarKey ) return false ;
158+
159+ // Check if key matches varName exactly or starts with varName
160+ return key === varName || key . startsWith ( varName ) ;
161+ } ) ;
162+
163+ const yVar = VOYAGE_BARGRAPH_OPTIONS . y_vars . find (
164+ ( y ) => y . var_name === varName && y . agg_fn === aggFn ,
165+ ) ;
166+
167+ // Find the corresponding data in response
168+ const yData = yDataKey ? response [ yDataKey ] : null ;
169+ if ( yVar && yData && Array . isArray ( yData ) ) {
153170 data . push ( {
154- x : values [ 0 ] as number [ ] ,
155- y : value as number [ ] ,
171+ x : xData as number [ ] ,
172+ y : yData as number [ ] ,
156173 type : 'bar' ,
157174 mode : 'lines' ,
158175 line : { shape : 'spline' } ,
159- name : ` ${ VOYAGE_BARGRAPH_OPTIONS . y_vars [ index ] . label [ lang ] } ` ,
176+ name : yVar . label [ lang ] ,
160177 } ) ;
161178 }
162- }
179+ } ) ;
163180 setBarData ( data ) ;
164181 }
165182 return ( ) => {
@@ -171,8 +188,6 @@ function BarGraph() {
171188 isError ,
172189 options_flat ,
173190 barGraphOptions . x_vars ,
174- barGraphOptions . y_vars ,
175- varName ,
176191 chips ,
177192 currentPage ,
178193 isSuccess ,
@@ -279,7 +294,7 @@ function BarGraph() {
279294 } ,
280295 } ,
281296 fixedrange : true ,
282- tickangle : width < 768 ? - 45 : 0 , // Rotate labels on mobile
297+ tickangle : width < 768 ? - 45 : 0 ,
283298 tickfont : {
284299 size : width < 400 ? 8 : 10 ,
285300 } ,
@@ -296,17 +311,17 @@ function BarGraph() {
296311 size : width < 400 ? 8 : 10 ,
297312 } ,
298313 } ,
299- showlegend : false ,
314+ showlegend : true ,
300315 margin : {
301316 l : width < 768 ? 50 : 60 ,
302317 r : width < 768 ? 20 : 30 ,
303318 t : width < 768 ? 40 : 50 ,
304- b : width < 768 ? 80 : 100 , // More bottom margin for rotated labels
319+ b : width < 768 ? 80 : 100 ,
305320 } ,
306321 autosize : false ,
307322 } }
308323 config = { {
309- responsive : false , // Disable Plotly's responsive to use our custom sizing
324+ responsive : false ,
310325 displayModeBar : false ,
311326 } }
312327 style = { {
0 commit comments