SVashishta1 commited on
Commit
a610301
·
1 Parent(s): 71abaa3
Files changed (1) hide show
  1. app.py +50 -16
app.py CHANGED
@@ -638,8 +638,8 @@ def generate_visualization(result_df, query):
638
  print("Visualization requested, attempting to create plot...")
639
 
640
  # Set common figure parameters
641
- fig_width = 1200
642
- fig_height = 650
643
 
644
  # Determine visualization type from query
645
  viz_type = 'bar' # Default
@@ -724,26 +724,60 @@ def generate_visualization(result_df, query):
724
  x_col = result_df.columns[0]
725
  y_col = numeric_cols[0] if numeric_cols else result_df.columns[1] if len(result_df.columns) > 1 else None
726
 
727
- if y_col:
728
- fig = px.bar(result_df, x=x_col, y=y_col, width=900)
729
- fig.update_layout(
730
- title_text=f'Bar Chart of {y_col} by {x_col}',
731
- bargap=0.2
732
- )
 
 
 
 
 
 
 
 
 
 
 
 
733
  else:
734
- fig = px.bar(result_df, x=x_col, width=900)
735
- fig.update_layout(
736
- title_text=f'Bar Chart of {x_col}',
737
- bargap=0.2
738
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
739
 
740
  # Set common layout properties
741
  fig.update_layout(
742
  width=fig_width,
743
  height=fig_height,
744
  template="plotly_white",
745
- margin=dict(l=40, r=40, t=80, b=80, pad=4),
746
- autosize=True
 
 
747
  )
748
 
749
  print(f"Created figure with width={fig_width}, height={fig_height}")
@@ -761,7 +795,7 @@ def generate_visualization(result_df, query):
761
  print("HTML conversion successful")
762
 
763
  # Return the HTML img tag
764
- return f"<img src='{img_src}' width='100%' style='max-width:1200px; min-height:650px; display:block; margin:0 auto;' />"
765
 
766
  except Exception as e:
767
  import traceback
 
638
  print("Visualization requested, attempting to create plot...")
639
 
640
  # Set common figure parameters
641
+ fig_width = 900 # Adjusted for a more square shape
642
+ fig_height = 800 # Increased to make it more square
643
 
644
  # Determine visualization type from query
645
  viz_type = 'bar' # Default
 
724
  x_col = result_df.columns[0]
725
  y_col = numeric_cols[0] if numeric_cols else result_df.columns[1] if len(result_df.columns) > 1 else None
726
 
727
+ # Check if we have many categories (more than 10)
728
+ if len(result_df) > 10:
729
+ # Use horizontal bar chart for many categories
730
+ if y_col:
731
+ fig = px.bar(
732
+ result_df,
733
+ y=x_col, # Swap x and y for horizontal orientation
734
+ x=y_col,
735
+ orientation='h', # Horizontal orientation
736
+ title=f'Bar Chart of {y_col} by {x_col}'
737
+ )
738
+ else:
739
+ fig = px.bar(
740
+ result_df,
741
+ y=x_col, # Swap x and y for horizontal orientation
742
+ orientation='h', # Horizontal orientation
743
+ title=f'Bar Chart of {x_col}'
744
+ )
745
  else:
746
+ # Use vertical bar chart for fewer categories
747
+ if y_col:
748
+ fig = px.bar(
749
+ result_df,
750
+ x=x_col,
751
+ y=y_col,
752
+ title=f'Bar Chart of {y_col} by {x_col}',
753
+ width=900,
754
+ height=800
755
+ )
756
+ else:
757
+ fig = px.bar(
758
+ result_df,
759
+ x=x_col,
760
+ title=f'Bar Chart of {x_col}',
761
+ width=900,
762
+ height=800
763
+ )
764
+
765
+ # Improve bar chart layout
766
+ fig.update_layout(
767
+ bargap=0.2, # Increase gap between bars
768
+ uniformtext_minsize=8, # Minimum text size
769
+ uniformtext_mode='hide' # Hide text if it doesn't fit
770
+ )
771
 
772
  # Set common layout properties
773
  fig.update_layout(
774
  width=fig_width,
775
  height=fig_height,
776
  template="plotly_white",
777
+ margin=dict(l=40, r=40, t=80, b=80, pad=4), # Balanced margins
778
+ autosize=True, # Allow the plot to resize with the container
779
+ plot_bgcolor='rgba(240,240,240,0.2)', # Light gray background
780
+ paper_bgcolor='white'
781
  )
782
 
783
  print(f"Created figure with width={fig_width}, height={fig_height}")
 
795
  print("HTML conversion successful")
796
 
797
  # Return the HTML img tag
798
+ return f"<img src='{img_src}' width='100%' style='max-width:900px; height:800px; object-fit:contain; display:block; margin:0 auto;' />"
799
 
800
  except Exception as e:
801
  import traceback