BizIntel_AI / tools /plot_generator.py
mgbam's picture
Update tools/plot_generator.py
b04cfbb verified
raw
history blame
893 Bytes
import pandas as pd
import plotly.graph_objects as go
def plot_metric_tool(file_path: str, date_col: str, value_col: str):
"""
Generic interactive line chart for any numeric metric.
Returns a Plotly Figure or error message string.
"""
df = pd.read_csv(file_path)
if date_col not in df.columns or value_col not in df.columns:
return f"❌ '{date_col}' or '{value_col}' column missing."
df[date_col] = pd.to_datetime(df[date_col], errors="coerce")
if df[date_col].isna().all():
return f"❌ '{date_col}' could not be parsed as dates."
fig = go.Figure(
go.Scatter(
x=df[date_col],
y=df[value_col],
mode="lines+markers",
line=dict(width=2),
)
)
fig.update_layout(title=f"{value_col} Trend", template="plotly_dark")
fig.write_image("metric_plot.png")
return fig