Spaces:
Sleeping
Sleeping
| """Grouped Bar Plot plugin""" | |
| # pylint: disable=R0801 | |
| from typing import Any, List | |
| import plotly.express as px | |
| from dash import html, dcc | |
| from dash.dependencies import Input, Output | |
| from ..base import BasePlotPlugin | |
| class GroupedBarPlotPlugin(BasePlotPlugin): | |
| """Grouped Bar Plot Plugin.""" | |
| name = "Grouped Bar Plot" | |
| def dropdown(self, id_suffix: str, label: str, options: List[str]) -> Any: | |
| """Create a dropdown control.""" | |
| 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: | |
| """Define controls for the grouped bar plot.""" | |
| cats = self.categorical_columns() | |
| nums = self.numeric_columns() | |
| return html.Div( | |
| [ | |
| self.dropdown("x", "Category", cats), | |
| self.dropdown("group", "Group", cats), | |
| self.dropdown("y", "Value", nums), | |
| ] | |
| ) | |
| def render(self, **kwargs: Any) -> Any: # pylint: disable=W0201 | |
| """Render the grouped bar plot.""" | |
| fig = px.bar( | |
| self.dataframe, | |
| x=kwargs["x_axis"], | |
| y=kwargs["y_axis"], | |
| color=kwargs["group_axis"], | |
| barmode="group", | |
| ) | |
| return dcc.Graph(figure=fig) | |
| def register_callbacks(self, app: Any) -> None: | |
| """Register callbacks for the grouped bar plot.""" | |
| def update(x_axis: str, group_axis: str, y_axis: str) -> Any: | |
| """Update the grouped bar plot based on controls.""" | |
| return self.render(x_axis=x_axis, group_axis=group_axis, y_axis=y_axis) | |