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

Add grouped bar plot

Browse files
src/ai_dashboard/plot_plugin/grouped_bar_plot.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Grouped Bar Plot plugin"""
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 GroupedBarPlotPlugin(BasePlotPlugin):
12
+ """Grouped Bar Plot Plugin."""
13
+
14
+ name = "Grouped Bar Plot"
15
+
16
+ def dropdown(self, id_suffix: str, label: str, options: List[str]) -> Any:
17
+ """Create a dropdown control."""
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
+ )
30
+
31
+ def controls(self) -> Any:
32
+ """Define controls for the grouped bar plot."""
33
+ cats = self.categorical_columns()
34
+ nums = self.numeric_columns()
35
+ return html.Div(
36
+ [
37
+ self.dropdown("x", "Category", cats),
38
+ self.dropdown("group", "Group", cats),
39
+ self.dropdown("y", "Value", nums),
40
+ ]
41
+ )
42
+
43
+ def render(self, **kwargs: Any) -> Any: # pylint: disable=W0201
44
+ """Render the grouped bar plot."""
45
+ fig = px.bar(
46
+ self.dataframe,
47
+ x=kwargs["x_axis"],
48
+ y=kwargs["y_axis"],
49
+ color=kwargs["group_axis"],
50
+ barmode="group",
51
+ )
52
+ return dcc.Graph(figure=fig)
53
+
54
+ def register_callbacks(self, app: Any) -> None:
55
+ """Register callbacks for the grouped bar plot."""
56
+
57
+ @app.callback( # type: ignore
58
+ Output({"type": "plot-output", "plot": self.name}, "children"),
59
+ Input({"type": "control", "plot": self.name, "axis": "x"}, "value"),
60
+ Input({"type": "control", "plot": self.name, "axis": "group"}, "value"),
61
+ Input({"type": "control", "plot": self.name, "axis": "y"}, "value"),
62
+ )
63
+ def update(x_axis: str, group_axis: str, y_axis: str) -> Any:
64
+ """Update the grouped bar plot based on controls."""
65
+ return self.render(x_axis=x_axis, group_axis=group_axis, y_axis=y_axis)