afshin-dini commited on
Commit
44939e1
·
1 Parent(s): 4646f5c

Add histogram plot

Browse files
src/ai_dashboard/plot_plugin/histogram_plot.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """This module is for plotting histograms in AI-Dashboard"""
2
+
3
+ from typing import Any
4
+ import logging
5
+
6
+ import plotly.express as px
7
+ from dash import html, dcc
8
+ from dash.dependencies import Input, Output
9
+
10
+ from ..base import BasePlotPlugin
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+
15
+ class HistogramPlotPlugin(BasePlotPlugin):
16
+ """Plugin for plotting histograms in AI-Dashboard"""
17
+
18
+ name = "Histogram"
19
+
20
+ def dropdown(self, id_suffix: str, label: str) -> Any:
21
+ """Generate a dropdown for plot controls."""
22
+ cols = self.dataframe.columns.tolist()
23
+ return html.Div(
24
+ [
25
+ html.Label(label),
26
+ dcc.Dropdown(
27
+ id={"type": "control", "plot": self.name, "axis": id_suffix},
28
+ options=[{"label": c, "value": c} for c in cols], # type: ignore
29
+ value=cols[0],
30
+ clearable=False,
31
+ persistence=True,
32
+ persistence_type="memory",
33
+ ),
34
+ ],
35
+ style={"width": "170px", "marginRight": "12px"},
36
+ )
37
+
38
+ def controls(self) -> Any:
39
+ """Generate plot controls."""
40
+ return html.Div(
41
+ [self.dropdown("x", "X-Axis")],
42
+ style={"display": "flex", "flexWrap": "wrap"},
43
+ )
44
+
45
+ def render(self, **kwargs: Any) -> Any:
46
+ """Render the histogram plot."""
47
+ x_axis = kwargs.get("x_axis")
48
+ fig = px.histogram(self.dataframe, x=x_axis, nbins=20, opacity=0.75)
49
+ return dcc.Graph(figure=fig)
50
+
51
+ def register_callbacks(self, app: Any) -> None:
52
+ """Register callbacks for interactivity."""
53
+
54
+ @app.callback( # type: ignore
55
+ Output({"type": "plot-output", "plot": self.name}, "children"),
56
+ Input({"type": "control", "plot": self.name, "axis": "x"}, "value"),
57
+ )
58
+ def update_bar(x_axis: str) -> Any:
59
+ """Update the bar chart based on dropdown selections."""
60
+ return self.render(x_axis=x_axis)