Spaces:
Sleeping
Sleeping
File size: 2,379 Bytes
fc611f1 3eaf500 a38f922 fc611f1 a38f922 fc611f1 a38f922 fc611f1 3eaf500 fc611f1 a38f922 fc611f1 a38f922 fc611f1 | 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | """This is the bar plot module 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 BarPlotPlugin(BasePlotPlugin):
"""Bar Plot Plugin for AI Dashboard."""
name = "Bar Chart"
def dropdown(self, id_suffix: str, label: str, option: List[str]) -> Any:
"""Generate a dropdown for plot controls."""
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 option], # type: ignore
value=option[0],
clearable=False,
persistence=True,
persistence_type="memory",
style={"color": "#000"},
),
],
style={"width": "170px", "marginRight": "12px"},
)
def controls(self) -> Any:
"""Generate plot controls."""
nums = self.numeric_columns()
cats = self.categorical_columns()
return html.Div(
[
self.dropdown("x", "X-Axis", cats),
self.dropdown("y", "Y-Axis", nums),
],
style={"display": "flex", "flexWrap": "wrap", "gap": "12px"},
)
def render(self, **kwargs: Any) -> Any: # pylint: disable=W0201
"""Render the bar plot."""
x_axis = kwargs.get("x_axis")
y_axis = kwargs.get("y_axis")
fig = px.bar(self.dataframe, x=x_axis, y=y_axis, text_auto=True)
return dcc.Graph(figure=fig)
def register_callbacks(self, app: Any) -> None:
"""Register callbacks for interactivity."""
@app.callback( # type: ignore
Output({"type": "plot-output", "plot": self.name}, "children"),
Input({"type": "control", "plot": self.name, "axis": "x"}, "value"),
Input({"type": "control", "plot": self.name, "axis": "y"}, "value"),
)
def update_bar(x_axis: str, y_axis: str) -> Any:
"""Update the bar chart based on dropdown selections."""
return self.render(x_axis=x_axis, y_axis=y_axis)
|