afshin-dini's picture
Update the margins
3eaf500
"""This module is for plotting histograms in 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 HistogramPlotPlugin(BasePlotPlugin):
"""Plugin for plotting histograms in AI-Dashboard"""
name = "Histogram"
def dropdown(self, id_suffix: str, label: str, options: 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 options], # type: ignore
value=options[0],
clearable=False,
persistence=True,
persistence_type="memory",
style={"color": "#000"},
),
],
style={"width": "130px"},
)
def controls(self) -> Any:
"""Generate plot controls."""
nums = self.numeric_columns()
return html.Div(
[self.dropdown("x", "X-Axis", nums)],
style={"display": "flex", "flexWrap": "wrap"},
)
def render(self, **kwargs: Any) -> Any:
"""Render the histogram plot."""
x_axis = kwargs.get("x_axis")
fig = px.histogram(self.dataframe, x=x_axis, nbins=20, opacity=0.75)
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"),
)
def update_bar(x_axis: str) -> Any:
"""Update the bar chart based on dropdown selections."""
return self.render(x_axis=x_axis)