Excel_AI_Assistant / plugins /outputs /chart_generator.py
JatinAutonomousLabs's picture
Upload 3 files
c5c6773 verified
#!/usr/bin/env python3
"""Chart Generation Plugin"""
import plotly.express as px
import plotly.graph_objects as go
from typing import Dict, Any
import pandas as pd
import json
class ChartGenerator:
"""Generate interactive charts."""
def create_chart_html(self, data: pd.DataFrame, chart_type: str, x: str, y: str = None,
title: str = "Chart", color: str = None, **kwargs) -> str:
if data.empty or x not in data.columns:
return json.dumps({"error": "Invalid data"})
try:
if chart_type == "bar":
fig = px.bar(data, x=x, y=y, title=title, color=color, **kwargs)
elif chart_type == "line":
fig = px.line(data, x=x, y=y, title=title, color=color, **kwargs)
elif chart_type == "scatter":
fig = px.scatter(data, x=x, y=y, title=title, color=color, **kwargs)
elif chart_type == "pie":
fig = px.pie(data, names=x, values=y, title=title, **kwargs)
else:
fig = px.bar(data, x=x, y=y, title=title)
fig.update_layout(template="plotly_white", height=400)
return fig.to_json()
except Exception as e:
return json.dumps({"error": str(e)})