afshin-dini commited on
Commit
db66a18
·
1 Parent(s): a38f922

Add box plot

Browse files
src/ai_dashboard/plot_plugin/box_plot.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Box Plot plugin for AI-Dashboard"""
2
+
3
+ from typing import Any, List
4
+ import plotly.express as px
5
+ from dash import html, dcc
6
+ from dash.dependencies import Input, Output
7
+
8
+ from ..base import BasePlotPlugin
9
+
10
+
11
+ class BoxPlotPlugin(BasePlotPlugin):
12
+ """Box Plot Plugin for AI-Dashboard"""
13
+
14
+ name = "Box Plot"
15
+
16
+ def dropdown(self, id_suffix: str, label: str, options: List[str]) -> Any:
17
+ """Generate a dropdown for plot controls."""
18
+ return html.Div(
19
+ [
20
+ html.Label(label),
21
+ dcc.Dropdown(
22
+ id={"type": "control", "plot": self.name, "axis": id_suffix},
23
+ options=[{"label": c, "value": c} for c in options], # type: ignore
24
+ value=options[0],
25
+ clearable=False,
26
+ persistence=True,
27
+ ),
28
+ ],
29
+ style={"width": "180px", "marginBottom": "10px"},
30
+ )
31
+
32
+ def controls(self) -> Any:
33
+ """Generate plot controls."""
34
+ cats = self.categorical_columns()
35
+ nums = self.numeric_columns()
36
+ return html.Div(
37
+ [
38
+ self.dropdown("x", "Category", cats),
39
+ self.dropdown("y", "Value", nums),
40
+ ]
41
+ )
42
+
43
+ def render(self, **kwargs: Any) -> Any: # pylint: disable=W0201
44
+ """Render the box plot based on selected axes."""
45
+ fig = px.box(self.dataframe, x=kwargs["x_axis"], y=kwargs["y_axis"])
46
+ return dcc.Graph(figure=fig)
47
+
48
+ def register_callbacks(self, app: Any) -> None:
49
+ """Register callbacks for interactivity."""
50
+
51
+ @app.callback( # type: ignore
52
+ Output({"type": "plot-output", "plot": self.name}, "children"),
53
+ Input({"type": "control", "plot": self.name, "axis": "x"}, "value"),
54
+ Input({"type": "control", "plot": self.name, "axis": "y"}, "value"),
55
+ )
56
+ def update(x_axis: str, y_axis: str) -> Any:
57
+ """Update the box plot based on dropdown selections."""
58
+ return self.render(x_axis=x_axis, y_axis=y_axis)