mgbam commited on
Commit
b04cfbb
·
verified ·
1 Parent(s): d037161

Update tools/plot_generator.py

Browse files
Files changed (1) hide show
  1. tools/plot_generator.py +12 -9
tools/plot_generator.py CHANGED
@@ -1,25 +1,28 @@
1
  import pandas as pd
2
  import plotly.graph_objects as go
3
 
4
- def plot_sales_tool(file_path: str, date_col: str = "Month") -> str:
5
  """
6
- Builds an interactive Plotly line chart for Sales.
7
- Saves a PNG fallback (sales_plot.png) for Streamlit thumbnail.
8
  """
9
  df = pd.read_csv(file_path)
10
 
11
- if date_col not in df.columns or "Sales" not in df.columns:
12
- return f"❌ '{date_col}' or 'Sales' column missing."
 
 
 
 
13
 
14
  fig = go.Figure(
15
  go.Scatter(
16
  x=df[date_col],
17
- y=df["Sales"],
18
  mode="lines+markers",
19
  line=dict(width=2),
20
  )
21
  )
22
- fig.update_layout(title="Sales Trend", template="plotly_dark")
23
- fig.write_image("sales_plot.png") # still save for PNG fallback
24
-
25
  return fig
 
1
  import pandas as pd
2
  import plotly.graph_objects as go
3
 
4
+ def plot_metric_tool(file_path: str, date_col: str, value_col: str):
5
  """
6
+ Generic interactive line chart for any numeric metric.
7
+ Returns a Plotly Figure or error message string.
8
  """
9
  df = pd.read_csv(file_path)
10
 
11
+ if date_col not in df.columns or value_col not in df.columns:
12
+ return f"❌ '{date_col}' or '{value_col}' column missing."
13
+
14
+ df[date_col] = pd.to_datetime(df[date_col], errors="coerce")
15
+ if df[date_col].isna().all():
16
+ return f"❌ '{date_col}' could not be parsed as dates."
17
 
18
  fig = go.Figure(
19
  go.Scatter(
20
  x=df[date_col],
21
+ y=df[value_col],
22
  mode="lines+markers",
23
  line=dict(width=2),
24
  )
25
  )
26
+ fig.update_layout(title=f"{value_col} Trend", template="plotly_dark")
27
+ fig.write_image("metric_plot.png")
 
28
  return fig