File size: 2,267 Bytes
f66a3de
 
3eaf500
f66a3de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3eaf500
 
f66a3de
3eaf500
 
f66a3de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Hexbin Plot plugin"""

# pylint: disable=R0801
from typing import Any, List
import plotly.express as px
from dash import html, dcc
from dash.dependencies import Input, Output

from ..base import BasePlotPlugin


class HexbinPlotPlugin(BasePlotPlugin):
    """Hexbin Plot Plugin Class."""

    name = "Hexbin Plot"

    def dropdown(self, id_suffix: str, label: str, options: List[str]) -> Any:
        """Create a dropdown control."""
        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:
        """Render the control panel for the hexbin plot."""
        nums = self.numeric_columns()
        return html.Div(
            [
                self.dropdown("x", "X-Axis", nums),
                self.dropdown("y", "Y-Axis", nums),
            ]
        )

    def render(self, **kwargs: Any) -> Any:  # pylint: disable=W0201
        """Render the hexbin plot based on selected axes."""
        fig = px.density_heatmap(
            self.dataframe,
            x=kwargs["x_axis"],
            y=kwargs["y_axis"],
            nbinsx=20,
            nbinsy=20,
            color_continuous_scale="Viridis",
        )
        return dcc.Graph(figure=fig)

    def register_callbacks(self, app: Any) -> None:
        """Register callbacks for the hexbin plot."""

        @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(x_axis: str, y_axis: str) -> Any:
            """Update the hexbin plot based on selected axes."""
            return self.render(x_axis=x_axis, y_axis=y_axis)