File size: 1,305 Bytes
c5c6773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/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)})