Spaces:
Sleeping
Sleeping
| """3D Scatter Plot plugin for AI-Dashboard""" | |
| # pylint: disable=R0801 | |
| from typing import Any, List | |
| import logging | |
| import plotly.express as px | |
| from dash import html, dcc | |
| from dash.dependencies import Input, Output | |
| from ..base import BasePlotPlugin | |
| logger = logging.getLogger(__name__) | |
| class Scatter3DPlotPlugin(BasePlotPlugin): | |
| """3D Scatter plot plugin.""" | |
| name = "3D Scatter Plot" | |
| def dropdown(self, id_suffix: str, label: str, options: List[str]) -> Any: | |
| """Create a dropdown component.""" | |
| return html.Div( | |
| [ | |
| html.Label(label), | |
| dcc.Dropdown( | |
| id={"type": "control", "plot": self.name, "axis": id_suffix}, | |
| options=[{"label": c, "value": c} for c in options], # type: ignore | |
| value=options[0], | |
| clearable=False, | |
| persistence=True, | |
| persistence_type="memory", | |
| style={"color": "#000"}, | |
| ), | |
| ], | |
| style={"width": "130px"}, | |
| ) | |
| def controls(self) -> Any: | |
| """Render control components for the plot.""" | |
| nums = self.numeric_columns() | |
| cats = self.categorical_columns() | |
| return html.Div( | |
| [ | |
| self.dropdown("x", "X-Axis", nums), | |
| self.dropdown("y", "Y-Axis", nums), | |
| self.dropdown("z", "Z-Axis", nums), | |
| self.dropdown("color", "Color By", nums + cats), | |
| self.dropdown("size", "Size", nums), | |
| ], | |
| style={"display": "flex", "flexDirection": "column"}, | |
| ) | |
| def render(self, **kwargs: Any) -> Any: | |
| """Render the 3D scatter plot.""" | |
| x = kwargs.get("x_axis") | |
| y = kwargs.get("y_axis") | |
| z = kwargs.get("z_axis") | |
| color = kwargs.get("color_axis") | |
| size = kwargs.get("size_axis") | |
| fig = px.scatter_3d( | |
| self.dataframe, | |
| x=x, | |
| y=y, | |
| z=z, | |
| color=color if color in self.dataframe.columns else None, | |
| size=size if size in self.dataframe.columns else None, | |
| ) | |
| return dcc.Graph(figure=fig) | |
| def register_callbacks(self, app: Any) -> None: | |
| """Register Dash callbacks for interactivity.""" | |
| def update( | |
| x_axis: str, y_axis: str, z_axis: str, color_axis: str, size_axis: str | |
| ) -> Any: | |
| """Update plot based on control inputs.""" | |
| return self.render( | |
| x_axis=x_axis, | |
| y_axis=y_axis, | |
| z_axis=z_axis, | |
| color_axis=color_axis, | |
| size_axis=size_axis, | |
| ) | |